# Attachments

> 用文件行、紧凑附件或图片网格展示资源，并组合状态、操作与图片预览。

网页来源：/components/attachments

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

`QuireAttachment` 展示一个附件，`QuireAttachments` 按稳定 `id` 组合多个附件并共享图片预览。既可用于文档和文件列表，也可放进消息或输入区；它们不选择或上传文件。

## 单份附件

`item` 至少提供 `id` 和 `name`。`size` 使用字节，未知时省略；`mediaType` 以 `image/` 开头时可使用 `thumbnailUrl` / `url` 显示图片。`actions` 显式列出允许的操作，收到 `action` 后由宿主执行。

### 示例：attachments/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireAttachment } from '@quirekit/ui/vue';
import type { QuireKitAttachmentActionDetails, QuireKitAttachmentItem } from '@quirekit/ui';
const item: QuireKitAttachmentItem = { id: 'report', name: '季度研究报告.pdf', mediaType: 'application/pdf', size: 248320, source: 'remote', actions: ['open', 'download'] };
const feedback = ref('打开和下载只发出意图，本示例不会访问远程文件。');
/** @param details - Explicit host operation request. */
function action(details: QuireKitAttachmentActionDetails): void { feedback.value = `收到 ${details.action}：${details.attachmentId}`; }
</script>
<template>
  <div class="p-qk-surface grid gap-3" data-attachment-basic>
    <QuireAttachment :item="item" @action="action" />
    <p class="m-0 text-qk-helper text-qk-text-tertiary" role="status">{{ feedback }}</p>
  </div>
</template>
```

## 列表、行内与图片网格

`layout="list"` 是默认文件行；`inline` 用于紧凑上下文，`grid` 突出图片预览。长名称保留完整提示并省略显示，集合随可用宽度换行。图片支持点击、Enter 预览、方向键切换和 Escape 返回；`:preview="false"` 可关闭预览。

### 示例：attachments/layouts

文件：`layouts.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireAttachments, QuireButton, QuireSegmented } from '@quirekit/ui/vue';
import type { QuireKitAttachmentActionDetails, QuireKitAttachmentItem, QuireKitAttachmentLayout } from '@quirekit/ui';
import { attachmentExamples } from './data';
const layout = ref<QuireKitAttachmentLayout>('grid');
const items = ref<QuireKitAttachmentItem[]>([...attachmentExamples]);
const feedback = ref('');
const layouts = [{ value: 'grid', label: '图片网格' }, { value: 'list', label: '文件列表' }, { value: 'inline', label: '紧凑附件' }];
/** @param details - Host intent; only this demo's removal mutates the list. */
function action(details: QuireKitAttachmentActionDetails): void {
  if (details.action === 'remove') items.value = items.value.filter(item => item.id !== details.attachmentId);
  feedback.value = `收到 ${details.action}：${details.attachmentId}`;
}
</script>
<template>
  <div class="p-qk-surface grid gap-4" data-attachments-layouts>
    <div class="flex flex-wrap items-center gap-2">
      <QuireSegmented v-model="layout" :items="layouts" label="附件布局" />
      <QuireButton variant="ghost" @click="items = [...attachmentExamples]; feedback = ''">恢复附件</QuireButton>
    </div>
    <QuireAttachments :items="items" :layout="layout" label="参考附件" @action="action" />
    <p v-if="feedback" class="m-0 text-qk-helper text-qk-text-tertiary" role="status">{{ feedback }}</p>
  </div>
</template>
```

文件：`data.ts`

```ts
import type { QuireKitAttachmentItem } from '@quirekit/ui';

