feat: migrate ListBox with virtual collection and accessible parts

This commit is contained in:
maoyingjie
2026-10-23 15:02:30 +08:00
parent dea5ded716
commit 65db60a7ac
39 changed files with 2694 additions and 12 deletions
+1
View File
@@ -100,6 +100,7 @@ const groups = [
{ title: "Toolbar 工具栏", url: "/components/toolbar" },
{ title: "NumberField 数字输入框", url: "/components/number-field" },
{ title: "Slider 滑块", url: "/components/slider" },
{ title: "ListBox 列表框", url: "/components/list-box" },
{ title: "Tooltip 工具提示", url: "/components/tooltip" },
{
title: "ToggleButton 切换按钮",
+236
View File
@@ -0,0 +1,236 @@
---
title: ListBox 列表框
description: 列表框展示一组选项,并允许用户选择一个或多个。
component: list-box
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Label,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview name="list-box-default" />
## 组件结构
```vue
<script setup lang="ts">
import {
ListBox,
ListBoxItem,
ListBoxItemIndicator,
ListBoxSection,
Label,
Description,
Header,
} from "@heroui-vue/components";
</script>
<template>
<ListBox aria-label="选项">
<ListBoxItem id="first" text-value="第一项">
<div>
<Label>第一项</Label>
<Description>选项说明</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxSection>
<Header>分组</Header>
<ListBoxItem id="second" text-value="第二项">
<Label>第二项</Label>
</ListBoxItem>
</ListBoxSection>
</ListBox>
</template>
```
## 示例
### 含禁用项
<ComponentPreview name="list-box-with-disabled-items" />
### 分组选项
<ComponentPreview name="list-box-with-sections" />
### 多选
<ComponentPreview name="list-box-multi-select" />
### 受控组件
使用 `v-model` 控制 key 数组。单选也使用数组,空选择为 `[]`。数字 `1` 与字符串 `"1"` 是不同的 key。父组件未接受更新时,可见选中状态保持父值。
<ComponentPreview name="list-box-controlled" />
### 虚拟滚动
ListBox 通过 `virtualized` 仅渲染视口附近的行,高效展示大数据集。下面保留基线的 1000 条数据、50px 固定行高和 400px 视口。完整数据传给 `items`,首字母导航、全选和范围选择使用完整集合。
Vue 使用现有 VueUse 虚拟列表适配,不导出 React 的 `Virtualizer` / `ListLayout`。当前支持竖向固定行高列表;可变行高、虚拟分组标题和拖放布局尚未支持。
<ComponentPreview name="list-box-virtualization" />
### 自定义勾选图标
<ComponentPreview name="list-box-custom-check-icon" />
### 渲染函数
Vue 使用 `as` 修改原生标签,普通和作用域插槽组合内容;状态属性和事件保留在实际 role 节点上。
<ComponentPreview name="list-box-render-function" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="list-box-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-list-box {
@apply rounded-lg border border-border bg-surface p-2;
}
.hv-list-box-item {
@apply rounded px-2 py-1 cursor-pointer;
}
.hv-list-box-item--danger {
@apply text-danger;
}
.hv-list-box-item__indicator {
@apply text-accent;
}
}
```
## 样式参考
保留固定 HeroUI 基线的 BEM 类与视觉数值,使用 `hv-` 命名空间。
| CSS 类 | 描述 |
| ---------------------------------------------------------- | -------------- |
| `.hv-list-box` | ListBox 根容器 |
| `.hv-list-box--default` / `.hv-list-box--danger` | 根变体 |
| `.hv-list-box-item` | 单个列表项 |
| `.hv-list-box-item--default` / `.hv-list-box-item--danger` | 项变体 |
| `.hv-list-box-item__indicator` | 选中指示图标 |
| `.hv-list-box-section` | 分组容器 |
### 交互状态
| 状态 | 属性与样式 |
| ------ | ----------------------------------------------------------- |
| 悬停 | `:hover` / `data-hovered="true"` |
| 聚焦 | `data-focused="true"`;键盘焦点 `data-focus-visible="true"` |
| 按下 | `:active` / `data-pressed="true"`,缩放至 0.98 |
| 已选中 | `aria-selected="true"` / `data-selected="true"` |
| 禁用 | `aria-disabled="true"` / `data-disabled="true"` |
| 指示器 | `data-visible="true"`;默认勾选使用源 stroke 动画 |
## API 参考
### ListBox / ListBoxRoot
| 属性 | 类型 | 默认值 | 描述 |
| -------------------------------- | ------------------------------------------- | ------------------------ | ------------------------------------------------------------------------------------ |
| `modelValue` | `readonly ListBoxKey[]` | — | 受控选择,使用 `v-model`。 |
| `selectedKeys` | `readonly ListBoxKey[]` | — | 受控选择别名;显式 `modelValue` 优先,包括 `undefined`。 |
| `defaultValue` | `readonly ListBoxKey[]` | `[]` | 非受控初始值。 |
| `defaultSelectedKeys` | `readonly ListBoxKey[]` | — | 初始值别名;`defaultValue` 优先。 |
| `selectionMode` | `"none" \| "single" \| "multiple"` | `"single"` | 不选择、单选或多选;单选最多一个 key。 |
| `selectionBehavior` | `"toggle" \| "replace"` | `"toggle"` | toggle 逐项切换;replace 点击或普通方向导航替换选择,Ctrl/Command 方向键仅移动焦点。 |
| `disabledKeys` | `readonly ListBoxKey[]` | `[]` | 禁用项;导航、范围与全选跳过这些项。 |
| `disallowEmptySelection` | `boolean` | `false` | 阻止用户清空选择,不自动选择初始项。 |
| `isDisabled` | `boolean` | `false` | 禁止交互并移除 Tab 停靠点;合并 Fieldset 禁用作用域。 |
| `isReadOnly` | `boolean` | `false` | 保留焦点导航,阻止改选与 action。 |
| `orientation` | `"vertical" \| "horizontal"` | `"vertical"` | 方向键导航方向。 |
| `dir` | `"ltr" \| "rtl"` | ConfigProvider / `"ltr"` | 横向导航方向。 |
| `loop` | `boolean` | `false` | 方向键到边界后循环。 |
| `variant` | `"default" \| "danger"` | `"default"` | 视觉变体。 |
| `as` | `string` | `"div"` | 根原生标签。 |
| `items` | `readonly T[]`,`T extends ListBoxDataItem` | — | 数据集合,具备唯一的 `id`;作用域插槽接收 `item` 和 `index`。 |
| `virtualized` | `boolean` | `false` | 开启竖向固定行高虚拟列表,必须提供 `items`。 |
| `rowHeight` | `number` | `50` | 每行高度(px),必须为正数。 |
| `overscan` | `number` | `5` | 视口外额外渲染的行数,初始化时确定。 |
| `aria-label` / `aria-labelledby` | `string` | — | 根列表框的可访问名称。 |
| `class` / `style` / DOM 事件 | Vue 原生属性 | — | 透传到根 role=listbox 节点,类增量合并。 |
`ListBoxKey` 为 `string | number`,数字必须有限;空字符串是有效 key。Vue 数组替代 RAC 的 `Set` 和 `"all"` 哨兵;使用实例 `selectAll()` 选择当前可用全集,受控时仍由父级接受提案。成员移除不会擅自清理父模型。ListBox 本身不是表单字段,不生成有名输入;Select 的表单提交由其字段层处理。
| 事件 | 载荷 | 描述 |
| ------------------- | -------------- | ------------------------------------------------------------------ |
| `update:modelValue` | `ListBoxKey[]` | 用户提出新的选择;拒绝后可重复提出相同选择。 |
| `selectionChange` | `ListBoxKey[]` | 同一次选择提案,供非 v-model 使用。 |
| `action` | `ListBoxKey` | 可用项被点击或 Enter/Space 激活;与选择更新分开,none 模式也触发。 |
| 插槽 / 实例方法 | 参数 | 描述 |
| --------------- | ------------------------------------- | -------------------------------------------- |
| 默认插槽 | 数据模式 `{ item: T, index: number }` | 静态部件模式直接放置 Item / Section。 |
| `empty` | 无 | items 为空时的内容。 |
| `focus(key?)` | `ListBoxKey?` | 聚焦根或指定可用项;虚拟项先滚动到可见范围。 |
| `selectAll()` | 无 | 多选模式下提出全选,跳过禁用项。 |
| `element` | `HTMLElement` | 根元素引用。 |
### ListBoxItem
| 属性 | 类型 | 默认值 | 描述 |
| ----------------------------------- | ----------------------- | ------------------- | ----------------------------------------------------------------------- |
| `id` | `ListBoxKey` | 必填 | 集合身份,不直接作为 DOM id;不同列表可复用相同 key。 |
| `textValue` | `string` | Label 文本 / 项文本 | 首字母导航文本。数据模式也应提供 items 的 textValue,支持未挂载项搜索。 |
| `isDisabled` | `boolean` | `false` | 项禁用,与根/disabledKeys 合并。 |
| `variant` | `"default" \| "danger"` | `"default"` | 视觉变体。 |
| `as` | `string` | `"div"` | 原生标签,对应 React render 的元素替换用途。 |
| `class` / `style` / ARIA / DOM 事件 | Vue 原生属性 | — | 透传实际 role=option 节点,调用方事件先执行,取消后不改选。 |
`@select` 接收可取消的 `CustomEvent<{ key: ListBoxKey; originalEvent: MouseEvent | KeyboardEvent }>`;`preventDefault()` 阻止选择和 action。Root 的 DOM `@keydown.prevent` 同样在内部导航前生效。
### ListBoxItemIndicator
| 属性 / 插槽 | 类型 | 默认值 | 描述 |
| ----------------- | ------------------ | ------------ | ----------------------------------- |
| `as` | `string` | `"span"` | 装饰性容器,aria-hidden。 |
| `class` / `style` | Vue 原生属性 | — | 增量合并。 |
| 默认插槽 | `ListBoxItemState` | 基线勾选图标 | 自定义选中图标,可使用 Tabler Vue。 |
### ListBoxSection
| 属性 | 类型 | 默认值 | 描述 |
| ------------------------ | ------------ | ------- | ------------------------------------------- |
| `as` | `string` | `"div"` | role=group 的原生容器。 |
| `class` / `style` / ARIA | Vue 原生属性 | — | 增量合并;显式 aria-label/labelledby 优先。 |
默认插槽放置 Header 与 Item。直接、Fragment 或原生容器内的 Header 自动关联;任意用户组件内的 Header 请提供显式 id 与 Section 的 aria-labelledby。Item 内 Label/Description 支持同样的静态规划边界,自定义包装请显式关联。显式 aria-label 不被自动标签覆盖。
### 作用域插槽状态
Item 和 ItemIndicator 的默认插槽共享以下状态:
| 状态 | 类型 | 描述 |
| ---------------- | --------- | ------------------ |
| `isSelected` | `boolean` | 项是否选中。 |
| `isFocused` | `boolean` | 项是否实际聚焦。 |
| `isFocusVisible` | `boolean` | 是否显示键盘焦点。 |
| `isDisabled` | `boolean` | 项是否禁用。 |
| `isPressed` | `boolean` | 是否按下。 |
| `isHovered` | `boolean` | 是否悬停。 |
## 无障碍
列表使用 role=listbox / option / group,支持单一 Tab 入口、方向键、Home/End、按视口移动的 PageUp/PageDown、Enter/Space、首字母搜索与 IME 避让。多选支持 Ctrl/Command+A 和 Shift 范围选择。横向导航遵循 dir,禁用项不可激活;只读项仍可聚焦。
node SSR 输出选中状态、名称和描述;虚拟列表首屏为滚动容器,挂载后按测量显示可见行。读屏、触屏和完整视觉比较的已验证范围见仓库验证报告,不声明已经完整兼容 React Aria。
+67
View File
@@ -0,0 +1,67 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Description,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Surface,
} from "@heroui-vue/components";
import { IconCheck } from "@tabler/icons-vue";
import { ref } from "vue";
import type { ListBoxKey } from "@heroui-vue/components";
const selected = ref<ListBoxKey[]>(["1"]);
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
];
</script>
<template>
<div class="space-y-4">
<Surface class="w-[256px] rounded-3xl shadow-surface">
<ListBox
:items="users"
v-slot="{ item: user }"
aria-label="用户"
selection-mode="multiple"
v-model="selected"
>
<ListBoxItem :id="user.id" :text-value="user.name">
<Avatar size="sm">
<AvatarImage :alt="user.name" :src="user.src" />
<AvatarFallback>{{ user.name[0] }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator v-slot="{ isSelected }">
<IconCheck v-if="isSelected" class="size-4 text-accent" />
</ListBoxItemIndicator>
</ListBoxItem>
</ListBox>
</Surface>
<p class="text-sm text-muted">
已选:{{ selected.length ? selected.join("、") : "无" }}
</p>
</div>
</template>
@@ -0,0 +1,58 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Description,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Surface,
} from "@heroui-vue/components";
import { IconCheck } from "@tabler/icons-vue";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
];
</script>
<template>
<Surface class="w-[256px] rounded-3xl shadow-surface">
<ListBox
:items="users"
v-slot="{ item: user }"
aria-label="用户"
selection-mode="multiple"
>
<ListBoxItem :id="user.id" :text-value="user.name">
<Avatar size="sm">
<AvatarImage :alt="user.name" :src="user.src" />
<AvatarFallback>{{ user.name[0] }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator v-slot="{ isSelected }">
<IconCheck v-if="isSelected" class="size-4 text-accent" />
</ListBoxItemIndicator>
</ListBoxItem>
</ListBox>
</Surface>
</template>
@@ -0,0 +1,58 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Description,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Surface,
} from "@heroui-vue/components";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
];
</script>
<template>
<ListBox
:items="users.slice(0, 2)"
v-slot="{ item: user }"
aria-label="负责人"
selection-mode="single"
class="w-56 rounded-xl border border-border/80 bg-surface p-1 shadow-sm"
>
<ListBoxItem
:id="user.id"
:text-value="user.name"
class="rounded-lg data-[focused=true]:bg-accent/10 data-[selected=true]:bg-accent/5"
>
<Avatar size="sm">
<AvatarImage :alt="user.name" :src="user.src" />
<AvatarFallback>{{ user.name[0] }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</template>
+54
View File
@@ -0,0 +1,54 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Description,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Surface,
} from "@heroui-vue/components";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
];
</script>
<template>
<ListBox
:items="users"
v-slot="{ item: user }"
aria-label="用户"
selection-mode="single"
class="w-[220px]"
>
<ListBoxItem :id="user.id" :text-value="user.name">
<Avatar size="sm">
<AvatarImage :alt="user.name" :src="user.src" />
<AvatarFallback>{{ user.name[0] }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</template>
+55
View File
@@ -0,0 +1,55 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Description,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Surface,
} from "@heroui-vue/components";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
];
</script>
<template>
<Surface class="w-[256px] rounded-3xl shadow-surface">
<ListBox
:items="users"
v-slot="{ item: user }"
aria-label="用户"
selection-mode="multiple"
>
<ListBoxItem :id="user.id" :text-value="user.name">
<Avatar size="sm">
<AvatarImage :alt="user.name" :src="user.src" />
<AvatarFallback>{{ user.name[0] }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</Surface>
</template>
@@ -0,0 +1,61 @@
<script setup lang="ts">
import {
Avatar,
AvatarImage,
AvatarFallback,
Description,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Surface,
} from "@heroui-vue/components";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
src: "https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
];
</script>
<template>
<ListBox
:items="users"
v-slot="{ item: user }"
aria-label="用户"
selection-mode="single"
class="w-[220px]"
as="div"
data-custom="true"
>
<ListBoxItem
:id="user.id"
:text-value="user.name"
as="span"
:data-custom="{ '1': 'foo', '2': 'bar', '3': 'baz' }[user.id]"
>
<Avatar size="sm">
<AvatarImage :alt="user.name" :src="user.src" />
<AvatarFallback>{{ user.name[0] }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</template>
@@ -0,0 +1,82 @@
<script setup lang="ts">
import {
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Label,
Description,
} from "@heroui-vue/components";
const firstNames = [
"Emma",
"Liam",
"Olivia",
"Noah",
"Ava",
"James",
"Sophia",
"Oliver",
"Isabella",
"Lucas",
"Mia",
"Ethan",
"Charlotte",
"Mason",
"Amelia",
"Logan",
"Harper",
"Alexander",
"Ella",
"Benjamin",
];
const lastNames = [
"Smith",
"Johnson",
"Williams",
"Brown",
"Jones",
"Garcia",
"Miller",
"Davis",
"Rodriguez",
"Martinez",
"Anderson",
"Taylor",
"Thomas",
"Jackson",
"White",
"Harris",
"Clark",
"Lewis",
"Robinson",
"Walker",
];
const users = Array.from({ length: 1000 }, (_, index) => {
const first = firstNames[index % firstNames.length];
const last =
lastNames[Math.floor(index / firstNames.length) % lastNames.length];
return {
id: index + 1,
name: `${first} ${last}`,
textValue: `${first} ${last}`,
email: `${first.toLowerCase()}.${last.toLowerCase()}@acme.com`,
};
});
</script>
<template>
<ListBox
:items="users"
virtualized
:row-height="50"
aria-label="包含 1000 项的虚拟化列表"
class="h-[400px] w-[300px] overflow-y-auto"
v-slot="{ item }"
>
<ListBoxItem :id="item.id" :text-value="item.name" class="h-full">
<div class="flex flex-col">
<Label>{{ item.name }}</Label>
<Description>{{ item.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</ListBox>
</template>
@@ -0,0 +1,88 @@
<script setup lang="ts">
import { ref } from "vue";
import { IconPencil, IconSquarePlus, IconTrash } from "@tabler/icons-vue";
import {
Description,
Header,
Kbd,
KbdAbbr,
KbdContent,
Label,
ListBox,
ListBoxItem,
ListBoxSection,
Separator,
Surface,
} from "@heroui-vue/components";
const message = ref("");
const actions = [
{
id: "new-file",
title: "新建文件",
description: "创建新文件",
shortcut: "N",
icon: IconSquarePlus,
},
{
id: "edit-file",
title: "编辑文件",
description: "进行修改",
shortcut: "E",
icon: IconPencil,
},
];
</script>
<template>
<div class="space-y-3">
<Surface class="w-[256px] rounded-3xl shadow-surface">
<ListBox
aria-label="文件操作"
class="w-full p-2"
selection-mode="none"
@action="message = `已选项目:${$event}`"
:disabled-keys="['delete-file']"
>
<ListBoxSection>
<Header>操作</Header>
<ListBoxItem
v-for="action in actions"
:key="action.id"
:id="action.id"
:text-value="action.title"
>
<div class="flex h-8 items-start justify-center pt-px">
<component :is="action.icon" class="size-4 shrink-0 text-muted" />
</div>
<div class="flex flex-col">
<Label>{{ action.title }}</Label>
<Description>{{ action.description }}</Description>
</div>
<Kbd class="ms-auto" variant="light">
<KbdAbbr key-value="command" />
<KbdContent>{{ action.shortcut }}</KbdContent>
</Kbd>
</ListBoxItem>
</ListBoxSection>
<Separator />
<ListBoxSection>
<Header>危险区域</Header>
<ListBoxItem id="delete-file" text-value="删除文件" variant="danger">
<div class="flex h-8 items-start justify-center pt-px">
<IconTrash class="size-4 shrink-0 text-danger" />
</div>
<div class="flex flex-col">
<Label>删除文件</Label>
<Description>移至废纸篓</Description>
</div>
<Kbd class="ms-auto" variant="light">
<KbdAbbr key-value="command" />
<KbdAbbr key-value="shift" />
<KbdContent>D</KbdContent>
</Kbd>
</ListBoxItem>
</ListBoxSection>
</ListBox>
</Surface>
<p v-if="message" role="status" class="text-sm text-muted">{{ message }}</p>
</div>
</template>
@@ -0,0 +1,87 @@
<script setup lang="ts">
import { ref } from "vue";
import { IconPencil, IconSquarePlus, IconTrash } from "@tabler/icons-vue";
import {
Description,
Header,
Kbd,
KbdAbbr,
KbdContent,
Label,
ListBox,
ListBoxItem,
ListBoxSection,
Separator,
Surface,
} from "@heroui-vue/components";
const message = ref("");
const actions = [
{
id: "new-file",
title: "新建文件",
description: "创建新文件",
shortcut: "N",
icon: IconSquarePlus,
},
{
id: "edit-file",
title: "编辑文件",
description: "进行修改",
shortcut: "E",
icon: IconPencil,
},
];
</script>
<template>
<div class="space-y-3">
<Surface class="w-[256px] rounded-3xl shadow-surface">
<ListBox
aria-label="文件操作"
class="w-full p-2"
selection-mode="none"
@action="message = `已选项目:${$event}`"
>
<ListBoxSection>
<Header>操作</Header>
<ListBoxItem
v-for="action in actions"
:key="action.id"
:id="action.id"
:text-value="action.title"
>
<div class="flex h-8 items-start justify-center pt-px">
<component :is="action.icon" class="size-4 shrink-0 text-muted" />
</div>
<div class="flex flex-col">
<Label>{{ action.title }}</Label>
<Description>{{ action.description }}</Description>
</div>
<Kbd class="ms-auto" variant="light">
<KbdAbbr key-value="command" />
<KbdContent>{{ action.shortcut }}</KbdContent>
</Kbd>
</ListBoxItem>
</ListBoxSection>
<Separator />
<ListBoxSection>
<Header>危险区域</Header>
<ListBoxItem id="delete-file" text-value="删除文件" variant="danger">
<div class="flex h-8 items-start justify-center pt-px">
<IconTrash class="size-4 shrink-0 text-danger" />
</div>
<div class="flex flex-col">
<Label>删除文件</Label>
<Description>移至废纸篓</Description>
</div>
<Kbd class="ms-auto" variant="light">
<KbdAbbr key-value="command" />
<KbdAbbr key-value="shift" />
<KbdContent>D</KbdContent>
</Kbd>
</ListBoxItem>
</ListBoxSection>
</ListBox>
</Surface>
<p v-if="message" role="status" class="text-sm text-muted">{{ message }}</p>
</div>
</template>