@tanstack/react-table 8.0.0-alpha.3 → 8.0.0-alpha.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -78
  2. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
  3. package/build/cjs/index.js +99 -12
  4. package/build/cjs/index.js.map +1 -1
  5. package/build/esm/index.js +41 -4686
  6. package/build/esm/index.js.map +1 -1
  7. package/build/stats-html.html +1 -1
  8. package/build/stats-react.json +29 -747
  9. package/build/types/index.d.ts +13 -7
  10. package/build/umd/index.development.js +51 -4688
  11. package/build/umd/index.development.js.map +1 -1
  12. package/build/umd/index.production.js +1 -1
  13. package/build/umd/index.production.js.map +1 -1
  14. package/package.json +4 -1
  15. package/src/index.tsx +107 -7
  16. package/build/cjs/aggregationTypes.js +0 -130
  17. package/build/cjs/aggregationTypes.js.map +0 -1
  18. package/build/cjs/core.js +0 -552
  19. package/build/cjs/core.js.map +0 -1
  20. package/build/cjs/createTable.js +0 -108
  21. package/build/cjs/createTable.js.map +0 -1
  22. package/build/cjs/features/ColumnSizing.js +0 -317
  23. package/build/cjs/features/ColumnSizing.js.map +0 -1
  24. package/build/cjs/features/Expanding.js +0 -255
  25. package/build/cjs/features/Expanding.js.map +0 -1
  26. package/build/cjs/features/Filters.js +0 -445
  27. package/build/cjs/features/Filters.js.map +0 -1
  28. package/build/cjs/features/Grouping.js +0 -249
  29. package/build/cjs/features/Grouping.js.map +0 -1
  30. package/build/cjs/features/Headers.js +0 -549
  31. package/build/cjs/features/Headers.js.map +0 -1
  32. package/build/cjs/features/Ordering.js +0 -86
  33. package/build/cjs/features/Ordering.js.map +0 -1
  34. package/build/cjs/features/Pagination.js +0 -194
  35. package/build/cjs/features/Pagination.js.map +0 -1
  36. package/build/cjs/features/Pinning.js +0 -149
  37. package/build/cjs/features/Pinning.js.map +0 -1
  38. package/build/cjs/features/RowSelection.js +0 -541
  39. package/build/cjs/features/RowSelection.js.map +0 -1
  40. package/build/cjs/features/Sorting.js +0 -327
  41. package/build/cjs/features/Sorting.js.map +0 -1
  42. package/build/cjs/features/Visibility.js +0 -166
  43. package/build/cjs/features/Visibility.js.map +0 -1
  44. package/build/cjs/filterTypes.js +0 -172
  45. package/build/cjs/filterTypes.js.map +0 -1
  46. package/build/cjs/sortTypes.js +0 -122
  47. package/build/cjs/sortTypes.js.map +0 -1
  48. package/build/cjs/utils/columnFilterRowsFn.js +0 -131
  49. package/build/cjs/utils/columnFilterRowsFn.js.map +0 -1
  50. package/build/cjs/utils/expandRowsFn.js +0 -38
  51. package/build/cjs/utils/expandRowsFn.js.map +0 -1
  52. package/build/cjs/utils/globalFilterRowsFn.js +0 -101
  53. package/build/cjs/utils/globalFilterRowsFn.js.map +0 -1
  54. package/build/cjs/utils/groupRowsFn.js +0 -155
  55. package/build/cjs/utils/groupRowsFn.js.map +0 -1
  56. package/build/cjs/utils/sortRowsFn.js +0 -94
  57. package/build/cjs/utils/sortRowsFn.js.map +0 -1
  58. package/build/cjs/utils.js +0 -146
  59. package/build/cjs/utils.js.map +0 -1
  60. package/build/types/aggregationTypes.d.ts +0 -22
  61. package/build/types/core.d.ts +0 -111
  62. package/build/types/createTable.d.ts +0 -53
  63. package/build/types/features/ColumnSizing.d.ts +0 -67
  64. package/build/types/features/Expanding.d.ts +0 -53
  65. package/build/types/features/Filters.d.ts +0 -98
  66. package/build/types/features/Grouping.d.ts +0 -82
  67. package/build/types/features/Headers.d.ts +0 -41
  68. package/build/types/features/Ordering.d.ts +0 -19
  69. package/build/types/features/Pagination.d.ts +0 -43
  70. package/build/types/features/Pinning.d.ts +0 -39
  71. package/build/types/features/RowSelection.d.ts +0 -66
  72. package/build/types/features/Sorting.d.ts +0 -78
  73. package/build/types/features/Visibility.d.ts +0 -47
  74. package/build/types/filterTypes.d.ts +0 -50
  75. package/build/types/sortTypes.d.ts +0 -18
  76. package/build/types/types.d.ts +0 -127
  77. package/build/types/utils/columnFilterRowsFn.d.ts +0 -2
  78. package/build/types/utils/expandRowsFn.d.ts +0 -2
  79. package/build/types/utils/globalFilterRowsFn.d.ts +0 -2
  80. package/build/types/utils/groupRowsFn.d.ts +0 -2
  81. package/build/types/utils/paginateRowsFn.d.ts +0 -2
  82. package/build/types/utils/sortRowsFn.d.ts +0 -2
  83. package/build/types/utils.d.ts +0 -28
  84. package/src/aggregationTypes.ts +0 -115
  85. package/src/core.tsx +0 -1182
  86. package/src/createTable.tsx +0 -241
  87. package/src/features/ColumnSizing.ts +0 -453
  88. package/src/features/Expanding.ts +0 -404
  89. package/src/features/Filters.ts +0 -762
  90. package/src/features/Grouping.ts +0 -466
  91. package/src/features/Headers.ts +0 -912
  92. package/src/features/Ordering.ts +0 -133
  93. package/src/features/Pagination.ts +0 -314
  94. package/src/features/Pinning.ts +0 -200
  95. package/src/features/RowSelection.ts +0 -831
  96. package/src/features/Sorting.ts +0 -547
  97. package/src/features/Visibility.ts +0 -279
  98. package/src/filterTypes.ts +0 -251
  99. package/src/sortTypes.ts +0 -159
  100. package/src/types.ts +0 -331
  101. package/src/utils/columnFilterRowsFn.ts +0 -155
  102. package/src/utils/expandRowsFn.ts +0 -50
  103. package/src/utils/globalFilterRowsFn.ts +0 -122
  104. package/src/utils/groupRowsFn.ts +0 -194
  105. package/src/utils/paginateRowsFn.ts +0 -34
  106. package/src/utils/sortRowsFn.ts +0 -115
  107. package/src/utils.tsx +0 -257
