@tanstack/react-table 0.0.1-alpha.9 → 8.0.0-alpha.14

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