fix(antd): Prevent unsaved dialog on modal form submission #6836
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Addresses issue #6833.
When
warnWhenUnsavedChanges
is true and a form within a modal (usinguseModalForm
) is dirty, submitting the form could incorrectly trigger the "unsaved changes" confirmation dialog.This was due to a race condition where the modal closing/unmounting process initiated navigation/unload checks before React had fully processed the
warnWhen
state update tofalse
(which is set synchronously in the coreuseForm
'sonFinish
handler).The fix involves modifying the
onFinish
handler withinpackages/antd/src/hooks/form/useModalForm/useModalForm.ts
. The modal'sclose()
call is now wrapped insetTimeout(..., 0)
to defer its execution to the next event loop cycle. This allows React sufficient time to process thesetWarnWhen(false)
state update, ensuring thatwarnWhen
is correctlyfalse
when the modal close/unmount events trigger any navigation checks.A new unit test has been added to
useModalForm
's spec file to verify this fix and prevent regressions.Manual end-to-end testing of this change in an example application was unfortunately blocked by unrelated build failures in
@refinedev/core
and@refinedev/antd
packages, and subsequent tooling instability related to pre-commit hooks. The fix is verified at the unit test level.PR Checklist
Please check if your PR fulfills the following requirements:
Bugs / Features
What is the current behavior?
What is the new behavior?
fixes (issue)
Notes for reviewers