Skip to content

Commit e196fae

Browse files
authored
fix: paginatedTable - calculate visible range on resize (#2248)
1 parent 1ad2ef1 commit e196fae

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/components/PaginatedTable/useScrollBasedChunks.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React from 'react';
22

3-
import {throttle} from 'lodash';
4-
5-
import {calculateElementOffsetTop} from './utils';
3+
import {calculateElementOffsetTop, rafThrottle} from './utils';
64

75
interface UseScrollBasedChunksProps {
86
parentRef: React.RefObject<HTMLElement>;
@@ -14,7 +12,6 @@ interface UseScrollBasedChunksProps {
1412
}
1513

1614
const DEFAULT_OVERSCAN_COUNT = 1;
17-
const THROTTLE_DELAY = 100;
1815

1916
export const useScrollBasedChunks = ({
2017
parentRef,
@@ -54,38 +51,45 @@ export const useScrollBasedChunks = ({
5451
return {start, end};
5552
}, [parentRef, tableRef, rowHeight, chunkSize, overscanCount, chunksCount]);
5653

57-
React.useEffect(() => {
54+
const updateVisibleChunks = React.useCallback(() => {
5855
const newRange = calculateVisibleRange();
59-
6056
if (newRange) {
6157
setStartChunk(newRange.start);
6258
setEndChunk(newRange.end);
6359
}
64-
}, [chunksCount, calculateVisibleRange]);
60+
}, [calculateVisibleRange]);
61+
62+
React.useEffect(() => {
63+
updateVisibleChunks();
64+
}, [chunksCount, updateVisibleChunks]);
6565

6666
const handleScroll = React.useCallback(() => {
67-
const newRange = calculateVisibleRange();
68-
if (newRange) {
69-
setStartChunk(newRange.start);
70-
setEndChunk(newRange.end);
71-
}
72-
}, [calculateVisibleRange]);
67+
updateVisibleChunks();
68+
}, [updateVisibleChunks]);
69+
70+
React.useEffect(() => {
71+
const throttledHandleZoom = rafThrottle(() => {
72+
updateVisibleChunks();
73+
});
74+
75+
window.addEventListener('resize', throttledHandleZoom);
76+
77+
return () => {
78+
window.removeEventListener('resize', throttledHandleZoom);
79+
};
80+
}, [updateVisibleChunks]);
7381

7482
React.useEffect(() => {
7583
const container = parentRef?.current;
7684
if (!container) {
7785
return undefined;
7886
}
7987

80-
const throttledHandleScroll = throttle(handleScroll, THROTTLE_DELAY, {
81-
leading: true,
82-
trailing: true,
83-
});
88+
const throttledHandleScroll = rafThrottle(handleScroll);
8489

8590
container.addEventListener('scroll', throttledHandleScroll);
8691
return () => {
8792
container.removeEventListener('scroll', throttledHandleScroll);
88-
throttledHandleScroll.cancel();
8993
};
9094
}, [handleScroll, parentRef]);
9195

0 commit comments

Comments
 (0)