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).
This commit is contained in:
maoyingjie
2026-10-29 16:21:09 +08:00
parent dd4f04c200
commit 9e8d89e48d
16 changed files with 1127 additions and 114 deletions
+244
View File
@@ -0,0 +1,244 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from "vue";
import {
Button,
Description,
Header,
Input,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
Table,
TableBody,
TableCell,
TableColumn,
TableContent,
TableHeader,
TableRow,
TableScrollContainer,
bindVirtualRows,
useVirtualRows,
} from "@heroui-vue/components";
/**
* HD-09 virtual layout harness: variable row heights, heading rows, loader
* rows, gap/padding, container resizing, scroll positioning and keyboard
* navigation across unmounted windows — with selection identity kept over
* filtering and appends.
*/
const headingHeight = 32;
const fallbackRow = 48;
const rowHeight = (index: number) =>
index % 3 === 0 ? 40 : index % 3 === 1 ? 64 : 80;
const query = ref("");
const allItems = ref(
Array.from({ length: 800 }, (_, index) => ({
id: index + 1,
textValue: `条目 ${index + 1}`,
heading: index % 5 === 0 ? `分组 ${Math.floor(index / 5) + 1}` : undefined,
})),
);
const filteredItems = computed(() => {
const text = query.value.trim();
if (!text) return allItems.value;
return allItems.value.filter((item) => item.textValue.includes(text));
});
const selected = ref<(string | number)[]>([]);
const listHeight = ref(320);
const loading = ref(false);
function appendItems() {
loading.value = true;
setTimeout(() => {
const start = allItems.value.length;
allItems.value = [
...allItems.value,
...Array.from({ length: 100 }, (_, index) => ({
id: start + index + 1,
textValue: `条目 ${start + index + 1}`,
heading:
(start + index) % 5 === 0
? `分组 ${Math.floor((start + index) / 5) + 1}`
: undefined,
})),
];
loading.value = false;
}, 300);
}
function selectFarItem() {
selected.value = [allItems.value[600]?.id ?? 601];
listboxRef.value?.focus(selected.value[0] as never);
}
const listboxRef = ref<{ focus: (key?: never) => void } | null>(null);
// ---- variable-height table window ----
const tableCount = ref(300);
const tableRowHeight = (index: number) => (index % 2 ? 44 : 72);
const tableRows = useVirtualRows({
count: () => tableCount.value,
rowHeight: tableRowHeight,
estimatedRowHeight: fallbackRow,
loaderHeight: 40,
gap: 4,
padding: 12,
overscan: 6,
});
const tableWindow = tableRows.window;
const tableItems = computed(() =>
Array.from({ length: tableCount.value }, (_, index) => ({
id: index + 1,
name: `行 ${index + 1}`,
})),
);
const visibleTableRows = computed(() =>
tableItems.value.slice(tableWindow.value.start, tableWindow.value.end),
);
const scrollRef = ref<HTMLElement | null>(null);
const scrollElement = () => scrollRef.value;
function jumpToRow(index: number) {
const element = scrollElement();
if (element) tableRows.scrollToIndex(element, index);
}
let detach: (() => void) | undefined;
onMounted(() => {
const element = scrollElement();
if (element) detach = bindVirtualRows(tableRows, element);
});
onUnmounted(() => detach?.());
</script>
<template>
<main style="padding: 24px; display: grid; gap: 24px">
<section style="display: grid; gap: 8px">
<h2>可变行高 / 分组 / 加载行 / gap / padding</h2>
<div style="display: flex; gap: 8px; align-items: center">
<Label for="virtual-filter">过滤</Label>
<Input
id="virtual-filter"
:model-value="query"
placeholder="按文本过滤…"
@update:model-value="query = $event"
/>
<Button
size="sm"
variant="secondary"
@click="listHeight = listHeight === 320 ? 520 : 320"
>
容器高度 {{ listHeight }}
</Button>
<Button size="sm" variant="secondary" @click="appendItems">
追加 100 项
</Button>
<Button size="sm" variant="secondary" @click="selectFarItem">
选中并定位第 601 项
</Button>
</div>
<ListBox
id="virtual-listbox"
ref="listboxRef"
:items="filteredItems"
v-model="selected"
selection-mode="multiple"
virtualized
:row-height="rowHeight"
:estimated-row-height="fallbackRow"
:heading-height="headingHeight"
:loader-height="40"
:gap="8"
:padding="12"
:style="{
height: listHeight + 'px',
width: '320px',
overflowY: 'auto',
}"
aria-label="虚拟行验证列表"
>
<template #default="{ item }">
<ListBoxItem
:id="item.id"
:text-value="item.textValue"
style="height: 100%"
>
<div style="display: flex; flex-direction: column">
<Label>{{ item.textValue }}</Label>
<Description>高度随索引变化</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</template>
<template #loader>
<div
data-slot="virtual-loader"
style="display: flex; align-items: center; justify-content: center"
>
<Button size="sm" variant="ghost" @click="appendItems">
加载更多…
</Button>
</div>
</template>
</ListBox>
<p data-probe-selected>已选:{{ selected.join(",") || "无" }}</p>
</section>
<section style="display: grid; gap: 8px">
<h2>表格虚拟窗口(useVirtualRows)</h2>
<div style="display: flex; gap: 8px">
<Button size="sm" variant="secondary" @click="jumpToRow(150)">
定位第 150 行
</Button>
<Button
size="sm"
variant="secondary"
@click="tableCount = tableCount === 300 ? 600 : 300"
>
行数 {{ tableCount }}
</Button>
</div>
<Table>
<TableScrollContainer>
<div
ref="scrollRef"
data-slot="table-virtual-viewport"
style="height: 300px; width: 100%; overflow: auto"
>
<TableContent
aria-label="可变行高表格"
style="min-width: 520px; table-layout: fixed"
:aria-rowcount="tableCount + 1"
>
<TableHeader aria-rowindex="1">
<TableColumn id="name" is-row-header :min-width="160">
名称
</TableColumn>
<TableColumn id="info" :min-width="200">说明</TableColumn>
</TableHeader>
<TableBody>
<tr
aria-hidden="true"
:style="{ height: tableWindow.padTop + 'px' }"
/>
<TableRow
v-for="row in visibleTableRows"
:key="row.id"
:row-key="row.id"
:aria-rowindex="row.id + 1"
:style="{
height: `${tableRows.rowExtentOf(row.id - 1)}px`,
}"
>
<TableCell>{{ row.name }}</TableCell>
<TableCell>行高 44/72 交替</TableCell>
</TableRow>
<tr
aria-hidden="true"
:style="{ height: tableWindow.padBottom + 'px' }"
/>
</TableBody>
</TableContent>
</div>
</TableScrollContainer>
</Table>
</section>
</main>
</template>