2016-12-13 12:12:26 +08:00
|
|
|
---
|
|
|
|
order: 12
|
|
|
|
title:
|
|
|
|
zh-CN: 表单联动
|
|
|
|
en-US: Coordinated Controls
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用 `setFieldsValue` 来动态设置其他控件的值。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Use `setFieldsValue` to set other control's value programmaticly.
|
|
|
|
|
2017-02-13 10:55:53 +08:00
|
|
|
````jsx
|
2016-12-13 12:12:26 +08:00
|
|
|
import { Form, Select, Input, Button } from 'antd';
|
|
|
|
const FormItem = Form.Item;
|
|
|
|
const Option = Select.Option;
|
|
|
|
|
2017-02-20 11:50:57 +08:00
|
|
|
class App extends React.Component {
|
|
|
|
handleSubmit = (e) => {
|
2016-12-13 12:12:26 +08:00
|
|
|
e.preventDefault();
|
|
|
|
this.props.form.validateFields((err, values) => {
|
|
|
|
if (!err) {
|
|
|
|
console.log('Received values of form: ', values);
|
|
|
|
}
|
|
|
|
});
|
2017-02-20 11:50:57 +08:00
|
|
|
}
|
|
|
|
handleSelectChange = (value) => {
|
2016-12-13 12:12:26 +08:00
|
|
|
console.log(value);
|
|
|
|
this.props.form.setFieldsValue({
|
|
|
|
note: `Hi, ${value === 'male' ? 'man' : 'lady'}!`,
|
|
|
|
});
|
2017-02-20 11:50:57 +08:00
|
|
|
}
|
2016-12-13 12:12:26 +08:00
|
|
|
render() {
|
|
|
|
const { getFieldDecorator } = this.props.form;
|
|
|
|
return (
|
|
|
|
<Form onSubmit={this.handleSubmit}>
|
|
|
|
<FormItem
|
|
|
|
label="Note"
|
|
|
|
labelCol={{ span: 4 }}
|
|
|
|
wrapperCol={{ span: 8 }}
|
|
|
|
>
|
|
|
|
{getFieldDecorator('note', {
|
|
|
|
rules: [{ required: true, message: 'Please input your note!' }],
|
|
|
|
})(
|
|
|
|
<Input />
|
|
|
|
)}
|
|
|
|
</FormItem>
|
|
|
|
<FormItem
|
|
|
|
label="Gender"
|
|
|
|
labelCol={{ span: 4 }}
|
|
|
|
wrapperCol={{ span: 8 }}
|
|
|
|
>
|
|
|
|
{getFieldDecorator('gender', {
|
|
|
|
rules: [{ required: true, message: 'Please select your gender!' }],
|
|
|
|
onChange: this.handleSelectChange,
|
|
|
|
})(
|
|
|
|
<Select placeholder="Select a option and change input text above">
|
|
|
|
<Option value="male">male</Option>
|
|
|
|
<Option value="female">female</Option>
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
</FormItem>
|
2017-03-02 10:49:06 +08:00
|
|
|
<FormItem
|
2017-03-10 19:19:36 +08:00
|
|
|
wrapperCol={{ span: 8, offset: 4 }}
|
2017-03-02 10:49:06 +08:00
|
|
|
>
|
2016-12-13 12:12:26 +08:00
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</FormItem>
|
|
|
|
</Form>
|
|
|
|
);
|
2017-02-20 11:50:57 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 12:12:26 +08:00
|
|
|
|
2017-02-20 11:50:57 +08:00
|
|
|
const WrappedApp = Form.create()(App);
|
|
|
|
|
|
|
|
ReactDOM.render(<WrappedApp />, mountNode);
|
2016-12-13 12:12:26 +08:00
|
|
|
````
|