Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions .changeset/new-spies-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@twilio-paste/file-uploader": patch
---

fix(FileUploaderDropzone): honor acceptedMimeTypes prop during drag and drop operations
Previously, the FileUploaderDropzone component would ignore the acceptedMimeTypes prop when files were dragged and dropped, only validating file types when using the native file picker. This fix ensures that MIME type validation is consistently applied to both drag-and-drop and file input selection methods.

Added MIME type validation to drag and drop event handler
Added support for wildcard MIME type patterns (e.g., "image/\*")
Added console warnings for rejected file types
Maintained backward compatibility - no breaking changes to existing API
Added comprehensive test coverage for validation scenarios

Fixes #4377
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { HTMLPasteProps } from "@twilio-paste/types";
import * as React from "react";

import { FileUploaderContext } from "./FileUploaderContext";
import { arrayToCsv } from "./utils";
import { arrayToCsv, isValidMimeType } from "./utils";

export interface FileUploaderDropzoneProps
extends Omit<
Expand Down Expand Up @@ -181,6 +181,15 @@ export const FileUploaderDropzone = React.forwardRef<HTMLInputElement, FileUploa
setFileInputKey((prev) => prev + 1);
setDragActive(false);

if (event.dataTransfer && event.dataTransfer.files && event.dataTransfer.files.length > 0) {
for (let file of event.dataTransfer.files) {
if (!isValidMimeType(file.type, acceptedMimeTypes)) {
console.warn(`File type not accepted: ${file.type}`);
return;
}
}
}

if (onDrop) {
onDrop(event);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/paste-core/components/file-uploader/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ export const arrayToCsv = (value: string[]): string => {

return value.join(",");
};

/**
*
* Checks if a file's mime type matches any of the accepted types, including wildcard types
* @param fileType: string
* @param acceptedTypes: string[]
* @returns boolean
*/
export const isValidMimeType = (fileType: string, acceptedTypes: string[]): boolean => {
return acceptedTypes.some((acceptedType: string) => {
if (acceptedType.endsWith('/*')) {
const baseType = acceptedType.slice(0, -2);
return fileType.startsWith(baseType + '/');
}

return fileType === acceptedType;
})
}