React

Attachment Field

A component where you can select files or upload via drag and drop.

Available since@seed-design/react@2.0.0, @seed-design/css@2.0.0

Installation

Default

Includes components that can be used when reordering files is not needed.

npx @seed-design/cli@latest add ui:attachment-field

Reorderable

Includes components that can be used when reordering files via drag and drop is needed. The dnd-kit dependency is added to the project.

npx @seed-design/cli@latest add ui:attachment-field-reorderable

Props

AttachmentField

Prop

Type

label?React.ReactNode
indicator?React.ReactNode
description?React.ReactNode
errorMessage?React.ReactNode
showRequiredIndicator?boolean | undefined
inputProps?React.InputHTMLAttributes<HTMLInputElement> | undefined
fieldRef?React.Ref<HTMLDivElement> | undefined
rootProps?React.HTMLAttributes<HTMLDivElement> | undefined
name?string | undefined
acceptedFileEntries?FileEntry[] | undefined
defaultAcceptedFileEntries?FileEntry[] | undefined
onAcceptedFileEntriesChange?((fileEntries: FileEntry[]) => void) | undefined
onFileReject?((rejections: FileRejection[]) => void) | undefined
onFileAccept?((acceptedEntries: FileEntry[], helpers: { updateFileEntryStatus: (id: string, details: FileStatusDetails) => void; }) => void) | undefined

AttachmentInput

Prop

Type

children?((context: UseFileUploadContext) => React.ReactNode) | undefined
onRetry?((fileEntry: FileEntry, helpers: Pick<UseFileUploadReturn, "updateFileEntryStatus">) => void) | undefined

AttachmentDropzone

Prop

Type

children?((context: UseFileUploadContext) => React.ReactNode) | undefined
onRetry?((fileEntry: FileEntry, helpers: Pick<UseFileUploadReturn, "updateFileEntryStatus">) => void) | undefined

AttachmentInputItem

Prop

Type

onRetry?(() => void) | undefined
fileEntryFileEntry

Usage

Basic usage

Use by combining one of AttachmentInput, AttachmentDropzone, AttachmentInputReorderable, or AttachmentDropzoneReorderable inside AttachmentField.

import {
  AttachmentField,
  AttachmentInput,
  AttachmentDropzone,
} from "seed-design/ui/attachment-field";

<AttachmentField>
  <AttachmentInput />
</AttachmentField>;

<AttachmentField>
  <AttachmentDropzone />
</AttachmentField>;
import { AttachmentField } from "seed-design/ui/attachment-field";
import {
  AttachmentInputReorderable,
  AttachmentDropzoneReorderable,
} from "seed-design/ui/attachment-field-reorderable";

<AttachmentField>
  <AttachmentInputReorderable />
</AttachmentField>;

<AttachmentField>
  <AttachmentDropzoneReorderable />
</AttachmentField>;

Building Items Directly

AttachmentInput, AttachmentDropzone, AttachmentInputReorderable, and AttachmentDropzoneReorderable use children as a render prop.

You can use acceptedFileEntries to render AttachmentInputItem directly. In this case, it's recommended to use the id provided by the FileEntry type as the key.

If children is not provided, the files registered in acceptedFileEntries are automatically rendered as AttachmentInputItem.

import {
  AttachmentField,
  AttachmentInput,
  AttachmentInputItem,
} from "seed-design/ui/attachment-field";

<AttachmentField>
  <AttachmentInput>
    {({ acceptedFileEntries }) =>
      acceptedFileEntries.map((entry) => (
        <AttachmentInputItem key={entry.id} fileEntry={entry} />
      ))
    }
  </AttachmentInput>
</AttachmentField>;

Uploading Files

Trigger

Using AttachmentInput lets you use a layout that includes a trigger (upload button).

Dropzone

Using AttachmentDropzone lets you use a layout that includes a drag-and-drop area.

Listening to Accepted File Changes

acceptedFileEntries is the list of files that have passed validation. You can detect change events for files registered in acceptedFileEntries with the onAcceptedFileEntriesChange callback.

