Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(collections/table): keep rows visible after a column rebuild
Elian Doran271a05c56205
diff --git a/apps/client/src/widgets/collections/table/tabulator.spec.tsx b/apps/client/src/widgets/collections/table/tabulator.spec.tsxnew file mode 100644index 0000000000..a59d9ce396--- /dev/null+++ b/apps/client/src/widgets/collections/table/tabulator.spec.tsx@@ -0,0 +1,40 @@+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, describe, expect, it, vi } from "vitest";++import Tabulator from "./tabulator";++const calls: string[] = [];++vi.mock("tabulator-tables", () => ({+ Tabulator: class {+ static registerModule() {}+ on(event: string, callback: () => void) {+ if (event === "tableBuilt") callback();+ }+ off() {}+ destroy() {}+ setColumns() { calls.push("setColumns"); }+ replaceData() { calls.push("replaceData"); }+ }+}));++describe("Tabulator", () => {+ const container = document.createElement("div");++ afterEach(() => {+ render(null, container);+ calls.length = 0;+ });++ it("sets new columns before new data, so the rows render after the scroll resets", async () => {+ await act(async () => render(<Tabulator columns={[ { title: "A", field: "a" } ]} data={[ { a: 1 } ]} />, container));+ calls.length = 0;++ // `setColumns()` scrolls to the top at once, while `replaceData()` renders in position a+ // microtask later: the other way round, it renders rows for a scroll offset since reset.+ await act(async () => render(<Tabulator columns={[ { title: "B", field: "b" } ]} data={[ { b: 1 } ]} />, container));++ expect(calls).toEqual([ "setColumns", "replaceData" ]);+ });+});diff --git a/apps/client/src/widgets/collections/table/tabulator.tsx b/apps/client/src/widgets/collections/table/tabulator.tsxindex d923866657..456eb46ed4 100644--- a/apps/client/src/widgets/collections/table/tabulator.tsx+++ b/apps/client/src/widgets/collections/table/tabulator.tsx@@ -69,13 +69,15 @@ export default function Tabulator<T extends {}>({ className, columns, data, modu};}, Object.values(events ?? {}));- // Change in data. replaceData rather than setData: it renders in position instead of- // resetting the scroll (see resetScroll in Tabulator's RowManager).- useEffect(() => { tabulatorRef.current?.replaceData(data); }, [ data ]);+ // Columns before data: `setColumns()` resets the scroll at once, while `replaceData()` renders+ // in position a microtask later, so set the other way round it renders at the old offset.useEffect(() => {if (!columns) return;tabulatorRef.current?.setColumns(columns);}, [ columns ]);+ // Change in data. replaceData rather than setData: it renders in position instead of+ // resetting the scroll (see resetScroll in Tabulator's RowManager).+ useEffect(() => { tabulatorRef.current?.replaceData(data); }, [ data ]);return (<div ref={containerRef} className={className} />