-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Description
Describe the bug
On an input type file, when cancelling the selection, the value of the registered fields goes from blank|null|undefined to a FileList with length 0, since we can't initialize something with a FileList, i guess that means that like the new value is different from the initial one, the field remain dirty.
I tried to workaround by using dirtyFields, checking that if the field was there and no value is set, i delete it from the set, however, that does not update isDirty.
I have it working by replacing the formState.dirty check on my submit button with:
const formDirty = () => {
if (dirtyFields.has('fileInput')) {
const { fileInput } = getValues();
if (!fileInput || !fileInput.length) {
dirtyFields.delete('fileInput');
}
}
return Boolean(dirtyFields.size);
};
That basically relies on dirtyFields.size, but i guess that is something should be handled internally.
To Reproduce
Steps to reproduce the behavior:
- Go to: https://codesandbox.io/s/react-hook-form-useform-template-qh1r8?file=/src/index.js
- Click on "choose file"
- Click cancel on the browser upload dialog
- See the submit enabled, the form is dirty
Codesandbox link (Required)
https://codesandbox.io/s/react-hook-form-useform-template-qh1r8?file=/src/index.js
Expected behavior
The form should not be dirty, button should not be enabled.
Desktop (please complete the following information):
- OS: Mac
- Browser: Chrome
- Version: Latest