fix: retain pending Toast and Table fidelity corrections

This commit is contained in:
maoyingjie
2026-10-25 21:24:09 +08:00
parent 2ce80b17da
commit a2ca6ed0b8
42 changed files with 2153 additions and 260 deletions
+42
View File
@@ -114,6 +114,48 @@ const allUsers: User[] = [
role: "前端工程师",
status: "在职",
},
{
email: "noah@acme.com",
id: 13,
name: "Noah Davis",
role: "后端工程师",
status: "在职",
},
{
email: "ava@acme.com",
id: 14,
name: "Ava Wilson",
role: "首席设计师",
status: "在职",
},
{
email: "oliver@acme.com",
id: 15,
name: "Oliver Martinez",
role: "前端工程师",
status: "在职",
},
{
email: "isabella@acme.com",
id: 16,
name: "Isabella Johnson",
role: "后端工程师",
status: "在职",
},
{
email: "mia@acme.com",
id: 17,
name: "Mia Davis",
role: "首席设计师",
status: "在职",
},
{
email: "william@acme.com",
id: 18,
name: "William Wilson",
role: "前端工程师",
status: "在职",
},
];
const ITEMS_PER_PAGE = 6;
+11 -4
View File
@@ -20,19 +20,26 @@ import {
<TableScrollContainer>
<TableContent aria-label="可调整列宽的表格" class="min-w-[700px]">
<TableHeader>
<TableColumn id="name" is-row-header :min-width="160">
<TableColumn
id="name"
is-row-header
default-width="1fr"
:min-width="160"
>
姓名
<TableColumnResizer />
</TableColumn>
<TableColumn id="role" :min-width="220">
<TableColumn id="role" default-width="1fr" :min-width="220">
角色
<TableColumnResizer />
</TableColumn>
<TableColumn id="status" :min-width="100">
<TableColumn id="status" default-width="1fr" :min-width="100">
状态
<TableColumnResizer />
</TableColumn>
<TableColumn id="email" :min-width="200">邮箱</TableColumn>
<TableColumn id="email" default-width="1fr" :min-width="200">
邮箱
</TableColumn>
</TableHeader>
<TableBody>
<TableRow row-key="1">
+23 -1
View File
@@ -122,6 +122,23 @@ function initials(name: string) {
.map((n) => n[0])
.join("");
}
const selectableIds = computed(() =>
sortedUsers.value.map((u) => String(u.id)),
);
const allChecked = computed(
() =>
selectableIds.value.length > 0 &&
selectableIds.value.every((id) => selectedKeys.value.includes(id)),
);
const someChecked = computed(
() =>
!allChecked.value &&
selectableIds.value.some((id) => selectedKeys.value.includes(id)),
);
function toggleAll() {
selectedKeys.value = allChecked.value ? [] : [...selectableIds.value];
}
</script>
<template>
@@ -138,7 +155,12 @@ function initials(name: string) {
>
<TableHeader>
<TableColumn class="pe-0">
<Checkbox aria-label="全选">
<Checkbox
aria-label="全选"
:is-selected="allChecked"
:is-indeterminate="someChecked"
@update:checked="toggleAll"
>
<CheckboxContent>
<CheckboxControl>
<CheckboxIndicator />
+10 -13
View File
@@ -1,13 +1,12 @@
<script setup lang="ts">
import { IconInbox } from "@tabler/icons-vue";
import {
EmptyState,
Table,
TableBody,
TableCell,
TableColumn,
TableContent,
TableHeader,
TableRow,
TableScrollContainer,
} from "@heroui-vue/components";
</script>
@@ -22,17 +21,15 @@ import {
<TableColumn>状态</TableColumn>
<TableColumn>邮箱</TableColumn>
</TableHeader>
<TableBody>
<TableRow row-key="empty" :is-disabled="true">
<TableCell :colspan="4">
<div
class="flex h-full w-full flex-col items-center justify-center gap-4 py-8 text-center"
>
<IconInbox class="size-6 text-muted" aria-hidden="true" />
<span class="text-sm text-muted">未找到结果</span>
</div>
</TableCell>
</TableRow>
<TableBody :colspan="4">
<template #empty>
<EmptyState
class="flex h-full w-full flex-col items-center justify-center gap-4 text-center"
>
<IconInbox class="size-6 text-muted" aria-hidden="true" />
<span class="text-sm text-muted">未找到结果</span>
</EmptyState>
</template>
</TableBody>
</TableContent>
</TableScrollContainer>
+8 -8
View File
@@ -117,28 +117,28 @@ const flatRows = () => renderRows(data, 0, []);
<TableBody>
<template v-for="row in flatRows()" :key="row.item.id">
<TableRow
v-slot="{ isExpanded, toggleExpanded }"
v-slot="{ isExpanded, toggleExpanded, isDisabled }"
:level="row.depth + 1"
:row-key="row.item.id"
:has-child-items="row.item.children.length > 0"
>
<TableCell>
<span
class="flex items-center gap-1"
:style="{ paddingLeft: row.depth * 16 + 'px' }"
>
<TableCell
v-slot="{ isTreeColumn, hasChildItems: cellHasChildren }"
>
<span class="flex items-center gap-1">
<Button
v-if="row.item.children.length > 0"
v-if="cellHasChildren && isTreeColumn"
is-icon-only
aria-label="切换行"
size="sm"
variant="ghost"
:is-disabled="isDisabled"
@click="toggleExpanded"
>
<IconChevronRight
aria-hidden="true"
class="size-4 text-muted transition-transform duration-150"
:class="isExpanded ? 'rotate-90' : ''"
:class="isExpanded ? 'rotate-90' : 'rtl:rotate-180'"
/>
</Button>
<span>{{ row.item.title }}</span>
+11 -1
View File
@@ -51,6 +51,9 @@ const selectedKeys = ref<TableKey[]>([]);
const allChecked = computed(
() => users.length > 0 && selectedKeys.value.length === users.length,
);
const someChecked = computed(
() => selectedKeys.value.length > 0 && !allChecked.value,
);
function toggleAll() {
selectedKeys.value = allChecked.value ? [] : users.map((u) => u.id);
}
@@ -77,6 +80,7 @@ function toggleOne(id: string) {
<Checkbox
aria-label="全选"
:is-selected="allChecked"
:is-indeterminate="someChecked"
@update:checked="toggleAll"
>
<CheckboxContent>
@@ -119,7 +123,13 @@ function toggleOne(id: string) {
<p class="text-sm text-muted">
已选:
<span class="font-medium">
{{ selectedKeys.length > 0 ? selectedKeys.join(", ") : "无" }}
{{
allChecked
? "全部"
: selectedKeys.length > 0
? selectedKeys.join(", ")
: "无"
}}
</span>
</p>
</div>
+41 -23
View File
@@ -25,7 +25,9 @@ import {
import {
createTable,
getCoreRowModel,
getPaginationRowModel,
getSortedRowModel,
type PaginationState,
type SortingState,
} from "@tanstack/table-core";
@@ -110,11 +112,11 @@ const columns = [
{ id: "email", header: "邮箱" },
] as const;
const sorting = ref<SortingState>([{ id: "name", desc: false }]);
const page = ref(1);
const ROWS_PER_PAGE = 4;
const sorting = ref<SortingState>([]);
const pagination = ref<PaginationState>({ pageIndex: 0, pageSize: 4 });
const ROWS_PER_PAGE = pagination.value.pageSize;
// Real TanStack table model: core row model + sorted row model.
// Real TanStack table model: core + sorted + paginated row models.
const table = computed(() =>
createTable({
data: users,
@@ -124,23 +126,35 @@ const table = computed(() =>
{ id: "status", accessorKey: "status" },
{ id: "email", accessorKey: "email" },
],
state: { sorting: sorting.value },
state: { sorting: sorting.value, pagination: pagination.value },
onStateChange: () => {},
renderFallbackValue: null,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
}),
);
const sortedUsers = computed(() =>
table.value.getSortedRowModel().rows.map((r) => r.original),
const pageRows = computed(() =>
table.value.getPaginationRowModel().rows.map((r) => r.original),
);
const paginatedItems = computed(() => {
const start = (page.value - 1) * ROWS_PER_PAGE;
return sortedUsers.value.slice(start, start + ROWS_PER_PAGE);
});
const totalPages = computed(() =>
Math.ceil(sortedUsers.value.length / ROWS_PER_PAGE),
const pageCount = computed(() => Math.max(1, table.value.getPageCount()));
const pages = computed(() =>
Array.from({ length: pageCount.value }, (_, i) => i + 1),
);
const pageIndex = computed(() => pagination.value.pageIndex);
const start = computed(() => pageIndex.value * ROWS_PER_PAGE + 1);
const end = computed(() =>
Math.min((pageIndex.value + 1) * ROWS_PER_PAGE, users.length),
);
const canPrevious = computed(() => pageIndex.value > 0);
const canNext = computed(() => pageIndex.value < pageCount.value - 1);
function goToPage(index: number) {
pagination.value = {
...pagination.value,
pageIndex: Math.max(0, Math.min(pageCount.value - 1, index)),
};
}
const sortDescriptor = computed(() => {
const s = sorting.value[0];
@@ -160,7 +174,6 @@ function onSortChange(
return;
}
sorting.value = [{ id: d.column, desc: d.direction === "descending" }];
page.value = 1;
}
function dirOf(id: string): "ascending" | "descending" | undefined {
const s = sorting.value[0];
@@ -172,7 +185,7 @@ function dirOf(id: string): "ascending" | "descending" | undefined {
<Table>
<TableScrollContainer>
<TableContent
aria-label="TanStack 表格"
aria-label="TanStack 表格示例"
class="min-w-[600px]"
:sort-descriptor="sortDescriptor"
@update:sort-descriptor="onSortChange"
@@ -192,7 +205,7 @@ function dirOf(id: string): "ascending" | "descending" | undefined {
</TableHeader>
<TableBody>
<TableRow
v-for="user in paginatedItems"
v-for="user in pageRows"
:key="user.id"
:row-key="String(user.id)"
>
@@ -215,25 +228,30 @@ function dirOf(id: string): "ascending" | "descending" | undefined {
<TableFooter>
<Pagination size="sm">
<PaginationSummary>
{{ sortedUsers.length }} 条 · 第 {{ page }} / {{ totalPages }} 页
{{ start }}–{{ end }} / 共 {{ users.length }} 条
</PaginationSummary>
<PaginationContent>
<PaginationItem>
<PaginationPrevious
:is-disabled="page === 1"
@click="page = Math.max(1, page - 1)"
:is-disabled="!canPrevious"
@click="goToPage(pageIndex - 1)"
>
<PaginationPreviousIcon />
上一页
</PaginationPrevious>
</PaginationItem>
<PaginationItem>
<PaginationLink is-active>{{ page }}</PaginationLink>
<PaginationItem v-for="p in pages" :key="p">
<PaginationLink
:is-active="p === pageIndex + 1"
@click="goToPage(p - 1)"
>
{{ p }}
</PaginationLink>
</PaginationItem>
<PaginationItem>
<PaginationNext
:is-disabled="page === totalPages"
@click="page = Math.min(totalPages, page + 1)"
:is-disabled="!canNext"
@click="goToPage(pageIndex + 1)"
>
下一页
<PaginationNextIcon />
+20 -14
View File
@@ -92,9 +92,10 @@ function generateUsers(count: number): User[] {
}
const users = generateUsers(1000);
const ITEM_HEIGHT = 48;
// Matches the fixed upstream Virtualizer layoutOptions (rowHeight/headingHeight 42).
const ITEM_HEIGHT = 42;
const rowKeys = users.map((user) => user.id);
const VIEWPORT = 320;
const VIEWPORT = 300;
const OVERSCAN = 8;
const scrollTop = ref(0);
const scrollEl = ref<HTMLElement | null>(null);
@@ -117,8 +118,8 @@ function focusRow(_key: string | number, index: number) {
if (!node) return;
const top = index * ITEM_HEIGHT;
if (top < node.scrollTop) node.scrollTop = top;
else if (top + ITEM_HEIGHT > node.scrollTop + VIEWPORT - 44)
node.scrollTop = top + ITEM_HEIGHT - VIEWPORT + 44;
else if (top + ITEM_HEIGHT > node.scrollTop + VIEWPORT)
node.scrollTop = top + ITEM_HEIGHT - VIEWPORT;
scrollTop.value = node.scrollTop;
}
function onScroll(event: Event) {
@@ -136,20 +137,22 @@ onUnmounted(() => {
<Table>
<div
ref="scrollEl"
class="h-[320px] w-full overflow-auto"
class="h-[300px] w-full overflow-auto"
data-slot="table-virtual-viewport"
>
<TableContent
aria-label="虚拟化表格"
class="min-w-[600px] table-fixed"
aria-label="1000 行虚拟化表格"
class="min-w-[700px] table-fixed"
:row-keys="rowKeys"
:aria-rowcount="users.length + 1"
@focus-row="focusRow"
>
<TableHeader aria-rowindex="1">
<TableColumn id="name" is-row-header>姓名</TableColumn>
<TableColumn id="role">角色</TableColumn>
<TableColumn id="email">邮箱</TableColumn>
<TableHeader aria-rowindex="1" class="h-full w-full">
<TableColumn id="name" is-row-header :min-width="160">
姓名
</TableColumn>
<TableColumn id="role" :min-width="220">角色</TableColumn>
<TableColumn id="email" :min-width="240">邮箱</TableColumn>
</TableHeader>
<TableBody>
<tr aria-hidden="true" :style="{ height: padTop + 'px' }" />
@@ -161,17 +164,20 @@ onUnmounted(() => {
:style="{ height: ITEM_HEIGHT + 'px' }"
>
<TableCell
class="h-12 whitespace-nowrap overflow-hidden text-ellipsis"
class="whitespace-nowrap overflow-hidden text-ellipsis"
:style="{ height: ITEM_HEIGHT + 'px' }"
>
{{ user.name }}
</TableCell>
<TableCell
class="h-12 whitespace-nowrap overflow-hidden text-ellipsis"
class="whitespace-nowrap overflow-hidden text-ellipsis"
:style="{ height: ITEM_HEIGHT + 'px' }"
>
{{ user.role }}
</TableCell>
<TableCell
class="h-12 whitespace-nowrap overflow-hidden text-ellipsis"
class="whitespace-nowrap overflow-hidden text-ellipsis"
:style="{ height: ITEM_HEIGHT + 'px' }"
>
{{ user.email }}
</TableCell>
+2 -1
View File
@@ -93,7 +93,8 @@ function history(message: string) {
<div
v-for="(item, index) in closedHistory"
:key="item.time + '-' + index"
class="flex items-start justify-between gap-3 rounded-md border border-border bg-default px-3 py-2 text-sm"
class="flex animate-in items-start justify-between gap-3 rounded-md border border-border bg-default px-3 py-2 text-sm duration-200 fade-in slide-in-from-top-2 motion-reduce:animate-none"
:style="{ animationDelay: `${index * 50}ms` }"
>
<div class="flex-1">
<span class="font-medium">{{ item.message }}</span>
+35 -22
View File
@@ -3,10 +3,12 @@ import { onBeforeUnmount } from "vue";
import {
Button,
ToastProvider,
createToastFunction,
ToastQueue,
type ToastContentValue,
} from "@heroui-vue/components";
import type { ToastVariants } from "@heroui-vue/styles";
type Placement = NonNullable<ToastVariants["placement"]>;
const placements = [
"top start",
@@ -17,31 +19,42 @@ const placements = [
"bottom end",
] as const;
// One queue + provider per placement so the toast is actually mounted
// into that corner of the viewport.
const instances = placements.map((placement) => {
const queue = new ToastQueue<ToastContentValue>({ maxVisibleToasts: 2 });
return { placement, queue, toast: createToastFunction(queue) };
// One queue + provider per placement so the toast mounts in that corner.
const placementQueues = Object.fromEntries(
placements.map((p) => [
p,
new ToastQueue<ToastContentValue>({ maxVisibleToasts: 3 }),
]),
) as Record<Placement, ToastQueue<ToastContentValue>>;
onBeforeUnmount(() => {
for (const q of Object.values(placementQueues)) q.destroy();
});
onBeforeUnmount(() =>
instances.forEach((instance) => instance.queue.destroy()),
);
function showToast(placement: Placement) {
placementQueues[placement].add({
description: "活动已创建",
title: "活动已创建",
variant: "default",
});
}
</script>
<template>
<div class="flex flex-wrap gap-3">
<template v-for="inst in instances" :key="inst.placement">
<Button
variant="secondary"
@click="inst.toast(`位置:${inst.placement}`, { timeout: 3000 })"
>
{{ inst.placement }}
</Button>
<ToastProvider
:queue="inst.queue"
:placement="inst.placement"
to="body"
/>
<div class="flex h-full flex-col items-center justify-center gap-6">
<template v-for="p in placements" :key="p">
<ToastProvider :placement="p" :queue="placementQueues[p]" to="body" />
</template>
<div class="flex max-w-xs flex-wrap justify-center gap-2">
<Button
v-for="p in placements"
:key="p"
size="sm"
variant="secondary"
@click="showToast(p)"
>
{{ p }}
</Button>
</div>
</div>
</template>