How to set the default values (multiple) of antd from dynamic form input box

  1. has done a project with react and antd components. When the dynamic form is needed, the from dynamic form of the antd official website (below)
is used.

clipboard.pnginputAdd field

clipboard.png

2. inputinitialValue

clipboard.png
when you click Add field, the default value of the following input is also Zhang Xiaopang.
what I want to achieve now is to display two different pieces of data on input by default, and then click Add field. The input that appears has no default value

.

is there a big boss to help solve it? Urgent, urgent
the following is the official code, which I used directly in the project. There is no change to
import {Form, Input, Icon, Button} from "antd";
const FormItem = Form.Item;

.

let uuid = 0;
class DynamicFieldSet extends React.Component {
remove = (k) = > {

const { form } = this.props;
// can use data-binding to get
const keys = form.getFieldValue("keys");
// We need at least one passenger
if (keys.length === 1) {
  return;
}

// can use data-binding to set
form.setFieldsValue({
  keys: keys.filter(key => key !== k),
});

}

add = () = > {

const { form } = this.props;
// can use data-binding to get
const keys = form.getFieldValue("keys");
const nextKeys = keys.concat(uuid);
uuidPP;
// can use data-binding to set
// important! notify form to detect changes
form.setFieldsValue({
  keys: nextKeys,
});

}

handleSubmit = (e) = > {

e.preventDefault();
this.props.form.validateFields((err, values) => {
  if (!err) {
    console.log("Received values of form: ", values);
  }
});

}

render () {

const { getFieldDecorator, getFieldValue } = this.props.form;
const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 4 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 20 },
  },
};
const formItemLayoutWithOutLabel = {
  wrapperCol: {
    xs: { span: 24, offset: 0 },
    sm: { span: 20, offset: 4 },
  },
};
getFieldDecorator("keys", { initialValue: [] });
const keys = getFieldValue("keys");
const formItems = keys.map((k, index) => {
  return (
    <FormItem
      {...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
      label={index === 0 ? "Passengers" : ""}
      required={false}
      key={k}
    >
      {getFieldDecorator(`names[${k}]`, {
        validateTrigger: ["onChange", "onBlur"],
        rules: [{
          required: true,
          whitespace: true,
          message: "Please input passenger"s name or delete this field.",
        }],
      })(
        <Input placeholder="passenger name" style={{ width: "60%", marginRight: 8 }} />
      )}
      {keys.length > 1 ? (
        <Icon
          className="dynamic-delete-button"
          type="minus-circle-o"
          disabled={keys.length === 1}
          onClick={() => this.remove(k)}
        />
      ) : null}
    </FormItem>
  );
});
return (
  <Form onSubmit={this.handleSubmit}>
    {formItems}
    <FormItem {...formItemLayoutWithOutLabel}>
      <Button type="dashed" onClick={this.add} style={{ width: "60%" }}>
        <Icon type="plus" /> Add field
      </Button>
    </FormItem>
    <FormItem {...formItemLayoutWithOutLabel}>
      <Button type="primary" htmlType="submit">Submit</Button>
    </FormItem>
  </Form>
);

}
}

const WrappedDynamicFieldSet = Form.create () (DynamicFieldSet);
ReactDOM.render (< WrappedDynamicFieldSet / >, mountNode);

Mar.03,2021

first, FormItem needs to be used with getFieldDecorator .
second, the name of getFieldDecorator in all FormItem cannot be repeated, preferably with the same name as the form feild .
third, dynamically add FormItem , and make sure that name of getFieldDecorator cannot be repeated with the previous one.
fourth, dynamically add FormItem elements, it is best to specify a unique key . For example: < Input key= {'only key'} / >


Please check out the blog: https://blog.csdn.net/smk108/. uses antd form to develop support for dynamically adding or decreasing table items. You can choose an implementation method with initial value components


.

post the code. I forgot to update it before.
/ / things needed by dynamic forms / / dynamic form removal events
remove = (k) = > {

const { form: { getFieldValue } } = this.props;
// can use data-binding to get
const keys = this.props.form.getFieldValue('keys');
// We need at least one passenger
if (keys.length === 1) {
  return;
}

// can use data-binding to set
this.props.form.setFieldsValue({
  keys: keys.filter(key => key !== k),
});

};
/ dynamic add / / the default is not available. You can execute the following add method in componentDidmount to render a dynamic input
add = () = > {

const { form: { getFieldValue } } = this.props;
// can use data-binding to get
const keys = this.props.form.getFieldValue('keys');
const nextKeys = keys.concat(this.state.uuid);
this.state.uuidPP;             //  
// can use data-binding to set
// important! notify form to detect changes
this.props.form.setFieldsValue({
  keys: nextKeys,
});

};
/ / definition and parsing within
getFieldDecorator ('keys', {initialValue: []}); / / keys is very important

const keys = getFieldValue('keys');
const formItems = keys.map((k, index) => {
  return (
    <FormItem
      // {...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
      {...formItemLayout}
      label={index === 0 ? '' : ''}
      required={false}
      key={k}
    >
      {getFieldDecorator(`names[${k}]`, {     //names names 
        validateTrigger: [
          'onChange',
          'onBlur',
        ],
        rules: [
          {
            required: true,
            whitespace: true,
            message: '.',
          },
        ],
      })(
        <Input
          placeholder=""
          style={{
            width: '90%',     // 90% inputremoveinput2 remove
            marginRight: 8,
          }}
        />,
      )}
      {keys.length > 1 ? (
        <Icon
          className="dynamic-delete-button"
          type="minus-circle-o"
          disabled={keys.length === 1}
          onClick={() => this.remove(k)}
        />
      ) : null}
    </FormItem>
  );
});

/ / the page uses
< Row >
< Col

in render
 xs={24}
 >
 {formItems}     **// formItems**
 <FormItem>
 <Button
 type="dashed"
 onClick={this.add}
 style={{ width: '100%' }}
 >
   <Icon type="plus" /> 
 </Button>
</FormItem>

< / Col >
< / Row >

Menu