Skip to content

Commit

Permalink
Remove prevSelectedPosition state and one useLayoutEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmahajan7 committed Nov 11, 2024
1 parent 0604403 commit 0ab2234
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react';
import type { Key, KeyboardEvent, RefAttributes } from 'react';
import type { Key, KeyboardEvent, RefAttributes, SetStateAction } from 'react';
import { flushSync } from 'react-dom';
import clsx from 'clsx';

Expand Down Expand Up @@ -340,7 +340,6 @@ function DataGrid<R, SR, K extends Key>(
const [selectedPosition, setSelectedPosition] = useState(
(): SelectCellState | EditCellState<R> => ({ idx: -1, rowIdx: minRowIdx - 1, mode: 'SELECT' })
);
const [prevSelectedPosition, setPrevSelectedPosition] = useState(selectedPosition);

/**
* refs
Expand Down Expand Up @@ -479,23 +478,6 @@ function DataGrid<R, SR, K extends Key>(
/**
* effects
*/
useLayoutEffect(() => {
if (
!selectedCellIsWithinSelectionBounds ||
isSamePosition(selectedPosition, prevSelectedPosition)
) {
setPrevSelectedPosition(selectedPosition);
return;
}

setPrevSelectedPosition(selectedPosition);

if (focusSinkRef.current !== null && selectedPosition.idx === -1) {
focusSinkRef.current.focus({ preventScroll: true });
scrollIntoView(focusSinkRef.current);
}
}, [selectedCellIsWithinSelectionBounds, selectedPosition, prevSelectedPosition]);

useLayoutEffect(() => {
if (shouldFocusCell) {
setShouldFocusCell(false);
Expand All @@ -521,6 +503,25 @@ function DataGrid<R, SR, K extends Key>(
/**
* event handlers
*/
function onSelectedCellPositionChange(
position: SetStateAction<SelectCellState | EditCellState<R>>
) {
if (
focusSinkRef.current !== null &&
typeof position !== 'function' &&
position.idx === -1 &&
!isSamePosition(position, selectedPosition)
) {
flushSync(() => {
setSelectedPosition(position);
});
focusSinkRef.current.focus({ preventScroll: true });
scrollIntoView(focusSinkRef.current);
} else {
setSelectedPosition(position);
}
}

function selectHeaderRow(args: SelectHeaderRowEvent) {
if (!onSelectedRowsChange) return;

Expand Down Expand Up @@ -715,7 +716,7 @@ function DataGrid<R, SR, K extends Key>(
}

if (isCellEditable(selectedPosition) && isDefaultCellInput(event)) {
setSelectedPosition(({ idx, rowIdx }) => ({
onSelectedCellPositionChange(({ idx, rowIdx }) => ({
idx,
rowIdx,
mode: 'EDIT',
Expand Down Expand Up @@ -763,13 +764,13 @@ function DataGrid<R, SR, K extends Key>(
const samePosition = isSamePosition(selectedPosition, position);

if (enableEditor && isCellEditable(position)) {
setSelectedPosition({ ...position, mode: 'EDIT', row, originalRow: row });
onSelectedCellPositionChange({ ...position, mode: 'EDIT', row, originalRow: row });
} else if (samePosition) {
// Avoid re-renders if the selected cell state is the same
scrollIntoView(getCellToScroll(gridRef.current!));
} else {
setShouldFocusCell(true);
setSelectedPosition({ ...position, mode: 'SELECT' });
onSelectedCellPositionChange({ ...position, mode: 'SELECT' });
}

if (onSelectedCellChange && !samePosition) {
Expand Down Expand Up @@ -926,7 +927,7 @@ function DataGrid<R, SR, K extends Key>(

const closeEditor = (shouldFocusCell: boolean) => {
setShouldFocusCell(shouldFocusCell);
setSelectedPosition(({ idx, rowIdx }) => ({ idx, rowIdx, mode: 'SELECT' }));
onSelectedCellPositionChange(({ idx, rowIdx }) => ({ idx, rowIdx, mode: 'SELECT' }));
};

const onRowChange = (row: R, commitChanges: boolean, shouldFocusCell: boolean) => {
Expand All @@ -940,7 +941,7 @@ function DataGrid<R, SR, K extends Key>(
closeEditor(shouldFocusCell);
});
} else {
setSelectedPosition((position) => ({ ...position, row }));
onSelectedCellPositionChange((position) => ({ ...position, row }));
}
};

Expand Down

0 comments on commit 0ab2234

Please sign in to comment.