Skip to content

Commit 3d459a8

Browse files
committed
fix: review fixes
1 parent cfc350d commit 3d459a8

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

src/components/PaginatedTable/PaginatedTable.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ export const PaginatedTable = <T, F>({
6767

6868
const tableRef = React.useRef<HTMLDivElement>(null);
6969

70-
const chunks = useScrollBasedChunks({
70+
const activeChunks = useScrollBasedChunks({
7171
parentRef,
7272
tableRef,
7373
totalItems: foundEntities,
7474
rowHeight,
7575
chunkSize,
7676
});
7777

78+
const lastChunkSize = React.useMemo(
79+
() => foundEntities % chunkSize || chunkSize,
80+
[foundEntities, chunkSize],
81+
);
82+
7883
const handleDataFetched = React.useCallback((total: number, found: number) => {
7984
setTotalEntities(total);
8085
setFoundEntities(found);
@@ -102,11 +107,11 @@ export const PaginatedTable = <T, F>({
102107
);
103108
}
104109

105-
return chunks.map((itemsCount, index) => (
110+
return activeChunks.map((isActive, index) => (
106111
<TableChunk<T, F>
107112
key={index}
108113
id={index}
109-
itemsCount={itemsCount}
114+
calculatedCount={index === activeChunks.length - 1 ? lastChunkSize : chunkSize}
110115
chunkSize={chunkSize}
111116
rowHeight={rowHeight}
112117
columns={columns}
@@ -117,7 +122,7 @@ export const PaginatedTable = <T, F>({
117122
getRowClassName={getRowClassName}
118123
renderErrorMessage={renderErrorMessage}
119124
onDataFetched={handleDataFetched}
120-
isActive={Boolean(itemsCount)}
125+
isActive={isActive}
121126
/>
122127
));
123128
};

src/components/PaginatedTable/TableChunk.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface TableChunkProps<T, F> {
1616
id: number;
1717
chunkSize: number;
1818
rowHeight: number;
19-
itemsCount: number;
19+
calculatedCount: number;
2020
columns: Column<T>[];
2121
filters?: F;
2222
sortParams?: SortParams;
@@ -33,7 +33,7 @@ interface TableChunkProps<T, F> {
3333
export const TableChunk = typedMemo(function TableChunk<T, F>({
3434
id,
3535
chunkSize,
36-
itemsCount,
36+
calculatedCount,
3737
rowHeight,
3838
columns,
3939
fetchData,
@@ -88,7 +88,7 @@ export const TableChunk = typedMemo(function TableChunk<T, F>({
8888
}
8989
}, [currentData, isActive, onDataFetched]);
9090

91-
const dataLength = currentData?.data?.length || itemsCount || chunkSize;
91+
const dataLength = currentData?.data?.length || calculatedCount;
9292

9393
const renderContent = () => {
9494
if (!isActive) {

src/components/PaginatedTable/useScrollBasedChunks.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const useScrollBasedChunks = ({
2323
rowHeight,
2424
chunkSize,
2525
overscanCount = DEFAULT_OVERSCAN_COUNT,
26-
}: UseScrollBasedChunksProps): number[] => {
26+
}: UseScrollBasedChunksProps): boolean[] => {
2727
const chunksCount = React.useMemo(
2828
() => Math.ceil(totalItems / chunkSize),
2929
[chunkSize, totalItems],
@@ -82,15 +82,11 @@ export const useScrollBasedChunks = ({
8282
}, [handleScroll, parentRef]);
8383

8484
return React.useMemo(() => {
85-
// 0 items represent inactive chunk
86-
const chunks = Array(chunksCount).fill(0);
87-
for (let i = startChunk; i < endChunk; i++) {
88-
chunks[i] = chunkSize;
85+
// boolean array that represents active chunks
86+
const activeChunks = Array(chunksCount).fill(false);
87+
for (let i = startChunk; i <= endChunk; i++) {
88+
activeChunks[i] = true;
8989
}
90-
91-
const lastChunkSize = totalItems % chunkSize || chunkSize;
92-
chunks[endChunk] = endChunk === chunksCount - 1 ? lastChunkSize : chunkSize;
93-
94-
return chunks;
95-
}, [chunksCount, startChunk, endChunk, totalItems, chunkSize]);
90+
return activeChunks;
91+
}, [chunksCount, startChunk, endChunk]);
9692
};

src/components/PaginatedTable/utils.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ export function calculateColumnWidth(newWidth: number, minWidth = 40, maxWidth =
2828

2929
export const typedMemo: <T>(Component: T) => T = React.memo;
3030

31+
/**
32+
* Calculates the total vertical offset (distance from top) of an element relative to its container
33+
* or the document body if no container is specified.
34+
*
35+
* This function traverses up through the DOM tree, accumulating offsetTop values
36+
* from each parent element until it reaches either the specified container or
37+
* the top of the document.
38+
*
39+
* @param element - The HTML element to calculate the offset for
40+
* @param container - Optional container element to stop the calculation at
41+
* @returns The total vertical offset in pixels
42+
*
43+
* Example:
44+
* const offset = calculateElementOffsetTop(myElement, myContainer);
45+
* // Returns the distance in pixels from myElement to the top of myContainer
46+
*/
3147
export function calculateElementOffsetTop(element: HTMLElement, container?: HTMLElement): number {
3248
let currentElement = element;
3349
let offsetTop = 0;

0 commit comments

Comments
 (0)