Skip to content

Commit 0383b9f

Browse files
committed
feat: support auto migrate your v3 form
1 parent 9f99946 commit 0383b9f

16 files changed

+632
-2
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,51 @@ import { Modal } from 'antd';
155155
});
156156
```
157157

158+
### migrate your form v3 to v4
159+
160+
You can auto migrate your `v3 form` by `antd4-codemod src --migrateform`, when execute this command, you should insure your already upgrade `antd v4`, also recommend your already execute above command.This scripts can't migrate all incompatible `api`, so if your codes aren't standard code(like `Form Form.Item`).We recommend you execute the command migrate form file one by one.When a file migrate, you should check incompatible `api`
161+
If `Form.Item` in your code is not a standard code, for example, alias `Form.Item` is `FormItem`, you can use `--formitem=FormItem` to rename `Form.Item`
162+
163+
```diff
164+
165+
- import { Form } from '@ant-design/compatible'; // remove compatible package
166+
- import '@ant-design/compatible/assets/index.css'; // if not includes compatible package, remove css
167+
168+
169+
-<Form className="login-form">
170+
- <Form.Item>
171+
- {getFieldDecorator('username', {
172+
- rules: [{ required: true, message: 'Please input your username!' }],
173+
- initialValue: 'antd',
174+
- })(<Input />)}
175+
+<Form
176+
+ className="login-form"
177+
+ initialValue={{
178+
+ username: 'antd',
179+
+ password: '123456'
180+
+ }}>
181+
+ <Form.Item
182+
+ name='username'
183+
+ rules={[{ required: true, message: 'Please input your username!' }]}>
184+
+ <Input />
185+
</Form.Item>
186+
<div>
187+
- {getFieldDecorator('password', {
188+
- initialValue: '123456',
189+
- rules: [{ required: true, message: 'Please input your Password!' }],
190+
- })(<Input />)}
191+
+ <Form.Item
192+
+ noStyle
193+
+ name='password'
194+
+ rules={[{ required: true, message: 'Please input your Password!' }]}><Input /></Form.Item>
195+
</div>
196+
</Form>;
197+
198+
- export default Form.create({})(Input) // remove Form.create
199+
+ export default Input;
200+
201+
```
202+
158203
## License
159204

160205
MIT

README.zh-CN.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,54 @@ import { Modal } from 'antd';
155155
});
156156
```
157157

158+
### migrate your form v3 to v4
159+
160+
使用 `antd4-codemod src --migrateform` 来自动迁移你的旧版 `Form`,使用此命令时确保你已经升级到 v4,并且推荐已经执行上述其他迁移脚本。此脚本并不会帮你迁移所有不兼容的 `api`,所以如果不是标准写法的话(`Form Form.Item`)推荐对用此命令对单个文件边迁移边及时修改不兼容 `api`
161+
162+
如果 `Form.Item` 不是标准的写法,譬如可能自己封装过为 `FormItem`,使用 `--formitem=FormItem` 来指定新的 `Form.Item` 命名。
163+
164+
```diff
165+
166+
- import { Form } from '@ant-design/compatible'; // 去掉兼容包的导入
167+
- import '@ant-design/compatible/assets/index.css'; // 如果不在有兼容包,去除兼容包css
168+
169+
+ import { Form } from 'antd';
170+
171+
-<Form className="login-form">
172+
- <Form.Item>
173+
- {getFieldDecorator('username', {
174+
- rules: [{ required: true, message: 'Please input your username!' }],
175+
- initialValue: 'antd',
176+
- })(<Input />)}
177+
+<Form
178+
+ className="login-form"
179+
+ initialValue={{
180+
+ username: 'antd',
181+
+ password: '123456'
182+
+ }}>
183+
+ <Form.Item
184+
+ name='username'
185+
+ rules={[{ required: true, message: 'Please input your username!' }]}>
186+
+ <Input />
187+
</Form.Item>
188+
<div>
189+
- {getFieldDecorator('password', {
190+
- initialValue: '123456',
191+
- rules: [{ required: true, message: 'Please input your Password!' }],
192+
- })(<Input />)}
193+
+ <Form.Item
194+
+ noStyle
195+
+ name='password'
196+
+ rules={[{ required: true, message: 'Please input your Password!' }]}><Input /></Form.Item>
197+
</div>
198+
</Form>;
199+
200+
201+
- export default Form.create({})(Input) // 去除掉Form.create
202+
+ export default Input;
203+
204+
```
205+
158206
## License
159207

