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

USWDS - File Input: Creates unique ID per upload. Fixes #4144 #4313

Merged
merged 5 commits into from
Oct 25, 2021

Conversation

mahoneycm
Copy link
Contributor

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)

  1. Go to https://designsystem.digital.gov/components/file-input/
  2. Upload any text file to the first file input component in the form.
  3. Upload the same file to the second file input component in the form.
  4. The second file input will display the name of the newly uploaded file but, but the loader spins forever.

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.

const createUniqueID = (name) => `${name}-${Math.floor(Date.now().toString() / 1000)}`;

Partnered with the existing makeSafeForID function:

const imageId = createUniqueID(makeSafeForID(fileName));

Example Output:
image

Screenshot

image

Before you hit Submit, make sure you’ve done whichever of these applies to you:

  • Follow the 18F Front End Coding Style Guide and Accessibility Guide.
  • Run npm test and make sure the tests for the files you have changed have passed.
  • Run your code through HTML_CodeSniffer and make sure it’s error free.
  • Title your pull request using this format: [Website] - [UI component]: Brief statement describing what this pull request solves.

Copy link
Contributor

@mejiaj mejiaj 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! Just some minor comments.

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// 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.

@@ -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);
Copy link
Contributor

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().

@thisisdano
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File Input: Loader spins indefinitely after selecting files with the same name in multiple inputs
4 participants