2016-04-12 17:02:17 +08:00
|
|
|
---
|
2016-10-31 15:42:57 +08:00
|
|
|
order: 5
|
2016-09-01 11:48:38 +08:00
|
|
|
title:
|
2016-07-31 09:53:51 +08:00
|
|
|
zh-CN: 动态增减表单项
|
2016-10-31 15:42:57 +08:00
|
|
|
en-US: Dynamic Form Item
|
2016-04-12 17:02:17 +08:00
|
|
|
---
|
|
|
|
|
2016-07-31 09:53:51 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-04-12 17:02:17 +08:00
|
|
|
动态增加、减少表单项。
|
|
|
|
|
2016-07-31 09:53:51 +08:00
|
|
|
## en-US
|
|
|
|
|
2016-07-31 10:01:58 +08:00
|
|
|
Add or remove form items dynamically.
|
2016-07-31 09:53:51 +08:00
|
|
|
|
2017-02-13 10:55:53 +08:00
|
|
|
````jsx
|
2016-11-10 16:11:22 +08:00
|
|
|
import { Form, Input, Icon, Button } from 'antd';
|
|
|
|
const FormItem = Form.Item;
|
2016-04-12 17:02:17 +08:00
|
|
|
|
|
|
|
let uuid = 0;
|
2016-11-10 16:11:22 +08:00
|
|
|
class DynamicFieldSet extends React.Component {
|
|
|
|
remove = (k) => {
|
2016-04-12 17:02:17 +08:00
|
|
|
const { form } = this.props;
|
|
|
|
// can use data-binding to get
|
2016-10-31 15:42:57 +08:00
|
|
|
const keys = form.getFieldValue('keys');
|
2016-11-10 16:11:22 +08:00
|
|
|
// We need at least one passenger
|
|
|
|
if (keys.length === 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-12 17:02:17 +08:00
|
|
|
// can use data-binding to set
|
|
|
|
form.setFieldsValue({
|
2016-11-02 18:08:48 +08:00
|
|
|
keys: keys.filter(key => key !== k),
|
2016-04-12 17:02:17 +08:00
|
|
|
});
|
2016-11-10 16:11:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
add = () => {
|
2016-04-12 17:02:17 +08:00
|
|
|
uuid++;
|
|
|
|
const { form } = this.props;
|
|
|
|
// can use data-binding to get
|
2016-10-31 15:42:57 +08:00
|
|
|
const keys = form.getFieldValue('keys');
|
|
|
|
const nextKeys = keys.concat(uuid);
|
2016-04-12 17:02:17 +08:00
|
|
|
// can use data-binding to set
|
|
|
|
// important! notify form to detect changes
|
|
|
|
form.setFieldsValue({
|
2016-10-31 15:42:57 +08:00
|
|
|
keys: nextKeys,
|
2016-04-12 17:02:17 +08:00
|
|
|
});
|
2016-11-10 16:11:22 +08:00
|
|
|
}
|
|
|
|
|
2016-12-31 23:47:07 +08:00
|
|
|
handleSubmit = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.form.validateFields((err, values) => {
|
|
|
|
if (!err) {
|
|
|
|
console.log('Received values of form: ', values);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-12 17:02:17 +08:00
|
|
|
render() {
|
2016-09-01 11:48:38 +08:00
|
|
|
const { getFieldDecorator, getFieldValue } = this.props.form;
|
2016-04-12 17:02:17 +08:00
|
|
|
const formItemLayout = {
|
2017-03-15 11:25:58 +08:00
|
|
|
labelCol: {
|
|
|
|
xs: { span: 24 },
|
|
|
|
sm: { span: 4 },
|
|
|
|
},
|
|
|
|
wrapperCol: {
|
|
|
|
xs: { span: 24 },
|
|
|
|
sm: { span: 20 },
|
|
|
|
},
|
2016-11-10 16:11:22 +08:00
|
|
|
};
|
|
|
|
const formItemLayoutWithOutLabel = {
|
2017-03-15 11:25:58 +08:00
|
|
|
wrapperCol: {
|
|
|
|
xs: { span: 24, offset: 0 },
|
|
|
|
sm: { span: 20, offset: 4 },
|
|
|
|
},
|
2016-04-12 17:02:17 +08:00
|
|
|
};
|
2016-12-31 23:47:07 +08:00
|
|
|
getFieldDecorator('keys', { initialValue: [] });
|
2016-11-10 16:11:22 +08:00
|
|
|
const keys = getFieldValue('keys');
|
|
|
|
const formItems = keys.map((k, index) => {
|
2016-04-12 17:02:17 +08:00
|
|
|
return (
|
2016-11-10 16:11:22 +08:00
|
|
|
<FormItem
|
|
|
|
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
|
|
|
|
label={index === 0 ? 'Passengers' : ''}
|
|
|
|
required={false}
|
|
|
|
key={k}
|
|
|
|
>
|
|
|
|
{getFieldDecorator(`names-${k}`, {
|
|
|
|
validateTrigger: ['onChange', 'onBlur'],
|
2016-07-04 12:13:29 +08:00
|
|
|
rules: [{
|
|
|
|
required: true,
|
|
|
|
whitespace: true,
|
2016-11-10 16:11:22 +08:00
|
|
|
message: "Please input passenger's name or delete this field.",
|
2016-07-04 12:13:29 +08:00
|
|
|
}],
|
2016-09-01 11:48:38 +08:00
|
|
|
})(
|
2016-11-23 20:17:44 +08:00
|
|
|
<Input placeholder="passenger name" style={{ width: '60%', marginRight: 8 }} />
|
2016-09-01 11:48:38 +08:00
|
|
|
)}
|
2016-11-10 16:11:22 +08:00
|
|
|
<Icon
|
2016-11-23 20:17:44 +08:00
|
|
|
className="dynamic-delete-button"
|
2016-11-10 16:11:22 +08:00
|
|
|
type="minus-circle-o"
|
|
|
|
disabled={keys.length === 1}
|
|
|
|
onClick={() => this.remove(k)}
|
|
|
|
/>
|
|
|
|
</FormItem>
|
2016-04-12 17:02:17 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
return (
|
2017-01-05 16:57:41 +08:00
|
|
|
<Form onSubmit={this.handleSubmit}>
|
2016-04-29 12:13:27 +08:00
|
|
|
{formItems}
|
2016-11-10 16:11:22 +08:00
|
|
|
<FormItem {...formItemLayoutWithOutLabel}>
|
|
|
|
<Button type="dashed" onClick={this.add} style={{ width: '60%' }}>
|
2016-12-31 23:47:07 +08:00
|
|
|
<Icon type="plus" /> Add field
|
2016-11-10 16:11:22 +08:00
|
|
|
</Button>
|
|
|
|
</FormItem>
|
2016-12-31 23:47:07 +08:00
|
|
|
<FormItem {...formItemLayoutWithOutLabel}>
|
|
|
|
<Button type="primary" htmlType="submit" size="large">Submit</Button>
|
|
|
|
</FormItem>
|
2016-04-12 17:02:17 +08:00
|
|
|
</Form>
|
|
|
|
);
|
2016-11-10 16:11:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const WrappedDynamicFieldSet = Form.create()(DynamicFieldSet);
|
|
|
|
ReactDOM.render(<WrappedDynamicFieldSet />, mountNode);
|
|
|
|
````
|
2016-04-12 17:02:17 +08:00
|
|
|
|
2016-11-10 16:11:22 +08:00
|
|
|
````css
|
2016-11-23 20:17:44 +08:00
|
|
|
.dynamic-delete-button {
|
2016-11-10 16:11:22 +08:00
|
|
|
cursor: pointer;
|
|
|
|
position: relative;
|
|
|
|
top: 4px;
|
|
|
|
font-size: 24px;
|
|
|
|
color: #999;
|
2016-11-23 20:17:44 +08:00
|
|
|
transition: all .3s;
|
2016-11-10 16:11:22 +08:00
|
|
|
}
|
2016-11-23 20:17:44 +08:00
|
|
|
.dynamic-delete-button:hover {
|
|
|
|
color: #777;
|
|
|
|
}
|
|
|
|
.dynamic-delete-button[disabled] {
|
2016-11-10 16:11:22 +08:00
|
|
|
cursor: not-allowed;
|
2016-11-23 20:17:44 +08:00
|
|
|
opacity: 0.5;
|
2016-11-10 16:11:22 +08:00
|
|
|
}
|
2016-04-12 17:02:17 +08:00
|
|
|
````
|