Skip to content

Commit

Permalink
[@mantine/form] Reset should set values to current initialValues (#5122)
Browse files Browse the repository at this point in the history
  • Loading branch information
dodas committed Oct 26, 2023
1 parent e006b48 commit d1fe092
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/mantine-form/src/tests/reset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ describe('@mantine/form/reset', () => {
expect(hook.result.current.isDirty()).toBe(false);
expect(hook.result.current.values).toStrictEqual({ a: 1, b: 2 });
});

it('resets values correctly after updating initial values', () => {
const hook = renderHook(() => useForm({ initialValues: { a: 1, b: 2 } }));
const newInitialState = { a: 3, b: 4 };

act(() => hook.result.current.setValues({ a: 100, b: 200 }));
act(() => hook.result.current.setInitialValues(newInitialState));
act(() => hook.result.current.reset());

expect(hook.result.current.values).toStrictEqual(newInitialState);
});
});
3 changes: 1 addition & 2 deletions src/mantine-form/src/use-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ export function useForm<

const clearErrors: ClearErrors = useCallback(() => _setErrors({}), []);
const reset: Reset = useCallback(() => {
_setValues(initialValues);
_setValues(valuesSnapshot.current);
clearErrors();
setValuesSnapshot(initialValues);
setDirty({});
resetTouched();
}, []);
Expand Down

0 comments on commit d1fe092

Please sign in to comment.