Skip to content
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

Fix ArrayInput makes edited form always dirty on input blur #8275

Merged
merged 5 commits into from
Oct 20, 2022

Conversation

WiXSL
Copy link
Contributor

@WiXSL WiXSL commented Oct 18, 2022

Fixes #8258

PROBLEM

SaveButton enable state may be true, even though no inputs have been changed.

This is because useFormState().isDirty might differ from useFormState().dirtyFields (react-hook-form/react-hook-form#4740)
This is the expected behavior of react-hook-form and it may happen when you have a missing defaultValue for one of the inputs

· dirtyFields will mark the field dirty or not individually.
· isDirty will always do a deep equal against your defaultValues to determined form is dirty or not.
react-hook-form/react-hook-form#7860 (reply in thread)

This is key, since defaultValue prop might be undefined, and in some cases you end up with isDirty being true but dirtyFields being an empty object. Like in the test I've added in this PR, plus the ones I've fixed in this PR

SOLUTION

Use useFormState().dirtyFields to enable/disable SaveButton instead of useFormState().isDirty

Copy link
Contributor

@slax57 slax57 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix seems good to me!
Nice job digging the issue out of RHF's repo.

Still, I'd be more confident having a second review by @fzaninotto on this one. I'll request it.

@slax57 slax57 requested a review from fzaninotto October 18, 2022 14:31
Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@fzaninotto fzaninotto merged commit 2fef6ab into master Oct 20, 2022
@fzaninotto fzaninotto deleted the fix-savebutton-state branch October 20, 2022 11:49
@fzaninotto fzaninotto added this to the 4.4.4 milestone Oct 20, 2022
@fzaninotto fzaninotto changed the title Fix improve SaveButton enable state Fix ArrayInput makes edited form always dirty on input blur Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ArrayInput makes edited form always dirty on input blur
3 participants