Skip to content

Commit c120bb6

Browse files
committed
feat: support auto migrate your v3 form
1 parent 554cf05 commit c120bb6

14 files changed

+573
-2
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,50 @@ 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+
162+
```diff
163+
164+
- import { Form } from '@ant-design/compatible'; // remove compatible package
165+
- import '@ant-design/compatible/assets/index.css'; // if not includes compatible package, remove css
166+
167+
168+
-<Form className="login-form">
169+
- <Form.Item>
170+
- {getFieldDecorator('username', {
171+
- rules: [{ required: true, message: 'Please input your username!' }],
172+
- initialValue: 'antd',
173+
- })(<Input />)}
174+
+<Form
175+
+ className="login-form"
176+
+ initialValue={{
177+
+ username: 'antd',
178+
+ password: '123456'
179+
+ }}>
180+
+ <Form.Item
181+
+ name='username'
182+
+ rules={[{ required: true, message: 'Please input your username!' }]}>
183+
+ <Input />
184+
</Form.Item>
185+
<div>
186+
- {getFieldDecorator('password', {
187+
- initialValue: '123456',
188+
- rules: [{ required: true, message: 'Please input your Password!' }],
189+
- })(<Input />)}
190+
+ <Form.Item
191+
+ noStyle
192+
+ name='password'
193+
+ rules={[{ required: true, message: 'Please input your Password!' }]}><Input /></Form.Item>
194+
</div>
195+
</Form>;
196+
197+
- export default Form.create({})(Input) // remove Form.create
198+
+ export default Input;
199+
200+
```
201+
158202
## License
159203

160204
MIT

README.zh-CN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,52 @@ 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+
```diff
163+
164+
- import { Form } from '@ant-design/compatible'; // 去掉兼容包的导入
165+
- import '@ant-design/compatible/assets/index.css'; // 如果不在有兼容包,去除兼容包css
166+
167+
+ import { Form } from 'antd';
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+
199+
- export default Form.create({})(Input) // 去除掉Form.create
200+
+ export default Input;
201+
202+
```
203+
158204
## License
159205

160206
MIT

bin/cli.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ async function run(filePath, args = {}) {
121121
const tsPaths = paths.filter(n => /.tsx?$/.test(n));
122122

123123
const extraScripts = args.extraScripts ? args.extraScripts.split(',') : [];
124-
124+
const usedTransformers = args.migrateform
125+
? ['v3-Form-to-FieldForm']
126+
: transformers.concat(extraScripts);
125127
// eslint-disable-next-line no-restricted-syntax
126-
for (const transformer of transformers.concat(extraScripts)) {
128+
for (const transformer of usedTransformers) {
127129
console.log(chalk.bgYellow.bold('JS/JSX files to convert'), jsPaths.length);
128130
console.log(chalk.bgBlue.bold('TS/TSX files to convert'), tsPaths.length);
129131

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Form } from 'antd';
2+
3+
const input = <input />;
4+
5+
const outerForm = (
6+
<Form.Item>
7+
{getFieldDecorator('outerform', {
8+
initialValue: 'outer'
9+
})(<input />)}
10+
</Form.Item>
11+
);
12+
13+
ReactDOM.render(
14+
<Form>
15+
<Form.Item>
16+
{getFieldDecorator('field1', {
17+
rules: [{ required: true }],
18+
initialValue: 'antd'
19+
})(<input style={{ width: 100 }} />)}
20+
</Form.Item>
21+
<Form.Item>
22+
{getFieldDecorator(field2, {
23+
rules: [{ required: true }],
24+
initialValue: 'antd-1'
25+
})(input)}
26+
</Form.Item>
27+
{outerForm}
28+
</Form>,
29+
mountNode
30+
);

0 commit comments

Comments
 (0)