Skip to content

Commit

Permalink
馃尦 include flush root render method to createFormControl (#9479)
Browse files Browse the repository at this point in the history
* include flush root render method to createFormControl

* update unit test
  • Loading branch information
bluebill1049 committed Nov 30, 2022
1 parent 15ec3f8 commit 6beb0ee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
48 changes: 47 additions & 1 deletion src/__tests__/useForm/reset.test.tsx
Expand Up @@ -9,7 +9,12 @@ import {
import { act, renderHook } from '@testing-library/react-hooks';

import { Controller } from '../../controller';
import { Control, UseFormRegister, UseFormReturn } from '../../types';
import {
Control,
UseFormRegister,
UseFormReset,
UseFormReturn,
} from '../../types';
import { useController } from '../../useController';
import { useFieldArray } from '../../useFieldArray';
import { useForm } from '../../useForm';
Expand Down Expand Up @@ -1195,6 +1200,47 @@ describe('reset', () => {
).toEqual('changed2');
});

it('should allow reset at child level before useForm mounted', () => {
type FormValues = {
firstName: string;
};

const NestChild = ({ reset }: { reset: UseFormReset<FormValues> }) => {
React.useEffect(() => {
reset({
firstName: 'test',
});
}, [reset]);

return null;
};

const Child = ({ reset }: { reset: UseFormReset<FormValues> }) => {
return <NestChild reset={reset} />;
};

function App() {
const { register, reset, handleSubmit } = useForm<FormValues>({
defaultValues: {
firstName: '',
},
});

return (
<form onSubmit={handleSubmit(() => {})}>
<input {...register('firstName')} />
<Child reset={reset} />
</form>
);
}

render(<App />);

expect((screen.getByRole('textbox') as HTMLInputElement).value).toEqual(
'test',
);
});

it('should reset field array async', () => {
let tempFields: unknown[] = [];

Expand Down
4 changes: 4 additions & 0 deletions src/logic/createFormControl.ts
Expand Up @@ -91,6 +91,7 @@ export function createFormControl<
TContext = any,
>(
props: UseFormProps<TFieldValues, TContext> = {},
flushRootRender: () => void,
): Omit<UseFormReturn<TFieldValues, TContext>, 'formState'> {
let _options = {
...defaultOptions,
Expand Down Expand Up @@ -641,6 +642,7 @@ export function createFormControl<
_subjects.watch.next({
name,
});
!_stateFlags.mount && flushRootRender();
};

const onChange: ChangeHandler = async (event) => {
Expand Down Expand Up @@ -1173,6 +1175,8 @@ export function createFormControl<
focus: '',
};

!_stateFlags.mount && flushRootRender();

_stateFlags.mount =
!_proxyFormState.isValid || !!keepStateOptions.keepIsValid;

Expand Down
4 changes: 3 additions & 1 deletion src/useForm.ts
Expand Up @@ -60,7 +60,9 @@ export function useForm<

if (!_formControl.current) {
_formControl.current = {
...createFormControl(props),
...createFormControl(props, () =>
updateFormState((formState) => ({ ...formState })),
),
formState,
};
}
Expand Down

0 comments on commit 6beb0ee

Please sign in to comment.