|
1
|
import { |
|
2
|
ColumnFilter, |
|
3
|
ColumnFiltersState, |
|
4
|
type ColumnSort, |
|
5
|
type ColumnOrderState, |
|
6
|
getCoreRowModel, |
|
7
|
PaginationState, |
|
8
|
type RowSelectionState, |
|
9
|
type SortingState, |
|
10
|
type TableOptions, |
|
11
|
type Updater, |
|
12
|
useReactTable, |
|
13
|
type VisibilityState, |
|
14
|
} from "@tanstack/react-table" |
|
15
|
import * as React from "react" |
|
16
|
import { |
|
17
|
DataTableColumnDef, |
|
18
|
DataTableColumnFilter, |
|
19
|
DataTableCommand, |
|
20
|
DataTableDateComparisonOperator, |
|
21
|
DataTableEmptyState, |
|
22
|
DataTableFilter, |
|
23
|
DataTableFilteringState, |
|
24
|
DataTableFilterOption, |
|
25
|
DataTablePaginationState, |
|
26
|
DataTableRow, |
|
27
|
DataTableRowSelectionState, |
|
28
|
DataTableSortingState, |
|
29
|
} from "./types" |
|
30
|
|
|
31
|
interface DataTableOptions<TData> |
|
32
|
extends Pick<TableOptions<TData>, "data" | "getRowId"> { |
|
33
|
/** |
|
34
|
* The columns to use for the table. |
|
35
|
*/ |
|
36
|
columns: DataTableColumnDef<TData, any>[] |
|
37
|
/** |
|
38
|
* The filters which the user can apply to the table. |
|
39
|
*/ |
|
40
|
filters?: DataTableFilter[] |
|
41
|
/** |
|
42
|
* The commands which the user can apply to selected rows. |
|
43
|
*/ |
|
44
|
commands?: DataTableCommand[] |
|
45
|
/** |
|
46
|
* Whether the data for the table is currently being loaded. |
|
47
|
*/ |
|
48
|
isLoading?: boolean |
|
49
|
/** |
|
50
|
* The state and callback for the filtering. |
|
51
|
*/ |
|
52
|
filtering?: { |
|
53
|
state: DataTableFilteringState |
|
54
|
onFilteringChange: (state: DataTableFilteringState) => void |
|
55
|
} |
|
56
|
/** |
|
57
|
* The state and callback for the row selection. |
|
58
|
*/ |
|
59
|
rowSelection?: { |
|
60
|
state: DataTableRowSelectionState |
|
61
|
onRowSelectionChange: (state: DataTableRowSelectionState) => void |
|
62
|
enableRowSelection?: |
|
63
|
| boolean |
|
64
|
| ((row: DataTableRow<TData>) => boolean) |
|
65
|
| undefined |
|
66
|
} |
|
67
|
/** |
|
68
|
* The state and callback for the sorting. |
|
69
|
*/ |
|
70
|
sorting?: { |
|
71
|
state: DataTableSortingState | null |
|
72
|
onSortingChange: (state: DataTableSortingState) => void |
|
73
|
} |
|
74
|
/** |
|
75
|
* The state and callback for the search, with optional debounce. |
|
76
|
*/ |
|
77
|
search?: { |
|
78
|
state: string |
|
79
|
onSearchChange: (state: string) => void |
|
80
|
/** |
|
81
|
* Debounce time in milliseconds for the search callback. |
|
82
|
* @default 300 |
|
83
|
*/ |
|
84
|
debounce?: number |
|
85
|
} |
|
86
|
/** |
|
87
|
* The state and callback for the pagination. |
|
88
|
*/ |
|
89
|
pagination?: { |
|
90
|
state: DataTablePaginationState |
|
91
|
onPaginationChange: (state: DataTablePaginationState) => void |
|
92
|
} |
|
93
|
/** |
|
94
|
* The function to execute when a row is clicked. |
|
95
|
*/ |
|
96
|
onRowClick?: ( |
|
97
|
event: React.MouseEvent<HTMLTableRowElement, MouseEvent>, |
|
98
|
row: TData |
|
99
|
) => void |
|
100
|
/** |
|
101
|
* The total count of rows. When working with pagination, this will be the total |
|
102
|
* number of rows available, not the number of rows currently being displayed. |
|
103
|
*/ |
|
104
|
rowCount?: number |
|
105
|
/** |
|
106
|
* Whether the page index should be reset the filtering, sorting, or pagination changes. |
|
107
|
* |
|
108
|
* @default true |
|
109
|
*/ |
|
110
|
autoResetPageIndex?: boolean |
|
111
|
/** |
|
112
|
* The state and callback for the column visibility. |
|
113
|
*/ |
|
114
|
columnVisibility?: { |
|
115
|
state: VisibilityState |
|
116
|
onColumnVisibilityChange: (state: VisibilityState) => void |
|
117
|
} |
|
118
|
/** |
|
119
|
* The state and callback for the column order. |
|
120
|
*/ |
|
121
|
columnOrder?: { |
|
122
|
state: ColumnOrderState |
|
123
|
onColumnOrderChange: (state: ColumnOrderState) => void |
|
124
|
} |
|
125
|
} |
|
126
|
|
|
127
|
interface UseDataTableReturn<TData> |
|
128
|
extends Pick< |
|
129
|
ReturnType<typeof useReactTable<TData>>, |
|
130
|
| "getHeaderGroups" |
|
131
|
| "getRowModel" |
|
132
|
| "getCanNextPage" |
|
133
|
| "getCanPreviousPage" |
|
134
|
| "nextPage" |
|
135
|
| "previousPage" |
|
136
|
| "getPageCount" |
|
137
|
| "getAllColumns" |
|
138
|
| "setColumnVisibility" |
|
139
|
| "setColumnOrder" |
|
140
|
> { |
|
141
|
getSorting: () => DataTableSortingState | null |
|
142
|
setSorting: ( |
|
143
|
sortingOrUpdater: |
|
144
|
| DataTableSortingState |
|
145
|
| ((prev: DataTableSortingState | null) => DataTableSortingState) |
|
146
|
) => void |
|
147
|
getFilters: () => DataTableFilter[] |
|
148
|
getFilterOptions: < |
|
149
|
T extends string | string[] | DataTableDateComparisonOperator |
|
150
|
>( |
|
151
|
id: string |
|
152
|
) => DataTableFilterOption<T>[] | null |
|
153
|
getFilterMeta: (id: string) => DataTableFilter | null |
|
154
|
getFiltering: () => DataTableFilteringState |
|
155
|
addFilter: (filter: DataTableColumnFilter) => void |
|
156
|
removeFilter: (id: string) => void |
|
157
|
clearFilters: () => void |
|
158
|
updateFilter: (filter: DataTableColumnFilter) => void |
|
159
|
getSearch: () => string |
|
160
|
onSearchChange: (search: string) => void |
|
161
|
getCommands: () => DataTableCommand[] |
|
162
|
getRowSelection: () => DataTableRowSelectionState |
|
163
|
onRowClick?: ( |
|
164
|
event: React.MouseEvent<HTMLTableRowElement, MouseEvent>, |
|
165
|
row: TData |
|
166
|
) => void |
|
167
|
emptyState: DataTableEmptyState |
|
168
|
isLoading: boolean |
|
169
|
showSkeleton: boolean |
|
170
|
pageIndex: number |
|
171
|
pageSize: number |
|
172
|
rowCount: number |
|
173
|
enablePagination: boolean |
|
174
|
enableFiltering: boolean |
|
175
|
enableSorting: boolean |
|
176
|
enableSearch: boolean |
|
177
|
enableColumnVisibility: boolean |
|
178
|
enableColumnOrder: boolean |
|
179
|
columnOrder: ColumnOrderState |
|
180
|
setColumnOrderFromArray: (order: string[]) => void |
|
181
|
} |
|
182
|
|
|
183
|
const useDataTable = <TData,>({ |
|
184
|
rowCount = 0, |
|
185
|
filters, |
|
186
|
commands, |
|
187
|
rowSelection, |
|
188
|
sorting, |
|
189
|
filtering, |
|
190
|
pagination, |
|
191
|
search, |
|
192
|
onRowClick, |
|
193
|
autoResetPageIndex = true, |
|
194
|
isLoading = false, |
|
195
|
columnVisibility, |
|
196
|
columnOrder, |
|
197
|
...options |
|
198
|
}: DataTableOptions<TData>): UseDataTableReturn<TData> => { |
|
199
|
const { state: sortingState, onSortingChange } = sorting ?? {} |
|
200
|
const { state: filteringState, onFilteringChange } = filtering ?? {} |
|
201
|
const { state: paginationState, onPaginationChange } = pagination ?? {} |
|
202
|
const { |
|
203
|
state: rowSelectionState, |
|
204
|
onRowSelectionChange, |
|
205
|
enableRowSelection, |
|
206
|
} = rowSelection ?? {} |
|
207
|
const { state: columnVisibilityState, onColumnVisibilityChange } = columnVisibility ?? {} |
|
208
|
const { state: columnOrderState, onColumnOrderChange } = columnOrder ?? {} |
|
209
|
|
|
210
|
// Store filter metadata like openOnMount |
|
211
|
|
|
212
|
const autoResetPageIndexHandler = React.useCallback(() => { |
|
213
|
return autoResetPageIndex |
|
214
|
? () => |
|
215
|
paginationState && |
|
216
|
onPaginationChange?.({ ...paginationState, pageIndex: 0 }) |
|
217
|
: undefined |
|
218
|
}, [autoResetPageIndex, paginationState, onPaginationChange]) |
|
219
|
|
|
220
|
const sortingStateHandler = React.useCallback(() => { |
|
221
|
return onSortingChange |
|
222
|
? (updaterOrValue: Updater<SortingState>) => { |
|
223
|
autoResetPageIndexHandler()?.() |
|
224
|
onSortingChangeTransformer( |
|
225
|
onSortingChange, |
|
226
|
sortingState |
|
227
|
)(updaterOrValue) |
|
228
|
} |
|
229
|
: undefined |
|
230
|
}, [onSortingChange, sortingState, autoResetPageIndexHandler]) |
|
231
|
|
|
232
|
const rowSelectionStateHandler = React.useCallback(() => { |
|
233
|
return onRowSelectionChange |
|
234
|
? (updaterOrValue: Updater<RowSelectionState>) => { |
|
235
|
onRowSelectionChangeTransformer( |
|
236
|
onRowSelectionChange, |
|
237
|
rowSelectionState |
|
238
|
)(updaterOrValue) |
|
239
|
} |
|
240
|
: undefined |
|
241
|
}, [onRowSelectionChange, rowSelectionState, autoResetPageIndexHandler]) |
|
242
|
|
|
243
|
const filteringStateHandler = React.useCallback(() => { |
|
244
|
return onFilteringChange |
|
245
|
? (updaterOrValue: Updater<ColumnFiltersState>) => { |
|
246
|
autoResetPageIndexHandler()?.() |
|
247
|
onFilteringChangeTransformer( |
|
248
|
onFilteringChange, |
|
249
|
filteringState |
|
250
|
)(updaterOrValue) |
|
251
|
} |
|
252
|
: undefined |
|
253
|
}, [onFilteringChange, filteringState, autoResetPageIndexHandler]) |
|
254
|
|
|
255
|
const paginationStateHandler = React.useCallback(() => { |
|
256
|
return onPaginationChange |
|
257
|
? onPaginationChangeTransformer(onPaginationChange, paginationState) |
|
258
|
: undefined |
|
259
|
}, [onPaginationChange, paginationState]) |
|
260
|
|
|
261
|
const columnVisibilityStateHandler = React.useCallback(() => { |
|
262
|
return onColumnVisibilityChange |
|
263
|
? (updaterOrValue: Updater<VisibilityState>) => { |
|
264
|
const value = |
|
265
|
typeof updaterOrValue === "function" |
|
266
|
? updaterOrValue(columnVisibilityState ?? {}) |
|
267
|
: updaterOrValue |
|
268
|
|
|
269
|
onColumnVisibilityChange(value) |
|
270
|
} |
|
271
|
: undefined |
|
272
|
}, [onColumnVisibilityChange, columnVisibilityState]) |
|
273
|
|
|
274
|
const columnOrderStateHandler = React.useCallback(() => { |
|
275
|
return onColumnOrderChange |
|
276
|
? (updaterOrValue: Updater<ColumnOrderState>) => { |
|
277
|
const value = |
|
278
|
typeof updaterOrValue === "function" |
|
279
|
? updaterOrValue(columnOrderState ?? []) |
|
280
|
: updaterOrValue |
|
281
|
|
|
282
|
onColumnOrderChange(value) |
|
283
|
} |
|
284
|
: undefined |
|
285
|
}, [onColumnOrderChange, columnOrderState]) |
|
286
|
|
|
287
|
const instance = useReactTable({ |
|
288
|
...options, |
|
289
|
getCoreRowModel: getCoreRowModel(), |
|
290
|
state: { |
|
291
|
rowSelection: rowSelectionState ?? {}, |
|
292
|
sorting: sortingState ? [sortingState] : undefined, |
|
293
|
columnFilters: Object.entries(filteringState ?? {}).map( |
|
294
|
([id, filter]) => ({ |
|
295
|
id, |
|
296
|
value: filter, |
|
297
|
}) |
|
298
|
), |
|
299
|
pagination: paginationState, |
|
300
|
columnVisibility: columnVisibilityState ?? {}, |
|
301
|
columnOrder: columnOrderState ?? [], |
|
302
|
}, |
|
303
|
enableRowSelection, |
|
304
|
rowCount, |
|
305
|
onColumnFiltersChange: filteringStateHandler(), |
|
306
|
onRowSelectionChange: rowSelectionStateHandler(), |
|
307
|
onSortingChange: sortingStateHandler(), |
|
308
|
onPaginationChange: paginationStateHandler(), |
|
309
|
onColumnVisibilityChange: columnVisibilityStateHandler(), |
|
310
|
onColumnOrderChange: columnOrderStateHandler(), |
|
311
|
manualSorting: true, |
|
312
|
manualPagination: true, |
|
313
|
manualFiltering: true, |
|
314
|
}) |
|
315
|
|
|
316
|
const getSorting = React.useCallback(() => { |
|
317
|
return instance.getState().sorting?.[0] ?? null |
|
318
|
}, [instance]) |
|
319
|
|
|
320
|
const setSorting = React.useCallback( |
|
321
|
( |
|
322
|
sortingOrUpdater: ColumnSort | ((prev: ColumnSort | null) => ColumnSort) |
|
323
|
) => { |
|
324
|
const currentSort = instance.getState().sorting?.[0] ?? null |
|
325
|
const newSorting = |
|
326
|
typeof sortingOrUpdater === "function" |
|
327
|
? sortingOrUpdater(currentSort) |
|
328
|
: sortingOrUpdater |
|
329
|
|
|
330
|
autoResetPageIndexHandler()?.() |
|
331
|
instance.setSorting([newSorting]) |
|
332
|
}, |
|
333
|
[instance, autoResetPageIndexHandler] |
|
334
|
) |
|
335
|
|
|
336
|
const getFilters = React.useCallback(() => { |
|
337
|
return filters ?? [] |
|
338
|
}, [filters]) |
|
339
|
|
|
340
|
const getFilterOptions = React.useCallback( |
|
341
|
<T extends string | string[] | DataTableDateComparisonOperator>( |
|
342
|
id: string |
|
343
|
) => { |
|
344
|
const filter = getFilters().find((filter) => filter.id === id) |
|
345
|
|
|
346
|
if (!filter) { |
|
347
|
return null |
|
348
|
} |
|
349
|
|
|
350
|
return ((filter as any).options as DataTableFilterOption<T>[]) || null |
|
351
|
}, |
|
352
|
[getFilters] |
|
353
|
) |
|
354
|
|
|
355
|
const getFilterMeta = React.useCallback( |
|
356
|
(id: string) => { |
|
357
|
return getFilters().find((filter) => filter.id === id) || null |
|
358
|
}, |
|
359
|
[getFilters] |
|
360
|
) |
|
361
|
|
|
362
|
const getFiltering = React.useCallback(() => { |
|
363
|
const state = instance.getState().columnFilters ?? [] |
|
364
|
return Object.fromEntries(state.map((filter) => [filter.id, filter.value])) |
|
365
|
}, [instance]) |
|
366
|
|
|
367
|
const addFilter = React.useCallback( |
|
368
|
(filter: DataTableColumnFilter) => { |
|
369
|
const currentFilters = getFiltering() |
|
370
|
const newFilters = { ...currentFilters, [filter.id]: filter.value } |
|
371
|
autoResetPageIndexHandler()?.() |
|
372
|
onFilteringChange?.(newFilters) |
|
373
|
}, |
|
374
|
[onFilteringChange, getFiltering, autoResetPageIndexHandler] |
|
375
|
) |
|
376
|
|
|
377
|
const removeFilter = React.useCallback( |
|
378
|
(id: string) => { |
|
379
|
const currentFilters = getFiltering() |
|
380
|
delete currentFilters[id] |
|
381
|
autoResetPageIndexHandler()?.() |
|
382
|
onFilteringChange?.(currentFilters) |
|
383
|
}, |
|
384
|
[onFilteringChange, getFiltering, autoResetPageIndexHandler] |
|
385
|
) |
|
386
|
|
|
387
|
const clearFilters = React.useCallback(() => { |
|
388
|
autoResetPageIndexHandler()?.() |
|
389
|
onFilteringChange?.({}) |
|
390
|
}, [onFilteringChange, autoResetPageIndexHandler]) |
|
391
|
|
|
392
|
const updateFilter = React.useCallback( |
|
393
|
(filter: ColumnFilter) => { |
|
394
|
addFilter(filter) |
|
395
|
}, |
|
396
|
[addFilter] |
|
397
|
) |
|
398
|
|
|
399
|
const { state: searchState, onSearchChange, debounce = 300 } = search ?? {} |
|
400
|
|
|
401
|
const [localSearch, setLocalSearch] = React.useState(searchState ?? "") |
|
402
|
const timeoutRef = React.useRef<ReturnType<typeof setTimeout>>() |
|
403
|
|
|
404
|
React.useEffect(() => { |
|
405
|
setLocalSearch(searchState ?? "") |
|
406
|
}, [searchState]) |
|
407
|
|
|
408
|
const getSearch = React.useCallback(() => { |
|
409
|
return localSearch |
|
410
|
}, [localSearch]) |
|
411
|
|
|
412
|
const debouncedSearchChange = React.useMemo(() => { |
|
413
|
if (!onSearchChange) { |
|
414
|
return undefined |
|
415
|
} |
|
416
|
|
|
417
|
return (value: string) => { |
|
418
|
if (timeoutRef.current) { |
|
419
|
clearTimeout(timeoutRef.current) |
|
420
|
} |
|
421
|
|
|
422
|
if (debounce <= 0) { |
|
423
|
autoResetPageIndexHandler()?.() |
|
424
|
onSearchChange(value) |
|
425
|
return |
|
426
|
} |
|
427
|
|
|
428
|
timeoutRef.current = setTimeout(() => { |
|
429
|
autoResetPageIndexHandler()?.() |
|
430
|
onSearchChange(value) |
|
431
|
}, debounce) |
|
432
|
} |
|
433
|
}, [onSearchChange, debounce, autoResetPageIndexHandler]) |
|
434
|
|
|
435
|
React.useEffect(() => { |
|
436
|
return () => { |
|
437
|
if (timeoutRef.current) { |
|
438
|
clearTimeout(timeoutRef.current) |
|
439
|
} |
|
440
|
} |
|
441
|
}, []) |
|
442
|
|
|
443
|
const onSearchChangeHandler = React.useCallback( |
|
444
|
(search: string) => { |
|
445
|
setLocalSearch(search) |
|
446
|
debouncedSearchChange?.(search) |
|
447
|
}, |
|
448
|
[debouncedSearchChange] |
|
449
|
) |
|
450
|
|
|
451
|
const getCommands = React.useCallback(() => { |
|
452
|
return commands ?? [] |
|
453
|
}, [commands]) |
|
454
|
|
|
455
|
const getRowSelection = React.useCallback(() => { |
|
456
|
return instance.getState().rowSelection |
|
457
|
}, [instance]) |
|
458
|
|
|
459
|
const rows = instance.getRowModel().rows |
|
460
|
|
|
461
|
const emptyState = React.useMemo(() => { |
|
462
|
const hasRows = rows.length > 0 |
|
463
|
const hasSearch = Boolean(searchState) |
|
464
|
const hasFilters = Object.keys(filteringState ?? {}).length > 0 |
|
465
|
|
|
466
|
if (hasRows) { |
|
467
|
return DataTableEmptyState.POPULATED |
|
468
|
} |
|
469
|
|
|
470
|
if (hasSearch || hasFilters) { |
|
471
|
return DataTableEmptyState.FILTERED_EMPTY |
|
472
|
} |
|
473
|
|
|
474
|
return DataTableEmptyState.EMPTY |
|
475
|
}, [rows, searchState, filteringState]) |
|
476
|
|
|
477
|
const showSkeleton = React.useMemo(() => { |
|
478
|
return isLoading === true && rows.length === 0 |
|
479
|
}, [isLoading, rows]) |
|
480
|
|
|
481
|
const enablePagination: boolean = !!pagination |
|
482
|
const enableFiltering: boolean = !!filtering |
|
483
|
const enableSorting: boolean = !!sorting |
|
484
|
const enableSearch: boolean = !!search |
|
485
|
const enableColumnVisibility: boolean = !!columnVisibility |
|
486
|
const enableColumnOrder: boolean = !!columnOrder |
|
487
|
|
|
488
|
const setColumnOrderFromArray = React.useCallback((order: string[]) => { |
|
489
|
instance.setColumnOrder(order) |
|
490
|
}, [instance]) |
|
491
|
|
|
492
|
return { |
|
493
|
// Table |
|
494
|
getHeaderGroups: instance.getHeaderGroups, |
|
495
|
getRowModel: instance.getRowModel, |
|
496
|
getAllColumns: instance.getAllColumns, |
|
497
|
setColumnVisibility: instance.setColumnVisibility, |
|
498
|
setColumnOrder: instance.setColumnOrder, |
|
499
|
// Pagination |
|
500
|
enablePagination, |
|
501
|
getCanNextPage: instance.getCanNextPage, |
|
502
|
getCanPreviousPage: instance.getCanPreviousPage, |
|
503
|
nextPage: instance.nextPage, |
|
504
|
previousPage: instance.previousPage, |
|
505
|
getPageCount: instance.getPageCount, |
|
506
|
pageIndex: instance.getState()?.pagination?.pageIndex ?? 0, |
|
507
|
pageSize: instance.getState()?.pagination?.pageSize ?? 10, |
|
508
|
rowCount, |
|
509
|
// Search |
|
510
|
enableSearch, |
|
511
|
getSearch, |
|
512
|
onSearchChange: onSearchChangeHandler, |
|
513
|
// Sorting |
|
514
|
enableSorting, |
|
515
|
getSorting, |
|
516
|
setSorting, |
|
517
|
// Filtering |
|
518
|
enableFiltering, |
|
519
|
getFilters, |
|
520
|
getFilterOptions, |
|
521
|
getFilterMeta, |
|
522
|
getFiltering, |
|
523
|
addFilter, |
|
524
|
removeFilter, |
|
525
|
clearFilters, |
|
526
|
updateFilter, |
|
527
|
// Commands |
|
528
|
getCommands, |
|
529
|
getRowSelection, |
|
530
|
// Handlers |
|
531
|
onRowClick, |
|
532
|
// Empty State |
|
533
|
emptyState, |
|
534
|
// Loading |
|
535
|
isLoading, |
|
536
|
showSkeleton, |
|
537
|
// Column Visibility |
|
538
|
enableColumnVisibility, |
|
539
|
// Column Order |
|
540
|
enableColumnOrder, |
|
541
|
columnOrder: instance.getState().columnOrder, |
|
542
|
setColumnOrderFromArray, |
|
543
|
} |
|
544
|
} |
|
545
|
|
|
546
|
function onSortingChangeTransformer( |
|
547
|
onSortingChange: (state: ColumnSort) => void, |
|
548
|
state?: ColumnSort | null |
|
549
|
) { |
|
550
|
return (updaterOrValue: Updater<SortingState>) => { |
|
551
|
const value = |
|
552
|
typeof updaterOrValue === "function" |
|
553
|
? updaterOrValue(state ? [state] : []) |
|
554
|
: updaterOrValue |
|
555
|
const columnSort = value[0] |
|
556
|
|
|
557
|
onSortingChange(columnSort) |
|
558
|
} |
|
559
|
} |
|
560
|
|
|
561
|
function onRowSelectionChangeTransformer( |
|
562
|
onRowSelectionChange: (state: RowSelectionState) => void, |
|
563
|
state?: RowSelectionState |
|
564
|
) { |
|
565
|
return (updaterOrValue: Updater<RowSelectionState>) => { |
|
566
|
const value = |
|
567
|
typeof updaterOrValue === "function" |
|
568
|
? updaterOrValue(state ?? {}) |
|
569
|
: updaterOrValue |
|
570
|
|
|
571
|
onRowSelectionChange(value) |
|
572
|
} |
|
573
|
} |
|
574
|
|
|
575
|
function onFilteringChangeTransformer( |
|
576
|
onFilteringChange: (state: DataTableFilteringState) => void, |
|
577
|
state?: DataTableFilteringState |
|
578
|
) { |
|
579
|
return (updaterOrValue: Updater<ColumnFiltersState>) => { |
|
580
|
const value = |
|
581
|
typeof updaterOrValue === "function" |
|
582
|
? updaterOrValue( |
|
583
|
Object.entries(state ?? {}).map(([id, filter]) => ({ |
|
584
|
id, |
|
585
|
value: filter, |
|
586
|
})) |
|
587
|
) |
|
588
|
: updaterOrValue |
|
589
|
|
|
590
|
const transformedValue = Object.fromEntries( |
|
591
|
value.map((filter) => [filter.id, filter.value]) |
|
592
|
) |
|
593
|
|
|
594
|
onFilteringChange(transformedValue) |
|
595
|
} |
|
596
|
} |
|
597
|
|
|
598
|
function onPaginationChangeTransformer( |
|
599
|
onPaginationChange: (state: PaginationState) => void, |
|
600
|
state?: PaginationState |
|
601
|
) { |
|
602
|
return (updaterOrValue: Updater<PaginationState>) => { |
|
603
|
const value = |
|
604
|
typeof updaterOrValue === "function" |
|
605
|
? updaterOrValue(state ?? { pageIndex: 0, pageSize: 10 }) |
|
606
|
: updaterOrValue |
|
607
|
|
|
608
|
onPaginationChange(value) |
|
609
|
} |
|
610
|
} |
|
611
|
|
|
612
|
export { useDataTable } |
|
613
|
export type { DataTableOptions, UseDataTableReturn } |