React

Attachment Display Field

A component for displaying and managing media from external sources based on URLs.

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

Installation

Default

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

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

Reorderable

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

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

Props

AttachmentDisplayField

Prop

Type

label?React.ReactNode
indicator?React.ReactNode
description?React.ReactNode
errorMessage?React.ReactNode
showRequiredIndicator?boolean | undefined
entries?DisplayItemEntry[] | undefined
defaultEntries?DisplayItemEntry[] | undefined
onEntriesChange?((entries: DisplayItemEntry[]) => void) | undefined

AttachmentDisplay

Prop

Type

onTriggerClick(helpers: Pick<UseAttachmentDisplayReturn, "addEntries" | "updateEntryStatus">) => void
children?((context: UseAttachmentDisplayContext) => React.ReactNode) | undefined
onRetry?((entry: DisplayItemEntry, helpers: Pick<UseAttachmentDisplayReturn, "updateEntryStatus">) => void) | undefined

AttachmentDisplayItem

Prop

Type

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

Usage

Basic usage

Use by combining AttachmentDisplay or AttachmentDisplayReorderable inside AttachmentDisplayField.

AttachmentDisplay does not use HTML <input type="file">. Call an external media picker via onTriggerClick and display the result by passing it to addEntries provided as a callback argument. Since addEntries internally handles maxEntries upper limit and single-mode (maxEntries={1}) substitution, it's safer than directly spreading entries.

Unlike AttachmentField, AttachmentDisplayField does not validate files or directly handle file objects.

import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field";

<AttachmentDisplayField defaultEntries={[]} maxEntries={10}>
  <AttachmentDisplay
    onTriggerClick={async ({ addEntries }) => {
      const pickedEntries = await openMediaPicker();
      addEntries(pickedEntries);
    }}
  />
</AttachmentDisplayField>;

A controlled approach that directly manages the list with entries and onEntriesChange is also supported. addEntries behaves the same way in this case as well (see Controlled).

Building Items Directly

AttachmentDisplay and AttachmentDisplayReorderable use children as a render prop.

You can use entries to render AttachmentDisplayItem directly. In this case, it's recommended to use the id provided by the DisplayItemEntry type as the key.

If children is not provided, entries is automatically rendered as AttachmentDisplayItem.

import {
  AttachmentDisplay,
  AttachmentDisplayField,
  AttachmentDisplayItem,
} from "seed-design/ui/attachment-display-field";

<AttachmentDisplayField defaultEntries={[]} maxEntries={10}>
  <AttachmentDisplay
    onTriggerClick={async ({ addEntries }) => {
      addEntries(await openMediaPicker());
    }}
  >
    {({ entries }) =>
      entries.map((entry) => (
        <AttachmentDisplayItem key={entry.id} entry={entry} />
      ))
    }
  </AttachmentDisplay>
</AttachmentDisplayField>;

Adding Entries

Trigger

AttachmentDisplay provides a layout that includes a trigger (upload button). The onTriggerClick callback runs when the trigger is clicked. It typically calls out to an external media picker. The callback receives { addEntries, updateEntryStatus } as arguments, letting you add the picker results and immediately update the upload status.

Listening to Entry Changes

entries is the list of items currently being displayed. You can detect change events for files registered in entries with the onEntriesChange callback.

Managing Item Status

Each item in entries can have a status of pending, uploading, success, or error. You can freely set the default status of a newly added item to match your intent (uploading for an item an external picker just handed off, success if you're hydrating media that's already been uploaded).

When integrating with an external upload API, use the updateEntryStatus helper, passed along with the onTriggerClick and onRetry callbacks, to update each 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 AttachmentDisplay runs with (entry, { updateEntryStatus }) as arguments. Use updateEntryStatus to set that item back to uploading to retry the upload.

Reordering Entries

Using AttachmentDisplayReorderable lets you change the order of items by dragging.

This component is provided as a separate snippet, ui:attachment-display-field-reorderable, to keep the dnd-kit dependency isolated.

Since reorderEntry 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

Disabled

You can disable the trigger button with the disabled prop.

Read Only

You can express a read-only state with the readOnly prop. The trigger, the file remove button, and reordering are all disabled.

Controlled

You can control the item list externally using entries and onEntriesChange.

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 pass Field-related props such as label, description, and errorMessage.

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 AttachmentDisplay.ItemBadge provided by @seed-design/react.

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

Attachment Display Field vs. Attachment Field

Use AttachmentField when you need to use the HTML <input type="file">, and use AttachmentDisplayField when you need to display media based on URLs by integrating with an external source.

Attachment FieldAttachment Display
Media sourceHTML <input type="file">Image URL
Data modelFile-based FileEntryURL-based DisplayItemEntry
File selection<input type="file">Not handled (delegated to onTriggerClick)
Upload via drag and dropAttachmentDropzoneNot handled
File validationaccept, maxFileSize, etc.Not handled
Form integration<input type="file"> syncNot handled

Table of contents