fix(virtualization): measure natural rows and align viewport geometry
This commit is contained in:
Vendored
+74
-17
@@ -107,9 +107,13 @@ 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 unknownTableRows = ref(false);
|
||||
const tableWidth = ref(800);
|
||||
const tableRowHeight = (index: number) =>
|
||||
unknownTableRows.value ? undefined : index % 2 ? 44 : 72;
|
||||
const tableRows = useVirtualRows({
|
||||
count: () => tableCount.value,
|
||||
keys: () => Array.from({ length: tableCount.value }, (_, i) => i + 1),
|
||||
rowHeight: tableRowHeight,
|
||||
estimatedRowHeight: fallbackRow,
|
||||
loaderHeight: 40,
|
||||
@@ -136,7 +140,10 @@ function jumpToRow(index: number) {
|
||||
let detach: (() => void) | undefined;
|
||||
onMounted(() => {
|
||||
const element = scrollElement();
|
||||
if (element) detach = bindVirtualRows(tableRows, element);
|
||||
if (element)
|
||||
detach = bindVirtualRows(tableRows, element, {
|
||||
content: element.querySelector("tbody") ?? undefined,
|
||||
});
|
||||
});
|
||||
onUnmounted(() => detach?.());
|
||||
</script>
|
||||
@@ -183,7 +190,8 @@ onUnmounted(() => detach?.());
|
||||
virtualized
|
||||
:row-height="resolvedRowHeight"
|
||||
:estimated-row-height="fallbackRow"
|
||||
:heading-height="headingHeight"
|
||||
:heading-height="estimatedRows ? () => undefined : headingHeight"
|
||||
:estimated-heading-height="32"
|
||||
:loader-height="40"
|
||||
:gap="8"
|
||||
:padding="12"
|
||||
@@ -277,7 +285,13 @@ onUnmounted(() => detach?.());
|
||||
<section style="display: grid; gap: 8px">
|
||||
<h2>表格虚拟窗口(useVirtualRows)</h2>
|
||||
<div style="display: flex; gap: 8px">
|
||||
<Button size="sm" variant="secondary" @click="jumpToRow(150)">
|
||||
<Button @click="unknownTableRows = !unknownTableRows">
|
||||
切换表格未知行高
|
||||
</Button>
|
||||
<Button @click="tableWidth = tableWidth === 800 ? 520 : 800">
|
||||
表格宽度 {{ tableWidth }}
|
||||
</Button>
|
||||
<Button size="sm" variant="secondary" @click="jumpToRow(149)">
|
||||
定位第 150 行
|
||||
</Button>
|
||||
<Button
|
||||
@@ -293,12 +307,18 @@ onUnmounted(() => detach?.());
|
||||
<div
|
||||
ref="scrollRef"
|
||||
data-slot="table-virtual-viewport"
|
||||
style="height: 300px; width: 100%; overflow: auto"
|
||||
:style="{
|
||||
height: '300px',
|
||||
width: `${tableWidth}px`,
|
||||
overflow: 'auto',
|
||||
}"
|
||||
>
|
||||
<TableContent
|
||||
aria-label="可变行高表格"
|
||||
style="min-width: 520px; table-layout: fixed"
|
||||
:aria-rowcount="tableCount + 1"
|
||||
:row-keys="tableItems.map((row) => row.id)"
|
||||
@focus-row="(_key, index) => jumpToRow(index)"
|
||||
>
|
||||
<TableHeader aria-rowindex="1">
|
||||
<TableColumn id="name" is-row-header :min-width="160">
|
||||
@@ -311,22 +331,59 @@ onUnmounted(() => detach?.());
|
||||
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>
|
||||
<template v-for="row in visibleTableRows" :key="row.id">
|
||||
<TableRow
|
||||
:row-key="row.id"
|
||||
:aria-rowindex="row.id + 1"
|
||||
:ref="(node) => tableRows.measureRow(row.id - 1, node)"
|
||||
:height="tableRowHeight(row.id - 1)"
|
||||
>
|
||||
<TableCell>{{ row.name }}</TableCell>
|
||||
<TableCell>
|
||||
{{
|
||||
unknownTableRows
|
||||
? "需要测量的表格内容,宽度改变后会重新换行。".repeat(
|
||||
10,
|
||||
)
|
||||
: "行高 44/72 交替"
|
||||
}}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
<tr
|
||||
v-if="row.id < tableCount"
|
||||
aria-hidden="true"
|
||||
style="height: 4px"
|
||||
/>
|
||||
</template>
|
||||
<tr
|
||||
v-if="tableWindow.end < tableCount"
|
||||
aria-hidden="true"
|
||||
:style="{ height: tableWindow.padBottom + 'px' }"
|
||||
/>
|
||||
<template v-else>
|
||||
<tr
|
||||
v-if="tableCount"
|
||||
aria-hidden="true"
|
||||
style="height: 4px"
|
||||
/>
|
||||
<tr role="presentation">
|
||||
<td colspan="2" style="padding: 0">
|
||||
<div
|
||||
role="status"
|
||||
data-probe-table-loader
|
||||
style="
|
||||
height: 40px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
"
|
||||
>
|
||||
加载更多…
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr aria-hidden="true" style="height: 12px" />
|
||||
</template>
|
||||
</TableBody>
|
||||
</TableContent>
|
||||
</div>
|
||||
|
||||
@@ -260,8 +260,8 @@
|
||||
]
|
||||
},
|
||||
"virtual-rows": {
|
||||
"exports": ["useVirtualRows", "bindVirtualRows"],
|
||||
"css": ["data-virtualized", "[data-slot=list-box][data-virtualized]"]
|
||||
"exports": ["useVirtualRows", "bindVirtualRows", "TableRow"],
|
||||
"css": ["data-virtualized", "[data-slot=list-box][data-virtualized]", "hv-table__fixed-cell-content"]
|
||||
},
|
||||
"list-box": {
|
||||
"exports": [
|
||||
|
||||
@@ -9,12 +9,13 @@ import {
|
||||
type UseVirtualRowsOptions,
|
||||
type VirtualRowHeight,
|
||||
type VirtualRowWindow,
|
||||
type VirtualRowsBindingOptions,
|
||||
type TableRowProps,
|
||||
} from "@heroui-vue/components";
|
||||
|
||||
// ---- row geometry API (px units; see the ListLayout/TableLayout mapping) ----
|
||||
const fixed: VirtualRowHeight = 50;
|
||||
const variable: VirtualRowHeight = (index: number) =>
|
||||
index % 2 ? 44 : 72;
|
||||
const variable: VirtualRowHeight = (index: number) => (index % 2 ? 44 : 72);
|
||||
const partial: VirtualRowHeight = (index: number) =>
|
||||
index % 2 ? undefined : 40;
|
||||
const options: UseVirtualRowsOptions = {
|
||||
@@ -33,7 +34,15 @@ const offset: number = rows.rowOffsetOf(10);
|
||||
const extent: number = rows.rowExtentOf(10);
|
||||
rows.update(0, 300);
|
||||
declare const element: HTMLElement;
|
||||
const detach: () => void = bindVirtualRows(rows, element);
|
||||
const binding: VirtualRowsBindingOptions = {
|
||||
content: element,
|
||||
scrollPaddingStart: () => 48,
|
||||
};
|
||||
const detach: () => void = bindVirtualRows(rows, element, binding);
|
||||
rows.measureRow(0, element);
|
||||
rows.resetMeasurements();
|
||||
const fixedRow: TableRowProps = { rowKey: 0, height: 42 };
|
||||
void fixedRow;
|
||||
rows.scrollToIndex(element, 10);
|
||||
|
||||
// @ts-expect-error Row heights are px numbers or index functions, not strings
|
||||
@@ -52,7 +61,8 @@ const listProps: ListBoxProps = {
|
||||
virtualized: true,
|
||||
rowHeight: (index: number) => (index % 2 ? 64 : 40),
|
||||
estimatedRowHeight: 50,
|
||||
headingHeight: 32,
|
||||
headingHeight: () => undefined,
|
||||
estimatedHeadingHeight: 32,
|
||||
loaderHeight: 40,
|
||||
gap: 8,
|
||||
padding: 12,
|
||||
@@ -82,3 +92,7 @@ void [
|
||||
badHeading,
|
||||
badRowFn,
|
||||
];
|
||||
|
||||
// @ts-expect-error Native row height uses px numbers
|
||||
const badTableRow: TableRowProps = { rowKey: 1, height: "42px" };
|
||||
void badTableRow;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { defineComponent, h, nextTick, ref, type PropType } from "vue";
|
||||
import {
|
||||
@@ -6,6 +6,14 @@ import {
|
||||
ListBoxItem,
|
||||
ListBoxItemIndicator,
|
||||
useVirtualRows,
|
||||
bindVirtualRows,
|
||||
Table,
|
||||
TableContent,
|
||||
TableHeader,
|
||||
TableColumn,
|
||||
TableBody,
|
||||
TableRow,
|
||||
TableCell,
|
||||
} from "../packages/vue/src";
|
||||
import type { ListBoxDataItem } from "../packages/vue/src/components/list-box/context";
|
||||
|
||||
@@ -313,3 +321,88 @@ it("review: numeric and string keys preserve local row identity on reorder", asy
|
||||
"number:0/number:0",
|
||||
]);
|
||||
});
|
||||
|
||||
it("measures unknown rows by typed key and preserves the scroll anchor", async () => {
|
||||
const keys = ref<(number | string)[]>(
|
||||
Array.from({ length: 20 }, (_, i) => i),
|
||||
);
|
||||
const rows = useVirtualRows({
|
||||
count: 20,
|
||||
keys,
|
||||
rowHeight: () => undefined,
|
||||
estimatedRowHeight: 50,
|
||||
overscan: 0,
|
||||
});
|
||||
const viewport = document.createElement("div");
|
||||
document.body.append(viewport);
|
||||
Object.defineProperty(viewport, "clientHeight", { value: 100 });
|
||||
viewport.scrollTop = 300;
|
||||
const detach = bindVirtualRows(rows, viewport);
|
||||
const node = document.createElement("div");
|
||||
viewport.append(node);
|
||||
vi.spyOn(node, "getBoundingClientRect").mockReturnValue({
|
||||
height: 100,
|
||||
} as DOMRect);
|
||||
rows.measureRow(0, node);
|
||||
await nextTick();
|
||||
expect(rows.rowExtentOf(0)).toBe(100);
|
||||
expect(viewport.scrollTop).toBe(350);
|
||||
expect(rows.window.value.start).toBe(6);
|
||||
keys.value = ["0", ...keys.value.slice(1)];
|
||||
expect(rows.rowExtentOf(0)).toBe(50); // string zero must not reuse numeric zero's size
|
||||
detach();
|
||||
viewport.remove();
|
||||
rows.measureRow(0, null);
|
||||
await nextTick();
|
||||
});
|
||||
|
||||
it("scroll positioning includes the body origin and sticky occlusion", () => {
|
||||
const rows = useVirtualRows({ count: 30, rowHeight: 50 });
|
||||
const viewport = document.createElement("div"),
|
||||
body = document.createElement("div");
|
||||
Object.defineProperty(viewport, "clientHeight", { value: 200 });
|
||||
vi.spyOn(viewport, "getBoundingClientRect").mockImplementation(
|
||||
() => ({ top: 0 }) as DOMRect,
|
||||
);
|
||||
vi.spyOn(body, "getBoundingClientRect").mockImplementation(
|
||||
() => ({ top: 36 - viewport.scrollTop }) as DOMRect,
|
||||
);
|
||||
const detach = bindVirtualRows(rows, viewport, {
|
||||
content: body,
|
||||
scrollPaddingStart: 40,
|
||||
});
|
||||
rows.scrollToIndex(viewport, 10);
|
||||
expect(viewport.scrollTop).toBe(386); // 36 + row top500 + height50 - viewport200
|
||||
rows.scrollToIndex(viewport, 0);
|
||||
expect(viewport.scrollTop).toBe(0);
|
||||
detach();
|
||||
});
|
||||
|
||||
it("explicit TableRow height uses a content box without changing ordinary cells", () => {
|
||||
const root = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, () =>
|
||||
h(TableContent, () => [
|
||||
h(TableHeader, () =>
|
||||
h(TableColumn, { isRowHeader: true }, () => "Header"),
|
||||
),
|
||||
h(TableBody, () => [
|
||||
h(TableRow, { rowKey: 1, height: 42 }, () =>
|
||||
h(TableCell, () => "Fixed"),
|
||||
),
|
||||
h(TableRow, { rowKey: 2 }, () => h(TableCell, () => "Natural")),
|
||||
]),
|
||||
]),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(root);
|
||||
expect(root.findAll('[data-slot="table-cell"]')[0].attributes("scope")).toBe(
|
||||
"row",
|
||||
);
|
||||
expect(root.findAll(".hv-table__fixed-cell-content")).toHaveLength(1);
|
||||
expect(
|
||||
root.findAll('[data-slot="table-row"]')[1].attributes("style") ?? "",
|
||||
).not.toContain("height");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user