Form表单

表单。

何时使用

代码演示

Form表单 - 图1

基本使用

表单。

  1. import {
  2. Form,
  3. TextField,
  4. Password,
  5. NumberField,
  6. EmailField,
  7. UrlField,
  8. DatePicker,
  9. Select,
  10. SelectBox,
  11. Button,
  12. Menu,
  13. Dropdown,
  14. Icon,
  15. } from 'choerodon-ui/pro';
  16. const { Option } = Select;
  17. function passwordValidator(value, name, form) {
  18. if (value !== form.getField('password').getValue()) {
  19. return '您两次输入的密码不一致,请重新输入';
  20. }
  21. return true;
  22. }
  23. function validationRenderer(error, props) {
  24. if (error.ruleName === 'valueMissing' && props.name === 'password') {
  25. return <span style={{ color: 'blue' }}>{error.validationMessage}(自定义)</span>;
  26. }
  27. }
  28. const menu = (
  29. <Menu>
  30. <Menu.Item>
  31. <a target="_blank" rel="noopener noreferrer" href="https://choerodon.io/">
  32. 1st menu item
  33. </a>
  34. </Menu.Item>
  35. <Menu.Item>
  36. <a target="_blank" rel="noopener noreferrer" href="https://choerodon.com.cn/">
  37. 2nd menu item
  38. </a>
  39. </Menu.Item>
  40. <Menu.Item>
  41. <a target="_blank" rel="noopener noreferrer" href="https://github.com/choerodon/choerodon-ui">
  42. 3rd menu item
  43. </a>
  44. </Menu.Item>
  45. </Menu>
  46. );
  47. const dropdown = (
  48. <Dropdown overlay={menu}>
  49. <Button funcType="flat" size="small">
  50. Click me <Icon type="arrow_drop_down" />
  51. </Button>
  52. </Dropdown>
  53. );
  54. ReactDOM.render(
  55. <Form id="basic" style={{ width: '4rem' }}>
  56. <TextField
  57. label="手机号"
  58. labelWidth={150}
  59. pattern="1[3-9]\d{9}"
  60. name="phone"
  61. required
  62. clearButton
  63. addonBefore="+86"
  64. addonAfter="中国大陆"
  65. />
  66. <Password label="密码" name="password" required validationRenderer={validationRenderer} />
  67. <Password
  68. label="确认密码"
  69. name="confirmPassword"
  70. required
  71. validator={passwordValidator}
  72. help="请输入与上方相同的密码"
  73. showHelp="tooltip"
  74. />
  75. <NumberField
  76. label="年龄"
  77. name="age"
  78. min={18}
  79. step={1}
  80. required
  81. help="我们需要确定你的年龄"
  82. addonAfter="周岁"
  83. />
  84. <SelectBox label="性别" name="sex" required>
  85. <Option value="M">男</Option>
  86. <Option value="F">女</Option>
  87. </SelectBox>
  88. <Select
  89. label="语言"
  90. name="language"
  91. required
  92. help="超过两行的帮助信息超过两行的帮助信息超过两行的帮助信息"
  93. >
  94. <Option value="zh-cn">简体中文</Option>
  95. <Option value="en-us">英语(美国)</Option>
  96. <Option value="ja-jp">日本語</Option>
  97. </Select>
  98. <EmailField label="邮箱" name="email" required addonAfter={dropdown} />
  99. <UrlField label="个人主页" name="homepage" required addonBefore="Http://" />
  100. <DatePicker label="生日" name="birth" required />
  101. <div>
  102. <Button type="submit">注册</Button>
  103. <Button type="reset" style={{ marginLeft: 8 }}>
  104. 重置
  105. </Button>
  106. </div>
  107. </Form>,
  108. mountNode,
  109. );

Form表单 - 图2

数据源

