fix(virtualization): measure natural rows and align viewport geometry

This commit is contained in:
maoyingjie
2026-10-29 17:13:12 +08:00
parent 9f7eb1fa8d
commit 5a00e25a25
18 changed files with 770 additions and 257 deletions
+94 -1
View File
@@ -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");
});