11import React from 'react' ;
22
3- import { getArray } from '../../utils' ;
43import { TableWithControlsLayout } from '../TableWithControlsLayout/TableWithControlsLayout' ;
54
65import { TableChunk } from './TableChunk' ;
@@ -32,7 +31,7 @@ export interface PaginatedTableProps<T, F> {
3231 columns : Column < T > [ ] ;
3332 getRowClassName ?: GetRowClassName < T > ;
3433 rowHeight ?: number ;
35- parentRef ? : React . RefObject < HTMLElement > ;
34+ parentRef : React . RefObject < HTMLElement > ;
3635 initialSortParams ?: SortParams ;
3736 onColumnsResize ?: HandleTableColumnsResize ;
3837 renderControls ?: RenderControls ;
@@ -42,7 +41,7 @@ export interface PaginatedTableProps<T, F> {
4241}
4342
4443export const PaginatedTable = < T , F > ( {
45- limit,
44+ limit : chunkSize ,
4645 initialEntitiesCount,
4746 fetchData,
4847 filters,
@@ -58,8 +57,8 @@ export const PaginatedTable = <T, F>({
5857 renderEmptyDataMessage,
5958 containerClassName,
6059} : PaginatedTableProps < T , F > ) => {
61- const initialTotal = initialEntitiesCount || limit ;
62- const initialFound = initialEntitiesCount || 0 ;
60+ const initialTotal = initialEntitiesCount || 0 ;
61+ const initialFound = initialEntitiesCount || 1 ;
6362
6463 const [ sortParams , setSortParams ] = React . useState < SortParams | undefined > ( initialSortParams ) ;
6564 const [ totalEntities , setTotalEntities ] = React . useState ( initialTotal ) ;
@@ -69,12 +68,18 @@ export const PaginatedTable = <T, F>({
6968 const tableRef = React . useRef < HTMLDivElement > ( null ) ;
7069
7170 const activeChunks = useScrollBasedChunks ( {
72- containerRef : parentRef ?? tableRef ,
71+ parentRef,
72+ tableRef,
7373 totalItems : foundEntities ,
74- itemHeight : rowHeight ,
75- chunkSize : limit ,
74+ rowHeight,
75+ 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 ) ;
@@ -88,10 +93,8 @@ export const PaginatedTable = <T, F>({
8893 setIsInitialLoad ( true ) ;
8994 if ( parentRef ?. current ) {
9095 parentRef . current . scrollTo ( 0 , 0 ) ;
91- } else {
92- tableRef . current ?. scrollTo ( 0 , 0 ) ;
9396 }
94- } , [ filters , initialFound , initialTotal , limit , parentRef ] ) ;
97+ } , [ filters , initialFound , initialTotal , parentRef ] ) ;
9598
9699 const renderChunks = ( ) => {
97100 if ( ! isInitialLoad && foundEntities === 0 ) {
@@ -104,15 +107,12 @@ export const PaginatedTable = <T, F>({
104107 ) ;
105108 }
106109
107- const totalLength = foundEntities || limit ;
108- const chunksCount = Math . ceil ( totalLength / limit ) ;
109-
110- return getArray ( chunksCount ) . map ( ( value ) => (
110+ return activeChunks . map ( ( isActive , index ) => (
111111 < TableChunk < T , F >
112- key = { value }
113- id = { value }
114- limit = { limit }
115- totalLength = { totalLength }
112+ key = { index }
113+ id = { index }
114+ calculatedCount = { index === activeChunks . length - 1 ? lastChunkSize : chunkSize }
115+ chunkSize = { chunkSize }
116116 rowHeight = { rowHeight }
117117 columns = { columns }
118118 fetchData = { fetchData }
@@ -122,7 +122,7 @@ export const PaginatedTable = <T, F>({
122122 getRowClassName = { getRowClassName }
123123 renderErrorMessage = { renderErrorMessage }
124124 onDataFetched = { handleDataFetched }
125- isActive = { activeChunks . includes ( value ) }
125+ isActive = { isActive }
126126 />
127127 ) ) ;
128128 } ;
0 commit comments