表单绑定数据源。

  1. import {
  2. DataSet,
  3. Form,
  4. TextField,
  5. NumberField,
  6. Password,
  7. EmailField,
  8. UrlField,
  9. DatePicker,
  10. Select,
  11. SelectBox,
  12. Switch,
  13. Lov,
  14. Button,
  15. } from 'choerodon-ui/pro';
  16. const { Option } = Select;
  17. function passwordValidator(value, name, record) {
  18. if (value !== record.get('password')) {
  19. return '您两次输入的密码不一致,请重新输入';
  20. }
  21. return true;
  22. }
  23. const defaultValidationMessages = {
  24. valueMissing: '请输入{label}。(自定义)',
  25. };
  26. class App extends React.Component {
  27. ds = new DataSet({
  28. fields: [
  29. {
  30. name: 'phone',
  31. type: 'string',
  32. label: '手机号',
  33. labelWidth: 150,
  34. required: true,
  35. pattern: '^1[3-9]\\d{9}$',
  36. }, // /^1[3-9]\d{9}$/
  37. {
  38. name: 'password',
  39. type: 'string',
  40. label: '密码',
  41. required: true,
  42. defaultValidationMessages,
  43. },
  44. {
  45. name: 'confirmPassword',
  46. type: 'string',
  47. label: '确认密码',
  48. required: true,
  49. validator: passwordValidator,
  50. },
  51. {
  52. name: 'age',
  53. type: 'number',
  54. label: '年龄',
  55. required: true,
  56. min: 18,
  57. step: 1,
  58. help: '我们需要确定你的年龄',
  59. },
  60. { name: 'sex', type: 'string', label: '性别', required: true },
  61. {
  62. name: 'language',
  63. type: 'string',
  64. label: '语言',
  65. required: true,
  66. help: '超过两行的帮助信息超过两行的帮助信息超过两行的帮助信息',
  67. },
  68. { name: 'email', type: 'email', label: '邮箱', required: true },
  69. { name: 'homepage', type: 'url', label: '个人主页', required: true },
  70. { name: 'birth', type: 'date', label: '生日', required: true },
  71. { name: 'code', type: 'object', label: '代码描述', lovCode: 'LOV_CODE' },
  72. { name: 'frozon', type: 'boolean', label: '是否冻结' },
  73. ],
  74. });
  75. changeField = () => {
  76. this.ds.current.getField('code').set('textField', 'description');
  77. };
  78. render() {
  79. return (
  80. <Form dataSet={this.ds} style={{ width: '4.5rem' }}>
  81. <TextField name="phone" />
  82. <Password name="password" />
  83. <Password name="confirmPassword" />
  84. <NumberField name="age" addonAfter="周岁" showHelp="tooltip" />
  85. <SelectBox name="sex">
  86. <Option value="M">男</Option>
  87. <Option value="F">女</Option>
  88. </SelectBox>
  89. <Select name="language">
  90. <Option value="zh-cn">简体中文</Option>
  91. <Option value="en-us">英语(美国)</Option>
  92. <Option value="ja-jp">日本語</Option>
  93. </Select>
  94. <EmailField name="email" />
  95. <UrlField name="homepage" />
  96. <DatePicker name="birth" />
  97. <Lov name="code" />
  98. <Switch name="frozon" />
  99. <div>
  100. <Button type="submit">注册</Button>
  101. <Button type="reset" style={{ marginLeft: 8 }}>
  102. 重置
  103. </Button>
  104. <Button onClick={this.changeField} style={{ marginLeft: 8 }}>
  105. 设置代码描述的textField
  106. </Button>
  107. </div>
  108. </Form>
  109. );
  110. }
  111. }
  112. ReactDOM.render(<App />, mountNode);

Form表单 - 图3

数据源记录

