-
Notifications
You must be signed in to change notification settings - Fork 1k
Enhance FileInput to Update event.target.files on Remove #6136
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
Enhance FileInput to Update event.target.files on Remove #6136
Conversation
// since inputRef.current.files is a read-only FileList | ||
// https://stackoverflow.com/a/64019766 | ||
/* eslint-disable no-undef */ | ||
const dt = new DataTransfer(); |
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.
this is used to store the new files object so that we're able to set it to input.current
when removed. we're not able to set an array to this value.
https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer
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.
If I add two files at the same time and then remove one file, event.target.files
is a FileList containing both files. I was expecting there to only be one file in the FileList.
This is what I'm seeing when I do that: |
after talking with jessica, we discussed the difference with for example, each time a user goes to add files to the input, |
I like this direction, it makes sense to me that My only uncertainty is when we remove files whether @taysea, @ericsoderberghp any opinions on this? |
@jcfilben As the original reporter of this issue, I expected the event.target.files list to contain the updated file list. So if you have a list of 5 files and remove one of them, you would receive a list of the 4 remaining files. |
With this PR, when you upload 5 files and then remove one of them, |
@amandadupell Fair enough. This brings up another issue however, which is the fact that you can't access { files } in typescript (Unless I'm doing something wrong). Should I create another issue for this? |
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.
Overall, I like the direction this is heading. Just had a few small comments.
window.HTMLInputElement.prototype, | ||
'files', | ||
).set; | ||
nativeInputValueSetter.call(inputRef.current, dt.files); |
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.
Shouldn't we be doing this irregardless of whether onChange
is provided? That way a test program could introspect the DOM to get the current value.
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.
i can move this out of the if statement based on onChange
Thank you for pointing this out! Please create a new issue covering this and include a codesandbox that reproduces the error you're seeing in Typescript. Someone will pick this up from out backlog moving forward. |
@amandadupell Done. the issue can be found below if anyone is immediately interested. |
@ericsoderberghp i spoke with jessica yesterday about aligning this PR with the native html https://stackoverflow.com/questions/1703228/how-can-i-clear-an-html-file-input-with-javascript considering this, i would be interested if this PR approach is a viable solution for how we decide to handle removing a file and how |
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.
This looks good to me. I think since there isn't a native way to remove a single file, showing the remaining file list seems like a reasonable behavior for event.target.files
when we remove a file.
What does this PR do?
Allows for a user to access
event.target.files
in theonChange
function when removing a file. Previously, this wasundefined
because we were passing in theevent
from the remove button'sonClick
, not the input'sonChange
(onClick
does not have afiles
value, only the FileInput'sonChange
does). This PR aligns theonChange
function so that theevent.target.files
is accurately adjusted on removal and allows a user to access the FileList.Where should the reviewer start?
FileInput.js
What testing has been done on this PR?
Tested with Storybook. Updated the FileInput/Multiple story to
console.log(event.target.files)
.How should this be manually tested?
Storybook
Do Jest tests follow these best practices?
screen
is used for querying.userEvent
is used in place offireEvent
.asFragment()
is used for snapshot testing.Any background context you want to provide?
Included link to a resource I looked into. Also, this method of using
dispatchEvent
can be seen in other Input components (see MaskedInput, Select, RangeInput).What are the relevant issues?
#6056
Screenshots (if appropriate)
Do the grommet docs need to be updated?
Yes, need to outline that
event.target.files
will correspond to the most recent event/action.Should this PR be mentioned in the release notes?
Yes.
Is this change backwards compatible or is it a breaking change?
Compatible.