fix(data-grid): preserve scroll when editing cells
This commit is contained in:
4 files changed
+69
-11
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { focusDataGridEditorWithoutScrolling, preserveDataGridScrollPosition } from "@/lib/dataGrid/dataGridEditorFocus";
|
||||
|
||||
describe("preserveDataGridScrollPosition", () => {
|
||||
it("restores the viewport after an editor is removed", () => {
|
||||
const scroller = { scrollLeft: 720, scrollTop: 180 };
|
||||
const restoreScroll = preserveDataGridScrollPosition(scroller);
|
||||
|
||||
scroller.scrollLeft = 0;
|
||||
scroller.scrollTop = 0;
|
||||
restoreScroll();
|
||||
|
||||
expect(scroller).toEqual({ scrollLeft: 720, scrollTop: 180 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("focusDataGridEditorWithoutScrolling", () => {
|
||||
it("prevents focus from moving the data grid viewport", () => {
|
||||
const scroller = { scrollLeft: 720, scrollTop: 180 };
|
||||
const input = {
|
||||
focus: vi.fn((options?: FocusOptions) => {
|
||||
expect(options).toEqual({ preventScroll: true });
|
||||
// Some WebViews still move a virtualized ancestor while mounting the editor.
|
||||
scroller.scrollLeft = 0;
|
||||
scroller.scrollTop = 0;
|
||||
}),
|
||||
};
|
||||
|
||||
focusDataGridEditorWithoutScrolling(input, scroller);
|
||||
|
||||
expect(input.focus).toHaveBeenCalledOnce();
|
||||
expect(scroller).toEqual({ scrollLeft: 720, scrollTop: 180 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
type DataGridEditorInput = Pick<HTMLElement, "focus">;
|
||||
type DataGridEditorScroller = Pick<HTMLElement, "scrollLeft" | "scrollTop">;
|
||||
|
||||
export function preserveDataGridScrollPosition(scroller?: DataGridEditorScroller | null) {
|
||||
if (!scroller) return () => {};
|
||||
const scrollLeft = scroller.scrollLeft;
|
||||
const scrollTop = scroller.scrollTop;
|
||||
return () => {
|
||||
if (scroller.scrollLeft !== scrollLeft) scroller.scrollLeft = scrollLeft;
|
||||
if (scroller.scrollTop !== scrollTop) scroller.scrollTop = scrollTop;
|
||||
};
|
||||
}
|
||||
|
||||
export function focusDataGridEditorWithoutScrolling(input: DataGridEditorInput, scroller?: DataGridEditorScroller | null) {
|
||||
const restoreScroll = preserveDataGridScrollPosition(scroller);
|
||||
|
||||
input.focus({ preventScroll: true });
|
||||
restoreScroll();
|
||||
}
|
||||
Reference in new issue
Block a user