表单绑定数据源某条记录。

  1. import { DataSet, Form, TextField, NumberField, Password, EmailField, UrlField, DatePicker, Select, SelectBox, Lov, Button } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. function passwordValidator(value, name, record) {
  4. if (value !== record.get('password')) {
  5. return '您两次输入的密码不一致,请重新输入';
  6. }
  7. return true;
  8. }
  9. class App extends React.Component {
  10. ds = new DataSet({
  11. autoCreate: true,
  12. fields: [
  13. { name: 'phone', type: 'string', label: '手机号', required: true },
  14. { name: 'password', type: 'string', label: '密码', required: true },
  15. { name: 'confirmPassword', type: 'string', label: '确认密码', required: true, validator: passwordValidator },
  16. { name: 'age', type: 'number', label: '年龄', required: true, help: '我们需要确定你的年龄' },
  17. { name: 'sex', type: 'string', label: '性别', required: true },
  18. { name: 'language', type: 'string', label: '语言', required: true, help: '超过两行的帮助信息超过两行的帮助信息超过两行的帮助信息' },
  19. { name: 'email', type: 'email', label: '邮箱', required: true },
  20. { name: 'homepage', type: 'url', label: '个人主页', required: true },
  21. { name: 'birth', type: 'date', label: '生日', required: true },
  22. { name: 'code', type: 'object', label: '代码描述', lovCode: 'LOV_CODE' },
  23. ],
  24. });
  25. render() {
  26. return (
  27. <Form record={this.ds.current} style={{ width: '4rem' }}>
  28. <TextField pattern="1[3-9]\d{9}" name="phone" />
  29. <Password name="password" />
  30. <Password name="confirmPassword" />
  31. <NumberField name="age" min={18} step={1} addonAfter="周岁" showHelp="tooltip" />
  32. <SelectBox name="sex">
  33. <Option value="M">男</Option>
  34. <Option value="F">女</Option>
  35. </SelectBox>
  36. <Select name="language">
  37. <Option value="zh-cn">简体中文</Option>
  38. <Option value="en-us">英语(美国)</Option>
  39. <Option value="ja-jp">日本語</Option>
  40. </Select>
  41. <EmailField name="email" />
  42. <UrlField name="homepage" />
  43. <DatePicker name="birth" />
  44. <Lov name="code" />
  45. <div>
  46. <Button type="submit">注册</Button>
  47. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  48. </div>
  49. </Form>
  50. );
  51. }
  52. }
  53. ReactDOM.render(
  54. <App />,
  55. mountNode
  56. );

Form表单 - 图4

布局

布局。

  1. import { Form, TextField, Password, NumberField, EmailField, UrlField, DatePicker, Select, SelectBox, TextArea, Button } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. function passwordValidator(value, name, form) {
  4. if (value !== form.getField('password').getValue()) {
  5. return '您两次输入的密码不一致,请重新输入';
  6. }
  7. return true;
  8. }
  9. ReactDOM.render(
  10. <Form labelWidth={[60, 70, 50]} columns={3}>
  11. <TextField colSpan={3} label="手机号" pattern="1[3-9]\d{9}" name="phone" required />
  12. <Password label="密码" name="password" required />
  13. <NumberField rowSpan={2} label="年龄" name="age" min={18} step={1} required addonAfter="周岁" />
  14. <Password label="确认密码" name="confirmPassword" required validator={passwordValidator} />
  15. <SelectBox colSpan={2} label="性别" name="sex" required>
  16. <Option value="M">男</Option>
  17. <Option value="F">女</Option>
  18. </SelectBox>
  19. <Select label="语言" name="language" required>
  20. <Option value="zh-cn">简体中文</Option>
  21. <Option value="en-us">英语(美国)</Option>
  22. <Option value="ja-jp">日本語</Option>
  23. </Select>
  24. <EmailField label="邮箱" name="email" />
  25. <TextArea rowSpan={2} colSpan={2} label="简介" name="description" required style={{ height: 80 }} help="请输入100~300个字符请输入100~300个字符请输入100~300个字符" />
  26. <UrlField label={<span>个人主页</span>} name="homepage" required help="请输入你的个人主页,如Github Pages个人博客" showHelp="tooltip" />
  27. <DatePicker label="生日" name="birth" required />
  28. <div newLine colSpan={3}>
  29. <Button type="submit">注册</Button>
  30. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  31. </div>
  32. </Form>,
  33. mountNode
  34. );

Form表单 - 图5

垂直布局

