A component that allows users to upload files via drag-and-drop or file selection.
If you want a more
react-aria-componentish component, check out rac'sFileTriggerandDropzonecomponents
import { FileDropzone } from "@opengovsg/oui"<FileDropzone aria-label="Upload files" />The FileDropzone component provides an accessible way for users to upload files through drag-and-drop or file selection. It supports various file types, size restrictions, and multiple file uploads.
If the component does not have a visible label (by passing a label prop), an aria-label or aria-labelledby prop must be passed instead to identify it to assistive technology.
Provide clear instructions to users with labels and descriptions.
Use the allowedMimeTypes prop to restrict which file types can be uploaded. Wildcards are supported (e.g., image/*).
Use the maxFiles prop to allow users to upload multiple files at once.
Set maxFileSize and minFileSize to enforce file size restrictions. Sizes are specified in bytes.
The FileDropzone automatically validates files against your constraints. Use the onError callback to handle validation errors and display error messages.
By default, image files are shown with a small preview. Use the imagePreview prop to control the preview size or disable it entirely.
Use showRejectedFiles to display files that failed validation along with their error messages.
Use the value and onChange props to control the selected files programmatically.
No files selected.
Use the children render prop to customize how uploaded files are displayed.
Use the size prop to change the size of the file dropzone.
Use the isDisabled prop to disable the file dropzone.
Use the isReadOnly prop to make the file dropzone read-only. Users can view uploaded files but cannot add or remove them.
The FileDropzone component automatically validates files based on:
allowedMimeTypesmaxFileSize and minFileSize (in bytes)maxFilesWhen validation fails, the component will:
onError callback with an error message (if provided)showRejectedFiles is enabledclassNames)itemClassNames)You can customize the FileDropzone component by passing custom Tailwind CSS classes to the component slots via the classNames and itemClassNames prop.
<FileDropzone
aria-label="Upload files"
classNames={{
dropzone: "border-dashed border-2 border-blue-500",
icon: "text-blue-500",
text: "text-sm",
}}
itemClassNames={{
base: "flex items-center space-x-2",
textContainer: "flex-1",
imageContainer: "w-12 h-12",
image: "object-cover",
name: "font-medium",
size: "text-sm text-gray-500",
error: "text-red-500",
actionButton: "text-blue-500",
}}
/>| Prop | Type | Default | Description |
|---|---|---|---|
label | React.ReactNode | - | The label for the file dropzone |
description | React.ReactNode | - | The description text shown below the dropzone |
errorMessage | React.ReactNode | - | The error message to display when validation fails |
value | FileItem[] | - | The currently selected files (controlled) |
defaultValue | FileItem[] | [] | The default files (uncontrolled) |
onChange | (files: FileItem[]) => void | - | Callback fired when the selected files change |
onError | (errorMessage: string) => void | - | Callback fired when file validation fails |
onRejection | (rejections: FileItem[]) => void | - | Callback fired when files are rejected |
allowedMimeTypes | string[] | [] (all types allowed) | Array of allowed MIME types (supports wildcards like image/*) |
maxFileSize | number | Number.POSITIVE_INFINITY | Maximum file size in bytes |
minFileSize | number | 0 | Minimum file size in bytes |
maxFiles | number | 1 | Maximum number of files allowed |
showFileSizeText | boolean | true | Whether to show file size information below the dropzone |
showRejectedFiles | boolean | false | Whether to display rejected files in the component |
hideDropzoneOnValue | boolean | true if maxFiles === 1 | Whether to hide the dropzone when files are uploaded |
imagePreview | "small" | "large" | null | "small" | Size of image preview, or null to disable previews |
size | "sm" | "md" | "md" | The size of the component |
isDisabled | boolean | false | Whether the component is disabled |
isReadOnly | boolean | false | Whether the component is read-only |
isInvalid | boolean | false | Whether the component should display as invalid |
isRequired | boolean | false | Whether the component is required |
classNames | SlotsToClasses<FileDropzoneSlots> | - | Custom CSS classes for component slots |
itemClassNames | SlotsToClasses<FileInfoDropzoneSlots> | - | Custom CSS classes for file info item slots |
validator | DropzoneOptions["validator"] | - | Custom file validator function |
children | (props: FileItemsRenderProps) => ReactNode | - | Render prop for custom file item rendering |