# Attachment Display Field URL: /react/components/attachment-display-field Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/components/attachment-display-field.mdx A component for displaying and managing media from external sources based on URLs. 사용 가능 버전: @seed-design/react@2.0.0, @seed-design/css@2.0.0 ## Preview ```tsx import { VStack } from "@seed-design/react"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; const sampleEntries: DisplayItemEntry[] = [ { id: "1", thumbnailUrl: "https://picsum.photos/seed/seed1/200/200", status: "success", }, { id: "2", thumbnailUrl: "https://picsum.photos/seed/seed2/200/200", status: "success", }, ]; // 외부 미디어 피커 모킹. 실제 환경에서는 네이티브 브릿지/모달/서버 호출 등으로 교체하세요. async function openMediaPicker(): Promise { const id = crypto.randomUUID(); return [ { id, thumbnailUrl: `https://picsum.photos/seed/${id}/200/200`, status: "success", }, ]; } export default function AttachmentDisplayPreview() { return ( { addEntries(await openMediaPicker()); }} /> ); } ``` ## Installation ### Default Includes components that can be used when reordering is not needed. - npm: npx @seed-design/cli@latest add ui:attachment-display-field - pnpm: pnpm dlx @seed-design/cli@latest add ui:attachment-display-field - yarn: yarn dlx @seed-design/cli@latest add ui:attachment-display-field - bun: bun x @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](https://dndkit.com/overview) dependency is added to the project. - npm: npx @seed-design/cli@latest add ui:attachment-display-field-reorderable - pnpm: pnpm dlx @seed-design/cli@latest add ui:attachment-display-field-reorderable - yarn: yarn dlx @seed-design/cli@latest add ui:attachment-display-field-reorderable - bun: bun x @seed-design/cli@latest add ui:attachment-display-field-reorderable ## Props ### `AttachmentDisplayField` ### `AttachmentDisplay` ### `AttachmentDisplayItem` ## Usage ### Basic usage Use by combining `AttachmentDisplay` or `AttachmentDisplayReorderable` inside `AttachmentDisplayField`. `AttachmentDisplay` does not use HTML ``. 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`](/react/components/attachment-field), `AttachmentDisplayField` does not validate files or directly handle file objects. ```tsx import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; { const pickedEntries = await openMediaPicker(); addEntries(pickedEntries); }} /> ; ``` 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](#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`. ```tsx import { AttachmentDisplay, AttachmentDisplayField, AttachmentDisplayItem, } from "seed-design/ui/attachment-display-field"; { addEntries(await openMediaPicker()); }} > {({ entries }) => entries.map((entry) => ( )) } ; ``` ## 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. ```tsx import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; const defaultEntries: DisplayItemEntry[] = [ { id: "1", thumbnailUrl: "https://picsum.photos/seed/trigger1/200/200", status: "success", }, ]; // 외부 미디어 피커 모킹. 실제 환경에서는 네이티브 브릿지/모달/서버 호출 등으로 교체하세요. async function openMediaPicker(): Promise { const id = crypto.randomUUID(); return [ { id, thumbnailUrl: `https://picsum.photos/seed/${id}/200/200`, status: "success", }, ]; } export default function AttachmentDisplayTrigger() { return ( { addEntries(await openMediaPicker()); }} /> ); } ``` ### 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. ```tsx "use client"; import { Text, VStack } from "@seed-design/react"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { useRef, useState } from "react"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; // 외부 미디어 피커 모킹. 실제 환경에서는 네이티브 브릿지/모달/서버 호출 등으로 교체하세요. async function openMediaPicker(): Promise { const id = crypto.randomUUID(); return [ { id, thumbnailUrl: `https://picsum.photos/seed/${id}/200/200`, status: "success", }, ]; } export default function AttachmentDisplayValueChanges() { const [entries, setEntries] = useState([]); const entriesRef = useRef(entries); const [logs, setLogs] = useState([]); entriesRef.current = entries; const addLog = (message: string) => { setLogs((prev) => [...prev, `[${new Date().toLocaleTimeString()}] ${message}`]); }; // addEntries로 추가하든 제거 버튼으로 지우든 변경은 항상 onEntriesChange로 흐르므로, // 추가/삭제 감지를 여기 한 곳에서 처리합니다. const handleEntriesChange = (next: DisplayItemEntry[]) => { const prev = entriesRef.current; const added = next.filter((n) => !prev.some((p) => p.id === n.id)); const removed = prev.filter((p) => !next.some((n) => n.id === p.id)); if (added.length > 0) addLog(`added: ${added.map((a) => a.id).join(", ")}`); if (removed.length > 0) addLog(`removed: ${removed.map((r) => r.id).join(", ")}`); setEntries(next); }; return ( {logs.length === 0 ? ( 아이템을 추가하거나 삭제하면 로그가 표시됩니다. ) : ( logs.map((log, index) => ( {log} )) )} { addEntries(await openMediaPicker()); }} /> ); } ``` ## 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. ```tsx "use client"; import { VStack } from "@seed-design/react"; import type { DisplayItemEntry, DisplayItemStatusDetails } from "@seed-design/react/primitive"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; const defaultEntries: DisplayItemEntry[] = [ { id: "1", thumbnailUrl: "https://picsum.photos/seed/upload1/200/200", status: "uploading", progress: 30, }, { id: "2", thumbnailUrl: "https://picsum.photos/seed/upload2/200/200", status: "success", }, { id: "3", thumbnailUrl: "https://picsum.photos/seed/upload3/200/200", status: "error", }, ]; // 외부 미디어 피커 모킹. 실제 환경에서는 네이티브 브릿지/모달/서버 호출 등으로 교체하세요. async function openMediaPicker(): Promise { const id = crypto.randomUUID(); return [ { id, thumbnailUrl: `https://picsum.photos/seed/${id}/200/200`, status: "uploading", }, ]; } // 실제 환경에서는 네이티브 브릿지 또는 외부 업로드 API와 연동하세요. // status는 컴포넌트가 콜백으로 전달하는 updateEntryStatus 헬퍼로만 갱신합니다. function simulateUpload( id: string, updateEntryStatus: (id: string, details: DisplayItemStatusDetails) => void, ) { updateEntryStatus(id, { status: "uploading", progress: 0 }); let progress = 0; const interval = setInterval(() => { progress += 20; if (progress >= 100) { clearInterval(interval); updateEntryStatus(id, Math.random() > 0.5 ? { status: "success" } : { status: "error" }); } else { updateEntryStatus(id, { status: "uploading", progress }); } }, 300); } export default function AttachmentDisplayStatus() { return ( { const pickedEntries = await openMediaPicker(); addEntries(pickedEntries); for (const entry of pickedEntries) { simulateUpload(entry.id, updateEntryStatus); } }} onRetry={(entry, { updateEntryStatus }) => simulateUpload(entry.id, updateEntryStatus)} /> ); } ``` - `uploading`: A [ProgressCircle](/react/components/progress-circle) is shown. - You can set `progress` to display the upload progress. - If `progress` is not set, it's shown in the [indeterminate](/react/components/progress-circle#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`](#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. ```tsx "use client"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { useState } from "react"; import { AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; import { AttachmentDisplayReorderable } from "seed-design/ui/attachment-display-field-reorderable"; const defaultEntries: DisplayItemEntry[] = [ { id: "1", thumbnailUrl: "https://picsum.photos/seed/reorder1/200/200", status: "success" }, { id: "2", thumbnailUrl: "https://picsum.photos/seed/reorder2/200/200", status: "success" }, { id: "3", thumbnailUrl: "https://picsum.photos/seed/reorder3/200/200", status: "success" }, ]; // 외부 미디어 피커 모킹. 실제 환경에서는 네이티브 브릿지/모달/서버 호출 등으로 교체하세요. async function openMediaPicker(): Promise { const id = crypto.randomUUID(); return [ { id, thumbnailUrl: `https://picsum.photos/seed/${id}/200/200`, status: "success", }, ]; } export default function AttachmentDisplayReorderableExample() { const [entries, setEntries] = useState(defaultEntries); return ( { addEntries(await openMediaPicker()); }} /> ); } ``` ## Examples ### Disabled You can disable the trigger button with the `disabled` prop. ```tsx import { VStack } from "@seed-design/react"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; const sampleEntries: DisplayItemEntry[] = [ { id: "1", thumbnailUrl: "https://picsum.photos/seed/disabled1/200/200", status: "success", }, ]; export default function AttachmentDisplayDisabled() { return ( {}} /> ); } ``` ### Read Only You can express a read-only state with the `readOnly` prop. The trigger, the file remove button, and reordering are all disabled. ```tsx import { VStack } from "@seed-design/react"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; const sampleEntries: DisplayItemEntry[] = [ { id: "1", thumbnailUrl: "https://picsum.photos/seed/readonly1/200/200", status: "success", }, { id: "2", thumbnailUrl: "https://picsum.photos/seed/readonly2/200/200", status: "success", }, ]; export default function AttachmentDisplayReadOnly() { return ( {}} /> ); } ``` ### Controlled You can control the item list externally using `entries` and `onEntriesChange`. ```tsx "use client"; import { HStack, Text, VStack } from "@seed-design/react"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { useState } from "react"; import { ActionButton } from "seed-design/ui/action-button"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; // 외부 미디어 피커 모킹. 실제 환경에서는 네이티브 브릿지/모달/서버 호출 등으로 교체하세요. async function openMediaPicker(): Promise { const id = crypto.randomUUID(); return [ { id, thumbnailUrl: `https://picsum.photos/seed/${id}/200/200`, status: "success", }, ]; } export default function AttachmentDisplayControlled() { const [entries, setEntries] = useState([]); return ( { addEntries(await openMediaPicker()); }} /> 현재 아이템: {entries.length}개 setEntries([])}> 전체 삭제 ); } ``` ### Custom Inset You can use the `--seed-attachment-input-extend-x` CSS variable to configure the scrolling item list to extend beyond the layout. ```tsx import { vars } from "@seed-design/css/vars"; import { VStack } from "@seed-design/react"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; import { TextField, TextFieldInput } from "seed-design/ui/text-field"; const defaultEntries: DisplayItemEntry[] = Array.from({ length: 8 }, (_, i) => ({ id: String(i + 1), thumbnailUrl: `https://picsum.photos/seed/inset${i + 1}/200/200`, status: "success", })); export default function AttachmentDisplayCustomInset() { return ( { // 외부 미디어 피커 호출 자리 }} /> ); } ``` ### Field Integration You can pass Field-related props such as `label`, `description`, and `errorMessage`. ```tsx "use client"; import { VStack } from "@seed-design/react"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { useState } from "react"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; const defaultEntries: DisplayItemEntry[] = [ { id: "1", thumbnailUrl: "https://picsum.photos/seed/field1/200/200", status: "success" }, ]; // 외부 미디어 피커 모킹. 실제 환경에서는 네이티브 브릿지/모달/서버 호출 등으로 교체하세요. async function openMediaPicker(): Promise { const id = crypto.randomUUID(); return [ { id, thumbnailUrl: `https://picsum.photos/seed/${id}/200/200`, status: "success", }, ]; } export default function AttachmentDisplayFieldExample() { const [entries, setEntries] = useState(defaultEntries); const invalid = entries.length < 1; return ( { addEntries(await openMediaPicker()); }} /> ); } ``` ### 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. ```tsx "use client"; import { IconArrowClockwiseCircularFill, IconXmarkFill } from "@karrotmarket/react-monochrome-icon"; import { AttachmentDisplay as SeedAttachmentDisplay, Icon, VStack } from "@seed-design/react"; import type { DisplayItemEntry } from "@seed-design/react/primitive"; import { AttachmentDisplay, AttachmentDisplayField } from "seed-design/ui/attachment-display-field"; import { ProgressCircle } from "seed-design/ui/progress-circle"; const LABEL_REMOVE = "삭제"; const LABEL_RETRY = "재시도"; function CustomImageItem({ entry, isCover, onRetry, }: { entry: DisplayItemEntry; isCover?: boolean; onRetry?: () => void; }) { return ( {isCover && 대표사진} {(e) => ( )} {onRetry && ( } /> {LABEL_RETRY} )} } /> ); } const defaultEntries: DisplayItemEntry[] = [ { id: "1", thumbnailUrl: "https://picsum.photos/seed/customizing1/200/200", status: "success" }, { id: "2", thumbnailUrl: "https://picsum.photos/seed/customizing2/200/200", status: "success" }, { id: "3", thumbnailUrl: "https://picsum.photos/seed/customizing3/200/200", status: "success" }, ]; export default function AttachmentDisplayCustomizingItems() { return ( { // 외부 미디어 피커 호출 자리 }} > {({ entries }) => entries.map((entry, index) => ( )) } ); } ``` ## Attachment Display Field vs. Attachment Field Use `AttachmentField` when you need to use the HTML ``, and use `AttachmentDisplayField` when you need to display media based on URLs by integrating with an external source. | | Attachment Field | Attachment Display | | ------------------------ | -------------------------- | ------------------------------------------- | | Media source | HTML `` | Image URL | | Data model | `File`-based `FileEntry` | URL-based `DisplayItemEntry` | | File selection | `` | Not handled (delegated to `onTriggerClick`) | | Upload via drag and drop | `AttachmentDropzone` | Not handled | | File validation | accept, maxFileSize, etc. | Not handled | | Form integration | `` sync | Not handled |