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 Warn about unsaved changes when modifying CheckGroupInput and ArrayInput #6954

Merged
merged 2 commits into from
Dec 7, 2021
Merged
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
13 changes: 9 additions & 4 deletions packages/ra-core/src/form/useWarnWhenUnsavedChanges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useWarnWhenUnsavedChanges = (
) => {
const history = useHistory();
const translate = useTranslate();
const { pristine, submitSucceeded } = useFormState(
const { pristine, submitSucceeded, submitting } = useFormState(
UseFormStateSubscription
);
const initialLocation = useRef(
Expand All @@ -32,7 +32,7 @@ const useWarnWhenUnsavedChanges = (
initialLocation.current
);

if (!pristine && !isInsideForm && !submitSucceeded) {
if (!pristine && !isInsideForm && !submitSucceeded && !submitting) {
return translate('ra.message.unsaved_changes');
}

Expand All @@ -44,13 +44,18 @@ const useWarnWhenUnsavedChanges = (
release();
}
};
}, [pristine, enable, history, translate, submitSucceeded]);
}, [pristine, enable, history, translate, submitSucceeded, submitting]);
};

const UseFormStateSubscription: UseFormStateParams = {
// For some reason, subscribing only to pristine does not rerender when a field become dirty
// because it has a defaultValue (not initialValue as setting an initialValue does not make the field dirty)
subscription: { pristine: true, dirtyFields: true, submitSucceeded: true },
subscription: {
pristine: true,
dirtyFields: true,
submitSucceeded: true,
submitting: true,
},
};

export default useWarnWhenUnsavedChanges;