feat: add autoResetFormWhenClose option to useModalForm hook #6835
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.
PR Checklist
Please check if your PR fulfills the following requirements:
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()
thenclose()
to work around these issues.What is the new behavior?
Added a new
autoResetFormWhenClose
option to themodalProps
configuration ofuseModalForm
that automatically resets the form todefaultValues
when the modal is closed.Usage Example:
Key Features:
true
(enabled by default for better UX)Implementation Details:
reset()
inhandleClose
form.resetFields()
inhandleClose
reset()
inhandleClose
Packages Modified
@refinedev/react-hook-form
autoResetFormWhenClose?: boolean
toUseModalFormProps
handleClose
to callreset()
when option is enabled@refinedev/antd
autoResetFormWhenClose?: boolean
toUseModalFormProps
handleClose
to callform.resetFields()
when option is enabled@refinedev/mantine
autoResetFormWhenClose?: boolean
toUseModalFormProps
handleClose
to callreset()
when option is enabledTests Added
Added test cases for
@refinedev/react-hook-form
:reset
is called whenautoResetFormWhenClose: true
and modal is closedreset
is NOT called whenautoResetFormWhenClose: false
Breaking Changes
None - This is a backward-compatible enhancement. All existing code continues to work exactly as before.
Notes for reviewers
autoResetFormWhenClose: true
) to provide better UX out of the boxhandleClose
function which is triggered by all modal close actions (close button, ESC key, outside click, programmatic close)This enhancement directly addresses the user experience issues mentioned in the original issue while maintaining full backward compatibility.