Skip to content

Commit 692aea8

Browse files
committed
fix: some refinements
1 parent 4e2c4dd commit 692aea8

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

src/components/PaginatedTable/PaginatedTable.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ import type {
1919
SortParams,
2020
} from './types';
2121
import {useScrollBasedChunks} from './useScrollBasedChunks';
22+
import {calculateElementOffsetTop} from './utils';
2223

2324
import './PaginatedTable.scss';
2425

26+
const HEADER_HEIGHT = 40;
27+
const CONTROLS_HEIGHT = 50;
28+
2529
export interface PaginatedTableProps<T, F> {
2630
limit: number;
2731
initialEntitiesCount?: number;
@@ -57,7 +61,7 @@ export const PaginatedTable = <T, F>({
5761
renderEmptyDataMessage,
5862
containerClassName,
5963
}: PaginatedTableProps<T, F>) => {
60-
const initialTotal = initialEntitiesCount || 1;
64+
const initialTotal = initialEntitiesCount || 0;
6165
const initialFound = initialEntitiesCount || 1;
6266

6367
const [sortParams, setSortParams] = React.useState<SortParams | undefined>(initialSortParams);
@@ -75,25 +79,19 @@ export const PaginatedTable = <T, F>({
7579
chunkSize,
7680
});
7781

78-
const handleDataFetched = React.useCallback(
79-
(total: number, found: number) => {
80-
if (total !== totalEntities) {
81-
setTotalEntities(total);
82-
}
83-
84-
if (found !== foundEntities) {
85-
setFoundEntities(found);
86-
}
87-
88-
setIsInitialLoad(false);
89-
},
90-
[foundEntities, totalEntities],
91-
);
82+
const handleDataFetched = React.useCallback((total: number, found: number) => {
83+
setTotalEntities(total);
84+
setFoundEntities(found);
85+
setIsInitialLoad(false);
86+
}, []);
9287

9388
// reset table on filters change
9489
React.useLayoutEffect(() => {
9590
if (parentRef?.current && tableRef.current && !initialTotal) {
96-
parentRef.current.scrollTo(0, tableRef.current.offsetTop);
91+
parentRef.current.scrollTo({
92+
left: 0,
93+
top: calculateElementOffsetTop(tableRef.current) - HEADER_HEIGHT - CONTROLS_HEIGHT,
94+
});
9795
}
9896
setTotalEntities(initialTotal);
9997
setFoundEntities(initialFound);

src/components/PaginatedTable/useScrollBasedChunks.ts

Lines changed: 3 additions & 1 deletion
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 {calculateElementOffsetTop} from './utils';
6+
57
interface UseScrollBasedChunksProps {
68
parentRef: React.RefObject<HTMLElement> | null;
79
tableRef: React.RefObject<HTMLElement>;
@@ -44,7 +46,7 @@ export const useScrollBasedChunks = ({
4446
return null;
4547
}
4648

47-
const tableOffset = table.offsetTop;
49+
const tableOffset = calculateElementOffsetTop(table, container);
4850
const containerScroll = container.scrollTop;
4951
const visibleStart = Math.max(containerScroll - tableOffset, 0);
5052
const visibleEnd = visibleStart + container.clientHeight;

src/components/PaginatedTable/utils.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ export function calculateColumnWidth(newWidth: number, minWidth = 40, maxWidth =
2727
}
2828

2929
export const typedMemo: <T>(Component: T) => T = React.memo;
30+
31+
export function calculateElementOffsetTop(element: HTMLElement, container?: HTMLElement): number {
32+
let currentElement = element;
33+
let offsetTop = 0;
34+
35+
while (currentElement && currentElement !== container) {
36+
offsetTop += currentElement.offsetTop;
37+
currentElement = currentElement.offsetParent as HTMLElement;
38+
}
39+
40+
return offsetTop;
41+
}

0 commit comments

Comments
 (0)