2019-07-03 20:14:39 +08:00
|
|
|
|
---
|
|
|
|
|
order: 2
|
|
|
|
|
title:
|
|
|
|
|
zh-CN: 表单方法调用(Class component)
|
|
|
|
|
en-US: Form methods (Class component)
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
|
|
我们推荐使用 `Form.useForm` 创建表单数据域进行控制。如果是在 class component 下,你也可以通过 `ref` 获取数据域。
|
|
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
|
|
We recommend use `Form.useForm` to create data control. If you are using class component, you can get it by `ref`.
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
import { Form, Input, Button, Select } from 'antd';
|
2020-04-21 16:42:56 +08:00
|
|
|
|
import { FormInstance } from 'antd/lib/form';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
|
|
const { Option } = Select;
|
|
|
|
|
|
|
|
|
|
const layout = {
|
|
|
|
|
labelCol: { span: 8 },
|
|
|
|
|
wrapperCol: { span: 16 },
|
|
|
|
|
};
|
|
|
|
|
const tailLayout = {
|
|
|
|
|
wrapperCol: { offset: 8, span: 16 },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Demo extends React.Component {
|
2020-04-21 16:42:56 +08:00
|
|
|
|
formRef = React.createRef<FormInstance>();
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
|
onGenderChange = (value: string) => {
|
2020-11-12 23:20:13 +08:00
|
|
|
|
switch (value) {
|
|
|
|
|
case 'male':
|
2020-12-17 15:09:18 +08:00
|
|
|
|
this.formRef.current!.setFieldsValue({ note: 'Hi, man!' });
|
2020-11-12 23:20:13 +08:00
|
|
|
|
return;
|
|
|
|
|
case 'female':
|
2020-12-17 15:09:18 +08:00
|
|
|
|
this.formRef.current!.setFieldsValue({ note: 'Hi, lady!' });
|
2020-11-12 23:20:13 +08:00
|
|
|
|
return;
|
|
|
|
|
case 'other':
|
2020-12-17 15:09:18 +08:00
|
|
|
|
this.formRef.current!.setFieldsValue({ note: 'Hi there!' });
|
2020-11-12 23:20:13 +08:00
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
|
onFinish = (values: any) => {
|
2019-07-03 20:14:39 +08:00
|
|
|
|
console.log(values);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onReset = () => {
|
2020-12-17 15:09:18 +08:00
|
|
|
|
this.formRef.current!.resetFields();
|
2019-07-03 20:14:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onFill = () => {
|
2020-12-17 15:09:18 +08:00
|
|
|
|
this.formRef.current!.setFieldsValue({
|
2019-07-03 20:14:39 +08:00
|
|
|
|
note: 'Hello world!',
|
|
|
|
|
gender: 'male',
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<Form {...layout} ref={this.formRef} name="control-ref" onFinish={this.onFinish}>
|
|
|
|
|
<Form.Item name="note" label="Note" rules={[{ required: true }]}>
|
|
|
|
|
<Input />
|
|
|
|
|
</Form.Item>
|
|
|
|
|
<Form.Item name="gender" label="Gender" rules={[{ required: true }]}>
|
|
|
|
|
<Select
|
|
|
|
|
placeholder="Select a option and change input text above"
|
|
|
|
|
onChange={this.onGenderChange}
|
|
|
|
|
allowClear
|
|
|
|
|
>
|
|
|
|
|
<Option value="male">male</Option>
|
|
|
|
|
<Option value="female">female</Option>
|
|
|
|
|
<Option value="other">other</Option>
|
|
|
|
|
</Select>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
<Form.Item
|
2019-07-19 14:07:39 +08:00
|
|
|
|
noStyle
|
2019-07-03 20:14:39 +08:00
|
|
|
|
shouldUpdate={(prevValues, currentValues) => prevValues.gender !== currentValues.gender}
|
|
|
|
|
>
|
2021-02-23 10:45:11 +08:00
|
|
|
|
{({ getFieldValue }) =>
|
|
|
|
|
getFieldValue('gender') === 'other' ? (
|
2019-07-03 20:14:39 +08:00
|
|
|
|
<Form.Item
|
|
|
|
|
name="customizeGender"
|
|
|
|
|
label="Customize Gender"
|
|
|
|
|
rules={[{ required: true }]}
|
|
|
|
|
>
|
|
|
|
|
<Input />
|
|
|
|
|
</Form.Item>
|
2021-02-23 10:45:11 +08:00
|
|
|
|
) : null
|
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
<Form.Item {...tailLayout}>
|
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
|
Submit
|
|
|
|
|
</Button>
|
|
|
|
|
<Button htmlType="button" onClick={this.onReset}>
|
|
|
|
|
Reset
|
|
|
|
|
</Button>
|
|
|
|
|
<Button type="link" htmlType="button" onClick={this.onFill}>
|
|
|
|
|
Fill form
|
|
|
|
|
</Button>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```css
|
|
|
|
|
#components-form-demo-control-ref .ant-btn {
|
|
|
|
|
margin-right: 8px;
|
|
|
|
|
}
|
|
|
|
|
```
|