2020-05-10 10:39:30 +08:00
|
|
|
---
|
|
|
|
order: 4.1
|
|
|
|
title:
|
|
|
|
zh-CN: 动态增减嵌套字段
|
|
|
|
en-US: Dynamic Form nest Items
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2021-10-29 18:24:50 +08:00
|
|
|
嵌套表单字段需要对 `field` 进行拓展,将 `field.name` 应用于控制字段。
|
2020-05-10 10:39:30 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2021-10-29 18:24:50 +08:00
|
|
|
Nest dynamic field need extends `field`. Pass `field.name` to nest item.
|
2020-05-10 10:39:30 +08:00
|
|
|
|
|
|
|
```jsx
|
|
|
|
import { Form, Input, Button, Space } from 'antd';
|
|
|
|
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
|
|
|
|
|
|
|
|
const Demo = () => {
|
|
|
|
const onFinish = values => {
|
|
|
|
console.log('Received values of form:', values);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form name="dynamic_form_nest_item" onFinish={onFinish} autoComplete="off">
|
|
|
|
<Form.List name="users">
|
2020-10-20 13:29:04 +08:00
|
|
|
{(fields, { add, remove }) => (
|
|
|
|
<>
|
2021-10-29 18:24:50 +08:00
|
|
|
{fields.map(({ key, name, ...restField }) => (
|
2021-03-24 12:24:22 +08:00
|
|
|
<Space key={key} style={{ display: 'flex', marginBottom: 8 }} align="baseline">
|
2020-10-20 13:29:04 +08:00
|
|
|
<Form.Item
|
2021-03-24 12:24:22 +08:00
|
|
|
{...restField}
|
|
|
|
name={[name, 'first']}
|
2020-10-20 13:29:04 +08:00
|
|
|
rules={[{ required: true, message: 'Missing first name' }]}
|
2020-05-10 10:39:30 +08:00
|
|
|
>
|
2020-10-20 13:29:04 +08:00
|
|
|
<Input placeholder="First Name" />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item
|
2021-03-24 12:24:22 +08:00
|
|
|
{...restField}
|
|
|
|
name={[name, 'last']}
|
2020-10-20 13:29:04 +08:00
|
|
|
rules={[{ required: true, message: 'Missing last name' }]}
|
|
|
|
>
|
|
|
|
<Input placeholder="Last Name" />
|
|
|
|
</Form.Item>
|
2021-03-24 12:24:22 +08:00
|
|
|
<MinusCircleOutlined onClick={() => remove(name)} />
|
2020-10-20 13:29:04 +08:00
|
|
|
</Space>
|
|
|
|
))}
|
|
|
|
<Form.Item>
|
|
|
|
<Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
|
|
|
|
Add field
|
|
|
|
</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</>
|
|
|
|
)}
|
2020-05-10 10:39:30 +08:00
|
|
|
</Form.List>
|
|
|
|
<Form.Item>
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
export default Demo;
|
2020-05-10 10:39:30 +08:00
|
|
|
```
|