Skip to content

Commit

Permalink
[@mantine/form] Fix useForm initialDirty stops form.isDirty from work…
Browse files Browse the repository at this point in the history
…ing as expected (#3372)
  • Loading branch information
jodysalt committed Jan 17, 2023
1 parent 4a81ed2 commit 05075e6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/mantine-form/src/get-status/get-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ describe('@mantine/form/get-status', () => {
expect(getStatus(TEST_STATUS, 'a.5')).toBe(false);
expect(getStatus(TEST_STATUS, 'l.d.0')).toBe(true);
expect(getStatus(TEST_STATUS, 'l.d.0.2')).toBe(false);
expect(getStatus(TEST_STATUS, 'd.0')).toBe(false);
});
});
2 changes: 1 addition & 1 deletion src/mantine-form/src/get-status/get-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function getStatus(status: FormStatus, path?: unknown) {
const paths = Object.keys(status);

if (typeof path === 'string') {
const nestedPaths = paths.filter((statusPath) => statusPath.includes(`${path}.`));
const nestedPaths = paths.filter((statusPath) => statusPath.startsWith(`${path}.`));
return status[path] || nestedPaths.some((statusPath) => status[statusPath]) || false;
}

Expand Down
16 changes: 10 additions & 6 deletions src/mantine-form/src/use-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,22 @@ export function useForm<
}, []);

const isDirty: GetFieldStatus<Values> = (path) => {
const isOverridden = Object.keys(dirty).length > 0;

if (isOverridden) {
return getStatus(dirty, path);
}

if (path) {
const overridenValue = getPath(path, dirty);
if (typeof overridenValue === 'boolean') {
return overridenValue;
}

const sliceOfValues = getPath(path, values);
const sliceOfInitialValues = getPath(path, _dirtyValues.current);
return !isEqual(sliceOfValues, sliceOfInitialValues);
}

const isOverridden = Object.keys(dirty).length > 0;
if (isOverridden) {
return getStatus(dirty);
}

return !isEqual(values, _dirtyValues.current);
};

Expand Down

0 comments on commit 05075e6

Please sign in to comment.