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

File Dropzone: Accepts Correct and Rejects Incorrect File Types #2750

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Copy link
Contributor

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 this

Components._.File.Dropzone.-.Default.Storybook.-.Google.Chrome.2024-09-17.15-48-22.mp4

}
});

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

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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';
Expand Down