2019-07-03 20:14:39 +08:00
|
|
|
|
---
|
|
|
|
|
order: 1
|
|
|
|
|
title:
|
|
|
|
|
zh-CN: 表单方法调用
|
|
|
|
|
en-US: Form methods
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
|
|
通过 `Form.useForm` 对表单数据域进行交互。
|
|
|
|
|
|
2020-03-04 11:26:37 +08:00
|
|
|
|
> 注意 `useForm` 是 [React Hooks](https://reactjs.org/docs/hooks-intro.html) 的实现,只能用于函数组件,class 组件请查看下面的例子。
|
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
|
|
Call form method with `Form.useForm`.
|
|
|
|
|
|
2020-03-04 11:26:37 +08:00
|
|
|
|
> Note that `useForm` is a [React Hooks](https://reactjs.org/docs/hooks-intro.html) that only works in functional component.
|
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
|
```tsx
|
|
|
|
|
import { Form, Input, Button, Select } from 'antd';
|
|
|
|
|
|
|
|
|
|
const { Option } = Select;
|
|
|
|
|
|
|
|
|
|
const layout = {
|
|
|
|
|
labelCol: { span: 8 },
|
|
|
|
|
wrapperCol: { span: 16 },
|
|
|
|
|
};
|
|
|
|
|
const tailLayout = {
|
|
|
|
|
wrapperCol: { offset: 8, span: 16 },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const Demo = () => {
|
|
|
|
|
const [form] = Form.useForm();
|
|
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
|
const onGenderChange = (value: string) => {
|
2020-03-22 17:58:59 +08:00
|
|
|
|
switch (value) {
|
2020-10-21 11:06:07 +08:00
|
|
|
|
case 'male':
|
|
|
|
|
form.setFieldsValue({ note: 'Hi, man!' });
|
2020-03-22 17:58:59 +08:00
|
|
|
|
return;
|
2020-10-21 11:06:07 +08:00
|
|
|
|
case 'female':
|
|
|
|
|
form.setFieldsValue({ note: 'Hi, lady!' });
|
2020-03-22 17:58:59 +08:00
|
|
|
|
return;
|
2020-10-21 11:06:07 +08:00
|
|
|
|
case 'other':
|
|
|
|
|
form.setFieldsValue({ note: 'Hi there!' });
|
2020-03-22 17:58:59 +08:00
|
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
|
const onFinish = (values: any) => {
|
2019-07-03 20:14:39 +08:00
|
|
|
|
console.log(values);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onReset = () => {
|
|
|
|
|
form.resetFields();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onFill = () => {
|
|
|
|
|
form.setFieldsValue({
|
|
|
|
|
note: 'Hello world!',
|
|
|
|
|
gender: 'male',
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Form {...layout} form={form} name="control-hooks" onFinish={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={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={onReset}>
|
|
|
|
|
Reset
|
|
|
|
|
</Button>
|
|
|
|
|
<Button type="link" htmlType="button" onClick={onFill}>
|
|
|
|
|
Fill form
|
|
|
|
|
</Button>
|
|
|
|
|
</Form.Item>
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
|
export default Demo;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```css
|
|
|
|
|
#components-form-demo-control-hooks .ant-btn {
|
|
|
|
|
margin-right: 8px;
|
|
|
|
|
}
|
|
|
|
|
```
|