垂直布局。

  1. import { Form, TextField, Password, NumberField, EmailField, UrlField, DatePicker, Select, SelectBox, CodeArea, Button } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. function passwordValidator(value, name, form) {
  4. if (value !== form.getField('password').getValue()) {
  5. return '您两次输入的密码不一致,请重新输入';
  6. }
  7. return true;
  8. }
  9. ReactDOM.render(
  10. <Form labelLayout="vertical" columns={3}>
  11. <TextField colSpan={3} label="手机号" pattern="1[3-9]\d{9}" name="phone" required />
  12. <Password label="密码" name="password" required />
  13. <Password label="确认密码" name="confirmPassword" required validator={passwordValidator} help="请输入与左侧相同的密码" />
  14. <NumberField rowSpan={2} label="年龄" name="age" min={18} step={1} required />
  15. <SelectBox label="性别" name="sex" required>
  16. <Option value="M">男</Option>
  17. <Option value="F">女</Option>
  18. </SelectBox>
  19. <Select label="语言" name="language" required>
  20. <Option value="zh-cn">简体中文</Option>
  21. <Option value="en-us">英语(美国)</Option>
  22. <Option value="ja-jp">日本語</Option>
  23. </Select>
  24. <EmailField label="邮箱" name="email" />
  25. <CodeArea rowSpan={2} colSpan={2} rows={4} label="简介" name="description" required />
  26. <UrlField label="个人主页" name="homepage" required />
  27. <DatePicker label="生日" name="birth" required />
  28. <div newLine colSpan={3}>
  29. <Button type="submit">注册</Button>
  30. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  31. </div>
  32. </Form>,
  33. mountNode
  34. );

Form表单 - 图6

占位符显示标签

占位符显示标签。

  1. import { Form, TextField, Password, NumberField, EmailField, UrlField, DatePicker, Select, SelectBox, TextArea, Button } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. function passwordValidator(value, name, form) {
  4. if (value !== form.getField('password').getValue()) {
  5. return '您两次输入的密码不一致,请重新输入';
  6. }
  7. return true;
  8. }
  9. ReactDOM.render(
  10. <Form labelLayout="placeholder" columns={3}>
  11. <TextField colSpan={3} label="手机号" pattern="1[3-9]\d{9}" name="phone" required />
  12. <Password label="密码" name="password" required />
  13. <Password label="确认密码" name="confirmPassword" required validator={passwordValidator} help="请输入与左侧相同的密码" />
  14. <NumberField rowSpan={2} label="年龄" name="age" min={18} step={1} required />
  15. <SelectBox label="性别" name="sex" required>
  16. <Option value="M">男</Option>
  17. <Option value="F">女</Option>
  18. </SelectBox>
  19. <Select label="语言" name="language" required>
  20. <Option value="zh-cn">简体中文</Option>
  21. <Option value="en-us">英语(美国)</Option>
  22. <Option value="ja-jp">日本語</Option>
  23. </Select>
  24. <EmailField label="邮箱" name="email" />
  25. <TextArea rowSpan={2} colSpan={2} label="简介" name="description" required style={{ height: 80 }} />
  26. <UrlField label="个人主页" name="homepage" required />
  27. <DatePicker label="生日" name="birth" required />
  28. <div newLine colSpan={3}>
  29. <Button type="submit">注册</Button>
  30. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  31. </div>
  32. </Form>,
  33. mountNode
  34. );

Form表单 - 图7

浮动标签

