2019-07-03 20:14:39 +08:00
|
|
|
---
|
|
|
|
order: 4
|
|
|
|
title:
|
|
|
|
zh-CN: 动态增减表单项
|
|
|
|
en-US: Dynamic Form Item
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2020-06-03 10:40:12 +08:00
|
|
|
动态增加、减少表单项。`add` 方法参数可用于设置初始值。
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2020-06-03 10:40:12 +08:00
|
|
|
Add or remove form items dynamically. `add` function support config initial value.
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2019-11-28 12:34:33 +08:00
|
|
|
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
|
2022-05-21 22:14:15 +08:00
|
|
|
import { Button, Form, Input } from 'antd';
|
|
|
|
import React from 'react';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
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 },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const onFinish = (values: any) => {
|
2019-07-03 20:14:39 +08:00
|
|
|
console.log('Received values of form:', values);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form name="dynamic_form_item" {...formItemLayoutWithOutLabel} onFinish={onFinish}>
|
2020-09-11 21:27:51 +08:00
|
|
|
<Form.List
|
|
|
|
name="names"
|
|
|
|
rules={[
|
|
|
|
{
|
|
|
|
validator: async (_, names) => {
|
|
|
|
if (!names || names.length < 2) {
|
|
|
|
return Promise.reject(new Error('At least 2 passengers'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
2020-10-20 13:29:04 +08:00
|
|
|
{(fields, { add, remove }, { errors }) => (
|
|
|
|
<>
|
|
|
|
{fields.map((field, index) => (
|
|
|
|
<Form.Item
|
|
|
|
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
|
|
|
|
label={index === 0 ? 'Passengers' : ''}
|
|
|
|
required={false}
|
|
|
|
key={field.key}
|
|
|
|
>
|
2019-07-03 20:14:39 +08:00
|
|
|
<Form.Item
|
2020-10-20 13:29:04 +08:00
|
|
|
{...field}
|
|
|
|
validateTrigger={['onChange', 'onBlur']}
|
|
|
|
rules={[
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
whitespace: true,
|
|
|
|
message: "Please input passenger's name or delete this field.",
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
noStyle
|
2019-07-03 20:14:39 +08:00
|
|
|
>
|
2020-10-20 13:29:04 +08:00
|
|
|
<Input placeholder="passenger name" style={{ width: '60%' }} />
|
2019-07-03 20:14:39 +08:00
|
|
|
</Form.Item>
|
2020-10-20 13:29:04 +08:00
|
|
|
{fields.length > 1 ? (
|
|
|
|
<MinusCircleOutlined
|
|
|
|
className="dynamic-delete-button"
|
|
|
|
onClick={() => remove(field.name)}
|
|
|
|
/>
|
|
|
|
) : null}
|
2019-07-03 20:14:39 +08:00
|
|
|
</Form.Item>
|
2020-10-20 13:29:04 +08:00
|
|
|
))}
|
|
|
|
<Form.Item>
|
|
|
|
<Button
|
|
|
|
type="dashed"
|
|
|
|
onClick={() => add()}
|
|
|
|
style={{ width: '60%' }}
|
|
|
|
icon={<PlusOutlined />}
|
|
|
|
>
|
|
|
|
Add field
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="dashed"
|
|
|
|
onClick={() => {
|
|
|
|
add('The head item', 0);
|
|
|
|
}}
|
|
|
|
style={{ width: '60%', marginTop: '20px' }}
|
|
|
|
icon={<PlusOutlined />}
|
|
|
|
>
|
|
|
|
Add field at head
|
|
|
|
</Button>
|
|
|
|
<Form.ErrorList errors={errors} />
|
|
|
|
</Form.Item>
|
|
|
|
</>
|
|
|
|
)}
|
2019-07-03 20:14:39 +08:00
|
|
|
</Form.List>
|
|
|
|
<Form.Item>
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-07-03 20:14:39 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
```css
|
|
|
|
.dynamic-delete-button {
|
|
|
|
position: relative;
|
|
|
|
top: 4px;
|
2021-01-30 19:39:02 +08:00
|
|
|
margin: 0 8px;
|
2019-07-03 20:14:39 +08:00
|
|
|
color: #999;
|
2021-01-30 19:39:02 +08:00
|
|
|
font-size: 24px;
|
|
|
|
cursor: pointer;
|
2019-07-03 20:14:39 +08:00
|
|
|
transition: all 0.3s;
|
|
|
|
}
|
|
|
|
.dynamic-delete-button:hover {
|
|
|
|
color: #777;
|
|
|
|
}
|
|
|
|
.dynamic-delete-button[disabled] {
|
|
|
|
cursor: not-allowed;
|
|
|
|
opacity: 0.5;
|
|
|
|
}
|
|
|
|
```
|
2019-12-25 17:12:36 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
[data-theme="dark"] .dynamic-delete-button {
|
|
|
|
color: rgba(255,255,255,.45);
|
|
|
|
}
|
|
|
|
[data-theme="dark"] .dynamic-delete-button:hover {
|
|
|
|
color: rgba(255,255,255,.65);
|
|
|
|
}
|
|
|
|
</style>
|