# EmptyState

> 可复用的占位插图与自由文案，按需搭配说明和操作。

网页来源：/components/empty-state

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

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

EmptyState 用于展示空列表、空表格或没有匹配项的搜索结果。插图、标题、说明和操作可以独立组合，
所有文案由调用方传入。

## 基础用法

`title` 和 `description` 都可省略。简短空态只传 `description` 即可，不必再放一个重复标题。默认只显示文字，
可直接放入内容区、列表或面板。

### 示例：empty-state/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { QuireEmptyState } from "@quirekit/ui/vue";
</script>

<template>
  <div class="mx-auto grid w-full max-w-xl content-center p-qk-surface">
    <QuireEmptyState title="暂无数据" description="当前时间范围内没有可展示的数据。" />
    <QuireEmptyState description="还没有添加内容" />
  </div>
</template>
```

## 图示与文案

设置 `illustrated` 显示插图，通过 `scene` 选择图案。`title` 设置标题，`description` 设置可选说明。
下面的两个输入框可以同时替换六种图案下的文字。

### 示例：empty-state/illustrations

文件：`illustrations.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import type { QuireKitEmptyStateScene } from "@quirekit/ui";
import { QuireEmptyState, QuireField, QuireInput } from "@quirekit/ui/vue";

const title = ref("还没有可展示的内容");
const description = ref("这段说明可以替换成适合当前页面的任意文案。");
const scenes: readonly QuireKitEmptyStateScene[] = ["data", "search", "create", "documents", "chart", "table"];
</script>

<template>
  <section aria-label="占位插图目录" class="mx-auto grid w-full max-w-3xl gap-5 p-qk-surface">
    <div class="grid gap-3">
      <QuireField label="占位标题" width="100%">
        <template #default="{ controlProps }"><QuireInput v-bind="controlProps" v-model="title" width="100%" /></template>
      </QuireField>
      <QuireField label="占位说明" width="100%">
        <template #default="{ controlProps }"><QuireInput v-bind="controlProps" v-model="description" width="100%" /></template>
      </QuireField>
    </div>
    <div class="grid min-w-0 gap-4" style="grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr))">
      <div v-for="scene in scenes" :key="scene" class="grid min-w-0 content-start rounded-qk-control border border-qk-border-subtle">
        <p class="m-0 px-4 pt-3 text-qk-helper text-qk-text-secondary">{{ scene }}</p>
        <QuireEmptyState :scene="scene" :title="title" :description="description" illustrated />
      </div>
    </div>
  </section>
</template>
```

| `scene` | 图案 |
| --- | --- |
| `data` | 数据条目 |
| `search` | 搜索 |
| `create` | 添加内容 |
| `documents` | 文档叠页 |
| `chart` | 图表 |
| `table` | 表格 |

默认图案为 `data`。插图采用内置 SVG，支持明暗主题，也可以通过 `illustration` 插槽替换成自定义图片。

## 纯数据展示

记录区、统计区与表格可以只展示插图和说明。表格中的空态放在跨列单元格内，保留列标题。
下面的开关分别切换示例数据和插图显示。

### 示例：empty-state/data

文件：`data.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { QuireEmptyState, QuireSwitch, QuireTable } from "@quirekit/ui/vue";

const populated = ref(false);
const illustrated = ref(true);
const rows = computed(() => populated.value ? [
  { name: "季度产品计划", owner: "产品组", activity: "已更新" },
  { name: "项目复盘记录", owner: "设计组", activity: "已审阅" }
] : []);
const columns = [
  { id: 'name', field: 'name' as const, title: '文档名称', width: '50%' },
  { id: 'owner', field: 'owner' as const, title: '所属团队' },
  { id: 'activity', field: 'activity' as const, title: '最近活动' },
];
</script>