浮动标签。

  1. import {
  2. Form,
  3. CheckBox,
  4. TextField,
  5. Password,
  6. NumberField,
  7. EmailField,
  8. UrlField,
  9. DatePicker,
  10. Range,
  11. Select,
  12. SelectBox,
  13. Switch,
  14. TextArea,
  15. Button,
  16. Icon,
  17. } from 'choerodon-ui/pro';
  18. const { Option } = Select;
  19. function passwordValidator(value, name, form) {
  20. if (value !== form.getField('password').getValue()) {
  21. return '您两次输入的密码不一致,请重新输入';
  22. }
  23. return true;
  24. }
  25. ReactDOM.render(
  26. <Form labelLayout="float" columns={3} header="Float Label">
  27. <TextField
  28. colSpan={3}
  29. label="手机号"
  30. pattern="1[3-9]\d{9}"
  31. name="phone"
  32. required
  33. placeholder="请输入手机号"
  34. addonBefore="+86"
  35. addonAfter="中国大陆"
  36. />
  37. <Password label="密码" name="password" required />
  38. <Password
  39. label="确认密码"
  40. name="confirmPassword"
  41. required
  42. validator={passwordValidator}
  43. help="请输入与左侧相同的密码"
  44. disabled
  45. />
  46. <NumberField rowSpan={2} label="年龄" name="age" min={18} step={1} required />
  47. <SelectBox label="性别" name="sex" required>
  48. <Option value="M">男</Option>
  49. <Option value="F">女</Option>
  50. </SelectBox>
  51. <Select label="语言" name="language" required>
  52. <Option value="zh-cn">简体中文</Option>
  53. <Option value="en-us">英语(美国)</Option>
  54. <Option value="ja-jp">日本語</Option>
  55. </Select>
  56. <EmailField prefix={<Icon type="mail_outline" />} label="邮箱" name="email" multiple />
  57. <TextArea
  58. rowSpan={2}
  59. colSpan={2}
  60. label="简介"
  61. name="description"
  62. required
  63. style={{ height: 80 }}
  64. placeholder="请输入简介"
  65. />
  66. <UrlField
  67. label="个人主页"
  68. name="homepage"
  69. required
  70. help="请输入你的个人主页,如Github Pages个人博客"
  71. showHelp="tooltip"
  72. />
  73. <DatePicker label="生日" name="birth" required />
  74. <Range label="阈值" name="threshold" required />
  75. <Switch label="是否冻结" name="frozen" required />
  76. <CheckBox label="是否显示" name="frozen" required />
  77. <div newLine colSpan={3}>
  78. <Button type="submit">注册</Button>
  79. <Button type="reset" style={{ marginLeft: 8 }}>
  80. 重置
  81. </Button>
  82. </div>
  83. </Form>,
  84. mountNode,
  85. );

Form表单 - 图8

表单头

表单头。

  1. import { Form, TextField, Password, NumberField, EmailField, UrlField, DatePicker, Select, SelectBox, Button } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. function passwordValidator(value, name, form) {
  4. if (value !== form.getField('password').getValue()) {
  5. return '您两次输入的密码不一致,请重新输入';
  6. }
  7. return true;
  8. }
  9. ReactDOM.render(
  10. <Form header="Custom Header">
  11. <TextField label="手机号" pattern="1[3-9]\d{9}" name="phone" required clearButton />
  12. <Password label="密码" name="password" required />
  13. <Password label="确认密码" name="confirmPassword" required validator={passwordValidator} />
  14. <NumberField label="年龄" name="age" min={18} step={1} required />
  15. <SelectBox label="性别" name="sex" required>
  16. <Option value="M">男</Option>
  17. <Option value="F">女</Option>
  18. </SelectBox>
  19. <Select label="语言" name="language" required>
  20. <Option value="zh-cn">简体中文</Option>
  21. <Option value="en-us">英语(美国)</Option>
  22. <Option value="ja-jp">日本語</Option>
  23. </Select>
  24. <EmailField label="邮箱" name="email" required />
  25. <UrlField label="个人主页" name="homepage" required />
  26. <DatePicker label="生日" name="birth" required />
  27. <div>
  28. <Button type="submit">注册</Button>
  29. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  30. </div>
  31. </Form>,
  32. mountNode
  33. );

Form表单 - 图9

响应式

