@tanstack/react-table 8.0.0-alpha.6 → 8.0.0-alpha.62

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