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

[Feature Request]: Add Event Parameter to Distinguish Adding / Removing Files from File Upload component #2018

Open
2 tasks done
NicoPowers opened this issue Feb 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@NicoPowers
Copy link

Description

Hello,

When we add or remove files from the file upload component, the event that is passed in to the onChange prop is as follows:

cancelBubble : boolean,
cancelable : boolean,
defaultPrevented : boolean
detail: {value: Array(File)}

However, when uploading or remove files (by hitting on the X on one of the files), the event cannot tell them apart, therefore when trying to implement logic to prevent the use from uploading duplicated files, it is impossible to know whether they are uploading or removing.

This is how I am trying to add logic to the file upload, using react-hook-form

<Controller
                  name={formField.key as Path<TData>}
                  control={props.reactHookForm.control}
                  key={formField.key as string}
                  render={({ field, fieldState }) => (
                    <FormField
                      label={formField.label}
                      errorText={fieldState.error?.message}
                    >
                      <FileUpload
                        onChange={(e) => {                            
                          console.log(e);
                          let filesToUpload = e.detail.value; 
                          let skipFileCheck = false;                          
                          // check to make sure the file is allowed
                          if (formField.allowedTypes.includes(DocumentType.ANY))
                          {
                            skipFileCheck = true;
                          }
                          if (!skipFileCheck)
                          {
                            filesToUpload.forEach((file: File) => {                            
                            if (!formField.allowedTypes.includes(file.type as DocumentType)) {
                              // remove the file from the list
                              filesToUpload = filesToUpload.filter((f) => f !== file);                              
                            }                                                          
                            });  
                          }
                          
                          // remove any files that already exist in the field
                          if (field.value)
                          {
                            field.value.forEach((file: File) => {                            
                            filesToUpload = filesToUpload.filter((f) => f !== file);
                            });                            
                          }
                          
                          
                          field.onChange(filesToUpload);
                        }}
                        value={field.value ? field.value : []}
                        i18nStrings={{
                          uploadButtonText: e =>
                            e ? "Choose files" : "Choose file",
                          dropzoneText: e =>
                            e
                              ? "Drop files to upload"
                              : "Drop file to upload",
                          removeFileAriaLabel: e =>
                            `Remove file ${e + 1}`,
                          limitShowFewer: "Show fewer files",
                          limitShowMore: "Show more files",
                          errorIconAriaLabel: "Error"
                        }}
                        showFileLastModified
                        showFileSize        
                        multiple
                        showFileThumbnail
                        tokenLimit={3}
                        constraintText="Hint text for file requirements"
                      />
                    </FormField>
                  )}
                />

Code of Conduct

@NicoPowers NicoPowers added the enhancement New feature or request label Feb 27, 2024
@jperals
Copy link
Member

jperals commented Feb 29, 2024

Hi Nicolas,

Note that the detail.value taken by onChange contains the new value with all files to be uploaded, not only the newly added or removed file(s). So I believe you would only need to run your type checks again against he new value, but after that you can just assign the field value to the new value without having to compare both arrays.

This part is unnecessary:

// remove any files that already exist in the field
if (field.value) {
  field.value.forEach((file: File) => {                            
   filesToUpload = filesToUpload.filter((f) => f !== file);
  });                            
}

Let me know if this helps.

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

No branches or pull requests

2 participants