响应式。

  1. import { Form, TextField, Password, NumberField, EmailField, UrlField, DatePicker, Select, SelectBox, Button } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. ReactDOM.render(
  4. <Form
  5. columns={{ xs: 1, sm: 1, md: 2, lg: 3, xl: 4, xxl: 5 }}
  6. labelLayout={{ xs: 'vertical' }}
  7. >
  8. <TextField label="手机号" pattern="1[3-9]\d{9}" name="phone" required clearButton addonBefore="+86" addonAfter="中国大陆" />
  9. <Password label="密码" name="password" required />
  10. <Password label="确认密码" name="confirmPassword" required help="请输入与上方相同的密码" showHelp="tooltip" />
  11. <NumberField label="年龄" name="age" min={18} step={1} required help="我们需要确定你的年龄" addonAfter="周岁" />
  12. <SelectBox label="性别" name="sex" required>
  13. <Option value="M">男</Option>
  14. <Option value="F">女</Option>
  15. </SelectBox>
  16. <Select label="语言" name="language" required help="超过两行的帮助信息超过两行的帮助信息超过两行的帮助信息">
  17. <Option value="zh-cn">简体中文</Option>
  18. <Option value="en-us">英语(美国)</Option>
  19. <Option value="ja-jp">日本語</Option>
  20. </Select>
  21. <EmailField label="邮箱" name="email" required />
  22. <UrlField label="个人主页" name="homepage" required addonBefore="Http://" />
  23. <DatePicker label="生日" name="birth" required />
  24. <div>
  25. <Button type="submit">注册</Button>
  26. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  27. </div>
  28. </Form>,
  29. mountNode
  30. );

Form表单 - 图10

禁用

表单。

  1. import { Form, TextField, Password, NumberField, EmailField, UrlField, DatePicker, Select, SelectBox, Button, Menu, Dropdown, Icon } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. function passwordValidator(value, name, form) {
  4. if (value !== form.getField('password').getValue()) {
  5. return '您两次输入的密码不一致,请重新输入';
  6. }
  7. return true;
  8. }
  9. const menu = (
  10. <Menu>
  11. <Menu.Item>
  12. <a target="_blank" rel="noopener noreferrer" href="https://choerodon.io/">1st menu item</a>
  13. </Menu.Item>
  14. <Menu.Item>
  15. <a target="_blank" rel="noopener noreferrer" href="https://choerodon.com.cn/">2nd menu item</a>
  16. </Menu.Item>
  17. <Menu.Item>
  18. <a target="_blank" rel="noopener noreferrer" href="https://github.com/choerodon/choerodon-ui">3rd menu item</a>
  19. </Menu.Item>
  20. </Menu>
  21. );
  22. const dropdown = (
  23. <Dropdown overlay={menu}>
  24. <Button funcType="flat" size="small">
  25. Click me <Icon type="arrow_drop_down" />
  26. </Button>
  27. </Dropdown>
  28. );
  29. ReactDOM.render(
  30. <Form disabled style={{ width: '4rem' }}>
  31. <TextField label="手机号" pattern="1[3-9]\d{9}" name="phone" required clearButton addonBefore="+86" addonAfter="中国大陆" />
  32. <Password label="密码" name="password" required />
  33. <Password label="确认密码" name="confirmPassword" required validator={passwordValidator} help="请输入与上方相同的密码" showHelp="tooltip" />
  34. <NumberField label="年龄" name="age" min={18} step={1} required help="我们需要确定你的年龄" addonAfter="周岁" />
  35. <SelectBox label="性别" name="sex" required>
  36. <Option value="M">男</Option>
  37. <Option value="F">女</Option>
  38. </SelectBox>
  39. <Select label="语言" name="language" required help="超过两行的帮助信息超过两行的帮助信息超过两行的帮助信息">
  40. <Option value="zh-cn">简体中文</Option>
  41. <Option value="en-us">英语(美国)</Option>
  42. <Option value="ja-jp">日本語</Option>
  43. </Select>
  44. <EmailField label="邮箱" name="email" required addonAfter={dropdown} />
  45. <UrlField label="个人主页" name="homepage" required addonBefore="Http://" />
  46. <DatePicker label="生日" name="birth" required />
  47. <Form>
  48. <div>
  49. <Button type="submit">注册</Button>
  50. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  51. </div>
  52. </Form>
  53. </Form>,
  54. mountNode
  55. );

Form表单 - 图11

显示原始值

