cloudscape-design / components

React components for Cloudscape Design System
https://cloudscape.design/
Apache License 2.0
2.33k stars 151 forks source link

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

Open NicoPowers opened 6 months ago

NicoPowers commented 6 months ago

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

jperals commented 6 months ago

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.