You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
)}
/>
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);
});
}
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: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
Code of Conduct
The text was updated successfully, but these errors were encountered: