-
Notifications
You must be signed in to change notification settings - Fork 934
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
USWDS - File Input: Creates unique ID per upload. Fixes #4144 #4313
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.
Nice work! Just some minor comments.
src/js/components/file-input.js
Outdated
| @@ -104,6 +104,9 @@ const replaceName = (s) => { | |||
| */ | |||
| const makeSafeForID = (name) => name.replace(/[^a-z0-9]/g, replaceName); | |||
|
|
|||
| // Creates a unique ID based on upload time. Here we devide by 1000 and use mathfloor to get rid of the milliseconds. | |||
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.
| // Creates a unique ID based on upload time. Here we devide by 1000 and use mathfloor to get rid of the milliseconds. | |
| // Takes a generated safe ID and creates a unique ID. |
src/js/components/file-input.js
Outdated
| @@ -221,7 +224,8 @@ const handleChange = (e, fileInputEl, instructions, dropTarget) => { | |||
|
|
|||
| // Starts with a loading image while preview is created | |||
| reader.onloadstart = function createLoadingImage() { | |||
| const imageId = makeSafeForID(fileName); | |||
| const imageId = createUniqueID(makeSafeForID(fileName)); | |||
| console.log(imageId); | |||
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.
Remove test code console.log().
|
Thank you! |
Description
#4144 pointed out that if a file with the same name is uploaded in two separate file input fields the preview spinner would spin indefinitely. After some research, I found this is because the two documents would share the same ID and the function that would replace the spinner would only select the first ID on the page. By implementing a function that assigns each upload an individual ID, an image preview properly loads.
The function used uses the date and time down to the second to assign an ID.
Steps to reproduce error
(As provided in #4144)
Additional information
Code to create unique ID based on upload time. Here we devide by 1000 and use mathfloor to get rid of the milliseconds.
Partnered with the existing makeSafeForID function:
Example Output:

Screenshot
Before you hit Submit, make sure you’ve done whichever of these applies to you:
npm testand make sure the tests for the files you have changed have passed.