-
Notifications
You must be signed in to change notification settings - Fork 72
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
File Dropzone: Accepts Correct and Rejects Incorrect File Types #2750
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ import { IconCancel } from '../../icons/svgs/icon-cancel'; | |
}) | ||
export class ModusFileDropzone { | ||
@State() dropzoneFiles: Array<File> = []; | ||
@State() error: 'maxFileCount' | 'maxFileNameLength' | 'maxTotalFileSize' | null = null; | ||
@State() error: 'maxFileCount' | 'maxFileNameLength' | 'maxTotalFileSize' | 'invalidFileType' | null = null; | ||
@State() fileDraggedOver = false; | ||
|
||
/** (optional) The dropzone's accepted file types */ | ||
|
@@ -141,6 +141,51 @@ export class ModusFileDropzone { | |
}; | ||
|
||
updateDropzoneState = (): void => { | ||
// Checks and delete invalid accepted-file-types | ||
if (this.acceptFileTypes) { | ||
const acceptedFileTypes = this.acceptFileTypes | ||
.split(',') | ||
.map((ext) => ext.trim()) | ||
.filter((ext) => ext.length > 0); | ||
const invalidFiles = []; | ||
|
||
for (let i = 0; i < this.dropzoneFiles.length; i++) { | ||
const fileType = this.dropzoneFiles[i].type; | ||
const [typeCategory, fileExtension] = fileType.split('/'); | ||
|
||
const isAccepted = acceptedFileTypes.some((acceptedType) => { | ||
if (acceptedType.includes('/')) { | ||
const [acceptedCategory, acceptedExtension] = acceptedType.split('/'); | ||
if (acceptedExtension === '*' && acceptedCategory === typeCategory) { | ||
return true; | ||
} | ||
return acceptedType === fileType; | ||
} else { | ||
return '.' + fileExtension === acceptedType; | ||
} | ||
}); | ||
|
||
if (!isAccepted) { | ||
invalidFiles.push(this.dropzoneFiles[i]); | ||
} | ||
} | ||
|
||
if (invalidFiles.length > 0) { | ||
this.error = 'invalidFileType'; | ||
this.errorMessageTop = `Some files are not of the accepted types. Please remove the following file(s) to continue: ${invalidFiles | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll check with our UX and get back on how to handle this specific error state with overflowing content. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @prashanth-offcl, It's Dion. I will continue the work for my personal Github account (this one). I wanted to follow-up on this comment to see if UX's response for this. |
||
.map((file) => file.name) | ||
.join(', ')}`; | ||
|
||
this.files.emit([this.dropzoneFiles, this.error]); | ||
return; | ||
} | ||
|
||
if (this.error === 'invalidFileType' && invalidFiles.length === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @diTrimInt if there are no errors then the error variables are reset at the end of this function so we can remove this |
||
this.error = null; | ||
this.errorMessageTop = ''; | ||
} | ||
} | ||
|
||
// Raise error if having multiple files is invalid. | ||
if (!this.multiple && this.dropzoneFiles.length > 1) { | ||
this.error = 'maxFileCount'; | ||
|
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.
@diTrimInt I'm still able to reproduce the issue with comma separated extensions (.doc,.docx,.jpg,.png). Can we have a separate check for
.
like thisComponents._.File.Dropzone.-.Default.Storybook.-.Google.Chrome.2024-09-17.15-48-22.mp4