显示原始值。

  1. import { DataSet, Form, TextField, NumberField, Password, EmailField, UrlField, DatePicker, Select, SelectBox, Switch, Lov, Button, Row, Col } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. function passwordValidator(value, name, record) {
  4. if (value !== record.get('password')) {
  5. return '您两次输入的密码不一致,请重新输入';
  6. }
  7. return true;
  8. }
  9. class App extends React.Component {
  10. ds = new DataSet({
  11. data: [{
  12. phone: 1800000000,
  13. age: 18,
  14. sex: 'F',
  15. language: 'zh-cn',
  16. email: 'xxx@choerodon.io',
  17. homepage: 'https://choerodon.io',
  18. birth: '1984-11-22',
  19. code: 'HR.EMPLOYEE_GENDER',
  20. frozon: 'true',
  21. }],
  22. fields: [
  23. { name: 'phone', type: 'string', label: '手机号', required: true, pattern: '^1[3-9]\\d{9}$' }, // /^1[3-9]\d{9}$/
  24. { name: 'password', type: 'string', label: '密码', required: true },
  25. { name: 'confirmPassword', type: 'string', label: '确认密码', required: true, validator: passwordValidator },
  26. { name: 'age', type: 'number', label: '年龄', required: true, min: 18, step: 1, help: '我们需要确定你的年龄' },
  27. { name: 'sex', type: 'string', label: '性别', required: true },
  28. { name: 'language', type: 'string', label: '语言', required: true, help: '超过两行的帮助信息超过两行的帮助信息超过两行的帮助信息' },
  29. { name: 'email', type: 'email', label: '邮箱', required: true },
  30. { name: 'homepage', type: 'url', label: '个人主页', required: true },
  31. { name: 'birth', type: 'date', label: '生日', required: true },
  32. { name: 'code', type: 'object', label: '代码描述', lovCode: 'LOV_CODE' },
  33. { name: 'frozon', type: 'boolean', label: '是否冻结' },
  34. ],
  35. });
  36. render() {
  37. return (
  38. <Row>
  39. <Col span={12}>
  40. <Form pristine dataSet={this.ds} style={{ width: '4rem' }}>
  41. <TextField name="phone" />
  42. <Password name="password" />
  43. <Password name="confirmPassword" />
  44. <NumberField name="age" addonAfter="周岁" showHelp="tooltip" />
  45. <SelectBox name="sex">
  46. <Option value="M">男</Option>
  47. <Option value="F">女</Option>
  48. </SelectBox>
  49. <Select name="language">
  50. <Option value="zh-cn">简体中文</Option>
  51. <Option value="en-us">英语(美国)</Option>
  52. <Option value="ja-jp">日本語</Option>
  53. </Select>
  54. <EmailField name="email" />
  55. <UrlField name="homepage" />
  56. <DatePicker name="birth" />
  57. <Lov name="code" />
  58. <Switch name="frozon" />
  59. </Form>
  60. </Col>
  61. <Col span={12}>
  62. <Form dataSet={this.ds} style={{ width: '4rem' }}>
  63. <TextField name="phone" />
  64. <Password name="password" />
  65. <Password name="confirmPassword" />
  66. <NumberField name="age" addonAfter="周岁" showHelp="tooltip" />
  67. <SelectBox name="sex">
  68. <Option value="M">男</Option>
  69. <Option value="F">女</Option>
  70. </SelectBox>
  71. <Select name="language">
  72. <Option value="zh-cn">简体中文</Option>
  73. <Option value="en-us">英语(美国)</Option>
  74. <Option value="ja-jp">日本語</Option>
  75. </Select>
  76. <EmailField name="email" />
  77. <UrlField name="homepage" />
  78. <DatePicker name="birth" />
  79. <Lov name="code" />
  80. <Switch name="frozon" />
  81. <div>
  82. <Button type="submit">注册</Button>
  83. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  84. </div>
  85. </Form>
  86. </Col>
  87. </Row>
  88. );
  89. }
  90. }
  91. ReactDOM.render(
  92. <App />,
  93. mountNode
  94. );

Form表单 - 图12

Modal中使用浮动标签

