2020-09-19 23:21:25 +08:00
|
|
|
---
|
|
|
|
order: 4.2
|
|
|
|
title:
|
|
|
|
zh-CN: 复杂的动态增减表单项
|
|
|
|
en-US: Complex Dynamic Form Item
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
这个例子演示了一个表单中包含多个表单控件的情况。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
This example demonstrates the case that a form contains multiple form controls.
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
import { Form, Input, Button, Space, Select } from 'antd';
|
|
|
|
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
|
|
|
|
|
2020-10-20 13:29:04 +08:00
|
|
|
const { Option } = Select;
|
|
|
|
|
2020-09-19 23:21:25 +08:00
|
|
|
const areas = [
|
|
|
|
{ label: 'Beijing', value: 'Beijing' },
|
|
|
|
{ label: 'Shanghai', value: 'Shanghai' },
|
|
|
|
];
|
|
|
|
|
|
|
|
const sights = {
|
|
|
|
Beijing: ['Tiananmen', 'Great Wall'],
|
|
|
|
Shanghai: ['Oriental Pearl', 'The Bund'],
|
|
|
|
};
|
|
|
|
|
|
|
|
const Demo = () => {
|
|
|
|
const [form] = Form.useForm();
|
|
|
|
|
|
|
|
const onFinish = values => {
|
|
|
|
console.log('Received values of form:', values);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleChange = () => {
|
|
|
|
form.setFieldsValue({ sights: [] });
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form form={form} name="dynamic_form_nest_item" onFinish={onFinish} autoComplete="off">
|
|
|
|
<Form.Item name="area" label="Area" rules={[{ required: true, message: 'Missing area' }]}>
|
|
|
|
<Select options={areas} onChange={handleChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.List name="sights">
|
2020-10-20 13:29:04 +08:00
|
|
|
{(fields, { add, remove }) => (
|
|
|
|
<>
|
|
|
|
{fields.map(field => (
|
|
|
|
<Space key={field.key} align="baseline">
|
|
|
|
<Form.Item
|
|
|
|
noStyle
|
|
|
|
shouldUpdate={(prevValues, curValues) =>
|
|
|
|
prevValues.area !== curValues.area || prevValues.sights !== curValues.sights
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{() => (
|
|
|
|
<Form.Item
|
|
|
|
{...field}
|
|
|
|
label="Sight"
|
|
|
|
name={[field.name, 'sight']}
|
|
|
|
fieldKey={[field.fieldKey, 'sight']}
|
|
|
|
rules={[{ required: true, message: 'Missing sight' }]}
|
|
|
|
>
|
|
|
|
<Select disabled={!form.getFieldValue('area')} style={{ width: 130 }}>
|
|
|
|
{(sights[form.getFieldValue('area')] || []).map(item => (
|
|
|
|
<Option key={item} value={item}>
|
|
|
|
{item}
|
|
|
|
</Option>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</Form.Item>
|
|
|
|
)}
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item
|
|
|
|
{...field}
|
|
|
|
label="Price"
|
|
|
|
name={[field.name, 'price']}
|
|
|
|
fieldKey={[field.fieldKey, 'price']}
|
|
|
|
rules={[{ required: true, message: 'Missing price' }]}
|
|
|
|
>
|
|
|
|
<Input />
|
|
|
|
</Form.Item>
|
2020-09-19 23:21:25 +08:00
|
|
|
|
2020-10-20 13:29:04 +08:00
|
|
|
<MinusCircleOutlined onClick={() => remove(field.name)} />
|
|
|
|
</Space>
|
|
|
|
))}
|
2020-09-19 23:21:25 +08:00
|
|
|
|
2020-10-20 13:29:04 +08:00
|
|
|
<Form.Item>
|
|
|
|
<Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
|
|
|
|
Add sights
|
|
|
|
</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</>
|
|
|
|
)}
|
2020-09-19 23:21:25 +08:00
|
|
|
</Form.List>
|
|
|
|
<Form.Item>
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
|
|
```
|