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

Commit

Permalink
Fix(useForm): fix potential side-effect of the values of form state
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Feb 25, 2021
1 parent 5c7a953 commit fe7942d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-moose-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-form": patch
---

Fix(useForm): fix potential side-effect of the values of form state
6 changes: 6 additions & 0 deletions src/useForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,25 +365,30 @@ describe("useForm", () => {
const { reset, setValue, setError, getState } = renderHelper({
defaultValues,
onReset,
children: <input data-testid="foo" name="foo" />,
});
const foo = getByTestId("foo");

setValue("foo", "🍎");
setError("foo", "Required");
const e = {};
// @ts-expect-error
act(() => reset(null, null, e));
expect(foo.value).toBe(defaultValues.foo);
expect(onReset).toHaveBeenCalledWith(defaultValues, options, e);
expect(getState()).toEqual({ ...initialState, values: defaultValues });

const values = { foo: "🍋" };
// @ts-expect-error
act(() => reset(values, null, e));
expect(foo.value).toBe(values.foo);
expect(onReset).toHaveBeenCalledWith(values, options, e);
expect(getState()).toEqual({ ...initialState, values });

const value = "🍎";
// @ts-expect-error
act(() => reset((prevValues) => ({ ...prevValues, foo: value }), null, e));
expect(foo.value).toBe(value);
expect(onReset).toHaveBeenCalledWith({ foo: value }, options, e);
expect(getState()).toEqual({ ...initialState, values: { foo: value } });

Expand All @@ -392,6 +397,7 @@ describe("useForm", () => {
setError("foo", error);
// @ts-expect-error
act(() => reset(null, ["values", "errors", "touched"], e));
expect(foo.value).toBe(value);
expect(onReset).toHaveBeenCalledWith({ foo: value }, options, e);
expect(getState()).toEqual({
...initialState,
Expand Down
16 changes: 11 additions & 5 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,13 @@ export default <V extends FormValues = FormValues>({
initialStateRef.current.values;

state[key] = nextValues;
initialStateRef.current.values = nextValues;
setNodesOrStateValue(initialStateRef.current.values, !!values);
initialStateRef.current = set(
initialStateRef.current,
"values",
nextValues,
true
);
setNodesOrStateValue(nextValues, !!values);
} else {
// @ts-expect-error
state[key] = initialStateRef.current[key];
Expand Down Expand Up @@ -865,11 +870,12 @@ export default <V extends FormValues = FormValues>({
name
);

initialStateRef.current.values = unset(
initialStateRef.current.values,
name,
initialStateRef.current = unset(
initialStateRef.current,
`values.${name}`,
true
);

delete fieldParsersRef.current[name];
delete fieldValidatorsRef.current[name];
delete controllersRef.current[name];
Expand Down

0 comments on commit fe7942d

Please sign in to comment.