<template>
  <section aria-label="纯数据空态示例" class="mx-auto grid w-full max-w-2xl gap-4 p-qk-surface">
    <div class="flex flex-wrap gap-4">
      <QuireSwitch v-model:checked="populated" label="显示本地示例数据" />
      <QuireSwitch v-model:checked="illustrated" label="显示场景图示" />
    </div>
    <div class="grid min-w-0 gap-4" style="grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr))">
      <section aria-label="活动记录" class="min-w-0 rounded-qk-control border border-qk-border-subtle">
        <h3 class="m-0 border-b border-qk-border-subtle px-4 py-3 text-qk-compact font-medium text-qk-text-primary">活动记录</h3>
        <div class="grid min-h-40 content-center">
          <QuireEmptyState v-if="!rows.length" title="暂无活动记录" description="本周还没有文档活动。" scene="data" size="sm" :heading-level="4" :illustrated="illustrated" />
          <ul v-else aria-label="活动记录列表" class="m-0 grid list-none gap-3 p-4">
            <li v-for="row in rows" :key="row.name" class="grid min-w-0 gap-1">
              <span class="break-words text-qk-compact text-qk-text-primary">{{ row.name }}</span>
              <span class="text-qk-helper text-qk-text-secondary">{{ row.activity }}</span>
            </li>
          </ul>
        </div>
      </section>
      <section aria-label="使用统计" class="min-w-0 rounded-qk-control border border-qk-border-subtle">
        <h3 class="m-0 border-b border-qk-border-subtle px-4 py-3 text-qk-compact font-medium text-qk-text-primary">使用统计</h3>
        <div class="grid min-h-40 content-center">
          <QuireEmptyState v-if="!rows.length" title="暂无统计数据" description="当前周期内没有可汇总的活动。" scene="chart" size="sm" :heading-level="4" :illustrated="illustrated" />
          <dl v-else class="m-0 grid gap-2 p-4 text-center">
            <dt class="text-qk-helper text-qk-text-secondary">参与文档</dt>
            <dd class="m-0 text-3xl font-medium tabular-nums text-qk-text-primary">{{ rows.length }} <span class="text-qk-helper font-normal">份</span></dd>
          </dl>
        </div>
      </section>
    </div>
    <QuireTable :rows="rows" :columns="columns" row-key="name" caption="文档明细">
      <template #empty><QuireEmptyState title="暂无文档明细" description="当前范围内没有可展示的记录。" scene="table" size="sm" :heading-level="4" :illustrated="illustrated" /></template>
    </QuireTable>
    <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">当前展示 {{ rows.length }} 条本地记录</p>
  </section>
</template>
```

## 下一步动作

需要提供创建或恢复入口时，在 `actions` 插槽中放入按钮或链接。操作完成后，由页面更新数据并展示结果。

### 示例：empty-state/actions

文件：`actions.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { QuireButton, QuireEmptyState } from "@quirekit/ui/vue";

const documents = ref<string[]>([]);
const heading = ref<HTMLHeadingElement>();

/** 创建第一份本地文档前，将焦点交给持续存在的列表标题。 */
function createDocument(): void {
  heading.value?.focus();
  documents.value = ["未命名文档"];
}

/** 清空本地列表，回到可以再次创建的空态。 */
function resetDocuments(): void {
  heading.value?.focus();
  documents.value = [];
}
</script>

<template>
  <section aria-label="文档创建示例" class="mx-auto grid w-full max-w-xl gap-4 p-qk-surface">
    <h3 ref="heading" tabindex="-1" class="m-0 text-qk-compact font-semibold text-qk-text-primary">我的文档</h3>
    <div class="grid min-h-[200px] content-center">
      <QuireEmptyState v-if="documents.length === 0" scene="create" illustrated title="开始记录新想法" description="创建一份本地示例文档，看看列表如何从空态变为内容。" :heading-level="4">
        <template #actions>
          <QuireButton color="primary" variant="solid" @click="createDocument">创建第一份文档</QuireButton>
        </template>
      </QuireEmptyState>
      <ul v-else aria-label="已创建文档" class="m-0 grid list-none gap-2 p-0">
        <li v-for="document in documents" :key="document" class="break-words text-qk-compact text-qk-text-primary">{{ document }}</li>
      </ul>
    </div>
    <div class="flex flex-wrap items-center justify-between gap-3">
      <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">已创建 {{ documents.length }} 份文档</p>
      <QuireButton variant="outline" :disabled="documents.length === 0" @click="resetDocuments">清空文档列表</QuireButton>
    </div>
  </section>
</template>
```

## 没有搜索结果

结合搜索输入使用时，页面负责查询和筛选，空态展示结果说明。清除搜索条件后，
恢复结果列表并将焦点返回搜索输入框，方便继续操作。

### 示例：empty-state/no-results

文件：`no-results.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { QuireButton, QuireEmptyState, QuireField, QuireInput } from "@quirekit/ui/vue";

const documents = ["季度产品计划", "团队设计规范", "项目复盘记录"];
const query = ref("未匹配的关键词");
const searchRegion = ref<HTMLElement>();
const results = computed(() => documents.filter((document) => document.includes(query.value.trim())));

