refine icon indicating copy to clipboard operation
refine copied to clipboard

feat: add autoResetFormWhenClose option to useModalForm hook

Open amulyavarshney opened this issue 5 months ago • 2 comments

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://refine.dev/docs/guides-concepts/contributing/#commit-convention
  • [x] Related issue(s) linked
  • [x] Tests for the changes have been added
  • [x] Docs have been added / updated
  • [x] Changesets have been added https://refine.dev/docs/guides-concepts/contributing/#creating-a-changeset

What is the current behavior?

Currently, the useModalForm hook has two UX issues:

  1. Create Action: When using useModalForm({refineCoreProps:{action: 'create'}}), if a user fills some fields then closes the modal without submitting, the next time they open the modal, the previous values are still there.

  2. Edit Action: When using useModalForm({refineCoreProps:{action: 'edit'}}), when closing a modal and opening another modal by calling show(id), the previous data is still visible while waiting for new data to be fetched. This creates confusion when the internet is slow.

Users currently need to manually call reset() then close() to work around these issues.

What is the new behavior?

Added a new autoResetFormWhenClose option to the modalProps configuration of useModalForm that automatically resets the form to defaultValues when the modal is closed.

Usage Example:

useModalForm({
  refineCoreProps: { action: 'create' },
  defaultValues: { name: '', email: '' },
  modalProps: {
    autoResetFormWhenClose: true // New option
  },
  resolver: valibotResolver(schema)
});

Key Features:

  • Default Value: true (enabled by default for better UX)
  • Backward Compatible: Existing code continues to work as before
  • Cross-Package Support: Implemented consistently across all form packages
  • Smart Reset: Only resets when modal is actually closed, not on other form actions

Implementation Details:

  • @refinedev/react-hook-form: Calls reset() in handleClose
  • @refinedev/antd: Calls form.resetFields() in handleClose
  • @refinedev/mantine: Calls reset() in handleClose

Packages Modified

  1. @refinedev/react-hook-form

    • Added autoResetFormWhenClose?: boolean to UseModalFormProps
    • Updated handleClose to call reset() when option is enabled
    • Added comprehensive test cases
  2. @refinedev/antd

    • Added autoResetFormWhenClose?: boolean to UseModalFormProps
    • Updated handleClose to call form.resetFields() when option is enabled
  3. @refinedev/mantine

    • Added autoResetFormWhenClose?: boolean to UseModalFormProps
    • Updated handleClose to call reset() when option is enabled

Tests Added

Added test cases for @refinedev/react-hook-form:

  • ✅ Verifies reset is called when autoResetFormWhenClose: true and modal is closed
  • ✅ Verifies reset is NOT called when autoResetFormWhenClose: false
  • ✅ Tests behavior for both create and edit actions

Breaking Changes

None - This is a backward-compatible enhancement. All existing code continues to work exactly as before.

Notes for reviewers

  • The feature is enabled by default (autoResetFormWhenClose: true) to provide better UX out of the box
  • Implementation is consistent across all three form packages (react-hook-form, antd, mantine)
  • The reset happens in the handleClose function which is triggered by all modal close actions (close button, ESC key, outside click, programmatic close)
  • Tests ensure the feature works correctly and doesn't interfere with existing functionality
  • TypeScript types are properly updated to include the new option with appropriate JSDoc documentation

This enhancement directly addresses the user experience issues mentioned in the original issue while maintaining full backward compatibility.

amulyavarshney avatar Jun 10 '25 12:06 amulyavarshney

🦋 Changeset detected

Latest commit: 0759a2b3ff80f11ad285bc2095c9b36941752f11

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@refinedev/react-hook-form Patch
@refinedev/mantine Patch
@refinedev/antd Patch

Not sure what this means? Click here to learn what changesets are.

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

changeset-bot[bot] avatar Jun 10 '25 12:06 changeset-bot[bot]

Deploy Preview for refine-doc-live-previews ready!

Built without sensitive environment variables

Name Link
Latest commit 0759a2b3ff80f11ad285bc2095c9b36941752f11
Latest deploy log https://app.netlify.com/projects/refine-doc-live-previews/deploys/6867bb3f75692f0008345830
Deploy Preview https://deploy-preview-6835--refine-doc-live-previews.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Jun 10 '25 12:06 netlify[bot]