@tanstack/react-table 8.0.0-alpha.2 → 8.0.0-alpha.20

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