Skip to content

Commit f96ce3e

Browse files
committed
fix: small refactor
1 parent 4494e98 commit f96ce3e

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

src/components/PaginatedTable/PaginatedTable.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const PaginatedTable = <T, F>({
7171

7272
const tableRef = React.useRef<HTMLDivElement>(null);
7373

74-
const activeChunks = useScrollBasedChunks({
74+
const chunks = useScrollBasedChunks({
7575
parentRef,
7676
tableRef,
7777
totalItems: foundEntities,
@@ -109,12 +109,11 @@ export const PaginatedTable = <T, F>({
109109
);
110110
}
111111

112-
const totalItemsCount = foundEntities || chunkSize;
113-
return activeChunks.map((isActive, index) => (
112+
return chunks.map((itemsCount, index) => (
114113
<TableChunk<T, F>
115114
key={index}
116115
id={index}
117-
totalItemsCount={totalItemsCount}
116+
itemsCount={itemsCount}
118117
chunkSize={chunkSize}
119118
rowHeight={rowHeight}
120119
columns={columns}
@@ -125,7 +124,7 @@ export const PaginatedTable = <T, F>({
125124
getRowClassName={getRowClassName}
126125
renderErrorMessage={renderErrorMessage}
127126
onDataFetched={handleDataFetched}
128-
isActive={isActive}
127+
isActive={Boolean(itemsCount)}
129128
/>
130129
));
131130
};

src/components/PaginatedTable/TableChunk.tsx

Lines changed: 4 additions & 11 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-
totalItemsCount: number;
19+
itemsCount: 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-
totalItemsCount,
36+
itemsCount,
3737
rowHeight,
3838
columns,
3939
fetchData,
@@ -88,12 +88,7 @@ export const TableChunk = typedMemo(function TableChunk<T, F>({
8888
}
8989
}, [currentData, isActive, onDataFetched]);
9090

91-
const chunkOffset = id * chunkSize;
92-
const remainingLength = totalItemsCount - chunkOffset;
93-
const calculatedChunkLength =
94-
remainingLength < chunkSize && remainingLength > 0 ? remainingLength : chunkSize;
95-
96-
const dataLength = currentData?.data?.length || calculatedChunkLength;
91+
const dataLength = currentData?.data?.length || itemsCount || chunkSize;
9792

9893
const renderContent = () => {
9994
if (!isActive) {
@@ -136,13 +131,11 @@ export const TableChunk = typedMemo(function TableChunk<T, F>({
136131
));
137132
};
138133

139-
const chunkHeight = dataLength ? dataLength * rowHeight : chunkSize * rowHeight;
140-
141134
return (
142135
<tbody
143136
id={id.toString()}
144137
style={{
145-
height: `${chunkHeight}px`,
138+
height: `${dataLength * rowHeight}px`,
146139
// Default display: table-row-group doesn't work in Safari and breaks the table
147140
// display: block works in Safari, but disconnects thead and tbody cell grids
148141
// Hack to make it work in all cases

src/components/PaginatedTable/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ export const DEFAULT_SORT_ORDER = DESCENDING;
1313
// Time in ms after which request will be sent
1414
export const DEFAULT_REQUEST_TIMEOUT = 200;
1515

16-
export const DEFAULT_TABLE_ROW_HEIGHT = 40;
16+
export const DEFAULT_TABLE_ROW_HEIGHT = 41;
1717

1818
export const DEFAULT_INTERSECTION_OBSERVER_MARGIN = '100%';

src/components/PaginatedTable/useScrollBasedChunks.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from 'react';
22

33
import {throttle} from 'lodash';
44

5+
import {getArray} from '../../utils';
6+
57
import {calculateElementOffsetTop} from './utils';
68

79
interface UseScrollBasedChunksProps {
@@ -23,12 +25,20 @@ export const useScrollBasedChunks = ({
2325
rowHeight,
2426
chunkSize,
2527
overscanCount = DEFAULT_OVERSCAN_COUNT,
26-
}: UseScrollBasedChunksProps): boolean[] => {
28+
}: UseScrollBasedChunksProps): number[] => {
2729
const chunksCount = React.useMemo(
2830
() => Math.ceil(totalItems / chunkSize),
2931
[chunkSize, totalItems],
3032
);
3133

34+
const chunkLengths = React.useMemo(
35+
() =>
36+
getArray(chunksCount).map((value) =>
37+
Math.min(chunkSize, totalItems - value * chunkSize),
38+
),
39+
[chunkSize, chunksCount, totalItems],
40+
);
41+
3242
const [startChunk, setStartChunk] = React.useState(0);
3343
const [endChunk, setEndChunk] = React.useState(
3444
Math.min(overscanCount, Math.max(chunksCount - 1, 0)),
@@ -82,12 +92,12 @@ export const useScrollBasedChunks = ({
8292
}, [handleScroll, parentRef]);
8393

8494
return React.useMemo(() => {
85-
// Create boolean array where true represents active chunks
86-
const activeChunks = Array(chunksCount).fill(false);
95+
// 0 items represent inactive chunk
96+
const chunks = Array(chunksCount).fill(0);
8797
for (let i = startChunk; i <= endChunk; i++) {
88-
activeChunks[i] = true;
98+
chunks[i] = chunkLengths[i];
8999
}
90100

91-
return activeChunks;
92-
}, [endChunk, startChunk, chunksCount]);
101+
return chunks;
102+
}, [chunksCount, startChunk, endChunk, chunkLengths]);
93103
};

0 commit comments

Comments
 (0)