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

[@mantine/form] Fix useForm initialDirty stops form.isDirty from work… #3372

Merged
merged 1 commit into from
Jan 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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