Skip to content

Commit

Permalink
🐞 fix #9765 fix issue with strictMode with isValid state (#9771)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebill1049 committed Jan 13, 2023
1 parent 4b4f1d8 commit 3b2dee3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion reports/api-extractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export type Control<TFieldValues extends FieldValues = FieldValues, TContext = a
_options: UseFormProps<TFieldValues, TContext>;
_getDirty: GetIsDirty;
_formState: FormState<TFieldValues>;
_updateValid: Noop;
_updateValid: (shouldUpdateValid?: boolean) => void;
_fields: FieldRefs;
_formValues: FieldValues;
_proxyFormState: ReadFormState;
Expand Down
35 changes: 35 additions & 0 deletions src/__tests__/useController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,39 @@ describe('useController', () => {
expect(select).toBeCalled();
expect(focus).toBeCalled();
});

it('should update isValid correctly with strict mode', async () => {
const App = () => {
const form = useForm({
mode: 'onChange',
defaultValues: {
name: '',
},
});
const { isValid } = form.formState;

return (
<React.StrictMode>
<FormProvider {...form}>
<Controller
render={({ field }) => (
<input value={field.value} onChange={field.onChange} />
)}
name="name"
rules={{
required: true,
}}
/>
<p>{isValid ? 'valid' : 'not'}</p>
</FormProvider>
</React.StrictMode>
);
};

render(<App />);

await waitFor(() => {
screen.getByText('not');
});
});
});
4 changes: 2 additions & 2 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ export function createFormControl<
timer = window.setTimeout(callback, wait);
};

const _updateValid = async () => {
if (_proxyFormState.isValid) {
const _updateValid = async (shouldUpdateValid?: boolean) => {
if (_proxyFormState.isValid || shouldUpdateValid) {
const isValid = _options.resolver
? isEmptyObject((await _executeSchema()).errors)
: await executeBuiltInValidation(_fields, true);
Expand Down
2 changes: 1 addition & 1 deletion src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ export type Control<
_options: UseFormProps<TFieldValues, TContext>;
_getDirty: GetIsDirty;
_formState: FormState<TFieldValues>;
_updateValid: Noop;
_updateValid: (shouldUpdateValid?: boolean) => void;
_fields: FieldRefs;
_formValues: FieldValues;
_proxyFormState: ReadFormState;
Expand Down
2 changes: 1 addition & 1 deletion src/useFieldArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export function useFieldArray<

control._names.focus = '';

control._proxyFormState.isValid && control._updateValid();
control._updateValid();
}, [fields, name, control]);

React.useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function useForm<

React.useEffect(() => {
if (!control._stateFlags.mount) {
control._proxyFormState.isValid && control._updateValid();
control._updateValid();
control._stateFlags.mount = true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/useFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function useFormState<TFieldValues extends FieldValues = FieldValues>(
isDirty,
});
}
control._updateValid();

_localProxyFormState.current.isValid && control._updateValid(true);

return () => {
_mounted.current = false;
Expand Down

0 comments on commit 3b2dee3

Please sign in to comment.