@@ -25,10 +25,11 @@ import { TableProps } from "@/types";
25
25
import { useDeepArrayMemo } from "@/hooks/useDeepArrayMemo" ;
26
26
import { HeaderCheckbox } from "./HeaderCheckbox" ;
27
27
import { useRowSelection } from "./useRowSelection" ;
28
- import { useColumnState } from "./useColumnState" ;
28
+ import { areStatesEqual , useColumnState } from "./useColumnState" ;
29
29
import { CHECKBOX_COLUMN , STATUS_COLUMN } from "./columnStateHelper" ;
30
30
31
31
const DEBOUNCE_TIME = 50 ;
32
+ const DEFAULT_TOTAL_ROWS_VALUE = Number . MAX_SAFE_INTEGER ;
32
33
33
34
export type InfiniteTableProps = Omit <
34
35
TableProps ,
@@ -42,14 +43,14 @@ export type InfiniteTableProps = Omit<
42
43
startRow : number ;
43
44
endRow : number ;
44
45
sortFields ?: Record < string , SortDirection > ;
45
- } ) => Promise < any [ ] > ;
46
+ } ) => Promise < any [ ] | undefined > ;
46
47
height ?: number ;
47
48
onColumnChanged ?: ( columnsState : ColumnState [ ] ) => void ;
48
49
onGetColumnsState ?: ( ) => ColumnState [ ] | undefined ;
49
50
onGetFirstVisibleRowIndex ?: ( ) => number | undefined ;
50
51
onChangeFirstVisibleRowIndex ?: ( index : number ) => void ;
51
52
onGetSelectedRowKeys ?: ( ) => any [ ] | undefined ;
52
- totalRows : number ;
53
+ totalRows ? : number ;
53
54
allRowSelectedMode ?: boolean ;
54
55
onAllRowSelectedModeChange ?: ( allRowSelectedMode : boolean ) => void ;
55
56
footer ?: React . ReactNode ;
@@ -78,7 +79,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
78
79
onChangeFirstVisibleRowIndex,
79
80
onGetFirstVisibleRowIndex,
80
81
onGetSelectedRowKeys,
81
- totalRows,
82
+ totalRows = DEFAULT_TOTAL_ROWS_VALUE ,
82
83
onAllRowSelectedModeChange,
83
84
allRowSelectedMode : allRowSelectedModeProps ,
84
85
footer,
@@ -92,7 +93,6 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
92
93
const firstTimeOnBodyScroll = useRef ( true ) ;
93
94
const allRowSelectedModeRef = useRef < boolean > ( false ) ;
94
95
const containerRef = useRef < HTMLDivElement > ( null ) ;
95
- const columnChangeListenerReady = useRef ( false ) ;
96
96
const totalHeight = footer ? heightProps + footerHeight : heightProps ;
97
97
const tableHeight = footer ? heightProps - footerHeight : heightProps ;
98
98
@@ -128,7 +128,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
128
128
const columns = useDeepArrayMemo ( columnsProps , "key" ) ;
129
129
130
130
const {
131
- applyColumnState ,
131
+ loadPersistedColumnState ,
132
132
columnsPersistedStateRef,
133
133
applyAndUpdateNewState,
134
134
} = useColumnState ( {
@@ -141,16 +141,19 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
141
141
142
142
const onColumnChanged = useCallback ( ( ) => {
143
143
const state = gridRef ?. current ?. api . getColumnState ( ) ;
144
- if ( ! columnChangeListenerReady . current ) {
145
- columnChangeListenerReady . current = true ;
144
+ if ( ! state ) {
146
145
return ;
147
146
}
148
- if ( ! state ) {
147
+ if ( areStatesEqual ( state , columnsPersistedStateRef . current ) ) {
149
148
return ;
150
149
}
151
150
applyAndUpdateNewState ( state ) ;
152
151
onColumnsChangedProps ?.( state ) ;
153
- } , [ applyAndUpdateNewState , onColumnsChangedProps ] ) ;
152
+ } , [
153
+ applyAndUpdateNewState ,
154
+ columnsPersistedStateRef ,
155
+ onColumnsChangedProps ,
156
+ ] ) ;
154
157
155
158
const onColumnMoved = useCallback ( ( ) => {
156
159
onColumnChanged ( ) ;
@@ -166,10 +169,6 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
166
169
[ onColumnChanged ] ,
167
170
) ;
168
171
169
- const onSortChanged = useCallback ( ( ) => {
170
- gridRef . current ?. api ?. purgeInfiniteCache ( ) ;
171
- } , [ ] ) ;
172
-
173
172
const getSortedFields = useCallback ( ( ) :
174
173
| Record < string , SortDirection >
175
174
| undefined => {
@@ -217,7 +216,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
217
216
const storedState = columnsPersistedStateRef . current ;
218
217
const storedStateKeys = storedState ?. map ( ( col : any ) => col . colId ) ;
219
218
220
- const restOfColumns = columns . map ( ( column ) => ( {
219
+ const restOfColumns : ColDef [ ] = columns . map ( ( column ) => ( {
221
220
field : column . key ,
222
221
sortable : column . isSortable ,
223
222
headerName : column . title ,
@@ -228,6 +227,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
228
227
229
228
// restOfColumns should be sorted by the order of the storedState
230
229
storedState &&
230
+ storedStateKeys &&
231
231
restOfColumns . sort ( ( a , b ) => {
232
232
const aIndex = storedStateKeys . indexOf ( a . field ) ;
233
233
const bIndex = storedStateKeys . indexOf ( b . field ) ;
@@ -274,6 +274,10 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
274
274
endRow,
275
275
sortFields : getSortedFields ( ) ,
276
276
} ) ;
277
+ if ( ! data ) {
278
+ params . failCallback ( ) ;
279
+ return ;
280
+ }
277
281
let lastRow = - 1 ;
278
282
if ( data . length < endRow - startRow ) {
279
283
lastRow = startRow + data . length ;
@@ -317,10 +321,8 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
317
321
}
318
322
}
319
323
gridRef . current ?. api . hideOverlay ( ) ;
320
- applyColumnState ( ) ;
321
324
} ,
322
325
[
323
- applyColumnState ,
324
326
getSortedFields ,
325
327
hasStatusColumn ,
326
328
onGetSelectedRowKeys ,
@@ -333,11 +335,12 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
333
335
334
336
const onGridReady = useCallback (
335
337
( params : GridReadyEvent ) => {
338
+ loadPersistedColumnState ( ) ;
336
339
params . api . setGridOption ( "datasource" , {
337
340
getRows,
338
341
} ) ;
339
342
} ,
340
- [ getRows ] ,
343
+ [ getRows , loadPersistedColumnState ] ,
341
344
) ;
342
345
343
346
const onRowDoubleClicked = useCallback (
@@ -397,7 +400,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
397
400
onDragStopped = { onColumnMoved }
398
401
onColumnResized = { onColumnResized }
399
402
rowModelType = { "infinite" }
400
- cacheBlockSize = { 20 }
403
+ cacheBlockSize = { 200 }
401
404
onSelectionChanged = { onSelectionChangedDebounced }
402
405
cacheOverflowSize = { 2 }
403
406
maxConcurrentDatasourceRequests = { 1 }
@@ -408,7 +411,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
408
411
onBodyScroll = { onBodyScroll }
409
412
blockLoadDebounceMillis = { DEBOUNCE_TIME }
410
413
suppressDragLeaveHidesColumns = { true }
411
- onSortChanged = { onSortChanged }
414
+ onSortChanged = { onColumnChanged }
412
415
/>
413
416
</ div >
414
417
{ footer && < div style = { { height : footerHeight } } > { footer } </ div > }
0 commit comments