refine
refine copied to clipboard
feat: add autoResetFormWhenClose option to useModalForm hook
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:
-
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. -
Edit Action: When using
useModalForm({refineCoreProps:{action: 'edit'}}), when closing a modal and opening another modal by callingshow(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()inhandleClose - @refinedev/antd: Calls
form.resetFields()inhandleClose - @refinedev/mantine: Calls
reset()inhandleClose
Packages Modified
-
@refinedev/react-hook-form
- Added
autoResetFormWhenClose?: booleantoUseModalFormProps - Updated
handleCloseto callreset()when option is enabled - Added comprehensive test cases
- Added
-
@refinedev/antd
- Added
autoResetFormWhenClose?: booleantoUseModalFormProps - Updated
handleCloseto callform.resetFields()when option is enabled
- Added
-
@refinedev/mantine
- Added
autoResetFormWhenClose?: booleantoUseModalFormProps - Updated
handleCloseto callreset()when option is enabled
- Added
Tests Added
Added test cases for @refinedev/react-hook-form:
- ✅ Verifies
resetis called whenautoResetFormWhenClose: trueand modal is closed - ✅ Verifies
resetis NOT called whenautoResetFormWhenClose: 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
handleClosefunction 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.
🦋 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
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...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify project configuration.