- Docs
- Components
- Forms
File upload
Choose or drop files: a drop zone over the browser's own file input, each upload with its status.
- Import
- @qeetrix/react/experimental
- Status
- ExperimentalSince 0.0.6
- Renders
<input type="file">Server-rendered- Tested
- WCAG 2.2 AAaxe on every pull request
01
Import
// Experimental: from the package's experimental entry, until it's stable.
import { FileUpload } from "@qeetrix/react/experimental";02
Props
And every native attribute of the element it renders.
| Prop | Type | Default | What it does |
|---|---|---|---|
onSelect | (files: File[]) => void | — | The files that pass accept, maxSize and multiple: upload them here. |
items | FileUploadItem[] | — | The files you're handling: { id, name, size, status, progress, error }. |
onRemove | (id: string) => void | — | A row's remove; without it, rows have none. |
accept | string | — | The types allowed: ".xml,.json" or "image/*". |
maxSize | number | — | The largest file, in bytes. |
multiple | boolean | false | Several at once. |
hint | ReactNode | from accept and maxSize | The line under the prompt. |
locale · messages | … | the provider's | Sizes and type lists in the locale; its words. |
03
Parts
Each part carries a data-slot, for tests and placement.
| data-slot | Element | What it is |
|---|---|---|
file-upload | <div> | The root. |
file-upload-zone | <label> | The drop zone. |
file-upload-input | <input type="file"> | The input; a Field labels it. |
file-upload-list | <ul> | The files. |
file-upload-item | <li> | A file and its status. |
04
Report each upload
Add the file as uploading, update its progress, then done or error.
const [items, setItems] = useState<FileUploadItem[]>([]);
const upload = (files: File[]) =>
files.forEach(async (file) => {
const id = crypto.randomUUID();
setItems((all) => [...all, { id, name: file.name, size: file.size, status: "uploading" }]);
const result = await uploadMetadata(file);
setItems((all) => all.map((item) => (item.id === id ? { ...item, ...result } : item)));
});