fix: retain pending Toast and Table fidelity corrections
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: Table 表格
|
||||
description: 数据展示表格:排序、选择、分页与自定义单元格。
|
||||
description: 表格以行和列展示结构化数据,支持排序、选择、列宽调整与无限滚动。
|
||||
component: table
|
||||
group: 组件
|
||||
---
|
||||
@@ -21,7 +21,7 @@ import {
|
||||
} from "@heroui-vue/components";
|
||||
```
|
||||
|
||||
<ComponentPreview name="table-basic" />
|
||||
<ComponentPreview name="table-basic" minHeight="320px" />
|
||||
|
||||
## 组件结构
|
||||
|
||||
@@ -48,47 +48,66 @@ import {
|
||||
|
||||
### 次要变体
|
||||
|
||||
<ComponentPreview name="table-secondary-variant" />
|
||||
<ComponentPreview name="table-secondary-variant" minHeight="320px" />
|
||||
|
||||
### 异步加载
|
||||
|
||||
<ComponentPreview name="table-async-loading" />
|
||||
使用 `TableLoadMore` 实现无限滚动。它会渲染一个哨兵行,滚动到视口时触发加载。
|
||||
|
||||
<ComponentPreview name="table-async-loading" minHeight="380px" />
|
||||
|
||||
### 排序
|
||||
|
||||
<ComponentPreview name="table-sorting" />
|
||||
在 `TableColumn` 上使用 `allows-sorting` 属性可启用列排序。在 `TableContent` 上使用 `sort-descriptor` 与 `update:sort-descriptor` 管理排序状态。将标签包裹在 `TableSortableColumnHeader` 中,并传入列的 `sort-direction` 以渲染默认升序/降序指示器。
|
||||
|
||||
<ComponentPreview name="table-sorting" minHeight="380px" />
|
||||
|
||||
### 选择
|
||||
|
||||
<ComponentPreview name="table-selection" />
|
||||
在 `TableContent` 上使用 `selection-mode` 启用行选择。使用复选框实现全选与逐行勾选;全选支持半选(indeterminate)状态。
|
||||
|
||||
<ComponentPreview name="table-selection" minHeight="380px" />
|
||||
|
||||
### 可展开行
|
||||
|
||||
<ComponentPreview name="table-expandable-rows" />
|
||||
行可嵌套以展示层级数据。使用 `tree-column` 指定列,并在该列单元格中渲染展开/收起按钮。使用 `expanded-keys` 控制哪些行处于展开状态。
|
||||
|
||||
<ComponentPreview name="table-expandable-rows" minHeight="420px" />
|
||||
|
||||
### 分页
|
||||
|
||||
<ComponentPreview name="table-pagination" />
|
||||
使用 `TableFooter` 在表格下方添加分页组件。
|
||||
|
||||
<ComponentPreview name="table-pagination" minHeight="400px" />
|
||||
|
||||
### 列宽调整
|
||||
|
||||
<ComponentPreview name="table-column-resizing" />
|
||||
将表格包裹在 `TableResizableContainer` 中,并在每个可调整宽度的列内添加 `TableColumnResizer`。列可用 `default-width="1fr"` 表示弹性初始宽度(映射为原生 `width: auto`,共享剩余空间;不实现 RAC 的 fr 权重比例)。
|
||||
|
||||
<ComponentPreview name="table-column-resizing" minHeight="420px" />
|
||||
|
||||
### 空状态
|
||||
|
||||
<ComponentPreview name="table-empty-state" />
|
||||
在 `TableBody` 上使用具名插槽 `#empty`,在表格无数据时展示自定义消息。空提示不参与选择、行计数与键盘导航。
|
||||
|
||||
<ComponentPreview name="table-empty-state" minHeight="300px" />
|
||||
|
||||
### 虚拟滚动
|
||||
|
||||
<ComponentPreview name="table-virtualization" />
|
||||
仅渲染视口内可见行,从而高效展示大数据集。本库使用固定行高窗口替代 React Aria `Virtualizer`。
|
||||
|
||||
<ComponentPreview name="table-virtualization" minHeight="380px" />
|
||||
|
||||
### TanStack Table
|
||||
|
||||
<ComponentPreview name="table-tanstack-table" />
|
||||
HeroUI 的 Table 作为无头表格库之上的渲染层。
|
||||
本示例使用 [TanStack Table](https://tanstack.com/table) 处理列定义、排序与分页,而本库负责样式与无障碍。
|
||||
|
||||
<ComponentPreview name="table-tanstack-table" minHeight="420px" />
|
||||
|
||||
### 自定义单元格
|
||||
|
||||
<ComponentPreview name="table-custom-cells" />
|
||||
<ComponentPreview name="table-custom-cells" minHeight="420px" />
|
||||
|
||||
## 自定义样式
|
||||
|
||||
@@ -100,7 +119,7 @@ import {
|
||||
|
||||
```css
|
||||
@layer components {
|
||||
.hv-table {
|
||||
.hv-table-root {
|
||||
@apply rounded-2xl;
|
||||
}
|
||||
.hv-table__column {
|
||||
@@ -124,12 +143,12 @@ Table 组件使用以下 CSS 类([查看源码样式](https://github.com/herou
|
||||
|
||||
- `.hv-table-root` - 根容器(命名为 `hv-table-root` 而非 `hv-table`,因为 `hv-table` 是 Tailwind CSS 内置的 `display: table` 工具类)
|
||||
- `.hv-table__scroll-container` - 横向滚动包裹层与自定义滚动条
|
||||
- `.hv-table__content` - `` 元素
|
||||
- `.hv-table__header` - 表头行(``)
|
||||
- `.hv-table__column` - 列表头单元格(``)
|
||||
- `.hv-table__body` - 表体(``)
|
||||
- `.hv-table__row` - 行(``)
|
||||
- `.hv-table__cell` - 数据单元格(``)
|
||||
- `.hv-table__content` - `<table>` 元素
|
||||
- `.hv-table__header` - 表头行(`<thead>`)
|
||||
- `.hv-table__column` - 列表头单元格(`<th>`)
|
||||
- `.hv-table__body` - 表体(`<tbody>`)
|
||||
- `.hv-table__row` - 行(`<tr>`)
|
||||
- `.hv-table__cell` - 数据单元格(`<td>`)
|
||||
- `.hv-table__footer` - 表底容器(位于 table 外部)
|
||||
|
||||
#### 进阶类
|
||||
@@ -150,7 +169,7 @@ Table 组件使用以下 CSS 类([查看源码样式](https://github.com/herou
|
||||
|
||||
Table 同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制:
|
||||
|
||||
- **悬停**:`:hover` 或 `[data-hover="true"]`(行背景变化)
|
||||
- **悬停**:`:hover` 或 `[data-hovered="true"]`(行背景变化)
|
||||
- **已选中**:`[data-selected="true"]`(行高亮)
|
||||
- **聚焦**:`:focus-visible` 或 `[data-focus-visible="true"]`(行、列与单元格的内嵌焦点环)
|
||||
- **禁用**:`:disabled` 或 `[aria-disabled="true"]`(降低透明度)
|
||||
@@ -204,22 +223,25 @@ Table 同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制
|
||||
|
||||
### TableColumn
|
||||
|
||||
| Prop / 事件 | 类型 | 默认值 | 描述 |
|
||||
| ------------------------------- | ------------------ | ------- | ----------------------------------------- |
|
||||
| `as` | `string` | `'th'` | 列头标签,scope=col |
|
||||
| `id` | `string` | — | 排序与树形列 id |
|
||||
| `allowsSorting` / `isRowHeader` | `boolean` | `false` | 排序列 / 对应正文单元格为 th[scope=row] |
|
||||
| `width` / `defaultWidth` | `number \| string` | — | 受控宽度 / 非受控初始宽度,数字为 px |
|
||||
| `minWidth` / `maxWidth` | `number` | — | 宽度上下限,优先于 Resizer 的缺省边界 |
|
||||
| `update:width` | `number` | — | 拖动/键盘提出的宽度,可使用 v-model:width |
|
||||
| 默认插槽 | 内容 | — | 列标签及排序、调整手柄 |
|
||||
| Prop / 事件 | 类型 | 默认值 | 描述 |
|
||||
| ------------------------------- | ------------------ | ------- | --------------------------------------------------------------------- |
|
||||
| `as` | `string` | `'th'` | 列头标签,scope=col |
|
||||
| `id` | `string` | — | 排序与树形列 id |
|
||||
| `allowsSorting` / `isRowHeader` | `boolean` | `false` | 排序列 / 对应正文单元格为 th[scope=row] |
|
||||
| `width` / `defaultWidth` | `number \| string` | — | 受控宽度 / 非受控初始宽度;数字为 px,`Nfr` 为弹性 auto(非权重比例) |
|
||||
| `minWidth` / `maxWidth` | `number` | — | 宽度上下限,优先于 Resizer 的缺省边界 |
|
||||
| `update:width` | `number` | — | 拖动/键盘提出的宽度,可使用 v-model:width |
|
||||
| 默认插槽 | 内容 | — | 列标签及排序、调整手柄 |
|
||||
|
||||
### TableBody
|
||||
|
||||
| Prop / 插槽 | 类型 | 默认值 | 描述 |
|
||||
| ----------- | -------- | --------- | ---------------------------------------------------------- |
|
||||
| `as` | `string` | `'tbody'` | 正文根 |
|
||||
| 默认插槽 | 行内容 | — | 动态行使用 v-for;空状态使用 colspan 单元格组合 EmptyState |
|
||||
| Prop / 插槽 | 类型 | 默认值 | 描述 |
|
||||
| ----------- | ------------------ | --------- | --------------------------------------------------------------------------- |
|
||||
| `as` | `string` | `'tbody'` | 正文根 |
|
||||
| `empty` | `boolean` | 自动判断 | 为 `true` 时强制空状态;省略时若默认插槽未渲染任何行则视为空 |
|
||||
| `colspan` | `number \| string` | `1` | 空状态单元格跨列数 |
|
||||
| 默认插槽 | 行内容 | — | 动态行使用 v-for |
|
||||
| `#empty` | 空状态 | — | 空数据时渲染;包在 `role="presentation"` 行内,不参与选择、行计数与键盘导航 |
|
||||
|
||||
### TableRow
|
||||
|
||||
@@ -308,7 +330,7 @@ Table 同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制
|
||||
|
||||
### TableLayout 与 Vue 虚拟滚动
|
||||
|
||||
未导出 React Aria TableLayout 类。示例使用固定 48px 行高、320px 滚动视口和 overscan 窗口,按完整 rowKeys 处理键盘请求;aria-rowcount 包括表头,aria-rowindex 为逻辑位置。仅当前窗口挂载。可变高度、拖放指标与 RAC 虚拟布局扩展暂不支持,不能把固定行高示例用于多行自动换高的数据。TanStack 示例使用可选 table-core 数据模型,库运行时不依赖它。
|
||||
未导出 React Aria TableLayout 类。示例使用固定 42px 行高、300px 滚动视口和 overscan 窗口(对齐上游 `layoutOptions.rowHeight/headingHeight = 42`),按完整 rowKeys 处理键盘请求;aria-rowcount 包括表头,aria-rowindex 为逻辑位置。仅当前窗口挂载。可变高度、拖放指标与 RAC 虚拟布局扩展暂不支持,不能把固定行高示例用于多行自动换高的数据。TanStack 示例使用可选 table-core 数据模型(含 `getPaginationRowModel`),库运行时不依赖它。
|
||||
|
||||
## 无障碍
|
||||
|
||||
|
||||
@@ -80,7 +80,77 @@ import {
|
||||
|
||||
### 配置
|
||||
|
||||
`timeout` 默认 4000ms;`0` 持久显示。悬停、焦点进入、页面隐藏和外部暂停分别管理,全部原因解除后继续剩余时间。可视上限隐藏旧消息但不删除、不停止它们的倒计时。
|
||||
在应用根节点渲染 Provider。`timeout` 默认 4000ms;`0` 持久显示。悬停、焦点进入、页面隐藏和外部暂停分别管理,全部原因解除后继续剩余时间。可视上限隐藏旧消息但不删除、不停止它们的倒计时。
|
||||
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Button,
|
||||
ToastProvider,
|
||||
createToastFunction,
|
||||
ToastQueue,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
const queue = new ToastQueue();
|
||||
const toast = createToastFunction(queue);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<ToastProvider :queue="queue" to="body" />
|
||||
<Button @click="toast('简单消息')">显示 Toast</Button>
|
||||
</div>
|
||||
</template>
|
||||
```
|
||||
|
||||
`toast()` 常用写法:
|
||||
|
||||
```ts
|
||||
// 基础 toast(默认约 4 秒后自动消失)
|
||||
toast("活动已创建");
|
||||
|
||||
// 变体方法(默认同样约 4 秒后自动消失)
|
||||
toast.success("文件已保存");
|
||||
toast.info("有新更新可用");
|
||||
toast.warning("请检查您的设置");
|
||||
toast.danger("出了点问题");
|
||||
|
||||
// 传入 options。利用返回的 id,操作按钮可以关闭它所属的 Toast。
|
||||
const eventId = toast("活动已创建", {
|
||||
description: "您的活动已安排在明天",
|
||||
variant: "default",
|
||||
timeout: 5000,
|
||||
onClose: () => console.log("已关闭"),
|
||||
actionProps: {
|
||||
label: "查看",
|
||||
onClick: () => toast.close(eventId),
|
||||
},
|
||||
});
|
||||
|
||||
// 原地更新已有 Toast(保持其在堆叠中的位置)。
|
||||
const id = toast("保存中…", { timeout: 0 });
|
||||
toast.update(id, "已保存", { variant: "success", timeout: 4000 });
|
||||
|
||||
// Promise 支持(自动显示加载指示)。Promise 结束时加载 Toast 会原地更新。
|
||||
toast.promise(uploadFile(), {
|
||||
loading: "正在上传文件…",
|
||||
success: (data) => `文件 ${data.filename} 已上传`,
|
||||
error: "上传文件失败",
|
||||
});
|
||||
|
||||
// 手动加载状态(持久 toast:不自动消失)
|
||||
const loadingId = toast("正在创建活动…", {
|
||||
isLoading: true,
|
||||
timeout: 0,
|
||||
});
|
||||
toast.update(loadingId, "活动已创建", { variant: "success", timeout: 4000 });
|
||||
|
||||
// 队列方法
|
||||
toast.close(key);
|
||||
toast.clear();
|
||||
toast.pauseAll();
|
||||
toast.resumeAll();
|
||||
```
|
||||
|
||||
## 自定义样式
|
||||
|
||||
@@ -262,11 +332,22 @@ Toast 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/
|
||||
|
||||
## 无障碍
|
||||
|
||||
区域使用 region 和可覆盖的 aria-label;通知使用 status。Alt+T 进入有消息的堆叠,Escape 返回之前的触发器;关闭当前条目时聚焦邻近存活条目,最后一条关闭后返回触发器。隐藏、退出与折叠的非首条通知设置 inert,内部按钮不能进入 Tab 顺序。悬停、焦点进入、页面隐藏暂停倒计时。
|
||||
对照固定上游 Toast primitive(react-aria `useToast`):
|
||||
|
||||
- 单条 Toast 为 `role="alertdialog"`、`aria-modal="false"`,并通过 `aria-labelledby` / `aria-describedby` 关联标题与说明
|
||||
- 内容层 `ToastContent` 为 `role="alert"`、`aria-atomic="true"`;首次绘制前 `aria-hidden`,挂载后可见以触发读屏播报
|
||||
- 区域使用 landmark `role="region"` 与可覆盖的 `aria-label`(默认 `Notifications`)
|
||||
- Alt+T 进入有消息的堆叠,Escape 返回之前的触发器;关闭当前条目时聚焦邻近存活条目,最后一条关闭后返回触发器
|
||||
- 新通知**不**抢焦点,避免打断用户;隐藏、退出与折叠的非首条通知设置 inert,内部按钮不能进入 Tab 顺序
|
||||
- 悬停、焦点进入、页面隐藏暂停倒计时
|
||||
|
||||
### 上游差异与 SSR
|
||||
|
||||
React 对象部件改具名导出,ReactNode 改 VNodeChild,onPress 改 DOM click;Provider 自动创建 Toast 根,作用域插槽负责内部内容,toastClass 定制根样式。采用单一自有队列计时,未运行 React,也未另启 Reka 的第二个倒计时。服务端全局 toast 为无操作,不保留跨请求消息;SSR 如需通知内容,每请求创建 ToastQueue 并传入 Provider,客户端沿用该请求数据,使用后 destroy。Teleport SSR 目标请依 Vue SSR 规则在客户端装载。
|
||||
React 对象部件改具名导出,ReactNode 改 VNodeChild,onPress 改 DOM click;`actionProps` 的按钮内容字段使用 `label`(上游为 `children`),事件使用 `onClick`(上游为 `onPress`)。Provider 自动创建 Toast 根,作用域插槽负责内部内容,toastClass 定制根样式。采用单一自有队列计时,未运行 React,也未另启 Reka 的第二个倒计时。
|
||||
|
||||
上游 `toast.getQueue()` 返回 react-stately 队列实例,本库不提供该方法(不暴露底层队列类型)。当前队列实例通过 `ToastProvider` 的 `queue` 插槽/暴露值,或自行创建的 `ToastQueue` 实例访问。
|
||||
|
||||
服务端全局 toast 为无操作,不保留跨请求消息;SSR 如需通知内容,每请求创建 ToastQueue 并传入 Provider,客户端沿用该请求数据,使用后 destroy。Teleport SSR 目标请依 Vue SSR 规则在客户端装载。
|
||||
|
||||
## 相关组件
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -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 />
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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 />
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user