@tanstack/react-table 8.0.0-alpha.8 → 8.0.0-alpha.83

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