Skip to content

Commit f7ca2e8

Browse files
Tinteflukasmasuch
andauthored
Add ability to set scroll behavior when calling ref.scrollTo (#1004)
* feature/scroll-behavior: add ability to set scroll behavior when calling ref.scrollTo * feature/scroll-behavior: add ability to set scroll behavior when calling ref.appendRow * Fix unit tets * Update again --------- Co-authored-by: Lukas Masuch <[email protected]>
1 parent 49507eb commit f7ca2e8

File tree

5 files changed

+67
-29
lines changed

5 files changed

+67
-29
lines changed

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/API.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,12 @@ scrollTo: (
418418
row: number,
419419
dir?: "horizontal" | "vertical" | "both",
420420
paddingX?: number,
421-
paddingY?: number
421+
paddingY?: number,
422+
options?: {
423+
hAlign?: "start" | "center" | "end";
424+
vAlign?: "start" | "center" | "end";
425+
behavior?: ScrollBehavior; // "auto" | "smooth" | "instant"
426+
}
422427
) => void;
423428
```
424429

@@ -432,7 +437,11 @@ Requests the data grid to scroll to a particular location. If only one direction
432437
## appendRow
433438

434439
```ts
435-
appendRow: (col: number, openOverlay: boolean = true) => Promise<void>;
440+
appendRow: (
441+
col: number,
442+
openOverlay: boolean = true,
443+
behavior?: ScrollBehavior; // "auto" | "smooth" | "instant"
444+
) => Promise<void>;
436445
```
437446

438447
Appends a row to the data grid.

packages/core/src/data-editor/data-editor.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ type ScrollToFn = (
687687
options?: {
688688
hAlign?: "start" | "center" | "end";
689689
vAlign?: "start" | "center" | "end";
690+
behavior?: ScrollBehavior;
690691
}
691692
) => void;
692693

@@ -697,7 +698,7 @@ export interface DataEditorRef {
697698
* @param col The column index to focus in the new row.
698699
* @returns A promise which waits for the append to complete.
699700
*/
700-
appendRow: (col: number, openOverlay?: boolean) => Promise<void>;
701+
appendRow: (col: number, openOverlay?: boolean, behavior?: ScrollBehavior) => Promise<void>;
701702
/**
702703
* Triggers cells to redraw.
703704
*/
@@ -1611,10 +1612,11 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
16111612
scrollX /= scale;
16121613
scrollY /= scale;
16131614
}
1614-
scrollRef.current.scrollTo(
1615-
scrollX + scrollRef.current.scrollLeft,
1616-
scrollY + scrollRef.current.scrollTop
1617-
);
1615+
scrollRef.current.scrollTo({
1616+
left: scrollX + scrollRef.current.scrollLeft,
1617+
top: scrollY + scrollRef.current.scrollTop,
1618+
behavior: options?.behavior ?? "auto",
1619+
});
16181620
}
16191621
}
16201622
}
@@ -1641,7 +1643,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
16411643
getCellContentRef.current = getCellContent;
16421644
rowsRef.current = rows;
16431645
const appendRow = React.useCallback(
1644-
async (col: number, openOverlay: boolean = true): Promise<void> => {
1646+
async (col: number, openOverlay: boolean = true, behavior?: ScrollBehavior): Promise<void> => {
16451647
const c = mangledCols[col];
16461648
if (c?.trailingRowOptions?.disabled === true) {
16471649
return;
@@ -1667,7 +1669,7 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
16671669
}
16681670

16691671
const row = typeof r === "number" ? r : bottom ? rows : 0;
1670-
scrollToRef.current(col - rowMarkerOffset, row);
1672+
scrollToRef.current(col - rowMarkerOffset, row, "both", 0, 0, behavior ? { behavior } : undefined);
16711673
setCurrent(
16721674
{
16731675
cell: [col, row],

packages/core/src/docs/examples/imperative-scroll.stories.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface ImperativeScrollProps {
2828
paddingX: number;
2929
vAlign?: "start" | "center" | "end";
3030
hAlign?: "start" | "center" | "end";
31+
behavior?: "smooth" | "instant" | "auto";
3132
}
3233

3334
export const ImperativeScroll: React.VFC<ImperativeScrollProps> = p => {
@@ -39,6 +40,7 @@ export const ImperativeScroll: React.VFC<ImperativeScrollProps> = p => {
3940
ref.current?.scrollTo(4, 99, "both", p.paddingX, p.paddingY, {
4041
vAlign: p.vAlign,
4142
hAlign: p.hAlign,
43+
behavior: p.behavior,
4244
});
4345
};
4446

@@ -74,6 +76,7 @@ export const ImperativeScroll: React.VFC<ImperativeScrollProps> = p => {
7476
paddingX: 0,
7577
vAlign: "start",
7678
hAlign: "start",
79+
behavior: "auto",
7780
};
7881
(ImperativeScroll as any).argTypes = {
7982
paddingY: 0,
@@ -86,4 +89,8 @@ export const ImperativeScroll: React.VFC<ImperativeScrollProps> = p => {
8689
control: { type: "select" },
8790
options: ["start", "center", "end", undefined],
8891
},
92+
behavior: {
93+
control: { type: "select" },
94+
options: ["smooth", "instant", "auto", undefined],
95+
},
8996
};

packages/core/test/data-editor.test.tsx

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ describe("data-editor", () => {
13961396
clientX: 300, // Col B
13971397
clientY: 36 + 32 + 16, // Row 1 (0 indexed)
13981398
});
1399-
1399+
14001400
const testKeys = [
14011401
{
14021402
keyCode: 74,
@@ -1422,24 +1422,24 @@ describe("data-editor", () => {
14221422
keyCode: 222,
14231423
key: "'",
14241424
},
1425-
]
1425+
];
14261426

14271427
for (const key of testKeys) {
14281428
fireEvent.keyDown(canvas, key);
14291429
fireEvent.keyUp(canvas, key);
1430-
1430+
14311431
const overlay = screen.getByDisplayValue(key.key);
14321432
expect(document.body.contains(overlay)).toBe(true);
1433-
1433+
14341434
vi.useFakeTimers();
14351435
fireEvent.keyDown(overlay, {
14361436
key: "Escape",
14371437
});
1438-
1438+
14391439
act(() => {
14401440
vi.runAllTimers();
14411441
});
1442-
1442+
14431443
expect(document.body.contains(overlay)).toBe(false);
14441444
}
14451445
});
@@ -3731,7 +3731,11 @@ describe("data-editor", () => {
37313731
act(() => {
37323732
vi.runAllTimers();
37333733
});
3734-
expect(Element.prototype.scrollTo).toBeCalledWith(0, 15_101);
3734+
expect(Element.prototype.scrollTo).toBeCalledWith({
3735+
behavior: "auto",
3736+
left: 0,
3737+
top: 15_101,
3738+
});
37353739
});
37363740

37373741
test("Imperative scrollTo pixel", async () => {
@@ -3751,7 +3755,11 @@ describe("data-editor", () => {
37513755
act(() => {
37523756
vi.runAllTimers();
37533757
});
3754-
expect(Element.prototype.scrollTo).toBeCalledWith(0, 533);
3758+
expect(Element.prototype.scrollTo).toBeCalledWith({
3759+
behavior: "auto",
3760+
left: 0,
3761+
top: 533,
3762+
});
37553763
});
37563764

37573765
test("Imperative scrollTo pixel start", async () => {
@@ -3780,7 +3788,11 @@ describe("data-editor", () => {
37803788
act(() => {
37813789
vi.runAllTimers();
37823790
});
3783-
expect(Element.prototype.scrollTo).toBeCalledWith(0, 1464);
3791+
expect(Element.prototype.scrollTo).toBeCalledWith({
3792+
behavior: "auto",
3793+
left: 0,
3794+
top: 1464,
3795+
});
37843796
});
37853797

37863798
test("Imperative scrollTo pixel center", async () => {
@@ -3809,7 +3821,11 @@ describe("data-editor", () => {
38093821
act(() => {
38103822
vi.runAllTimers();
38113823
});
3812-
expect(Element.prototype.scrollTo).toBeCalledWith(0, 998.5);
3824+
expect(Element.prototype.scrollTo).toBeCalledWith({
3825+
behavior: "auto",
3826+
left: 0,
3827+
top: 998.5,
3828+
});
38133829
});
38143830

38153831
test("Imperative scrollTo pixel end", async () => {
@@ -3838,7 +3854,11 @@ describe("data-editor", () => {
38383854
act(() => {
38393855
vi.runAllTimers();
38403856
});
3841-
expect(Element.prototype.scrollTo).toBeCalledWith(0, 533);
3857+
expect(Element.prototype.scrollTo).toBeCalledWith({
3858+
behavior: "auto",
3859+
left: 0,
3860+
top: 533,
3861+
});
38423862
});
38433863

38443864
test("Imperative damage gets right cell", async () => {

0 commit comments

Comments
 (0)