Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge 0aefe6a into 57e168c
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Nov 7, 2020
2 parents 57e168c + 0aefe6a commit 33c3d6a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .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
2 changes: 1 addition & 1 deletion examples/src/BasicForm/index.tsx
Expand Up @@ -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",
]);
Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Expand Up @@ -405,11 +405,14 @@ const useForm = <V extends FormValues = FormValues>({
);

const reset = useCallback<Reset<V>>(
(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(
Expand Down
5 changes: 4 additions & 1 deletion src/types/index.ts
Expand Up @@ -148,7 +148,10 @@ export interface ValidateField<V> {
}

export interface Reset<V> {
(values?: V, exclude?: (keyof FormState<V>)[]): void;
(
values?: V | ((previousValues: V) => V),
exclude?: (keyof FormState<V>)[]
): void;
}

export interface EventHandler {
Expand Down
5 changes: 4 additions & 1 deletion src/types/react-cool-form.d.ts
Expand Up @@ -107,7 +107,10 @@ declare module "react-cool-form" {
}

interface Reset<V> {
(values?: V, exclude?: (keyof FormState<V>)[]): void;
(
values?: V | ((previousValues: V) => V),
exclude?: (keyof FormState<V>)[]
): void;
}

interface EventHandler {
Expand Down

0 comments on commit 33c3d6a

Please sign in to comment.