ant-design/components/form/demo/coordinated.md

74 lines
1.8 KiB
Markdown
Raw Normal View History

2016-12-13 12:12:26 +08:00
---
order: 11
2016-12-13 12:12:26 +08:00
title:
zh-CN: 表单联动
en-US: Coordinated Controls
---
## zh-CN
使用 `setFieldsValue` 来动态设置其他控件的值。
## en-US
Use `setFieldsValue` to set other control's value programmaticly.
2019-05-07 14:57:32 +08:00
```jsx
import { Form, Select, Input, Button } from 'antd';
2018-06-27 15:55:04 +08:00
2018-12-22 16:48:30 +08:00
const { Option } = Select;
2016-12-13 12:12:26 +08:00
class App extends React.Component {
2019-05-07 14:57:32 +08:00
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);
}
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +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'}!`,
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2016-12-13 12:12:26 +08:00
render() {
const { getFieldDecorator } = this.props.form;
return (
<Form labelCol={{ span: 5 }} wrapperCol={{ span: 12 }} onSubmit={this.handleSubmit}>
2019-05-07 14:57:32 +08:00
<Form.Item label="Note">
2016-12-13 12:12:26 +08:00
{getFieldDecorator('note', {
rules: [{ required: true, message: 'Please input your note!' }],
2019-05-07 14:57:32 +08:00
})(<Input />)}
2018-12-22 16:48:30 +08:00
</Form.Item>
2019-05-07 14:57:32 +08:00
<Form.Item label="Gender">
2016-12-13 12:12:26 +08:00
{getFieldDecorator('gender', {
rules: [{ required: true, message: 'Please select your gender!' }],
})(
2017-06-19 11:25:30 +08:00
<Select
placeholder="Select a option and change input text above"
onChange={this.handleSelectChange}
>
2016-12-13 12:12:26 +08:00
<Option value="male">male</Option>
<Option value="female">female</Option>
2019-05-07 14:57:32 +08:00
</Select>,
2016-12-13 12:12:26 +08:00
)}
2018-12-22 16:48:30 +08:00
</Form.Item>
2019-05-07 14:57:32 +08:00
<Form.Item wrapperCol={{ span: 12, offset: 5 }}>
2016-12-13 12:12:26 +08:00
<Button type="primary" htmlType="submit">
Submit
</Button>
2018-12-22 16:48:30 +08:00
</Form.Item>
2016-12-13 12:12:26 +08:00
</Form>
);
}
}
2016-12-13 12:12:26 +08:00
2018-12-17 10:02:08 +08:00
const WrappedApp = Form.create({ name: 'coordinated' })(App);
ReactDOM.render(<WrappedApp />, mountNode);
2019-05-07 14:57:32 +08:00
```