You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
273 lines
6.4 KiB
273 lines
6.4 KiB
5 years ago
|
---
|
||
|
title: 从 v3 到 v4
|
||
|
skip: true
|
||
|
---
|
||
|
|
||
|
新版本 Form 对使用方式进行了简化,因而如果你是从 v3 迁移上来。你可以参考本文做迁移工作。
|
||
|
|
||
|
## 去除 Form.create
|
||
|
|
||
|
v4 的 Form 不再需要通过 `Form.create()` 创建上下文。Form 组件现在自带数据域,因而 `getFieldDecorator` 也不再需要,直接写入 Form.Item 即可:
|
||
|
|
||
|
```jsx
|
||
|
// antd v3
|
||
|
const Demo = ({ form: { getFieldDecorator } }) => (
|
||
|
<Form>
|
||
|
<Form.Item>
|
||
|
{getFieldDecorator('username', {
|
||
|
rules: [{ required: true }],
|
||
|
})(<Input />)}
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
|
||
|
const WrappedDemo = Form.create()(Demo);
|
||
|
```
|
||
|
|
||
|
改成:
|
||
|
|
||
|
```jsx
|
||
|
// antd v4
|
||
|
const Demo = () => (
|
||
|
<Form>
|
||
|
<Form.Item name="username" rules={[{ required: true }]}>
|
||
|
<Input />
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
```
|
||
|
|
||
|
由于移除了 `Form.create()`,原本的 `onFieldsChange` 等方法移入 Form 中,通过 `fields` 对 Form 进行控制。参考[示例](/components/form/#components-form-demo-global-state)。
|
||
|
|
||
|
## 表单控制调整
|
||
|
|
||
|
Form 自带表单控制实体,如需要调用 form 方法,可以通过 `Form.useForm()` 创建 Form 实体进行操作:
|
||
|
|
||
|
```jsx
|
||
|
// antd v3
|
||
|
const Demo = ({ form: { setFieldsValue } }) => {
|
||
|
React.useEffect(() => {
|
||
|
setFieldsValue({
|
||
|
username: 'Bamboo',
|
||
|
});
|
||
|
}, []);
|
||
|
|
||
|
return (
|
||
|
<Form>
|
||
|
<Form.Item>
|
||
|
{getFieldDecorator('username', {
|
||
|
rules: [{ required: true }],
|
||
|
})(<Input />)}
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const WrappedDemo = Form.create()(Demo);
|
||
|
```
|
||
|
|
||
|
改成:
|
||
|
|
||
|
```jsx
|
||
|
// antd v4
|
||
|
const Demo = () => {
|
||
|
const [form] = Form.useForm();
|
||
|
|
||
|
React.useEffect(() => {
|
||
|
form.setFieldsValue({
|
||
|
username: 'Bamboo',
|
||
|
});
|
||
|
}, []);
|
||
|
|
||
|
return (
|
||
|
<Form form={form}>
|
||
|
<Form.Item name="username" rules={[{ required: true }]}>
|
||
|
<Input />
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
};
|
||
|
```
|
||
|
|
||
|
对于 class component,也可以通过 `ref` 获得实体:
|
||
|
|
||
|
```jsx
|
||
|
// antd v4
|
||
|
class Demo extends React.Component {
|
||
|
formRef = React.createRef();
|
||
|
|
||
|
componentDidMount() {
|
||
|
this.formRef.setFieldsValue({
|
||
|
username: 'Bamboo',
|
||
|
});
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
return (
|
||
|
<Form ref={formRef}>
|
||
|
<Form.Item name="username" rules={[{ required: true }]}>
|
||
|
<Input />
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
由于 Form.Item 内置字段绑定,如果需要不带样式的表单绑定,可以使用 `inline` 属性移除额外样式:
|
||
|
|
||
|
```jsx
|
||
|
// antd v3
|
||
|
const Demo = ({ form: { setFieldsValue } }) => {
|
||
|
return <Form>{getFieldDecorator('username')(<Input />)}</Form>;
|
||
|
};
|
||
|
|
||
|
const WrappedDemo = Form.create()(Demo);
|
||
|
```
|
||
|
|
||
|
改成:
|
||
|
|
||
|
```jsx
|
||
|
// antd v4
|
||
|
const Demo = () => {
|
||
|
return (
|
||
|
<Form>
|
||
|
<Form.Item name="username" inline>
|
||
|
<Input />
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
};
|
||
|
```
|
||
|
|
||
|
## 字段联动调整
|
||
|
|
||
|
新版 Form 采用增量更新方式,仅会更新需要更新的字段。因而如果有字段关联更新,或者跟随整个表单更新而更新。可以使用 [`dependencies`](/components/form/#dependencies) 或 [`shouldUpdate`](/components/form/#shouldUpdate)。
|
||
|
|
||
|
## onFinish 替代 onSubmit
|
||
|
|
||
|
对于表单校验,过去版本需要通过监听 `onSubmit` 事件手工触发 `validateFields`。新版直接使用 `onFinish` 事件,该事件仅当校验通过后才会执行:
|
||
|
|
||
|
```jsx
|
||
|
// antd v3
|
||
|
const Demo = ({ form: { getFieldDecorator, validateFields } }) => {
|
||
|
const onSubmit = e => {
|
||
|
e.preventDefault();
|
||
|
validateFields((err, values) => {
|
||
|
if (!err) {
|
||
|
console.log('Received values of form: ', values);
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
|
||
|
return (
|
||
|
<Form onSubmit={onSubmit}>
|
||
|
<Form.Item>
|
||
|
{getFieldDecorator('username', {
|
||
|
rules: [{ required: true }],
|
||
|
})(<Input />)}
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const WrappedDemo = Form.create()(Demo);
|
||
|
```
|
||
|
|
||
|
改成:
|
||
|
|
||
|
```jsx
|
||
|
// antd v4
|
||
|
const Demo = () => {
|
||
|
const onFinish = values => {
|
||
|
console.log('Received values of form: ', values);
|
||
|
};
|
||
|
|
||
|
return (
|
||
|
<Form onFinish={onFinish}>
|
||
|
<Form.Item name="username" rules={[{ required: true }]}>
|
||
|
<Input />
|
||
|
</Form.Item>
|
||
|
</Form>
|
||
|
);
|
||
|
};
|
||
|
```
|