Skip to content

Commit

Permalink
feat: support options
Browse files Browse the repository at this point in the history
  • Loading branch information
Wxh16144 committed Jul 13, 2023
1 parent e9b9f13 commit e94bac9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type DefaultColumnOptions<R, SR> = Pick<

export interface DataGridHandle {
element: HTMLDivElement | null;
scrollToCell: (position: PartialPosition) => void;
scrollToCell: (position: PartialPosition, scrollIntoViewOptions?: boolean | ScrollIntoViewOptions) => void;
selectCell: (position: Position, enableEditor?: Maybe<boolean>) => void;
}

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

/**
* computed values
Expand Down Expand Up @@ -441,13 +442,14 @@ function DataGrid<R, SR, K extends Key>(

useImperativeHandle(ref, () => ({
element: gridRef.current,
scrollToCell({ idx, rowIdx }) {
scrollToCell({ idx, rowIdx }, options) {
const scrollToIdx =
idx !== undefined && idx > lastFrozenColumnIndex && idx < columns.length ? idx : undefined;
const scrollToRowIdx =
rowIdx !== undefined && isRowIdxWithinViewportBounds(rowIdx) ? rowIdx : undefined;

if (scrollToIdx !== undefined || scrollToRowIdx !== undefined) {
scrollIntoViewOptions.current = options;
setScrollToPosition({ idx: scrollToIdx, rowIdx: scrollToRowIdx });
}
},
Expand Down Expand Up @@ -678,7 +680,11 @@ function DataGrid<R, SR, K extends Key>(
);
}

function selectCell(position: Position, enableEditor?: Maybe<boolean>): void {
function selectCell(
position: Position,
enableEditor?: Maybe<boolean>,
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions
): void {
if (!isCellWithinSelectionBounds(position)) return;
commitEditorChanges();

Expand All @@ -687,7 +693,7 @@ function DataGrid<R, SR, K extends Key>(
setSelectedPosition({ ...position, mode: 'EDIT', row, originalRow: row });
} else if (isSamePosition(selectedPosition, position)) {
// Avoid re-renders if the selected cell state is the same
scrollIntoView(getCellToScroll(gridRef.current!));
scrollIntoView(getCellToScroll(gridRef.current!), scrollIntoViewOptions);
} else {
shouldFocusCellRef.current = true;
setSelectedPosition({ ...position, mode: 'SELECT' });
Expand Down Expand Up @@ -1125,6 +1131,7 @@ function DataGrid<R, SR, K extends Key>(
scrollToPosition={scrollToPosition}
setScrollToCellPosition={setScrollToPosition}
gridElement={gridRef.current!}
scrollIntoViewOptions={scrollIntoViewOptions.current}
/>
)}
</div>
Expand Down
6 changes: 4 additions & 2 deletions src/ScrollToCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ export interface PartialPosition {
export default function ScrollToCell({
scrollToPosition: { idx, rowIdx },
gridElement,
setScrollToCellPosition
setScrollToCellPosition,
scrollIntoViewOptions
}: {
scrollToPosition: PartialPosition;
gridElement: HTMLDivElement;
setScrollToCellPosition: (cell: null) => void;
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions | undefined;
}) {
const ref = useRef<HTMLDivElement>(null);

useLayoutEffect(() => {
// scroll until the cell is completely visible
// this is needed if the grid has auto-sized columns
scrollIntoView(ref.current);
scrollIntoView(ref.current, scrollIntoViewOptions);
});

useLayoutEffect(() => {
Expand Down
13 changes: 11 additions & 2 deletions src/utils/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ export function stopPropagation(event: React.SyntheticEvent) {
event.stopPropagation();
}

export function scrollIntoView(element: Maybe<Element>) {
element?.scrollIntoView({ inline: 'nearest', block: 'nearest', behavior: 'smooth' });
export function scrollIntoView(
element: Maybe<Element>,
coverOptions?: boolean | ScrollIntoViewOptions
) {
let options: boolean | ScrollIntoViewOptions = { inline: 'nearest', block: 'nearest' };
if (typeof coverOptions === 'boolean') {
options = coverOptions;
} else if(typeof coverOptions === 'object') {
options = Object.assign(options, coverOptions);
}
element?.scrollIntoView(options);
}
2 changes: 1 addition & 1 deletion website/demos/ScrollToCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function ScrollToCell({ direction }: Props) {
}

function scrollToCell() {
gridRef.current!.scrollToCell({ idx, rowIdx });
gridRef.current!.scrollToCell({ idx, rowIdx }, { behavior: 'smooth' }); // smooth transition
}

return (
Expand Down

0 comments on commit e94bac9

Please sign in to comment.