diff --git a/.changeset/healthy-geese-guess.md b/.changeset/healthy-geese-guess.md new file mode 100644 index 00000000..f6af3119 --- /dev/null +++ b/.changeset/healthy-geese-guess.md @@ -0,0 +1,5 @@ +--- +"react-cool-form": patch +--- + +feat(useForm): accept function as the values argv of the reset method diff --git a/examples/src/BasicForm/index.tsx b/examples/src/BasicForm/index.tsx index f8e973c5..90fb10aa 100644 --- a/examples/src/BasicForm/index.tsx +++ b/examples/src/BasicForm/index.tsx @@ -184,7 +184,7 @@ export default (): JSX.Element => { }; const handleResetClick = (): void => { - reset({ ...initialValues, text: { nest: "new test" } }, [ + reset((prevValues) => ({ ...prevValues, text: { nest: "new test" } }), [ "touched", "submitCount", ]); diff --git a/src/index.ts b/src/index.ts index 8a6d67f4..c94f5a93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -405,11 +405,14 @@ const useForm = ({ ); const reset = useCallback>( - (values, exclude = []) => - resetStateRef(values, exclude, (nextValues) => - setAllDomsValue(nextValues) - ), - [resetStateRef, setAllDomsValue] + (values, exclude = []) => { + resetStateRef( + isFunction(values) ? values(stateRef.current.values) : values, + exclude, + (nextValues) => setAllDomsValue(nextValues) + ); + }, + [resetStateRef, setAllDomsValue, stateRef] ); const getOptions = useCallback( diff --git a/src/types/index.ts b/src/types/index.ts index 934f1ac7..8df68093 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -148,7 +148,10 @@ export interface ValidateField { } export interface Reset { - (values?: V, exclude?: (keyof FormState)[]): void; + ( + values?: V | ((previousValues: V) => V), + exclude?: (keyof FormState)[] + ): void; } export interface EventHandler { diff --git a/src/types/react-cool-form.d.ts b/src/types/react-cool-form.d.ts index 3cda6706..65de1d90 100644 --- a/src/types/react-cool-form.d.ts +++ b/src/types/react-cool-form.d.ts @@ -107,7 +107,10 @@ declare module "react-cool-form" { } interface Reset { - (values?: V, exclude?: (keyof FormState)[]): void; + ( + values?: V | ((previousValues: V) => V), + exclude?: (keyof FormState)[] + ): void; } interface EventHandler {