Skip to content

Commit e39a402

Browse files
committed
feat: support options
1 parent 8ad15eb commit e39a402

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

src/DataGrid.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type DefaultColumnOptions<R, SR> = Pick<
8585

8686
export interface DataGridHandle {
8787
element: HTMLDivElement | null;
88-
scrollToCell: (position: PartialPosition) => void;
88+
scrollToCell: (position: PartialPosition, scrollIntoViewOptions?: boolean | ScrollIntoViewOptions) => void;
8989
selectCell: (position: Position, enableEditor?: Maybe<boolean>) => void;
9090
}
9191

@@ -293,6 +293,7 @@ function DataGrid<R, SR, K extends Key>(
293293
const lastSelectedRowIdx = useRef(-1);
294294
const focusSinkRef = useRef<HTMLDivElement>(null);
295295
const shouldFocusCellRef = useRef(false);
296+
const scrollIntoViewOptions = useRef<boolean | ScrollIntoViewOptions>();
296297

297298
/**
298299
* computed values
@@ -441,13 +442,14 @@ function DataGrid<R, SR, K extends Key>(
441442

442443
useImperativeHandle(ref, () => ({
443444
element: gridRef.current,
444-
scrollToCell({ idx, rowIdx }) {
445+
scrollToCell({ idx, rowIdx }, options) {
445446
const scrollToIdx =
446447
idx !== undefined && idx > lastFrozenColumnIndex && idx < columns.length ? idx : undefined;
447448
const scrollToRowIdx =
448449
rowIdx !== undefined && isRowIdxWithinViewportBounds(rowIdx) ? rowIdx : undefined;
449450

450451
if (scrollToIdx !== undefined || scrollToRowIdx !== undefined) {
452+
scrollIntoViewOptions.current = options;
451453
setScrollToPosition({ idx: scrollToIdx, rowIdx: scrollToRowIdx });
452454
}
453455
},
@@ -678,7 +680,11 @@ function DataGrid<R, SR, K extends Key>(
678680
);
679681
}
680682

681-
function selectCell(position: Position, enableEditor?: Maybe<boolean>): void {
683+
function selectCell(
684+
position: Position,
685+
enableEditor?: Maybe<boolean>,
686+
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions
687+
): void {
682688
if (!isCellWithinSelectionBounds(position)) return;
683689
commitEditorChanges();
684690

@@ -687,7 +693,7 @@ function DataGrid<R, SR, K extends Key>(
687693
setSelectedPosition({ ...position, mode: 'EDIT', row, originalRow: row });
688694
} else if (isSamePosition(selectedPosition, position)) {
689695
// Avoid re-renders if the selected cell state is the same
690-
scrollIntoView(getCellToScroll(gridRef.current!));
696+
scrollIntoView(getCellToScroll(gridRef.current!), scrollIntoViewOptions);
691697
} else {
692698
shouldFocusCellRef.current = true;
693699
setSelectedPosition({ ...position, mode: 'SELECT' });
@@ -1125,6 +1131,7 @@ function DataGrid<R, SR, K extends Key>(
11251131
scrollToPosition={scrollToPosition}
11261132
setScrollToCellPosition={setScrollToPosition}
11271133
gridElement={gridRef.current!}
1134+
scrollIntoViewOptions={scrollIntoViewOptions.current}
11281135
/>
11291136
)}
11301137
</div>

src/ScrollToCell.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ export interface PartialPosition {
1111
export default function ScrollToCell({
1212
scrollToPosition: { idx, rowIdx },
1313
gridElement,
14-
setScrollToCellPosition
14+
setScrollToCellPosition,
15+
scrollIntoViewOptions
1516
}: {
1617
scrollToPosition: PartialPosition;
1718
gridElement: HTMLDivElement;
1819
setScrollToCellPosition: (cell: null) => void;
20+
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions | undefined;
1921
}) {
2022
const ref = useRef<HTMLDivElement>(null);
2123

2224
useLayoutEffect(() => {
2325
// scroll until the cell is completely visible
2426
// this is needed if the grid has auto-sized columns
25-
scrollIntoView(ref.current);
27+
scrollIntoView(ref.current, scrollIntoViewOptions);
2628
});
2729

2830
useLayoutEffect(() => {

src/utils/domUtils.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ export function stopPropagation(event: React.SyntheticEvent) {
44
event.stopPropagation();
55
}
66

7-
export function scrollIntoView(element: Maybe<Element>) {
8-
element?.scrollIntoView({ inline: 'nearest', block: 'nearest', behavior: 'smooth' });
7+
export function scrollIntoView(
8+
element: Maybe<Element>,
9+
coverOptions?: boolean | ScrollIntoViewOptions
10+
) {
11+
let options: boolean | ScrollIntoViewOptions = { inline: 'nearest', block: 'nearest' };
12+
if (typeof coverOptions === 'boolean') {
13+
options = coverOptions;
14+
} else if(typeof coverOptions === 'object') {
15+
options = Object.assign(options, coverOptions);
16+
}
17+
element?.scrollIntoView(options);
918
}

website/demos/ScrollToCell.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function ScrollToCell({ direction }: Props) {
4141
}
4242

4343
function scrollToCell() {
44-
gridRef.current!.scrollToCell({ idx, rowIdx });
44+
gridRef.current!.scrollToCell({ idx, rowIdx }, { behavior: 'smooth' }); // smooth transition
4545
}
4646

4747
return (

0 commit comments

Comments
 (0)