mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-16 01:41:02 +08:00
6776bb8916
* docs: update demo * chore: add script * test: fix demo test * docs: convert demos * chore: move script * test: remove react-dom import * chore: update deps * docs: update riddle js * test: fix image test * docs: fix riddle demo
68 lines
1.9 KiB
Markdown
68 lines
1.9 KiB
Markdown
---
|
|
order: 4.1
|
|
title:
|
|
zh-CN: 动态增减嵌套字段
|
|
en-US: Dynamic Form nest Items
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
嵌套表单字段需要对 `field` 进行拓展,将 `field.name` 应用于控制字段。
|
|
|
|
## en-US
|
|
|
|
Nest dynamic field need extends `field`. Pass `field.name` to nest item.
|
|
|
|
```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">
|
|
{(fields, { add, remove }) => (
|
|
<>
|
|
{fields.map(({ key, name, ...restField }) => (
|
|
<Space key={key} style={{ display: 'flex', marginBottom: 8 }} align="baseline">
|
|
<Form.Item
|
|
{...restField}
|
|
name={[name, 'first']}
|
|
rules={[{ required: true, message: 'Missing first name' }]}
|
|
>
|
|
<Input placeholder="First Name" />
|
|
</Form.Item>
|
|
<Form.Item
|
|
{...restField}
|
|
name={[name, 'last']}
|
|
rules={[{ required: true, message: 'Missing last name' }]}
|
|
>
|
|
<Input placeholder="Last Name" />
|
|
</Form.Item>
|
|
<MinusCircleOutlined onClick={() => remove(name)} />
|
|
</Space>
|
|
))}
|
|
<Form.Item>
|
|
<Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
|
|
Add field
|
|
</Button>
|
|
</Form.Item>
|
|
</>
|
|
)}
|
|
</Form.List>
|
|
<Form.Item>
|
|
<Button type="primary" htmlType="submit">
|
|
Submit
|
|
</Button>
|
|
</Form.Item>
|
|
</Form>
|
|
);
|
|
};
|
|
|
|
export default () => <Demo />;
|
|
```
|