mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 15:19:58 +08:00
145 lines
3.4 KiB
Markdown
145 lines
3.4 KiB
Markdown
---
|
|
order: 5
|
|
title:
|
|
zh-CN: 动态增减表单项
|
|
en-US: Dynamic Form Item
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
动态增加、减少表单项。
|
|
|
|
## en-US
|
|
|
|
Add or remove form items dynamically.
|
|
|
|
```jsx
|
|
import { Form, Input, Icon, Button } from 'antd';
|
|
|
|
let id = 0;
|
|
|
|
class DynamicFieldSet extends React.Component {
|
|
remove = k => {
|
|
const { form } = this.props;
|
|
// can use data-binding to get
|
|
const keys = form.getFieldValue('keys');
|
|
// We need at least one passenger
|
|
if (keys.length === 1) {
|
|
return;
|
|
}
|
|
|
|
// can use data-binding to set
|
|
form.setFieldsValue({
|
|
keys: keys.filter(key => key !== k),
|
|
});
|
|
};
|
|
|
|
add = () => {
|
|
const { form } = this.props;
|
|
// can use data-binding to get
|
|
const keys = form.getFieldValue('keys');
|
|
const nextKeys = keys.concat(id++);
|
|
// can use data-binding to set
|
|
// important! notify form to detect changes
|
|
form.setFieldsValue({
|
|
keys: nextKeys,
|
|
});
|
|
};
|
|
|
|
handleSubmit = e => {
|
|
e.preventDefault();
|
|
this.props.form.validateFields((err, values) => {
|
|
if (!err) {
|
|
const { keys, names } = values;
|
|
console.log('Received values of form: ', values);
|
|
console.log('Merged values:', keys.map(key => names[key]));
|
|
}
|
|
});
|
|
};
|
|
|
|
render() {
|
|
const { getFieldDecorator, getFieldValue } = this.props.form;
|
|
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 },
|
|
},
|
|
};
|
|
getFieldDecorator('keys', { initialValue: [] });
|
|
const keys = getFieldValue('keys');
|
|
const formItems = keys.map((k, index) => (
|
|
<Form.Item
|
|
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
|
|
label={index === 0 ? 'Passengers' : ''}
|
|
required={false}
|
|
key={k}
|
|
>
|
|
{getFieldDecorator(`names[${k}]`, {
|
|
validateTrigger: ['onChange', 'onBlur'],
|
|
rules: [
|
|
{
|
|
required: true,
|
|
whitespace: true,
|
|
message: "Please input passenger's name or delete this field.",
|
|
},
|
|
],
|
|
})(<Input placeholder="passenger name" style={{ width: '60%', marginRight: 8 }} />)}
|
|
{keys.length > 1 ? (
|
|
<Icon
|
|
className="dynamic-delete-button"
|
|
type="minus-circle-o"
|
|
onClick={() => this.remove(k)}
|
|
/>
|
|
) : null}
|
|
</Form.Item>
|
|
));
|
|
return (
|
|
<Form onSubmit={this.handleSubmit}>
|
|
{formItems}
|
|
<Form.Item {...formItemLayoutWithOutLabel}>
|
|
<Button type="dashed" onClick={this.add} style={{ width: '60%' }}>
|
|
<Icon type="plus" /> Add field
|
|
</Button>
|
|
</Form.Item>
|
|
<Form.Item {...formItemLayoutWithOutLabel}>
|
|
<Button type="primary" htmlType="submit">
|
|
Submit
|
|
</Button>
|
|
</Form.Item>
|
|
</Form>
|
|
);
|
|
}
|
|
}
|
|
|
|
const WrappedDynamicFieldSet = Form.create({ name: 'dynamic_form_item' })(DynamicFieldSet);
|
|
ReactDOM.render(<WrappedDynamicFieldSet />, mountNode);
|
|
```
|
|
|
|
```css
|
|
.dynamic-delete-button {
|
|
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;
|
|
}
|
|
```
|