Validating Files

You can validate a file the user selected before it's registered in acceptedFileEntries.

The onFileReject callback runs for each invalid file. In that callback, you can check the file and the error codes to display an error message.

Max Files

You can limit the maximum number of uploadable files with maxFiles. The default value is 1. Once the maximum is reached, the trigger and dropzone are disabled.

If more files are selected than can be uploaded, files up to the uploadable limit are registered in acceptedFileEntries. The onFileReject callback runs for each of the remaining files. In this case, the error for the rejected files is "TOO_MANY_FILES".

Invalid File Type

You can restrict the kinds of files that can be uploaded with accept.

You can specify a MIME type (image/png, image/*) or an extension (.png, .jpg, .jpeg) format, and if you pass a string[], each string is joined with ,.

Restricting the kinds of files a user can select via the accept attribute registered on <input type="file"> is only a convenience feature that works in the browser UI. So users can still select files of a restricted kind through a feature like Show all files in the browser's file selection dialog. Files selected this way trigger the onFileReject callback with an "INVALID_TYPE" error.

If file type validation is required, that validation must also be performed on the server.

File Size

You can use the minFileSize and maxFileSize props.

  • If a file the user selected is smaller than minFileSize, the onFileReject callback runs with a "FILE_TOO_SMALL" error.
  • If a file the user selected is larger than maxFileSize, the onFileReject callback runs with a "FILE_TOO_LARGE" error.

Custom Validation

You can add your own validation for each file with the validate prop. By returning a custom error code, you can branch the message by error type in onFileReject.

Managing File Status

Each item in acceptedFileEntries can have a status of pending, uploading, success, or error. The default status for a newly added item is pending.

If you integrate with an external upload API right after file selection, you can show the user the uploading status of each file item. Receive the newly added file in the onFileAccept callback, and use the updateFileEntryStatus helper provided alongside it to update the item's status.

  • uploading: A ProgressCircle is shown.
    • You can set progress to display the upload progress.
    • If progress is not set, it's shown in the indeterminate state.
  • error: A retry button is shown.
    • When clicked, the onRetry callback set on AttachmentInputItem runs.

A file item that passed validation remains in acceptedFileEntries regardless of its status, so it's included when a native form is submitted.

Reordering Files

Using AttachmentInputReorderable or AttachmentDropzoneReorderable lets you change the order of files by dragging.

Both components are provided as a separate snippet, ui:attachment-field-reorderable, to keep the dnd-kit dependency isolated.

Since reorderFileEntry is provided via Context, you can implement your own drag-and-drop behavior if needed, or integrate with a drag-and-drop library your project is already using.

Examples

Showing Thumbnails

To show an image preview instead of the file name and size, restrict the user to selecting only image files by setting accept to something like "image/*" or ["image/png", "image/jpeg"].

For some image formats such as "image/heic", the image preview may not display depending on the browser.

Disabled

The disabled prop disables the trigger and dropzone to block new file selection, and marks <input type="file"> as disabled so its value is not sent when the form is submitted.

Read Only

You can display attached files as read-only with the readOnly prop. The trigger, dropzone, file remove button, and reordering are all disabled, but the value of <input type="file"> is preserved and sent along when the form is submitted.

Controlled

You can control the file list externally using acceptedFileEntries and onAcceptedFileEntriesChange.

Custom Inset

You can use the --seed-attachment-input-extend-x CSS variable to configure the scrolling item list to extend beyond the layout.

Field Integration

You can use Field-related props such as label, description, and errorMessage.

Form (Uncontrolled)

Files registered in acceptedFileEntries are synced to the files of <input type="file">, so you can use <form> and the name prop to include them in the formData.

React Hook Form

Customizing Items

If you need additional customization beyond the default item layout provided by the snippet, you can build the item yourself using elements such as AttachmentInput.ItemBadge provided by @seed-design/react.

In the example below, AttachmentInput.ItemBadge is used to show a "Main photo" badge on the first image.

Table of contents