- Add the registry
- Add file-input
Usage
FileInput holds the chosen files as File objects. It doesn't upload them: send them yourself,
or give it a name to submit them with a form.
FileInputDropzone accepts dropped files and opens the file picker on click. It's a button, so
use spans inside it. It renders an upload icon above its content: pass icon to replace the icon,
or null to remove it.
FileInputList renders a FileInputItem for each file, with a preview, the file's name and size,
and a button to remove it. FileInputItemMetadata truncates a long name in the middle with
Truncate, so the extension stays visible. To customize an item, such as to
show upload progress, pass FileInputList a function that renders each file, as in the example
below.
accept, maxSize and maxFiles limit which files are accepted. onFileReject is called with
each rejected file and the reason. Folders dropped on the dropzone are ignored.
Removing a file moves focus to the next file's remove button, or the previous one's. When the last file is removed, focus moves to the dropzone or the trigger, or, if there's neither, to the next focusable element on the page.
Composition
Examples
Upload progress
Put a FileInputItemProgress in FileInputItemMetadata to show progress while a file uploads.
With a label
In a Field, the FieldLabel labels the dropzone and the trigger. Set
invalid on the Field and explain a rejected file with a FieldError. The field's
FieldDescription and FieldError describe the dropzone and the trigger, which become invalid or
disabled along with the field.
One contact per row, up to 5 MB.
In a form
Give a Field or the FileInput a name, and the files are submitted under
it, each as a separate value, so formData.getAll(name) returns them all. When the FileInput is
outside the form, pass the form's id as form. With required, the form won't submit without a
file, and validation fails with valueMissing. A Form's onFormSubmit
receives the File objects as an array.
Resetting the form restores defaultValue, or clears the files if there isn't one. When that
changes the files, onValueChange is called with the reason none, so the parent of a controlled
FileInput decides whether to apply the change.
With a button
FileInputTrigger opens the file picker from a button, when a drop zone would take too much room.
Disabled
Props
The file also exports formatFileSize(bytes, locale?), which formats a size such as "2.4 MB" for
locale, the same way FileInput formats each file's size with its own locale.
Base UI's styling (opens in a new tab) and composition (opens in a new tab) guides say more about className, style and render.
FileInput
FileInputDropzone
FileInputTrigger
FileInputList
FileInputItem
FileInputItemPreview
FileInputItemMetadata
FileInputItemProgress
FileInputItemRemove
useFileInput
useFileInput() reads the FileInput that your part renders in, and returns its files and
controls:
To accept files dropped on your own element, pass its drop event's dataTransfer to
getDroppedFiles(dataTransfer), then pass the result to add. getDroppedFiles returns the files
without any folders, which browsers list as empty files.