--- order: 15 title: 动态增减表单项 --- 动态增加、减少表单项。 ````jsx import { Form, Input, Button } from 'antd'; let uuid = 0; let Demo = React.createClass({ remove(k) { const { form } = this.props; // can use data-binding to get let keys = form.getFieldValue('keys'); keys = keys.filter((key) => { return key !== k; }); // can use data-binding to set form.setFieldsValue({ keys, }); }, add() { uuid++; const { form } = this.props; // can use data-binding to get let keys = form.getFieldValue('keys'); keys = keys.concat(uuid); // can use data-binding to set // important! notify form to detect changes form.setFieldsValue({ keys, }); }, submit(e) { e.preventDefault(); console.log(this.props.form.getFieldsValue()); }, render() { const { getFieldProps, getFieldValue } = this.props.form; getFieldProps('keys', { initialValue: [0], }); const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 18 }, }; const formItems = getFieldValue('keys').map((k) => { return ( ); }); return (
{formItems}
); }, }); Demo = Form.create()(Demo); ReactDOM.render(, mountNode); ````