Skip to content

Commit

Permalink
Merge pull request #6954 from marmelab/fix-usewarn-wuc
Browse files Browse the repository at this point in the history
Fix Warn about unsaved changes when modifying CheckGroupInput and ArrayInput
  • Loading branch information
djhi authored Dec 7, 2021
2 parents 51640ba + 85f8b11 commit e3f55be
Showing 1 changed file with 9 additions and 4 deletions.
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;

0 comments on commit e3f55be

Please sign in to comment.