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:
@@ -9,6 +9,8 @@ import {
|
||||
TableHeader,
|
||||
TableRow,
|
||||
TableScrollContainer,
|
||||
bindVirtualRows,
|
||||
useVirtualRows,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
interface User {
|
||||
@@ -95,96 +97,92 @@ const users = generateUsers(1000);
|
||||
// Matches the fixed upstream Virtualizer layoutOptions (rowHeight/headingHeight 42).
|
||||
const ITEM_HEIGHT = 42;
|
||||
const rowKeys = users.map((user) => user.id);
|
||||
const VIEWPORT = 300;
|
||||
const OVERSCAN = 8;
|
||||
const scrollTop = ref(0);
|
||||
const scrollEl = ref<HTMLElement | null>(null);
|
||||
|
||||
const startIndex = computed(() =>
|
||||
Math.max(0, Math.floor(scrollTop.value / ITEM_HEIGHT) - OVERSCAN),
|
||||
const rows = useVirtualRows({
|
||||
count: () => users.length,
|
||||
rowHeight: ITEM_HEIGHT,
|
||||
overscan: 8,
|
||||
});
|
||||
const rowWindow = rows.window;
|
||||
const scrollRef = ref<HTMLElement | null>(null);
|
||||
const visible = computed(() =>
|
||||
users.slice(rowWindow.value.start, rowWindow.value.end),
|
||||
);
|
||||
const endIndex = computed(() =>
|
||||
Math.min(
|
||||
users.length,
|
||||
Math.ceil((scrollTop.value + VIEWPORT) / ITEM_HEIGHT) + OVERSCAN,
|
||||
),
|
||||
);
|
||||
const visible = computed(() => users.slice(startIndex.value, endIndex.value));
|
||||
const padTop = computed(() => startIndex.value * ITEM_HEIGHT);
|
||||
const padBottom = computed(() => (users.length - endIndex.value) * ITEM_HEIGHT);
|
||||
|
||||
// The scrolling container is the viewport wrapper: a native table cannot be
|
||||
// one (its height is a minimum and overflow needs a definite height — the
|
||||
// pinned source scrolls its virtual grid, a non-table element).
|
||||
const scrollElement = () => scrollRef.value ?? null;
|
||||
function focusRow(_key: string | number, index: number) {
|
||||
const node = scrollEl.value;
|
||||
if (!node) return;
|
||||
const top = index * ITEM_HEIGHT;
|
||||
if (top < node.scrollTop) node.scrollTop = top;
|
||||
else if (top + ITEM_HEIGHT > node.scrollTop + VIEWPORT)
|
||||
node.scrollTop = top + ITEM_HEIGHT - VIEWPORT;
|
||||
scrollTop.value = node.scrollTop;
|
||||
}
|
||||
function onScroll(event: Event) {
|
||||
scrollTop.value = (event.target as HTMLElement).scrollTop;
|
||||
const element = scrollElement();
|
||||
if (element) rows.scrollToIndex(element, index);
|
||||
}
|
||||
let detach: (() => void) | undefined;
|
||||
onMounted(() => {
|
||||
scrollEl.value?.addEventListener("scroll", onScroll, { passive: true });
|
||||
});
|
||||
onUnmounted(() => {
|
||||
scrollEl.value?.removeEventListener("scroll", onScroll);
|
||||
const element = scrollElement();
|
||||
if (element) detach = bindVirtualRows(rows, element);
|
||||
});
|
||||
onUnmounted(() => detach?.());
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Table>
|
||||
<div
|
||||
ref="scrollEl"
|
||||
class="h-[300px] w-full overflow-auto"
|
||||
data-slot="table-virtual-viewport"
|
||||
>
|
||||
<TableContent
|
||||
aria-label="1000 行虚拟化表格"
|
||||
class="min-w-[700px] table-fixed"
|
||||
:row-keys="rowKeys"
|
||||
:aria-rowcount="users.length + 1"
|
||||
@focus-row="focusRow"
|
||||
<TableScrollContainer>
|
||||
<div
|
||||
ref="scrollRef"
|
||||
class="h-[300px] w-full overflow-auto"
|
||||
data-slot="table-virtual-viewport"
|
||||
>
|
||||
<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' }" />
|
||||
<TableRow
|
||||
v-for="user in visible"
|
||||
:key="user.id"
|
||||
:row-key="user.id"
|
||||
:aria-rowindex="user.id + 1"
|
||||
:style="{ height: ITEM_HEIGHT + 'px' }"
|
||||
>
|
||||
<TableCell
|
||||
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
<TableContent
|
||||
aria-label="1000 行虚拟化表格"
|
||||
class="min-w-[700px] table-fixed"
|
||||
:row-keys="rowKeys"
|
||||
:aria-rowcount="users.length + 1"
|
||||
@focus-row="focusRow"
|
||||
>
|
||||
<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: rowWindow.padTop + 'px' }"
|
||||
/>
|
||||
<TableRow
|
||||
v-for="user in visible"
|
||||
:key="user.id"
|
||||
:row-key="user.id"
|
||||
:aria-rowindex="user.id + 1"
|
||||
:style="{ height: ITEM_HEIGHT + 'px' }"
|
||||
>
|
||||
{{ user.name }}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
:style="{ height: ITEM_HEIGHT + 'px' }"
|
||||
>
|
||||
{{ user.role }}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
:style="{ height: ITEM_HEIGHT + 'px' }"
|
||||
>
|
||||
{{ user.email }}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
<tr aria-hidden="true" :style="{ height: padBottom + 'px' }" />
|
||||
</TableBody>
|
||||
</TableContent>
|
||||
</div>
|
||||
<TableCell
|
||||
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
:style="{ height: ITEM_HEIGHT + 'px' }"
|
||||
>
|
||||
{{ user.name }}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
:style="{ height: ITEM_HEIGHT + 'px' }"
|
||||
>
|
||||
{{ user.role }}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
||||
:style="{ height: ITEM_HEIGHT + 'px' }"
|
||||
>
|
||||
{{ user.email }}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
<tr
|
||||
aria-hidden="true"
|
||||
:style="{ height: rowWindow.padBottom + 'px' }"
|
||||
/>
|
||||
</TableBody>
|
||||
</TableContent>
|
||||
</div>
|
||||
</TableScrollContainer>
|
||||
</Table>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user