/** 清除条件前把焦点交还持续存在的输入框，并恢复全部本地结果。 */
function clearSearch(): void {
  searchRegion.value?.querySelector<HTMLInputElement>("input")?.focus();
  query.value = "";
}

/** 恢复初始搜索条件，让无结果流程可以重复尝试。 */
function restoreSearch(): void {
  searchRegion.value?.querySelector<HTMLInputElement>("input")?.focus();
  query.value = "未匹配的关键词";
}
</script>

<template>
  <section ref="searchRegion" aria-label="文档搜索示例" class="mx-auto grid w-full max-w-xl gap-4 p-qk-surface">
    <QuireField label="搜索文档" description="按名称筛选已有的三份文档。" width="100%">
      <template #default="{ controlProps }">
        <QuireInput v-bind="controlProps" v-model="query" width="100%" type="search" />
      </template>
    </QuireField>
    <div class="grid min-h-[200px] content-center">
      <QuireEmptyState v-if="results.length === 0" scene="search" illustrated title="没有匹配的文档" :description="`没有找到包含“${query}”的文档。可以换一个关键词，或清除搜索条件。`">
        <template #actions>
          <QuireButton variant="outline" @click="clearSearch">清除搜索条件</QuireButton>
        </template>
      </QuireEmptyState>
      <ul v-else aria-label="搜索结果" class="m-0 grid list-none gap-3 p-0">
        <li v-for="document in results" :key="document" class="text-qk-compact text-qk-text-primary">{{ document }}</li>
      </ul>
    </div>
    <div class="flex flex-wrap items-center justify-between gap-3">
      <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">找到 {{ results.length }} 份文档</p>
      <QuireButton variant="ghost" @click="restoreSearch">恢复无结果查询</QuireButton>
    </div>
  </section>
</template>
```

## 紧凑空态

`size="sm"` 适用于窄侧栏和短列表，默认的 `md` 适用于内容区。长标题和说明会自动换行。

### 示例：empty-state/compact

文件：`compact.vue`

```vue
<script setup lang="ts">
import { QuireEmptyState } from "@quirekit/ui/vue";
</script>

<template>
  <aside aria-label="最近记录面板" class="mx-auto grid w-full max-w-xs content-center p-4">
    <QuireEmptyState title="暂无最近记录" size="sm" />
  </aside>
</template>
```

## 自定义内容

通过 `illustration`、`title`、`description` 和 `actions` 插槽替换相应内容，
每个插槽都提供当前 `size` 与 `scene`。

自定义插图作为装饰内容展示；说明文字放在标题或说明中，交互控件放在 `actions` 中。

### 示例：empty-state/custom

文件：`custom.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { quireKitEditorIconData } from "@quirekit/ui";
import { QuireButton, QuireEmptyState, QuireIcon } from "@quirekit/ui/vue";

const template = ref<string>();
const heading = ref<HTMLHeadingElement>();

/** 添加一份可见的本地模板，并把消失的动作焦点交回宿主标题。 */
function addTemplate(): void {
  heading.value?.focus();
  template.value = "团队会议纪要";
}

/** 移除示例模板，允许再次体验完整空态动作。 */
function resetTemplate(): void {
  heading.value?.focus();
  template.value = undefined;
}
</script>

<template>
  <section aria-label="自定义模板空态示例" class="mx-auto grid w-full max-w-xl gap-4 p-qk-surface">
    <h3 ref="heading" tabindex="-1" class="m-0 text-qk-compact font-semibold text-qk-text-primary">团队模板</h3>
    <div class="grid content-center">
      <QuireEmptyState v-if="!template" scene="create" illustrated title="还没有团队模板" label="团队模板空状态" :heading-level="4">
        <template #illustration="{ size, scene }">
          <QuireIcon :data-scene="scene" :data="quireKitEditorIconData.fileText" :size="size === 'sm' ? 20 : 24" />
        </template>
        <template #title>还没有<span class="font-semibold">团队模板</span></template>
        <template #description>把重复使用的内容整理为模板，让团队从同一份约定开始。可以先添加一份<strong class="font-medium">会议纪要</strong>。</template>
        <template #actions="{ size }">
          <QuireButton :size="size" color="primary" variant="solid" @click="addTemplate">添加会议纪要模板</QuireButton>
        </template>
      </QuireEmptyState>
      <p v-else class="m-0 break-words text-qk-compact text-qk-text-primary">可用模板：{{ template }}</p>
    </div>
    <div class="flex flex-wrap items-center justify-between gap-3">
      <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">可用模板 {{ template ? 1 : 0 }} 份</p>
      <QuireButton variant="outline" :disabled="!template" @click="resetTemplate">移除示例模板</QuireButton>
    </div>
  </section>
