Form & Form.Item正确的嵌套结构

  1. <Form>
  2. <Form.Item></Form.Item>
  3. </Form>

Form.Item

  1. const formItemLayout = {
  2. labelCol: {span: 6},
  3. wrapperCol: {span: 18},
  4. }
  5. <Form.Item
  6. {...formItemLayout}
  7. label="内容"
  8. required
  9. hasFeedback>
  10. {getFieldDecorator('content', {
  11. initialValue: '',
  12. rules: [{
  13. required: true, message: '请输入内容'
  14. }],
  15. })(<Input
  16. prefix={<Icon type="user" />}
  17. type="number"
  18. placeholder="请输入内容"
  19. />)}
  20. </Form.Item>

要删除 Input的 value属性,否则控制台报错
antd 里面很多的组件都是内部用state去管理,state根据控制层的props变化,
这样state可以有设置默认值等各种行为

prefix表单前缀

  1. const prefixSelector = getFieldDecorator('prefix', {
  2. initialValue: '86',
  3. })(
  4. <Select style={{ width: 70 }}>
  5. <Option value="86">+86</Option>
  6. <Option value="87">+87</Option>
  7. </Select>,
  8. )
  9. <Form.Item label="Phone Number">
  10. {getFieldDecorator('phone', {
  11. rules: [{ required: true, message: 'Please input your phone number!' }],
  12. })(<Input addonBefore={prefixSelector} style={{ width: '100%' }} />)}
  13. </Form.Item>

ValuePropName

  1. <Form.Item
  2. {...formItemLayout}
  3. label="内容"
  4. required
  5. hasFeedback>
  6. {getFieldDecorator('content', {
  7. initialValue: '',
  8. valuePropName: 'fileList',
  9. // getValueFromEvent: this.handleChange,
  10. rules: [{
  11. required: true, message: '请输入内容'
  12. }],
  13. })(<Upload />)}
  14. </Form.Item>
  15. <Form.Item {...tailFormItemLayout}>
  16. {getFieldDecorator('agreement', {
  17. valuePropName: 'checked',
  18. })(
  19. <Checkbox>
  20. I have read the <a href="">agreement</a>
  21. </Checkbox>,
  22. )}
  23. </Form.Item>

表单验证

  1. function validateToNextPassword (rule, value, callback) {
  2. if (value && state.confirmDirty) {
  3. form.validateFields(['confirm'], { force: true });
  4. }
  5. callback();
  6. };
  7. <Form.Item label="E-mail">
  8. {getFieldDecorator('email', {
  9. rules: [
  10. {
  11. type: 'email',
  12. message: 'The input is not valid E-mail!',
  13. },
  14. {
  15. required: true,
  16. message: 'Please input your E-mail!',
  17. },
  18. {
  19. validator: this.validateToNextPassword,
  20. },
  21. ],
  22. })(<Input />)}
  23. </Form.Item>

Switch表单属性

Picker

  1. const pickProps = [
  2. "help",
  3. "extra",
  4. "labelCol",
  5. "wrapperCol",
  6. "colon",
  7. "hasFeedback",
  8. "validateStatus",
  9. "hasFeedback",
  10. "getValueFromEvent", // fieldProps
  11. "initialValue",
  12. "normalize",
  13. "trigger",
  14. "valuePropName",
  15. "validateTrigger",
  16. "validateFirst"
  17. ]