Modal中使用浮动标签Form。

  1. import { Form, CheckBox, TextField, Password, NumberField, EmailField, UrlField, DatePicker, Range, Select, SelectBox, Switch, TextArea, Button, Modal } from 'choerodon-ui/pro';
  2. const { Option } = Select;
  3. function passwordValidator(value, name, form) {
  4. if (value !== form.getField('password').getValue()) {
  5. return '您两次输入的密码不一致,请重新输入';
  6. }
  7. return true;
  8. }
  9. const modalContent = (
  10. <Form labelLayout="float" columns={3} header="Float Label">
  11. <TextField colSpan={3} label="手机号" pattern="1[3-9]\d{9}" name="phone" required placeholder="请输入手机号" />
  12. <Password label="密码" name="password" required />
  13. <Password label="确认密码" name="confirmPassword" required validator={passwordValidator} help="请输入与左侧相同的密码" disabled />
  14. <NumberField rowSpan={2} label="年龄" name="age" min={18} step={1} required />
  15. <SelectBox label="性别" name="sex" required>
  16. <Option value="M">男</Option>
  17. <Option value="F">女</Option>
  18. </SelectBox>
  19. <div style={{ height: 500 }} />
  20. <Select label="语言" name="language" required>
  21. <Option value="zh-cn">简体中文</Option>
  22. <Option value="en-us">英语(美国)</Option>
  23. <Option value="ja-jp">日本語</Option>
  24. </Select>
  25. <EmailField label="邮箱" name="email" multiple />
  26. <TextArea rowSpan={2} colSpan={2} label="简介" name="description" required style={{ height: 80 }} placeholder="请输入简介" />
  27. <UrlField label="个人主页" name="homepage" required help="请输入你的个人主页,如Github Pages个人博客" showHelp="tooltip" />
  28. <DatePicker label="生日" name="birth" required />
  29. <Range label="阈值" name="threshold" required />
  30. <Switch label="是否冻结" name="frozen" required />
  31. <CheckBox label="是否显示" name="frozen" required />
  32. <div newLine colSpan={3}>
  33. <Button type="submit">注册</Button>
  34. <Button type="reset" style={{ marginLeft: 8 }}>重置</Button>
  35. </div>
  36. </Form>
  37. );
  38. const openModal = () => {
  39. Modal.open({
  40. drawer: true,
  41. children: modalContent,
  42. });
  43. }
  44. ReactDOM.render(
  45. <Button onClick={openModal}>Open</Button>,
  46. mountNode
  47. );

API

Form

参数说明类型默认值
action表单提交请求地址,当设置了 dataSet 时,该属性无作用string
method表单提交的 HTTP Method, 可选值 GET POSTstringPOST
target表单提交的目标,当表单设置了设置 target 且没有 dataSet 时作浏览器默认提交,否则作 Ajax 提交string
processParamsAjax 提交时的参数回调(event) => object
labelWidth内部控件的标签宽度。如果为数组则分别对应每列的标签宽度。数组长度不够列数,以默认值补全, 响应式参考Responsivenumber| number[] | object100
labelAlign标签文字对齐方式, 只在 labelLayout 为horizontal时起作用,可选值: left center right, 响应式参考Responsivestring | objectright
labelLayout标签位置, 可选值 horizontal vertical placeholder float none, 响应式参考Responsivestring | objecthorizontal
dataIndex对照 record 在 DataSet 中的 indexnumberds.currentIndex
record对照 record, 优先级高于 dataSet 和 dataIndexRecord
columns列数, 响应式参考Responsivenumber | object1
pristine显示原始值booleanfalse
onSubmit提交回调Function
onReset重置回调Function
onSuccess提交成功回调Function
onError提交失败回调Function

更多属性请参考 DataSetComponent

Form Layout

Form 子元素属性

参数说明类型默认值
rowSpan表单下控件跨越的行数number1
colSpan表单下控件跨越的列数number1
newLine另起新行boolean
label标签string | ReactNode
name字段名。可获取 DataSet 的字段属性,如 label,require 等,一般用于控件外需要嵌套其他元素时使用。string
labelWidth标签宽度。同列控件中标签宽度取最大设定值。子元素的 labelWidth 无法响应式。number

Form Responsive

响应式可设置参数columns labelWidth labelAlign labelLayout 为一个键值对。

键值可参考 Responsive BreakPoints