Skip to content

Commit

Permalink
feat: improve restoring scroll position in infinite tables
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Sep 20, 2024
1 parent 543df4a commit 89e483d
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/components/InfiniteTable/InfiniteTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
} = props;

const gridRef = useRef<AgGridReact>(null);
const firstTimeDataLoaded = useRef(true);
const firstTimeOnBodyScroll = useRef(true);
const allRowSelectedModeRef = useRef<boolean>(false);
const containerRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -265,6 +266,13 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
totalRows,
]);

const scrollToSavedPosition = useCallback(() => {
const firstVisibleRowIndex = onGetFirstVisibleRowIndex?.();
if (firstVisibleRowIndex && gridRef.current?.api) {
gridRef.current.api.ensureIndexVisible(firstVisibleRowIndex, "top");
}
}, [onGetFirstVisibleRowIndex]);

const getRows = useCallback(
async (params: IGetRowsParams) => {
gridRef.current?.api.showLoadingOverlay();
Expand Down Expand Up @@ -321,13 +329,18 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
}
}
gridRef.current?.api.hideOverlay();
if (firstTimeDataLoaded.current) {
firstTimeDataLoaded.current = false;
scrollToSavedPosition();
}
},
[
getSortedFields,
hasStatusColumn,
onGetSelectedRowKeys,
onRequestData,
onRowStatus,
scrollToSavedPosition,
selectedRowKeysPendingToRender,
setSelectedRowKeysPendingToRender,
],
Expand All @@ -350,16 +363,6 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
[onRowDoubleClick],
);

const onFirstDataRendered = useCallback(
(params: FirstDataRenderedEvent) => {
const firstVisibleRowIndex = onGetFirstVisibleRowIndex?.();
if (firstVisibleRowIndex) {
params.api.ensureIndexVisible(firstVisibleRowIndex, "top");
}
},
[onGetFirstVisibleRowIndex],
);

const onBodyScroll = useCallback(
(params: BodyScrollEvent) => {
if (!firstTimeOnBodyScroll.current) {
Expand Down Expand Up @@ -407,7 +410,6 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
infiniteInitialRowCount={50}
maxBlocksInCache={10}
onGridReady={onGridReady}
onFirstDataRendered={onFirstDataRendered}
onBodyScroll={onBodyScroll}
blockLoadDebounceMillis={DEBOUNCE_TIME}
suppressDragLeaveHidesColumns={true}
Expand Down

0 comments on commit 89e483d

Please sign in to comment.