Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dirty flag when calling resetForm with current values #1986

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({

const dirty = React.useMemo(
() => !isEqual(initialValues.current, state.values),
[state.values]
[initialValues.current, state.values]
jaredpalmer marked this conversation as resolved.
Show resolved Hide resolved
);

const isValid = React.useMemo(
Expand Down
13 changes: 13 additions & 0 deletions test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,19 @@ describe('<Formik>', () => {
});
});

describe('resetForm', () => {
it('should reset dirty when reseting to same values', () => {
const { getProps } = renderFormik();
expect(getProps().dirty).toBe(false);

getProps().setFieldValue('name', 'jared-next');
expect(getProps().dirty).toBe(true);

getProps().resetForm({ values: getProps().values });
expect(getProps().dirty).toBe(false);
});
});

describe('prepareDataForValidation', () => {
it('should works correctly with instances', () => {
class SomeClass {}
Expand Down