package/src/core.tsx DELETED
@@ -1,1182 +0,0 @@
1
- import {
2
- flattenBy,
3
- functionalUpdate,
4
- propGetter,
5
- memo,
6
- flexRender,
7
- RequiredKeys,
8
- } from './utils'
9
-
10
- import {
11
- Updater,
12
- PropGetterValue,
13
- Options,
14
- TableState,
15
- ColumnDef,
16
- Row,
17
- Column,
18
- Cell,
19
- Header,
20
- AccessorFn,
21
- HeaderRenderProps,
22
- TableProps,
23
- TableBodyProps,
24
- PropGetter,
25
- Getter,
26
- RowProps,
27
- CellProps,
28
- ReactTable,
29
- RowValues,
30
- Renderable,
31
- } from './types'
32
-
33
- import * as Visibility from './features/Visibility'
34
- import * as Ordering from './features/Ordering'
35
- import * as Pinning from './features/Pinning'
36
- import * as Headers from './features/Headers'
37
- import * as Filters from './features/Filters'
38
- import * as Sorting from './features/Sorting'
39
- import * as Grouping from './features/Grouping'
40
- import * as Expanding from './features/Expanding'
41
- import * as ColumnSizing from './features/ColumnSizing'
42
- import * as Pagination from './features/Pagination'
43
- import * as RowSelection from './features/RowSelection'
44
- import { RowModel } from '.'
45
-
46
- const features = [
47
- Visibility,
48
- Ordering,
49
- Pinning,
50
- Headers,
51
- Filters,
52
- Sorting,
53
- Grouping,
54
- Expanding,
55
- ColumnSizing,
56
- Pagination,
57
- RowSelection,
58
- ]
59
-
60
- export type CoreOptions<
61
- TData,
62
- TValue,
63
- TFilterFns,
64
- TSortingFns,
65
- TAggregationFns
66
- > = {
67
- data: TData[]
68
- columns: ColumnDef<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
69
- debug?: boolean
70
- defaultColumn?: Partial<
71
- ColumnDef<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
72
- >
73
- initialState?: Partial<TableState>
74
- state?: Partial<TableState>
75
- getSubRows?: (originalRow: TData, index: number) => TData[]
76
- getRowId?: (
77
- originalRow: TData,
78
- index: number,
79
- parent?: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
80
- ) => string
81
- onStateChange?: (newState: TableState) => void
82
- autoResetAll?: boolean
83
- }
84
-
85
- export type TableCore<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> =
86
- {
87
- rerender: () => void
88
- initialState: TableState
89
- internalState: TableState
90
- reset: () => void
91
- options: RequiredKeys<
92
- Options<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
93
- 'state'
94
- >
95
- updateOptions: (
96
- newOptions: Options<
97
- TData,
98
- TValue,
99
- TFilterFns,
100
- TSortingFns,
101
- TAggregationFns
102
- >
103
- ) => void
104
- getRowId: (
105
- _: TData,
106
- index: number,
107
- parent?: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
108
- ) => string
109
- getState: () => TableState
110
- setState: (updater: Updater<TableState>) => void
111
- getDefaultColumn: () => Partial<
112
- ColumnDef<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
113
- >
114
- getColumnDefs: () => ColumnDef<
115
- TData,
116
- TValue,
117
- TFilterFns,
118
- TSortingFns,
119
- TAggregationFns
120
- >[]
121
- createColumn: (
122
- columnDef: ColumnDef<
123
- TData,
124
- TValue,
125
- TFilterFns,
126
- TSortingFns,
127
- TAggregationFns
128
- >,
129
- depth: number,
130
- parent?: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
131
- ) => Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
132
- getAllColumns: () => Column<
133
- TData,
134
- TValue,
135
- TFilterFns,
136
- TSortingFns,
137
- TAggregationFns
138
- >[]
139
- getAllFlatColumns: () => Column<
140
- TData,
141
- TValue,
142
- TFilterFns,
143
- TSortingFns,
144
- TAggregationFns
145
- >[]
146
- getAllFlatColumnsById: () => Record<
147
- string,
148
- Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
149
- >
150
- getAllLeafColumns: () => Column<
151
- TData,
152
- TValue,
153
- TFilterFns,
154
- TSortingFns,
155
- TAggregationFns
156
- >[]
157
- getColumn: (
158
- columnId: string
159
- ) => Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
160
- getColumnWidth: (columnId: string) => number
161
- getTotalWidth: () => number
162
- createCell: (
163
- row: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
164
- column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
165
- value: any
166
- ) => Cell<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
167
- createRow: (
168
- id: string,
169
- original: TData | undefined,
170
- rowIndex: number,
171
- depth: number,
172
- values: Record<string, any>
173
- ) => Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
174
- getCoreRowModel: () => RowModel<
175
- TData,
176
- TValue,
177
- TFilterFns,
178
- TSortingFns,
179
- TAggregationFns
180
- >
181
- getCoreRows: () => Row<
182
- TData,
183
- TValue,
184
- TFilterFns,
185
- TSortingFns,
186
- TAggregationFns
187
- >[]
188
- getCoreFlatRows: () => Row<
189
- TData,
190
- TValue,
191
- TFilterFns,
192
- TSortingFns,
193
- TAggregationFns
194
- >[]
195
- getCoreRowsById: () => Record<
196
- string,
197
- Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
198
- >
199
- getRowModel: () => RowModel<
200
- TData,
201
- TValue,
202
- TFilterFns,
203
- TSortingFns,
204
- TAggregationFns
205
- >
206
- getRows: () => Row<
207
- TData,
208
- TValue,
209
- TFilterFns,
210
- TSortingFns,
211
- TAggregationFns
212
- >[]
213
- getFlatRows: () => Row<
214
- TData,
215
- TValue,
216
- TFilterFns,
217
- TSortingFns,
218
- TAggregationFns
219
- >[]
220
- getRowsById: () => Record<
221
- string,
222
- Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
223
- >
224
- getRow: (
225
- id: string
226
- ) => Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
227
- getCell: (
228
- rowId: string,
229
- columnId: string
230
- ) => Cell<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
231
- getTableProps: PropGetter<TableProps>
232
- getTableBodyProps: PropGetter<TableBodyProps>
233
- getRowProps: <TGetter extends Getter<RowProps>>(
234
- rowId: string,
235
- userProps?: TGetter
236
- ) => undefined | PropGetterValue<RowProps, TGetter>
237
- getCellProps: <TGetter extends Getter<CellProps>>(
238
- rowId: string,
239
- columnId: string,
240
- userProps?: TGetter
241
- ) => undefined | PropGetterValue<CellProps, TGetter>
242
- getTableWidth: () => number
243
- getLeftTableWidth: () => number
244
- getCenterTableWidth: () => number
245
- getRightTableWidth: () => number
246
- }
247
-
248
- export type CoreRow<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> = {
249
- id: string
250
- index: number
251
- original?: TData
252
- depth: number
253
- values: RowValues
254
- leafRows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
255
- subRows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
256
- getRowProps: PropGetter<RowProps>
257
- originalSubRows?: TData[]
258
- getAllCells: () => Cell<
259
- TData,
260
- TValue,
261
- TFilterFns,
262
- TSortingFns,
263
- TAggregationFns
264
- >[]
265
- getAllCellsByColumnId: () => Record<
266
- string,
267
- Cell<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
268
- >
269
- }
270
-
271
- export type CoreColumnDef<
272
- TData,
273
- TValue,
274
- TFilterFns,
275
- TSortingFns,
276
- TAggregationFns
277
- > = {
278
- id: string
279
- accessorKey?: string & keyof TData
280
- accessorFn?: AccessorFn<TData>
281
- header?:
282
- | string
283
- | Renderable<{
284
- instance: ReactTable<
285
- TData,
286
- TValue,
287
- TFilterFns,
288
- TSortingFns,
289
- TAggregationFns
290
- >
291
- header: Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
292
- column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
293
- }>
294
- } & // | Renderable<{ // | string // header?: // accessorKey?: never // id: string // accessorFn: AccessorFn<TData> // | {
295
- // instance: ReactTable<
296
- // TData,
297
- // TValue,
298
- // TFilterFns,
299
- // TSortingFns,
300
- // TAggregationFns
301
- // >
302
- // header: Header<
303
- // TData,
304
- // TValue,
305
- // TFilterFns,
306
- // TSortingFns,
307
- // TAggregationFns
308
- // >
309
- // column: Column<
310
- // TData,
311
- // TValue,
312
- // TFilterFns,
313
- // TSortingFns,
314
- // TAggregationFns
315
- // >
316
- // }>
317
- // }
318
- // | {
319
- // accessorKey: string & keyof TData
320
- // id?: string
321
- // accessorFn?: never
322
- // header?:
323
- // | string
324
- // | Renderable<{
325
- // instance: ReactTable<
326
- // TData,
327
- // TValue,
328
- // TFilterFns,
329
- // TSortingFns,
330
- // TAggregationFns
331
- // >
332
- // header: Header<
333
- // TData,
334
- // TValue,
335
- // TFilterFns,
336
- // TSortingFns,
337
- // TAggregationFns
338
- // >
339
- // column: Column<
340
- // TData,
341
- // TValue,
342
- // TFilterFns,
343
- // TSortingFns,
344
- // TAggregationFns
345
- // >
346
- // }>
347
- // }
348
- // | {
349
- // id: string
350
- // accessorKey?: never
351
- // accessorFn?: never
352
- // header?:
353
- // | string
354
- // | Renderable<{
355
- // instance: ReactTable<
356
- // TData,
357
- // TValue,
358
- // TFilterFns,
359
- // TSortingFns,
360
- // TAggregationFns
361
- // >
362
- // header: Header<
363
- // TData,
364
- // TValue,
365
- // TFilterFns,
366
- // TSortingFns,
367
- // TAggregationFns
368
- // >
369
- // column: Column<
370
- // TData,
371
- // TValue,
372
- // TFilterFns,
373
- // TSortingFns,
374
- // TAggregationFns
375
- // >
376
- // }>
377
- // }
378
- // | {
379
- // header: string
380
- // id?: string
381
- // accessorKey?: never
382
- // accessorFn?: never
383
- // }
384
- {
385
- __generated: true
386
- width?: number
387
- minWidth?: number
388
- maxWidth?: number
389
- columns?: ColumnDef<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
390
- footer?: Renderable<{
391
- instance: ReactTable<
392
- TData,
393
- TValue,
394
- TFilterFns,
395
- TSortingFns,
396
- TAggregationFns
397
- >
398
- header: Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
399
- column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
400
- }>
401
- cell?: Renderable<{
402
- instance: ReactTable<
403
- TData,
404
- TValue,
405
- TFilterFns,
406
- TSortingFns,
407
- TAggregationFns
408
- >
409
- row: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
410
- column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
411
- cell: Cell<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
412
- value: TValue
413
- }>
414
- defaultIsVisible?: boolean
415
- }
416
-
417
- export type CoreColumn<
418
- TData,
419
- TValue,
420
- TFilterFns,
421
- TSortingFns,
422
- TAggregationFns
423
- > = {
424
- id: string
425
- depth: number
426
- accessorFn?: AccessorFn<TData>
427
- columnDef: ColumnDef<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
428
- getWidth: () => number
429
- columns: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
430
- parent?: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
431
- getFlatColumns: () => Column<
432
- TData,
433
- TValue,
434
- TFilterFns,
435
- TSortingFns,
436
- TAggregationFns
437
- >[]
438
- getLeafColumns: () => Column<
439
- TData,
440
- TValue,
441
- TFilterFns,
442
- TSortingFns,
443
- TAggregationFns
444
- >[]
445
- }
446
-
447
- export function createTableInstance<
448
- TData,
449
- TValue,
450
- TFilterFns,
451
- TSortingFns,
452
- TAggregationFns
453
- >(
454
- options: Options<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
455
- rerender: () => void
456
- ): ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
457
- if (process.env.NODE_ENV !== 'production' && options.debug) {
458
- console.info('Creating React Table Instance...')
459
- }
460
-
461
- let instance = {} as ReactTable<
462
- TData,
463
- TValue,
464
- TFilterFns,
465
- TSortingFns,
466
- TAggregationFns
467
- >
468
-
469
- const defaultOptions = features.reduce((obj, feature) => {
470
- return Object.assign(obj, (feature as any).getDefaultOptions?.(instance))
471
- }, {})
472
-
473
- const defaultState = {}
474
-
475
- const buildOptions = (
476
- options: Options<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
477
- ) => ({
478
- state: defaultState,
479
- ...defaultOptions,
480
- ...options,
481
- })
482
-
483
- instance.options = buildOptions(options)
484
-
485
- const initialState = {
486
- ...features.reduce((obj, feature) => {
487
- return Object.assign(obj, (feature as any).getInitialState?.())
488
- }, {}),
489
- ...(options.initialState ?? {}),
490
- } as TableState
491
-
492
- const finalInstance: ReactTable<
493
- TData,
494
- TValue,
495
- TFilterFns,
496
- TSortingFns,
497
- TAggregationFns
498
- > = {
499
- ...instance,
500
- ...features.reduce((obj, feature) => {
501
- return Object.assign(obj, (feature as any).getInstance?.(instance))
502
- }, {}),
503
- rerender,
504
- initialState,
505
- internalState: initialState,
506
- reset: () => {
507
- instance.setState(instance.initialState)
508
- },
509
- updateOptions: newOptions => {
510
- instance.options = buildOptions(newOptions)
511
- },
512
-
513
- getRowId: (
514
- _: TData,
515
- index: number,
516
- parent?: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
517
- ) => `${parent ? [parent.id, index].join('.') : index}`,
518
-
519
- getState: () => {
520
- let state = {
521
- ...instance.internalState,
522
- ...instance.options.state,
523
- }
524
-
525
- return state
526
- },
527
-
528
- setState: (
529
- updater: Updater<TableState>,
530
- shouldRerender: boolean = true
531
- ) => {
532
- const onStateChange = instance.options.onStateChange
533
-
534
- let internalState = instance.internalState
535
- let newState = functionalUpdate(updater, internalState)
536
-
537
- instance.internalState = newState
538
-
539
- if (onStateChange) {
540
- onStateChange(newState)
541
- return
542
- }
543
-
544
- if (shouldRerender) {
545
- instance.rerender()
546
- }
547
- },
548
-
549
- getDefaultColumn: memo(
550
- () => [instance.options.defaultColumn],
551
- defaultColumn => {
552
- defaultColumn = (defaultColumn ?? {}) as Partial<
553
- ColumnDef<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
554
- >
555
-
556
- return {
557
- header: (
558
- props: HeaderRenderProps<
559
- Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
560
- >
561
- ) => props.header.column.id,
562
- footer: (
563
- props: HeaderRenderProps<
564
- Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
565
- >
566
- ) => props.header.column.id,
567
- cell: ({ value = '' }: { value: any }): JSX.Element =>
568
- typeof value === 'boolean' ? value.toString() : value,
569
- ...features.reduce((obj, feature) => {
570
- return Object.assign(obj, (feature as any).getDefaultColumn?.())
571
- }, {}),
572
- ...defaultColumn,
573
- } as Partial<
574
- ColumnDef<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
575
- >
576
- },
577
- { debug: instance.options.debug, key: 'getDefaultColumn' }
578
- ),
579
-
580
- getColumnDefs: () => instance.options.columns,
581
-
582
- createColumn: (columnDef, depth: number, parent) => {
583
- const defaultColumn = instance.getDefaultColumn()
584
-
585
- let id =
586
- columnDef.id ??
587
- columnDef.accessorKey ??
588
- (typeof columnDef.header === 'string' ? columnDef.header : undefined)
589
-
590
- let accessorFn: AccessorFn<TData> | undefined
591
-
592
- if (columnDef.accessorFn) {
593
- accessorFn = columnDef.accessorFn
594
- } else if (columnDef.accessorKey) {
595
- accessorFn = (originalRow?: TData) =>
596
- (originalRow as any)[columnDef.accessorKey]
597
- }
598
-
599
- if (!id) {
600
- if (process.env.NODE_ENV !== 'production') {
601
- throw new Error(
602
- columnDef.accessorFn
603
- ? `Columns require an id when using an accessorFn`
604
- : `Columns require an id when using a non-string header`
605
- )
606
- }
607
- throw new Error()
608
- }
609
-
610
- let column: CoreColumn<
611
- TData,
612
- TValue,
613
- TFilterFns,
614
- TSortingFns,
615
- TAggregationFns
616
- > = {
617
- ...ColumnSizing.defaultColumnSizing,
618
- ...defaultColumn,
619
- ...columnDef,
620
- id: `${id}`,
621
- accessorFn,
622
- parent: parent as any,
623
- depth,
624
- columnDef,
625
- columns: [],
626
- getWidth: () => instance.getColumnWidth(column.id),
627
- getFlatColumns: memo(
628
- () => [true],
629
- () => {
630
- return [
631
- column as Column<
632
- TData,
633
- TValue,
634
- TFilterFns,
635
- TSortingFns,
636
- TAggregationFns
637
- >,
638
- ...column.columns?.flatMap(d => d.getFlatColumns()),
639
- ]
640
- },
641
- {
642
- key: 'column.getFlatColumns',
643
- debug: instance.options.debug,
644
- }
645
- ),
646
- getLeafColumns: memo(
647
- () => [instance.getOrderColumnsFn()],
648
- orderColumns => {
649
- if (column.columns?.length) {
650
- let leafColumns = column.columns.flatMap(column =>
651
- column.getLeafColumns()
652
- )
653
-
654
- return orderColumns(leafColumns)
655
- }
656
-
657
- return [
658
- column as Column<
659
- TData,
660
- TValue,
661
- TFilterFns,
662
- TSortingFns,
663
- TAggregationFns
664
- >,
665
- ]
666
- },
667
- {
668
- key: 'column.getLeafColumns',
669
- debug: instance.options.debug,
670
- }
671
- ),
672
- }
673
-
674
- column = features.reduce((obj, feature) => {
675
- return Object.assign(
676
- obj,
677
- (feature as any).createColumn?.(column, instance)
678
- )
679
- }, column)
680
-
681
- // Yes, we have to convert instance to uknown, because we know more than the compiler here.
682
- return column as Column<
683
- TData,
684
- TValue,
685
- TFilterFns,
686
- TSortingFns,
687
- TAggregationFns
688
- >
689
- },
690
-
691
- getAllColumns: memo(
692
- () => [instance.getColumnDefs()],
693
- columnDefs => {
694
- if (process.env.NODE_ENV !== 'production' && instance.options.debug)
695
- console.info('Building Columns...')
696
-
697
- const recurseColumns = (
698
- columnDefs: ColumnDef<
699
- TData,
700
- TValue,
701
- TFilterFns,
702
- TSortingFns,
703
- TAggregationFns
704
- >[],
705
- parent?: Column<
706
- TData,
707
- TValue,
708
- TFilterFns,
709
- TSortingFns,
710
- TAggregationFns
711
- >,
712
- depth = 0
713
- ): Column<
714
- TData,
715
- TValue,
716
- TFilterFns,
717
- TSortingFns,
718
- TAggregationFns
719
- >[] => {
720
- return columnDefs.map(columnDef => {
721
- const column = instance.createColumn(columnDef, depth, parent)
722
-
723
- column.columns = columnDef.columns
724
- ? recurseColumns(columnDef.columns, column, depth + 1)
725
- : []
726
-
727
- return column
728
- })
729
- }
730
-
731
- return recurseColumns(columnDefs)
732
- },
733
- { key: 'getAllColumns', debug: instance.options.debug }
734
- ),
735
-
736
- getAllFlatColumns: memo(
737
- () => [instance.getAllColumns()],
738
- allColumns => {
739
- return allColumns.flatMap(column => {
740
- return column.getFlatColumns()
741
- })
742
- },
743
- { key: 'getAllFlatColumns', debug: instance.options.debug }
744
- ),
745
-
746
- getAllFlatColumnsById: memo(
747
- () => [instance.getAllFlatColumns()],
748
- flatColumns => {
749
- return flatColumns.reduce((acc, column) => {
750
- acc[column.id] = column
751
- return acc
752
- }, {} as Record<string, Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>>)
753
- },
754
- { key: 'getAllFlatColumnsById', debug: instance.options.debug }
755
- ),
756
-
757
- getAllLeafColumns: memo(
758
- () => [instance.getAllColumns(), instance.getOrderColumnsFn()],
759
- (allColumns, orderColumns) => {
760
- let leafColumns = allColumns.flatMap(column => column.getLeafColumns())
761
- return orderColumns(leafColumns)
762
- },
763
- { key: 'getAllLeafColumns', debug: instance.options.debug }
764
- ),
765
-
766
- getColumn: columnId => {
767
- const column = instance.getAllFlatColumnsById()[columnId]
768
-
769
- if (!column) {
770
- if (process.env.NODE_ENV !== 'production') {
771
- console.warn(
772
- `[React Table] Column with id ${columnId} does not exist.`
773
- )
774
- }
775
- throw new Error()
776
- }
777
-
778
- return column
779
- },
780
-
781
- getColumnWidth: (columnId: string) => {
782
- const column = instance.getColumn(columnId)
783
-
784
- if (!column) {
785
- throw new Error()
786
- }
787
-
788
- const columnSize = instance.getState().columnSizing[column.id]
789
-
790
- return Math.min(
791
- Math.max(
792
- column.minWidth ?? ColumnSizing.defaultColumnSizing.minWidth,
793
- columnSize ?? column.width ?? ColumnSizing.defaultColumnSizing.width
794
- ),
795
- column.maxWidth ?? ColumnSizing.defaultColumnSizing.maxWidth
796
- )
797
- },
798
-
799
- createCell: (row, column, value) => {
800
- const cell: Cell<
801
- TData,
802
- TValue,
803
- TFilterFns,
804
- TSortingFns,
805
- TAggregationFns
806
- > = {
807
- id: `${row.id}_${column.id}`,
808
- rowId: row.id,
809
- columnId: column.id,
810
- row,
811
- column,
812
- value,
813
- getCellProps: userProps =>
814
- instance.getCellProps(row.id, column.id, userProps)!,
815
- renderCell: () =>
816
- flexRender(column.cell, { instance, column, row, cell, value }),
817
- }
818
-
819
- features.forEach(feature => {
820
- Object.assign(
821
- cell,
822
- (feature as any).createCell?.(
823
- cell as Cell<
824
- TData,
825
- TValue,
826
- TFilterFns,
827
- TSortingFns,
828
- TAggregationFns
829
- > &
830
- Grouping.GroupingCell,
831
- column,
832
- row as Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
833
- instance
834
- )
835
- )
836
- }, {})
837
-
838
- return cell
839
- },
840
-
841
- createRow: (id, original, rowIndex, depth, values) => {
842
- let row: CoreRow<
843
- TData,
844
- TValue,
845
- TFilterFns,
846
- TSortingFns,
847
- TAggregationFns
848
- > = {
849
- id,
850
- index: rowIndex,
851
- original,
852
- depth,
853
- values,
854
- subRows: [],
855
- leafRows: [],
856
- getRowProps: userProps => instance.getRowProps(row.id, userProps)!,
857
- getAllCells: undefined!,
858
- getAllCellsByColumnId: undefined!,
859
- }
860
-
861
- row.getAllCells = memo(
862
- () => [instance.getAllLeafColumns()],
863
- leafColumns => {
864
- return leafColumns.map(column => {
865
- return instance.createCell(
866
- row as Row<
867
- TData,
868
- TValue,
869
- TFilterFns,
870
- TSortingFns,
871
- TAggregationFns
872
- >,
873
- column,
874
- row.values[column.id]
875
- )
876
- })
877
- },
878
- {
879
- key: process.env.NODE_ENV !== 'production' ? 'row.getAllCells' : '',
880
- debug: instance.options.debug,
881
- }
882
- )
883
-
884
- row.getAllCellsByColumnId = memo(
885
- () => [row.getAllCells()],
886
- allCells => {
887
- return allCells.reduce((acc, cell) => {
888
- acc[cell.columnId] = cell
889
- return acc
890
- }, {} as Record<string, Cell<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>>)
891
- },
892
- {
893
- key: 'row.getAllCellsByColumnId',
894
- debug: instance.options.debug,
895
- }
896
- )
897
-
898
- features.forEach(feature => {
899
- Object.assign(row, (feature as any).createRow?.(row, instance))
900
- })
901
-
902
- return row as Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
903
- },
904
-
905
- getCoreRowModel: memo(
906
- () => [instance.options.data],
907
- (
908
- data
909
- ): {
910
- rows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
911
- flatRows: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
912
- rowsById: Record<
913
- string,
914
- Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
915
- >
916
- } => {
917
- if (process.env.NODE_ENV !== 'production' && instance.options.debug)
918
- console.info('Accessing...')
919
-
920
- // Access the row model using initial columns
921
- const rows: Row<
922
- TData,
923
- TValue,
924
- TFilterFns,
925
- TSortingFns,
926
- TAggregationFns
927
- >[] = []
928
- const flatRows: Row<
929
- TData,
930
- TValue,
931
- TFilterFns,
932
- TSortingFns,
933
- TAggregationFns
934
- >[] = []
935
- const rowsById: Record<
936
- string,
937
- Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
938
- > = {}
939
-
940
- const leafColumns = instance.getAllLeafColumns()
941
-
942
- const accessRow = (
943
- originalRow: TData,
944
- rowIndex: number,
945
- depth = 0,
946
- parentRows: Row<
947
- TData,
948
- TValue,
949
- TFilterFns,
950
- TSortingFns,
951
- TAggregationFns
952
- >[],
953
- parent?: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
954
- ) => {
955
- const id = instance.getRowId(originalRow, rowIndex, parent)
956
-
957
- if (!id) {
958
- if (process.env.NODE_ENV !== 'production') {
959
- throw new Error(`getRowId expected an ID, but got ${id}`)
960
- }
961
- }
962
-
963
- const values: Record<string, any> = {}
964
-
965
- for (let i = 0; i < leafColumns.length; i++) {
966
- const column = leafColumns[i]
967
- if (column && column.accessorFn) {
968
- values[column.id] = column.accessorFn(originalRow, rowIndex)
969
- }
970
- }
971
-
972
- // Make the row
973
- const row = instance.createRow(
974
- id,
975
- originalRow,
976
- rowIndex,
977
- depth,
978
- values
979
- )
980
-
981
- // Push instance row into the parentRows array
982
- parentRows.push(row)
983
- // Keep track of every row in a flat array
984
- flatRows.push(row)
985
- // Also keep track of every row by its ID
986
- rowsById[id] = row
987
-
988
- // Get the original subrows
989
- if (instance.options.getSubRows) {
990
- const originalSubRows = instance.options.getSubRows(
991
- originalRow,
992
- rowIndex
993
- )
994
-
995
- // Then recursively access them
996
- if (originalSubRows?.length) {
997
- row.originalSubRows = originalSubRows
998
- const subRows: Row<
999
- TData,
1000
- TValue,
1001
- TFilterFns,
1002
- TSortingFns,
1003
- TAggregationFns
1004
- >[] = []
1005
-
1006
- for (let i = 0; i < row.originalSubRows.length; i++) {
1007
- accessRow(
1008
- row.originalSubRows[i] as TData,
1009
- i,
1010
- depth + 1,
1011
- subRows,
1012
- row
1013
- )
1014
- }
1015
- // Keep the new subRows array on the row
1016
- row.subRows = subRows
1017
- row.leafRows = flattenBy(subRows, d => d.leafRows)
1018
- }
1019
- }
1020
- }
1021
-
1022
- for (let i = 0; i < data.length; i++) {
1023
- accessRow(data[i] as TData, i, 0, rows)
1024
- }
1025
-
1026
- return { rows, flatRows, rowsById }
1027
- },
1028
- {
1029
- key: 'getRowModel',
1030
- debug: instance.options.debug,
1031
- onChange: () => {
1032
- instance._notifyRowSelectionReset()
1033
- instance._notifyFiltersReset()
1034
- },
1035
- }
1036
- ),
1037
-
1038
- // The standard
1039
-
1040
- getCoreRows: () => {
1041
- return instance.getCoreRowModel().rows
1042
- },
1043
-
1044
- getCoreFlatRows: () => {
1045
- return instance.getCoreRowModel().flatRows
1046
- },
1047
-
1048
- getCoreRowsById: () => {
1049
- return instance.getCoreRowModel().rowsById
1050
- },
1051
-
1052
- // The final calls start at the bottom of the model,
1053
- // expanded rows, which then work their way up
1054
-
1055
- getRowModel: () => {
1056
- return instance.getExpandedRowModel()
1057
- },
1058
-
1059
- getRows: () => {
1060
- return instance.getRowModel().rows
1061
- },
1062
-
1063
- getFlatRows: () => {
1064
- return instance.getRowModel().flatRows
1065
- },
1066
-
1067
- getRowsById: () => {
1068
- return instance.getRowModel().rowsById
1069
- },
1070
-
1071
- getRow: (id: string) => {
1072
- const row = instance.getRowsById()[id]
1073
-
1074
- if (!row) {
1075
- if (process.env.NODE_ENV !== 'production') {
1076
- throw new Error(`getRow expected an ID, but got ${id}`)
1077
- }
1078
- throw new Error()
1079
- }
1080
-
1081
- return row
1082
- },
1083
-
1084
- getCell: (rowId: string, columnId: string) => {
1085
- const row = instance.getRow(rowId)
1086
-
1087
- if (!row) {
1088
- if (process.env.NODE_ENV !== 'production') {
1089
- throw new Error(`[React Table] could not find row with id ${rowId}`)
1090
- }
1091
- throw new Error()
1092
- }
1093
-
1094
- const cell = row.getAllCellsByColumnId()[columnId]
1095
-
1096
- if (!cell) {
1097
- if (process.env.NODE_ENV !== 'production') {
1098
- throw new Error(
1099
- `[React Table] could not find cell ${columnId} in row ${rowId}`
1100
- )
1101
- }
1102
- throw new Error()
1103
- }
1104
-
1105
- return cell
1106
- },
1107
-
1108
- getTableProps: userProps => {
1109
- return propGetter(
1110
- {
1111
- role: 'table',
1112
- },
1113
- userProps
1114
- )
1115
- },
1116
-
1117
- getTableBodyProps: userProps => {
1118
- return propGetter(
1119
- {
1120
- role: 'rowgroup',
1121
- },
1122
- userProps
1123
- )
1124
- },
1125
-
1126
- getRowProps: (rowId, userProps) => {
1127
- const row = instance.getRow(rowId)
1128
- if (!row) {
1129
- return
1130
- }
1131
-
1132
- return propGetter(
1133
- {
1134
- key: row.id,
1135
- role: 'row',
1136
- },
1137
- userProps
1138
- )
1139
- },
1140
-
1141
- getCellProps: (rowId, columnId, userProps) => {
1142
- const cell = instance.getCell(rowId, columnId)
1143
-
1144
- if (!cell) {
1145
- return
1146
- }
1147
-
1148
- return propGetter(
1149
- {
1150
- key: cell.id,
1151
- role: 'gridcell',
1152
- },
1153
- userProps
1154
- )
1155
- },
1156
-
1157
- getTableWidth: () =>
1158
- instance.getHeaderGroups()[0]?.headers.reduce((sum, header) => {
1159
- return sum + header.getWidth()
1160
- }, 0) ?? 0,
1161
- getLeftTableWidth: () =>
1162
- instance.getLeftHeaderGroups()[0]?.headers.reduce((sum, header) => {
1163
- return sum + header.getWidth()
1164
- }, 0) ?? 0,
1165
- getCenterTableWidth: () =>
1166
- instance.getCenterHeaderGroups()[0]?.headers.reduce((sum, header) => {
1167
- return sum + header.getWidth()
1168
- }, 0) ?? 0,
1169
- getRightTableWidth: () =>
1170
- instance.getRightHeaderGroups()[0]?.headers.reduce((sum, header) => {
1171
- return sum + header.getWidth()
1172
- }, 0) ?? 0,
1173
- }
1174
-
1175
- instance = Object.assign(instance, finalInstance)
1176
-
1177
- // This won't trigger a rerender yet, but it will force
1178
- // pagination derivation to run (particularly pageSize detection)
1179
- instance.setPagination(d => d)
1180
-
1181
- return instance
1182
- }