/** Local, original design-site artwork keeps the public example deterministic. */
export const attachmentExamples: readonly QuireKitAttachmentItem[] = [
  { id: 'coast', name: '海岸晨光.svg', mediaType: 'image/svg+xml', size: 32768, source: 'remote', url: '/examples/image/coast.svg', thumbnailUrl: '/examples/image/coast-small.svg', alt: '暖色日光下的蓝绿色海岸', actions: ['download', 'remove'] },
  { id: 'ridge', name: '山间暮色.svg', mediaType: 'image/svg+xml', size: 24576, source: 'remote', url: '/examples/image/ridge.svg', alt: '暮色中层叠的山脊与月亮', actions: ['download', 'remove'] },
  { id: 'report', name: '项目计划与交付清单.pdf', mediaType: 'application/pdf', size: 248320, source: 'remote', actions: ['open', 'remove'] }
];
```

集合变化时保持每项 `id`，不要使用数组索引或 URL。集合的预览工具栏复用当前附件允许的操作；删除当前图片会显示相邻图片，全部移除则关闭。原触发器消失时焦点返回集合；如果宿主连集合本身一起卸载，应恢复到仍存在的输入或入口。

## 横向附件带

`layout="strip"` 将附件保持在一行，图片使用缩略图，普通文件使用紧凑卡片与 [FileIcon](/raw/components/file-icon.md)。超出宽度后可使用两端按钮、触控板或横向手势查看；滚轮到达边缘后仍允许页面滚动。Tab 聚焦隐藏的操作时自动滚动到可见位置，不引入 Toolbar 的方向键导航。

就绪附件的移除按钮在桌面悬停或键盘聚焦时出现，图片上使用圆形覆盖按钮，不常驻白底操作托盘。触屏保持移除入口可见，并扩大图片区域，避免 44px 操作目标遮住图片中心；失败时的必要恢复操作始终可达。

### 示例：attachments/strip

文件：`strip.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireAttachments, QuireButton } from '@quirekit/ui/vue';
import type { QuireKitAttachmentItem } from '@quirekit/ui';
import { attachmentExamples } from './data';
const initial: QuireKitAttachmentItem[] = [
  ...attachmentExamples.map(item => ({ ...item, actions: ['remove'] as const })),
  { id: 'word', name: '需求确认与范围说明.docx', size: 90112, actions: ['remove'] },
  { id: 'excel', name: '实施阶段预算.xlsx', size: 56400, actions: ['remove'] },
  { id: 'video', name: '产品演示.mp4', size: 7340032, actions: ['remove'] },
  { id: 'zip', name: '上线交付资料.zip', size: 9227468, actions: ['remove'] }
];
const items = ref([...initial]);
</script>
<template>
  <div class="grid gap-4 p-qk-surface" data-attachments-strip>
    <QuireAttachments :items="items" layout="strip" label="横向附件" @action="details => { if (details.action === 'remove') items = items.filter(item => item.id !== details.attachmentId); }" />
    <div><QuireButton variant="ghost" @click="items = [...initial]">恢复附件带</QuireButton></div>
  </div>
</template>
```

文件：`data.ts`

```ts
import type { QuireKitAttachmentItem } from '@quirekit/ui';

/** Local, original design-site artwork keeps the public example deterministic. */
export const attachmentExamples: readonly QuireKitAttachmentItem[] = [
  { id: 'coast', name: '海岸晨光.svg', mediaType: 'image/svg+xml', size: 32768, source: 'remote', url: '/examples/image/coast.svg', thumbnailUrl: '/examples/image/coast-small.svg', alt: '暖色日光下的蓝绿色海岸', actions: ['download', 'remove'] },
  { id: 'ridge', name: '山间暮色.svg', mediaType: 'image/svg+xml', size: 24576, source: 'remote', url: '/examples/image/ridge.svg', alt: '暮色中层叠的山脊与月亮', actions: ['download', 'remove'] },
  { id: 'report', name: '项目计划与交付清单.pdf', mediaType: 'application/pdf', size: 248320, source: 'remote', actions: ['open', 'remove'] }
];
```

## 上传状态与恢复

`state` 支持 `ready`、`uploading`、`failed`、`unavailable`。已知 `progress` 显示进度条，未知进度显示 Shimmer；`statusText` 补充具体原因。只有 ready 可打开/下载，failed 或 unavailable 可重试；移除始终需要宿主明确允许。

### 示例：attachments/states

文件：`states.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireAttachments, QuireButton } from '@quirekit/ui/vue';
import type { QuireKitAttachmentActionDetails, QuireKitAttachmentItem } from '@quirekit/ui';
/** @returns A fresh host-owned state fixture; no real uploads are performed. */
function initial(): QuireKitAttachmentItem[] {
  return [
    { id: 'upload', name: '项目资料.zip', size: 7340032, state: 'uploading', progress: 42, actions: ['remove'] },
    { id: 'pending', name: '背景素材.png', state: 'uploading', statusText: '正在准备上传', actions: ['remove'] },
    { id: 'failed', name: '需求说明.pdf', size: 172032, state: 'failed', statusText: '连接中断，文件已保留', actions: ['retry', 'remove'] },
    { id: 'unavailable', name: '会议附件.docx', state: 'unavailable', statusText: '访问链接已失效', actions: ['retry', 'remove'] },
    { id: 'thumbnail', name: '预览暂不可用.svg', mediaType: 'image/svg+xml', url: '/examples/image/attachment-unavailable.svg', actions: ['download', 'remove'] }
  ];
}
const items = ref(initial());
const feedback = ref('重试和完成由本地示例模拟，不会上传。');
/** @param details - Host intent to remove or restart the simulated resource. */
function action(details: QuireKitAttachmentActionDetails): void {
  if (details.action === 'remove') items.value = items.value.filter(item => item.id !== details.attachmentId);
  if (details.action === 'retry') items.value = items.value.map(item => item.id === details.attachmentId ? { ...item, state: 'uploading', progress: 0, statusText: '正在重试' } : item);
  feedback.value = `收到 ${details.action}：${details.attachmentId}`;
}
/** Settle only active work; failed/unavailable items stay available for explicit recovery. */
function finish(): void {
  items.value = items.value.map(item => item.state === 'uploading' ? { ...item, state: 'ready', statusText: '已就绪', actions: ['open', 'remove'] } : item);
}
</script>
<template>
  <div class="p-qk-surface grid gap-3" data-attachments-states>
    <QuireAttachments :items="items" label="附件处理状态" @action="action" />
    <div class="flex flex-wrap gap-2"><QuireButton @click="finish">模拟完成</QuireButton><QuireButton variant="ghost" @click="items = initial()">恢复状态</QuireButton></div>
    <p class="m-0 text-qk-helper text-qk-text-tertiary" role="status">{{ feedback }}</p>
  </div>
