From 9e8d89e48da24d09027b24e7c256869b8d77b8f9 Mon Sep 17 00:00:00 2001 From: maoyingjie Date: Thu, 29 Oct 2026 16:21:09 +0800 Subject: [PATCH] feat(collections): virtual row layout with variable heights, headings, loader and gap/padding ListBox virtualized mode grows a full row stream (padding spacers, data-driven heading rows, item rows, #loader row) with px semantics mapped from the pinned ListLayout contract: rowHeight may be a per-index function falling back to estimatedRowHeight, gap sits between content rows only, padding is end spacers, and every input is re-read per layout pass. Scroll ownership resolves to the list root or the nearest scrolling ancestor so popovers scroll as a whole with a sticky search field (data-virtualized hook; the historical 320px self-scroll rule stays for non-virtualized lists). Table gains the public useVirtualRows/bindVirtualRows pure geometry helpers (prefix sums + binary search, no state kernel): variable heights, estimated fallback, gap/padding and a loader reservation that stays consistent with the rendered spacers. headingHeight and dropIndicatorThickness remain unavailable (no sectioned header model, no drag-and-drop) and are recorded as such. Pinned demo values stay explicit (table 42px, list-box/autocomplete 50px); the table demo scrolls an outer viewport div because a native table cannot be a scroll container (verified: 300px height blown out to 45036px). --- apps/docs/components/list-box.md | 33 ++- apps/docs/components/table.md | 16 +- .../demos/autocomplete-virtualization.vue | 10 +- apps/docs/demos/table-virtualization.vue | 156 +++++----- apps/playground/src/App.vue | 7 + docs/compatibility.md | 10 +- docs/upstream.md | 6 +- packages/styles/components/autocomplete.css | 16 +- .../src/components/collection/virtual-rows.ts | 169 +++++++++++ .../vue/src/components/list-box/ListBox.vue | 173 ++++++++++- .../vue/src/components/list-box/context.ts | 24 +- packages/vue/src/index.ts | 13 + tests/fixtures/virtual-rows.vue | 244 ++++++++++++++++ tests/package/client-checks.json | 4 + tests/package/virtual-rows-contract.ts | 84 ++++++ tests/virtual-rows.test.ts | 276 ++++++++++++++++++ 16 files changed, 1127 insertions(+), 114 deletions(-) create mode 100644 packages/vue/src/components/collection/virtual-rows.ts create mode 100644 tests/fixtures/virtual-rows.vue create mode 100644 tests/package/virtual-rows-contract.ts create mode 100644 tests/virtual-rows.test.ts diff --git a/apps/docs/components/list-box.md b/apps/docs/components/list-box.md index bb4ebc0..5d8a6e4 100644 --- a/apps/docs/components/list-box.md +++ b/apps/docs/components/list-box.md @@ -77,10 +77,27 @@ import { ListBox 通过 `virtualized` 仅渲染视口附近的行,高效展示大数据集。下面保留基线的 1000 条数据、50px 固定行高和 400px 视口。完整数据传给 `items`,首字母导航、全选和范围选择使用完整集合。 -Vue 使用现有 VueUse 虚拟列表适配,不导出 React 的 `Virtualizer` / `ListLayout`。当前支持竖向固定行高列表;可变行高、虚拟分组标题和拖放布局尚未支持。 +Vue 使用现有 VueUse 虚拟列表适配,不导出 React 的 `Virtualizer` / `ListLayout`;布局参数在 ListBox 上直接提供(见下文 ListLayout 映射)。支持竖向固定与可变行高、数据驱动的虚拟分组标题(`items` 项的 `heading` 字段)、`#loader` 加载行以及 `gap` / `padding` 行流布局。拖放布局(放置指示线)在 Vue 侧没有对应实现,`dropIndicatorThickness` 不提供(见 ListLayout 映射记录)。滚动容器自动探测:列表自身可滚时绑定自身,否则绑定最近的滚动祖先(例如弹层整体滚动并配 sticky 搜索框的 Autocomplete)。 +#### ListLayout 映射 + +不导出 React Aria 的 `ListLayout` 实例;等价布局参数直接在 ListBox 上提供,单位均为 px、默认值与固定源布局契约一致(rowHeight 48、headingHeight 48、loaderHeight 48、gap 0、padding 0;Vue 侧 `rowHeight` 的数据模式默认值为 50,与基线示例一致)。上游三个虚拟化示例的显式值(Table 42、ListBox/Autocomplete 50)是示例传值而非布局默认值,示例保持原值不机械统一。 + +| ListLayout 参数 | Vue 对应 | 更新语义 | +| --- | --- | --- | +| `rowHeight` | `rowHeight`(number 或 `(index) => number \| undefined`) | 每次布局重读;响应式高度变化无需重挂载。 | +| `estimatedRowHeight` | `estimatedRowHeight` | 函数行高返回 `undefined` 时的回退;无默认,回退到 `rowHeight` 数值(数据模式 50)。 | +| `headingHeight` | `headingHeight` | 虚拟 `heading` 行计高;非虚拟模式的分组仍用 `ListBoxSection`,语义与渲染结构不同。 | +| `estimatedHeadingHeight` | 不提供 | Vue 侧标题行高固定(`headingHeight`),没有可变标题测量路径;如实记录为差异。 | +| `loaderHeight` | `loaderHeight` + `#loader` 插槽 | `#loader` 存在时渲染为行流末尾的内容行。 | +| `dropIndicatorThickness` | 不提供 | Vue 侧 ListBox/Table 没有拖放实现,无放置指示线可度量;不补空参数冒充能力。 | +| `gap` | `gap` | 内容行之间的间距,计入较早一行的行占位;padding 行不计。 | +| `padding` | `padding` | 行流两端的占位行,不计 gap。 | + +虚拟分组标题是数据驱动的(`items` 项的 `heading` 字段);动态过滤与异步追加后,条目按逻辑 `id` 保持身份、选择与键盘导航,`aria-posinset` / `aria-setsize` 始终相对完整可见集合计算,窗口外未挂载的行不丢失身份。 + ### 自定义勾选图标 @@ -163,10 +180,15 @@ Vue 使用 `as` 修改原生标签,普通和作用域插槽组合内容;状 | `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` | 视口外额外渲染的行数,初始化时确定。 | +| `items` | `readonly T[]`,`T extends ListBoxDataItem` | — | 数据集合,具备唯一的 `id`;作用域插槽接收 `item` 和 `index`。虚拟模式下可选 `heading` 字段在其行前渲染分组标题行(仅 `headingHeight` 计高,不参与选择与 `aria-posinset` 索引)。 | +| `virtualized` | `boolean` | `false` | 开启竖向虚拟列表,必须提供 `items`。 | +| `rowHeight` | `number \| (index: number) => number \| undefined` | `50` | 每行高度(px)。函数映射行索引到可变行高,返回 `undefined` 回退 `estimatedRowHeight`。每次布局重读,响应式高度无需重挂载。 | +| `estimatedRowHeight` | `number` | `rowHeight` 数值或 `50` | 可变行高函数返回 `undefined` 时的回退高度(px)。 | +| `headingHeight` | `number` | `48` | 虚拟分组标题行高度(px)。 | +| `loaderHeight` | `number` | `48` | `#loader` 加载行高度(px)。 | +| `gap` | `number` | `0` | 内容行(条目/标题/加载行)之间的间距(px);padding 行不计 gap。 | +| `padding` | `number` | `0` | 列表内边距(px),渲染为行流两端的占位行。 | +| `overscan` | `number` | `5` | 视口外额外渲染的行数,初始化时确定。 | | `aria-label` / `aria-labelledby` | `string` | — | 根列表框的可访问名称。 | | `class` / `style` / DOM 事件 | Vue 原生属性 | — | 透传到根 role=listbox 节点,类增量合并。 | @@ -182,6 +204,7 @@ Vue 使用 `as` 修改原生标签,普通和作用域插槽组合内容;状 | --------------- | ------------------------------------- | -------------------------------------------- | | 默认插槽 | 数据模式 `{ item: T, index: number }` | 静态部件模式直接放置 Item / Section。 | | `empty` | 无 | items 为空时的内容。 | +| `loader` | 无 | 虚拟行流末尾的加载行(如「加载更多」),按 `loaderHeight` 计高。 | | `focus(key?)` | `ListBoxKey?` | 聚焦根或指定可用项;虚拟项先滚动到可见范围。 | | `selectAll()` | 无 | 多选模式下提出全选,跳过禁用项。 | | `element` | `HTMLElement` | 根元素引用。 | diff --git a/apps/docs/components/table.md b/apps/docs/components/table.md index e8822ba..7c286ab 100644 --- a/apps/docs/components/table.md +++ b/apps/docs/components/table.md @@ -96,7 +96,7 @@ import { ### 虚拟滚动 -仅渲染视口内可见行,从而高效展示大数据集。本库使用固定行高窗口替代 React Aria `Virtualizer`。 +仅渲染视口内可见行,从而高效展示大数据集。本库以 `useVirtualRows` 行几何组合式替代 React Aria `Virtualizer`,支持固定与可变行高、`gap` / `padding` 与加载行预留(见下文 TableLayout 映射)。示例保留基线的 42px 显式行高、300px 视口与外层滚动容器:原生 `` 不能作为滚动容器(`height` 是最小高度且 `overflow` 需要确定高度,实测 300px 被内容撑到 45036px),而固定源滚动的是虚拟网格元素;列宽用 `minWidth`(fr 语义由 TableContent 列布局承担),长文本按列宽截断。 @@ -332,7 +332,19 @@ Table 同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制 ### TableLayout 与 Vue 虚拟滚动 -未导出 React Aria TableLayout 类。示例使用固定 42px 行高、300px 滚动视口和 overscan 窗口(对齐上游 `layoutOptions.rowHeight/headingHeight = 42`),按完整 rowKeys 处理键盘请求;aria-rowcount 包括表头,aria-rowindex 为逻辑位置。仅当前窗口挂载。可变高度、拖放指标与 RAC 虚拟布局扩展暂不支持,不能把固定行高示例用于多行自动换高的数据。TanStack 示例使用可选 table-core 数据模型(含 `getPaginationRowModel`),库运行时不依赖它。 +未导出 React Aria TableLayout 类;行几何由公共组合式 `useVirtualRows` / `bindVirtualRows` 提供(纯测量,无状态内核:选择、身份与焦点仍归集合/表格模型)。示例使用固定 42px 行高、300px 滚动视口和 overscan 窗口(对齐上游 `layoutOptions.rowHeight/headingHeight = 42`;42 是示例显式值,布局默认 48),按完整 rowKeys 处理键盘请求;aria-rowcount 包括表头,aria-rowindex 为逻辑位置。仅当前窗口挂载。TanStack 示例使用可选 table-core 数据模型(含 `getPaginationRowModel`),库运行时不依赖它。 + +| `useVirtualRows` 选项 | 类型 | 默认值 | 描述 | +| --- | --- | --- | --- | +| `count` | `MaybeRefOrGetter` | — | 表体行数;getter 响应式更新行流。 | +| `rowHeight` | `number \| (index: number) => number \| undefined` | — | 行高(px);函数映射行索引到可变行高,返回 `undefined` 回退 `estimatedRowHeight`。每次布局重读。 | +| `estimatedRowHeight` | `number` | `50` | 可变行高回退(px)。 | +| `loaderHeight` | `number` | `0` | 行流末尾的加载行预留(px),在最后一行的 gap 之后、尾部 padding 之前;计入 `totalHeight` 与 `window.padBottom`。 | +| `gap` | `number` | `0` | 行之间的间距(px),计入较早一行的 `rowExtentOf`;padding 不计 gap。 | +| `padding` | `number` | `0` | 列表内边距(px),计入行流两端(`padTop` 含头部、`padBottom` 含尾部)。 | +| `overscan` | `number` | `8` | 视口外额外渲染的行数。 | + +`window` 提供 `start` / `end` / `padTop` / `padBottom`(渲染 `` 占位的 spacer 高度,padTop + 已渲染行 + padBottom 恒等于 `totalHeight`),`scrollToIndex` 按行偏移滚动并夹在行流内,`bindVirtualRows` 把窗口更新绑定到滚动容器(滚动与容器缩放)。TableLayout 的 `headingHeight` / `estimatedHeadingHeight` 不提供:Vue 表格没有分区表头模型,如实记录为差异;`dropIndicatorThickness` 同 ListBox——Vue 侧无拖放实现,不提供空参数。 ## 无障碍 diff --git a/apps/docs/demos/autocomplete-virtualization.vue b/apps/docs/demos/autocomplete-virtualization.vue index d6aa2e2..c2b4c54 100644 --- a/apps/docs/demos/autocomplete-virtualization.vue +++ b/apps/docs/demos/autocomplete-virtualization.vue @@ -156,12 +156,10 @@ const keepResults = () => true; - + +