Skip to content

fix(antd): Prevent unsaved dialog on modal form submission #6836

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zackseal89
Copy link

@zackseal89 zackseal89 commented Jun 10, 2025

Addresses issue #6833.

When warnWhenUnsavedChanges is true and a form within a modal (using useModalForm) 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 to false (which is set synchronously in the core useForm's onFinish handler).

The fix involves modifying the onFinish handler within packages/antd/src/hooks/form/useModalForm/useModalForm.ts. The modal's close() call is now wrapped in setTimeout(..., 0) to defer its execution to the next event loop cycle. This allows React sufficient time to process the setWarnWhen(false) state update, ensuring that warnWhen is correctly false 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

Addresses issue refinedev#6833.

When `warnWhenUnsavedChanges` is true and a form within a modal
(using `useModalForm`) 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 to `false` (which is set
synchronously in the core `useForm`'s `onFinish` handler).

The fix involves modifying the `onFinish` handler within
`packages/antd/src/hooks/form/useModalForm/useModalForm.ts`.
The modal's `close()` call is now wrapped in `setTimeout(..., 0)`
to defer its execution to the next event loop cycle. This allows
React sufficient time to process the `setWarnWhen(false)` state
update, ensuring that `warnWhen` is correctly `false` 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.
@zackseal89 zackseal89 requested a review from a team as a code owner June 10, 2025 13:04
Copy link

netlify bot commented Jun 10, 2025

Deploy Preview for refine-doc-live-previews failed.

Built without sensitive environment variables

Name Link
🔨 Latest commit c7aa43a
🔍 Latest deploy log https://app.netlify.com/projects/refine-doc-live-previews/deploys/68482d7703d3b10008cc31d3

Copy link

changeset-bot bot commented Jun 10, 2025

⚠️ No Changeset found

Latest commit: c7aa43a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant