@tanstack/react-table 8.0.0-alpha.4 → 8.0.0-alpha.42

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