160208
MIT

bin/cli.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ function getRunnerArgs(
112112

113113
async function run(filePath, args = {}) {
114114
const extraScripts = args.extraScripts ? args.extraScripts.split(',') : [];
115-
115+
const usedTransformers = args.migrateform
116+
? ['v3-Form-to-FieldForm']
117+
: transformers.concat(extraScripts);
116118
// eslint-disable-next-line no-restricted-syntax
117-
for (const transformer of transformers.concat(extraScripts)) {
119+
for (const transformer of usedTransformers) {
118120
// eslint-disable-next-line no-await-in-loop
119121
await transform(transformer, 'babylon', filePath, args);
120122
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Form } from 'antd';
2+
3+
const input = <input />;
4+
5+
ReactDOM.render(
6+
<div>
7+
<Form.Item>
8+
{getFieldDecorator('field1', {
9+
rules: [{ required: true }]
10+
})(<input style={{ width: 100 }} />)}
11+
</Form.Item>
12+
<Form.Item>
13+
{getFieldDecorator('field2', {
14+
rules: [{ required: true }]
15+
})(input)}
16+
</Form.Item>
17+
</div>,
18+
mountNode
19+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Form } from 'antd';
2+
3+
const input = <input />;
4+
5+
ReactDOM.render(
6+
<div>
7+
<Form.Item name='field1' rules={[{ required: true }]}>
8+
<input style={{ width: 100 }} />
9+
</Form.Item>
10+
<Form.Item name='field2' rules={[{ required: true }]}>
11+
{input}
12+
</Form.Item>
13+
</div>,
14+
mountNode
15+
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Form } from 'antd';
2+
3+
const input = <input />;
4+
5+
ReactDOM.render(
6+
<div>
7+
<div>
8+
{getFieldDecorator('field1', {
9+
rules: [{ required: true }]
10+
})(<input style={{ width: 100 }} />)}
11+
</div>
12+
<div>
13+
{getFieldDecorator('field2', {
14+
rules: [{ required: true }]
15+
})(input)}
16+
</div>
17+
</div>,
18+
mountNode
19+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Form } from 'antd';
2+
3+
const input = <input />;
4+
5+
ReactDOM.render(
6+
<div>
7+
<div>
8+
<Form.Item noStyle name='field1' rules={[{ required: true }]}><input style={{ width: 100 }} /></Form.Item>
9+
</div>
10+
<div>
11+
<Form.Item noStyle name='field2' rules={[{ required: true }]}>{input}</Form.Item>
12+
</div>
13+
</div>,
14+
mountNode
15+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Form, Icon as LegacyIcon } from '@ant-design/compatible';
2+
3+
class AntForm extends React.Component {
4+
render() {
5+
return (
6+
<Form>
7+
<Form.Item>
8+
{getFieldDecorator('field1', {
9+
rules: [{ required: true }],
10+
initialValue: 'antd'
11+
})(<input style={{ width: 100 }} />)}
12+
</Form.Item>
13+
</Form>
14+
);
15+
}
16+
}
17+
18+
export default Form.create({})(AntForm);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Form } from 'antd';
2+
import { Icon as LegacyIcon } from '@ant-design/compatible';
3+
4+
class AntForm extends React.Component {
5+
render() {
6+
return (
7+
<Form
8+
initialValue={{
9+
field1: 'antd'
10+
}}>
11+
<Form.Item name='field1' rules={[{ required: true }]}>
12+
<input style={{ width: 100 }} />
13+
</Form.Item>
14+
</Form>
15+
);
16+
}
17+
}
18+
19+
export default AntForm;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Form } from 'antd';
2+
import { FormItem } from 'components';
3+
4+
const input = <input />;
5+
6+
ReactDOM.render(
7+
<div>
8+
<FormItem>
9+
{getFieldDecorator('field1', {
10+
rules: [{ required: true }]
11+
})(<input style={{ width: 100 }} />)}
12+
</FormItem>
13+
<FormItem>
14+
{getFieldDecorator('field2', {
15+
rules: [{ required: true }]
16+
})(input)}
17+
</FormItem>
18+
</div>,
19+
mountNode
20+
);

0 commit comments

Comments
 (0)