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
+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>