-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Conversation
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
Fixes #8258
PROBLEM
SaveButton
enable state may be true, even though no inputs have been changed.This is because
useFormState().isDirty
might differ fromuseFormState().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 missingdefaultValue
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 beundefined
, and in some cases you end up withisDirty
beingtrue
butdirtyFields
being an empty object. Like in the test I've added in this PR, plus the ones I've fixed in this PRSOLUTION
Use
useFormState().dirtyFields
to enable/disableSaveButton
instead ofuseFormState().isDirty