</template>
```

图片缩略图加载失败有独立的重新加载入口，不触发上传重试，也不修改宿主状态。上传失败不会自动清空附件，便于保留草稿和重试。

## 消息与输入区

[MessagePart 的 attachment 类型](/raw/components/chat-message-part.md#附件)直接使用同一个 `items` 合同；[Composer 附件组合](/raw/components/chat-composer.md#图片与文件上下文)展示本地选择与横向附件带。

FileUpload 管理原生 File 与选择校验，FileUploadList 用于其原生选择列表；Attachments 面向宿主提供的本地或远程资源投影。上传请求、鉴权、下载和 object URL 的创建/释放仍由宿主负责。公共组件不会替宿主释放 URL。

## API

### QuireAttachments

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| items | 是 | — | 按显示顺序提供的附件；每个 id 必须唯一。 |
| label | 否 | — | 集合辅助名称；省略时取语言包。 |
| layout | 否 | "list" | 展示密度，默认 list。 |
| preview | 否 | true | 是否允许图片预览，默认 true；业务 open 不受此项控制。 |

**items**

```ts
readonly QuireKitAttachmentItem[]
```

```ts
readonly ({
  /** 集合内唯一、跨更新稳定的身份；不使用索引或资源地址。 */
  id: string;
  /** 可读文件名。 */
  name: string;
  /** MIME 类型；图片类型可启用缩略图和预览。 */
  mediaType?: undefined | string;
  /** 字节数；省略或无效时不显示大小。 */
  size?: undefined | number;
  /** 可选来源元数据，不改变 URL 所有权。 */
  source?: undefined | "local" | "remote";
  /** 宿主持有的资源地址；公共 UI 不创建或释放它。 */
  url?: string | undefined;
  /** 独立缩略图地址；省略时图片使用 url。 */
  thumbnailUrl?: string | undefined;
  /** 图片说明，省略时使用 name。 */
  alt?: string | undefined;
  /** 展示状态，默认 ready。 */
  state?: undefined | "ready" | "uploading" | "failed" | "unavailable";
  /** 上传百分比；有限数钳制至 0–100，无效或省略时为未知进度。 */
  progress?: number | undefined;
  /** 宿主补充的状态说明，例如失败原因。 */
  statusText?: string | undefined;
  /** 明确允许的操作，默认不显示任何业务操作；UI 再按 state 过滤。 */
  actions?: undefined | readonly ("open" | "download" | "retry" | "remove")[];
})[]
```

**label**

```ts
string | undefined
```

```ts
undefined | string
```

**layout**

```ts
QuireKitAttachmentLayout | undefined
```

```ts
undefined | "list" | "inline" | "grid" | "strip"
```

**preview**

```ts
boolean | undefined
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| action | 否 | — |  |

**action**

```ts
[details: QuireKitAttachmentActionDetails]
```

#### Slots

无。

#### 公开实例成员

无。

#### 相关类型