</template>
```

## 尺寸与可访问性

- 内置插图在 `md` 下宽 144 px，在 `sm` 下宽 96 px；自定义插槽的默认容器为 48 px 和 32 px。
- 标题默认是普通段落，使用紧凑、次要文字样式；只有需要文档标题结构时才设置 `headingLevel`。
- 整组通过可见标题命名，没有标题时使用说明，也可以通过 `label` 提供更简短的辅助名称。
- 空态按普通内容读取；需要主动播报结果变化时，可由页面单独提供结果数量提示。

## API

### QuireEmptyState

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| title | 否 | — | 可选的空结果标题；简短空态可只传 description。 |
| description | 否 | — | 解释空结果原因或下一步的可选说明。 |
| id | 否 | — | 可选的稳定 group id。 |
| label | 否 | — | 整组的简短辅助名称；省略时由可见标题命名。 |
| headingLevel | 否 | — | 显式标题语义；省略时使用普通段落。 |
| size | 否 | "md" | 内容区或紧凑面板的展示尺寸。 |
| scene | 否 | "data" | 选择内置占位插图的图案。 |
| illustrated | 否 | false | 显示内置插图；提供 illustration 插槽时优先显示插槽内容。 |

**title**

```ts
string | undefined
```

```ts
undefined | string
```

**description**

```ts
string | undefined
```

```ts
undefined | string
```

**id**

```ts
string | undefined
```

```ts
undefined | string
```

**label**

```ts
string | undefined
```

```ts
undefined | string
```

**headingLevel**

```ts
QuireKitEmptyStateHeadingLevel | undefined
```

```ts
undefined | 2 | 3 | 4 | 5 | 6
```

**size**

```ts
QuireKitEmptyStateSize | undefined
```

```ts
undefined | "sm" | "md"
```

**scene**

```ts
QuireKitEmptyStateScene | undefined
```

```ts
undefined | "search" | "data" | "create" | "documents" | "chart" | "table"
```

**illustrated**

```ts
boolean | undefined
```

#### Events

无。

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| illustration | 否 | — | 自定义装饰插图；说明和操作分别使用对应插槽。 |
| title | 否 | — | 替换可见标题，保留辅助关联与显式选择的标题语义。 |
| description | 否 | — | 替换空结果说明，保留外层 description 关联。 |
| actions | 否 | — | 自定义下一步操作的按钮或链接。 |

**illustration**

```ts
QuireKitEmptyStateSlotProps
```

```ts
{
  /** 当前内容密度，供自定义插图或动作选择相应尺寸。 */
  size: "sm" | "md";
  /** 当前选择的插图图案。 */
  scene: "search" | "data" | "create" | "documents" | "chart" | "table";
}
```

**title**

```ts
QuireKitEmptyStateSlotProps
```

```ts
{
  /** 当前内容密度，供自定义插图或动作选择相应尺寸。 */
  size: "sm" | "md";
  /** 当前选择的插图图案。 */
  scene: "search" | "data" | "create" | "documents" | "chart" | "table";
}
```

**description**

```ts
QuireKitEmptyStateSlotProps
```

```ts
{
  /** 当前内容密度，供自定义插图或动作选择相应尺寸。 */
  size: "sm" | "md";
  /** 当前选择的插图图案。 */
  scene: "search" | "data" | "create" | "documents" | "chart" | "table";
}
```

**actions**

```ts
QuireKitEmptyStateSlotProps
```

```ts
{
  /** 当前内容密度，供自定义插图或动作选择相应尺寸。 */
  size: "sm" | "md";
  /** 当前选择的插图图案。 */
  scene: "search" | "data" | "create" | "documents" | "chart" | "table";
}
```

#### 公开实例成员

无。

#### 相关类型

```ts
type QuireKitEmptyStateSlotProps = {
  /** 当前内容密度，供自定义插图或动作选择相应尺寸。 */
  size: "sm" | "md";
  /** 当前选择的插图图案。 */
  scene: "search" | "data" | "create" | "documents" | "chart" | "table";
};
```

```ts
type QuireKitEmptyStateSize = "sm" | "md";
```

```ts
type QuireKitEmptyStateScene = "search" | "data" | "create" | "documents" | "chart" | "table";
```
