2016-04-12 17:02:17 +08:00
|
|
|
|
---
|
|
|
|
|
order: 15
|
2016-09-01 11:48:38 +08:00
|
|
|
|
title:
|
2016-07-31 09:53:51 +08:00
|
|
|
|
zh-CN: 动态增减表单项
|
|
|
|
|
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
|
|
|
|
|
2016-04-12 17:02:17 +08:00
|
|
|
|
````jsx
|
|
|
|
|
import { Form, Input, Button } from 'antd';
|
|
|
|
|
|
|
|
|
|
let uuid = 0;
|
|
|
|
|
let Demo = React.createClass({
|
2016-09-01 11:48:38 +08:00
|
|
|
|
componentWillMount() {
|
|
|
|
|
this.props.form.setFieldsValue({
|
|
|
|
|
keys: [0],
|
|
|
|
|
});
|
|
|
|
|
},
|
2016-04-12 17:02:17 +08:00
|
|
|
|
remove(k) {
|
|
|
|
|
const { form } = this.props;
|
|
|
|
|
// can use data-binding to get
|
|
|
|
|
let keys = form.getFieldValue('keys');
|
|
|
|
|
keys = keys.filter((key) => {
|
|
|
|
|
return key !== k;
|
|
|
|
|
});
|
|
|
|
|
// can use data-binding to set
|
|
|
|
|
form.setFieldsValue({
|
|
|
|
|
keys,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
add() {
|
|
|
|
|
uuid++;
|
|
|
|
|
const { form } = this.props;
|
|
|
|
|
// can use data-binding to get
|
|
|
|
|
let keys = form.getFieldValue('keys');
|
|
|
|
|
keys = keys.concat(uuid);
|
|
|
|
|
// can use data-binding to set
|
|
|
|
|
// important! notify form to detect changes
|
|
|
|
|
form.setFieldsValue({
|
|
|
|
|
keys,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
submit(e) {
|
|
|
|
|
e.preventDefault();
|
2016-07-04 12:13:29 +08:00
|
|
|
|
this.props.form.validateFields((errors, values) => {
|
|
|
|
|
if (errors) {
|
|
|
|
|
console.log(errors);
|
|
|
|
|
}
|
|
|
|
|
console.log(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 = {
|
|
|
|
|
labelCol: { span: 6 },
|
|
|
|
|
wrapperCol: { span: 18 },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const formItems = getFieldValue('keys').map((k) => {
|
|
|
|
|
return (
|
2016-07-31 09:53:51 +08:00
|
|
|
|
<Form.Item {...formItemLayout} label={`good friend${k}:`} key={k}>
|
2016-09-01 11:48:38 +08:00
|
|
|
|
{getFieldDecorator(`name${k}`, {
|
2016-07-04 12:13:29 +08:00
|
|
|
|
rules: [{
|
|
|
|
|
required: true,
|
|
|
|
|
whitespace: true,
|
2016-07-31 09:53:51 +08:00
|
|
|
|
message: "Your good friend's name",
|
2016-07-04 12:13:29 +08:00
|
|
|
|
}],
|
2016-09-01 11:48:38 +08:00
|
|
|
|
})(
|
2016-09-21 22:27:26 +08:00
|
|
|
|
<Input style={{ width: '60%', marginRight: 8 }} />
|
2016-09-01 11:48:38 +08:00
|
|
|
|
)}
|
2016-07-31 09:53:51 +08:00
|
|
|
|
<Button onClick={() => this.remove(k)}>remove</Button>
|
2016-04-12 17:02:17 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
return (
|
2016-09-09 13:55:46 +08:00
|
|
|
|
<Form horizontal>
|
2016-04-29 12:13:27 +08:00
|
|
|
|
{formItems}
|
2016-04-12 17:02:17 +08:00
|
|
|
|
<Form.Item wrapperCol={{ span: 18, offset: 6 }}>
|
2016-07-31 09:53:51 +08:00
|
|
|
|
<Button onClick={this.add} style={{ marginRight: 8 }}>add good friend</Button>
|
|
|
|
|
<Button type="primary" onClick={this.submit}>submit</Button>
|
2016-04-12 17:02:17 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Demo = Form.create()(Demo);
|
|
|
|
|
|
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
|
|
|
````
|