```ts
type QuireKitAttachmentItem = {
  /** 集合内唯一、跨更新稳定的身份；不使用索引或资源地址。 */
  id: string;
  /** 可读文件名。 */
  name: string;
  /** MIME 类型；图片类型可启用缩略图和预览。 */
  mediaType?: undefined | string;
  /** 字节数；省略或无效时不显示大小。 */
  size?: undefined | number;
  /** 可选来源元数据，不改变 URL 所有权。 */
  source?: undefined | "local" | "remote";
  /** 宿主持有的资源地址；公共 UI 不创建或释放它。 */
  url?: string | undefined;
  /** 独立缩略图地址；省略时图片使用 url。 */
  thumbnailUrl?: string | undefined;
  /** 图片说明，省略时使用 name。 */
  alt?: string | undefined;
  /** 展示状态，默认 ready。 */
  state?: undefined | "ready" | "uploading" | "failed" | "unavailable";
  /** 上传百分比；有限数钳制至 0–100，无效或省略时为未知进度。 */
  progress?: number | undefined;
  /** 宿主补充的状态说明，例如失败原因。 */
  statusText?: string | undefined;
  /** 明确允许的操作，默认不显示任何业务操作；UI 再按 state 过滤。 */
  actions?: undefined | readonly ("open" | "download" | "retry" | "remove")[];
};
```

```ts
type QuireKitAttachmentAction = "open" | "download" | "retry" | "remove";
```

### QuireAttachment

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| item | 是 | — | 稳定的文件/图片展示数据。 |
| layout | 否 | "list" | 展示密度，默认 list。 |
| preview | 否 | true | 是否允许图片预览，默认 true；业务 open 不受此项控制。 |

**item**

```ts
QuireKitAttachmentItem
```

```ts
{
  /** 集合内唯一、跨更新稳定的身份；不使用索引或资源地址。 */
  id: string;
  /** 可读文件名。 */
  name: string;
  /** MIME 类型；图片类型可启用缩略图和预览。 */
  mediaType?: undefined | string;
  /** 字节数；省略或无效时不显示大小。 */
  size?: undefined | number;
  /** 可选来源元数据，不改变 URL 所有权。 */
  source?: undefined | "local" | "remote";
  /** 宿主持有的资源地址；公共 UI 不创建或释放它。 */
  url?: string | undefined;
  /** 独立缩略图地址；省略时图片使用 url。 */
  thumbnailUrl?: string | undefined;
  /** 图片说明，省略时使用 name。 */
  alt?: string | undefined;
  /** 展示状态，默认 ready。 */
  state?: undefined | "ready" | "uploading" | "failed" | "unavailable";
  /** 上传百分比；有限数钳制至 0–100，无效或省略时为未知进度。 */
  progress?: number | undefined;
  /** 宿主补充的状态说明，例如失败原因。 */
  statusText?: string | undefined;
  /** 明确允许的操作，默认不显示任何业务操作；UI 再按 state 过滤。 */
  actions?: undefined | readonly ("open" | "download" | "retry" | "remove")[];
}
```

**layout**

```ts
QuireKitAttachmentLayout | undefined
```

```ts
undefined | "list" | "inline" | "grid" | "strip"
```

**preview**

```ts
boolean | undefined
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| action | 否 | — |  |

**action**

```ts
[details: QuireKitAttachmentActionDetails]
```

#### Slots

无。

#### 公开实例成员

无。

#### 相关类型

```ts
type QuireKitAttachmentItem = {
  /** 集合内唯一、跨更新稳定的身份；不使用索引或资源地址。 */
  id: string;
  /** 可读文件名。 */
  name: string;
  /** MIME 类型；图片类型可启用缩略图和预览。 */
  mediaType?: undefined | string;
  /** 字节数；省略或无效时不显示大小。 */
  size?: undefined | number;
  /** 可选来源元数据，不改变 URL 所有权。 */
  source?: undefined | "local" | "remote";
  /** 宿主持有的资源地址；公共 UI 不创建或释放它。 */
  url?: string | undefined;
  /** 独立缩略图地址；省略时图片使用 url。 */
  thumbnailUrl?: string | undefined;
  /** 图片说明，省略时使用 name。 */
  alt?: string | undefined;
  /** 展示状态，默认 ready。 */
  state?: undefined | "ready" | "uploading" | "failed" | "unavailable";
  /** 上传百分比；有限数钳制至 0–100，无效或省略时为未知进度。 */
  progress?: number | undefined;
  /** 宿主补充的状态说明，例如失败原因。 */
  statusText?: string | undefined;
  /** 明确允许的操作，默认不显示任何业务操作；UI 再按 state 过滤。 */
  actions?: undefined | readonly ("open" | "download" | "retry" | "remove")[];
};
```

```ts
type QuireKitAttachmentAction = "open" | "download" | "retry" | "remove";
```
