Skip to content

Commit

Permalink
[DataGrid] Use binary search to find column indexes in virtualization (
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Jun 18, 2021
1 parent f0324fe commit a5974e7
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { useGridApiMethod } from '../../root/useGridApiMethod';
import { useGridApiEventHandler } from '../../root/useGridApiEventHandler';
import {
gridColumnsMetaSelector,
visibleGridColumnsLengthSelector,
visibleGridColumnsSelector,
} from '../columns/gridColumnsSelector';
import { useGridSelector } from '../core/useGridSelector';
Expand All @@ -27,6 +26,28 @@ type UseVirtualColumnsReturnType = [
UpdateRenderedColsFnType,
];

// Uses binary search to avoid looping through all possible positions
function getIdxFromScroll(
offset: number,
positions: number[],
sliceStart = 0,
sliceEnd = positions.length,
): number {
if (positions.length <= 0) {
return -1;
}

if (sliceStart >= sliceEnd) {
return sliceStart;
}

const pivot = sliceStart + Math.floor((sliceEnd - sliceStart) / 2);
const itemOffset = positions[pivot];
return offset <= itemOffset
? getIdxFromScroll(offset, positions, sliceStart, pivot)
: getIdxFromScroll(offset, positions, pivot + 1, sliceEnd);
}

export const useGridVirtualColumns = (
options: GridOptions,
apiRef: GridApiRef,
Expand All @@ -37,21 +58,11 @@ export const useGridVirtualColumns = (
const containerPropsRef = React.useRef<GridContainerProps | null>(null);
const lastScrollLeftRef = React.useRef<number>(0);
const columnsMeta = useGridSelector(apiRef, gridColumnsMetaSelector);
const visibleColumnCount = useGridSelector(apiRef, visibleGridColumnsLengthSelector);
const visibleColumns = useGridSelector(apiRef, visibleGridColumnsSelector);

const getColumnIdxFromScroll = React.useCallback(
(left: number) => {
const positions = columnsMeta.positions;

if (!visibleColumnCount) {
return -1;
}
let colIdx = [...positions].reverse().findIndex((p) => left >= p);
colIdx = positions.length - 1 - colIdx;
return colIdx;
},
[columnsMeta.positions, visibleColumnCount],
(left: number) => getIdxFromScroll(left, columnsMeta.positions),
[columnsMeta.positions],
);

const getColumnFromScroll = React.useCallback(
Expand Down

0 comments on commit a5974e7

Please sign in to comment.