Skip to content

Commit b47852c

Browse files
authored
feat: add move-errors-to-formState codemod
2 parents 8468017 + 9e5aba5 commit b47852c

19 files changed

+449
-42
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
ecmaVersion: 12,
1010
sourceType: 'module'
1111
},
12+
ignorePatterns: ['**/__testfixtures__/**/*.js'],
1213
plugins: ['@typescript-eslint'],
1314
rules: {
1415
semi: ['error', 'always'],

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ This will start an interactive wizard, and then run the specified transform.
3636

3737
#### `v7/update-register`
3838

39-
Update the `register` API inside a component which use `useForm` of React Hook Form. This transform is not applied if the component doesn't use `useForm`.
39+
Update the `register` API inside a component that use `useForm` of React Hook Form. This transform is not applied if the component doesn't use `useForm`.
4040

4141
npx @hookform/codemod v7/update-register
4242

@@ -77,6 +77,47 @@ With a custom `register` name
7777

7878
</details>
7979

80+
#### `v7/move-errors-to-formState`
81+
82+
It moves `errors` API into `formState` inside a component that use `useForm` of React Hook Form. This transform is not applied if the component doesn't use `useForm`.
83+
84+
npx @hookform/codemod v7/move-errors-to-formState
85+
86+
<details>
87+
<summary>Examples</summary>
88+
89+
```diff
90+
- const { errors } = useForm();
91+
+ const { formState: { errors } } = useForm();
92+
93+
- const { errors: customErrors } = useForm();
94+
+ const { formState: { errors: customErrors } } = useForm();
95+
96+
- const { errors, formState: { isDirty } } = useForm();
97+
+ const { formState: { isDirty, errors } } = useForm();
98+
99+
- const { errors: customErrors, formState: { isDirty } } = useForm();
100+
+ const { formState: { isDirty, errors: customErrors } } = useForm();
101+
```
102+
103+
With a custom `register` name
104+
105+
```diff
106+
function MyForm() {
107+
- const { errors, formState } = useForm();
108+
+ const { formState } = useForm();
109+
+ const { errors } = formState;
110+
111+
const isDirty = formState.isDirty;
112+
113+
return (
114+
//
115+
);
116+
}
117+
```
118+
119+
</details>
120+
80121
## Backers
81122

82123
Thanks goes to all our backers! [[Become a backer](https://opencollective.com/react-hook-form#backer)].

bin/cli.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ const TRANSFORMER_INQUIRER_CHOICES = [
9494
{
9595
name: 'v7/update-register: Transforms register api from v6 to v7',
9696
value: 'v7/update-register'
97+
},
98+
{
99+
name: 'v7/move-errors-to-formState: Move `errors` key to `formState` key',
100+
value: 'v7/move-errors-to-formState'
97101
}
98102
];
99103

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { formState: {
5+
errors,
6+
} } = useForm();
7+
8+
return (
9+
<form>
10+
<span>{errors.username.message}</span>
11+
</form>
12+
);
13+
};
14+
15+
export default Form;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { formState: {
5+
errors,
6+
} } = useForm();
7+
8+
return (
9+
<form>
10+
<span>{errors.username.message}</span>
11+
</form>
12+
);
13+
};
14+
15+
export default Form;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { errors } = useForm();
5+
6+
return (
7+
<form>
8+
<span>{errors.username.message}</span>
9+
</form>
10+
);
11+
};
12+
13+
export default Form;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { formState: {
5+
errors,
6+
} } = useForm();
7+
8+
return (
9+
<form>
10+
<span>{errors.username.message}</span>
11+
</form>
12+
);
13+
};
14+
15+
export default Form;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { errors: customErrors } = useForm();
5+
6+
return (
7+
<form>
8+
<span>{customErrors.username.message}</span>
9+
</form>
10+
);
11+
};
12+
13+
export default Form;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { formState: {
5+
errors: customErrors,
6+
} } = useForm();
7+
8+
return (
9+
<form>
10+
<span>{customErrors.username.message}</span>
11+
</form>
12+
);
13+
};
14+
15+
export default Form;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { errors, formState: { isDirty } } = useForm();
5+
6+
return (
7+
<form>
8+
<span>{errors.username.message}</span>
9+
</form>
10+
);
11+
};
12+
13+
export default Form;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const {
5+
formState: {
6+
isDirty,
7+
errors,
8+
},
9+
} = useForm();
10+
11+
return (
12+
<form>
13+
<span>{errors.username.message}</span>
14+
</form>
15+
);
16+
};
17+
18+
export default Form;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { errors, formState } = useForm();
5+
6+
const diry = formState.isDirty;
7+
8+
return (
9+
<form>
10+
<span>{errors.username.message}</span>
11+
</form>
12+
);
13+
};
14+
15+
export default Form;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const {
5+
formState,
6+
} = useForm();
7+
8+
const {
9+
errors,
10+
} = formState;
11+
12+
const diry = formState.isDirty;
13+
14+
return (
15+
<form>
16+
<span>{errors.username.message}</span>
17+
</form>
18+
);
19+
};
20+
21+
export default Form;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const { errors: customErrors, formState } = useForm();
5+
6+
const diry = formState.isDirty;
7+
8+
return (
9+
<form>
10+
<span>{errors.username.message}</span>
11+
</form>
12+
);
13+
};
14+
15+
export default Form;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useForm } from 'react-hook-form';
2+
3+
const Form = () => {
4+
const {
5+
formState,
6+
} = useForm();
7+
8+
const {
9+
errors: customErrors,
10+
} = formState;
11+
12+
const diry = formState.isDirty;
13+
14+
return (
15+
<form>
16+
<span>{errors.username.message}</span>
17+
</form>
18+
);
19+
};
20+
21+
export default Form;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineTest } from '../../../utils/defineTest';
2+
3+
/* global jest */
4+
jest.autoMockOff();
5+
6+
const fixtures = [
7+
'function-component',
8+
'with-custom-errors',
9+
'with-formState-1',
10+
'with-formState-2',
11+
'with-formState-3',
12+
'already-migrated'
13+
];
14+
15+
fixtures.forEach(defineTest(__dirname, 'v7/move-errors-to-formState'));

0 commit comments

Comments
 (0)