--- order: 4 title: zh-CN: 动态增减表单项 en-US: Dynamic Form Item --- ## zh-CN 动态增加、减少表单项。`add` 方法参数可用于设置初始值。 ## en-US Add or remove form items dynamically. `add` function support config initial value. ```jsx import { Form, Input, Button } from 'antd'; import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'; 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 }, }, }; const DynamicFieldSet = () => { const onFinish = values => { console.log('Received values of form:', values); }; return (
{ if (!names || names.length < 2) { return Promise.reject(new Error('At least 2 passengers')); } }, }, ]} > {(fields, { add, remove }, { errors }) => ( <> {fields.map((field, index) => ( {fields.length > 1 ? ( remove(field.name)} /> ) : null} ))} )}
); }; ReactDOM.render(, mountNode); ``` ```css .dynamic-delete-button { margin: 0 8px; cursor: pointer; position: relative; top: 4px; font-size: 24px; color: #999; transition: all 0.3s; } .dynamic-delete-button:hover { color: #777; } .dynamic-delete-button[disabled] { cursor: not-allowed; opacity: 0.5; } ```