@tanstack/react-table 0.0.1-alpha.8 → 8.0.0-alpha.11

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 +85 -0
  4. package/build/cjs/index.js.map +1 -0
  5. package/build/esm/index.js +73 -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 +3 -0
  10. package/build/umd/index.development.js +105 -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 +56 -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
@@ -1,907 +0,0 @@
1
- import {
2
- Cell,
3
- Column,
4
- FooterGroupProps,
5
- FooterProps,
6
- Getter,
7
- Header,
8
- HeaderGroup,
9
- HeaderGroupProps,
10
- HeaderProps,
11
- PropGetterValue,
12
- ReactTable,
13
- Row,
14
- } from '../types'
15
- import { propGetter, memo, flexRender } from '../utils'
16
-
17
- export type HeadersRow<
18
- TData,
19
- TValue,
20
- TFilterFns,
21
- TSortingFns,
22
- TAggregationFns
23
- > = {
24
- _getAllVisibleCells: () => Cell<
25
- TData,
26
- TValue,
27
- TFilterFns,
28
- TSortingFns,
29
- TAggregationFns
30
- >[]
31
- getVisibleCells: () => Cell<
32
- TData,
33
- TValue,
34
- TFilterFns,
35
- TSortingFns,
36
- TAggregationFns
37
- >[]
38
- getLeftVisibleCells: () => Cell<
39
- TData,
40
- TValue,
41
- TFilterFns,
42
- TSortingFns,
43
- TAggregationFns
44
- >[]
45
- getCenterVisibleCells: () => Cell<
46
- TData,
47
- TValue,
48
- TFilterFns,
49
- TSortingFns,
50
- TAggregationFns
51
- >[]
52
- getRightVisibleCells: () => Cell<
53
- TData,
54
- TValue,
55
- TFilterFns,
56
- TSortingFns,
57
- TAggregationFns
58
- >[]
59
- }
60
-
61
- export type HeadersInstance<
62
- TData,
63
- TValue,
64
- TFilterFns,
65
- TSortingFns,
66
- TAggregationFns
67
- > = {
68
- createHeader: (
69
- column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
70
- options: {
71
- id?: string
72
- isPlaceholder?: boolean
73
- placeholderId?: string
74
- depth: number
75
- }
76
- ) => Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
77
- getHeaderGroups: () => HeaderGroup<
78
- TData,
79
- TValue,
80
- TFilterFns,
81
- TSortingFns,
82
- TAggregationFns
83
- >[]
84
- getLeftHeaderGroups: () => HeaderGroup<
85
- TData,
86
- TValue,
87
- TFilterFns,
88
- TSortingFns,
89
- TAggregationFns
90
- >[]
91
- getCenterHeaderGroups: () => HeaderGroup<
92
- TData,
93
- TValue,
94
- TFilterFns,
95
- TSortingFns,
96
- TAggregationFns
97
- >[]
98
- getRightHeaderGroups: () => HeaderGroup<
99
- TData,
100
- TValue,
101
- TFilterFns,
102
- TSortingFns,
103
- TAggregationFns
104
- >[]
105
-
106
- getFooterGroups: () => HeaderGroup<
107
- TData,
108
- TValue,
109
- TFilterFns,
110
- TSortingFns,
111
- TAggregationFns
112
- >[]
113
- getCenterFooterGroups: () => HeaderGroup<
114
- TData,
115
- TValue,
116
- TFilterFns,
117
- TSortingFns,
118
- TAggregationFns
119
- >[]
120
- getLeftFooterGroups: () => HeaderGroup<
121
- TData,
122
- TValue,
123
- TFilterFns,
124
- TSortingFns,
125
- TAggregationFns
126
- >[]
127
- getRightFooterGroups: () => HeaderGroup<
128
- TData,
129
- TValue,
130
- TFilterFns,
131
- TSortingFns,
132
- TAggregationFns
133
- >[]
134
-
135
- getFlatHeaders: () => Header<
136
- TData,
137
- TValue,
138
- TFilterFns,
139
- TSortingFns,
140
- TAggregationFns
141
- >[]
142
- getLeftFlatHeaders: () => Header<
143
- TData,
144
- TValue,
145
- TFilterFns,
146
- TSortingFns,
147
- TAggregationFns
148
- >[]
149
- getCenterFlatHeaders: () => Header<
150
- TData,
151
- TValue,
152
- TFilterFns,
153
- TSortingFns,
154
- TAggregationFns
155
- >[]
156
- getRightFlatHeaders: () => Header<
157
- TData,
158
- TValue,
159
- TFilterFns,
160
- TSortingFns,
161
- TAggregationFns
162
- >[]
163
-
164
- getLeafHeaders: () => Header<
165
- TData,
166
- TValue,
167
- TFilterFns,
168
- TSortingFns,
169
- TAggregationFns
170
- >[]
171
- getLeftLeafHeaders: () => Header<
172
- TData,
173
- TValue,
174
- TFilterFns,
175
- TSortingFns,
176
- TAggregationFns
177
- >[]
178
- getCenterLeafHeaders: () => Header<
179
- TData,
180
- TValue,
181
- TFilterFns,
182
- TSortingFns,
183
- TAggregationFns
184
- >[]
185
- getRightLeafHeaders: () => Header<
186
- TData,
187
- TValue,
188
- TFilterFns,
189
- TSortingFns,
190
- TAggregationFns
191
- >[]
192
-
193
- getHeader: (
194
- id: string
195
- ) => Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
196
-
197
- getHeaderGroupProps: <TGetter extends Getter<HeaderGroupProps>>(
198
- id: string,
199
- userProps?: TGetter
200
- ) => undefined | PropGetterValue<HeaderGroupProps, TGetter>
201
- getFooterGroupProps: <TGetter extends Getter<FooterGroupProps>>(
202
- id: string,
203
- userProps?: TGetter
204
- ) => undefined | PropGetterValue<FooterGroupProps, TGetter>
205
- getHeaderProps: <TGetter extends Getter<HeaderProps>>(
206
- headerId: string,
207
- userProps?: TGetter
208
- ) => undefined | PropGetterValue<HeaderProps, TGetter>
209
- getFooterProps: <TGetter extends Getter<FooterProps>>(
210
- headerId: string,
211
- userProps?: TGetter
212
- ) => undefined | PropGetterValue<FooterProps, TGetter>
213
- getTotalWidth: () => number
214
- }
215
-
216
- //
217
-
218
- export function createRow<
219
- TData,
220
- TValue,
221
- TFilterFns,
222
- TSortingFns,
223
- TAggregationFns
224
- >(
225
- row: Row<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
226
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
227
- ): HeadersRow<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
228
- return {
229
- _getAllVisibleCells: memo(
230
- () => [
231
- row
232
- .getAllCells()
233
- .filter(cell => cell.column.getIsVisible())
234
- .map(d => d.id)
235
- .join('_'),
236
- ],
237
- _ => {
238
- return row.getAllCells().filter(cell => cell.column.getIsVisible())
239
- },
240
- 'row._getAllVisibleCells',
241
- instance.options.debug
242
- ),
243
- getVisibleCells: memo(
244
- () => [
245
- row.getLeftVisibleCells(),
246
- row.getCenterVisibleCells(),
247
- row.getRightVisibleCells(),
248
- ],
249
- (left, center, right) => [...left, ...center, ...right],
250
- 'row.getVisibleCells',
251
- instance.options.debug
252
- ),
253
- getCenterVisibleCells: memo(
254
- () => [
255
- row._getAllVisibleCells(),
256
- instance.getState().columnPinning.left,
257
- instance.getState().columnPinning.right,
258
- ],
259
- (allCells, left, right) => {
260
- const leftAndRight = [...(left ?? []), ...(right ?? [])]
261
-
262
- return allCells.filter(d => !leftAndRight.includes(d.columnId))
263
- },
264
- 'row.getCenterVisibleCells',
265
- instance.options.debug
266
- ),
267
- getLeftVisibleCells: memo(
268
- () => [
269
- row._getAllVisibleCells(),
270
- instance.getState().columnPinning.left,
271
- ,
272
- ],
273
- (allCells, left) => {
274
- const cells = (left ?? [])
275
- .map(columnId => allCells.find(cell => cell.columnId === columnId)!)
276
- .filter(Boolean)
277
-
278
- return cells
279
- },
280
- 'row.getLeftVisibleCells',
281
- instance.options.debug
282
- ),
283
- getRightVisibleCells: memo(
284
- () => [
285
- row._getAllVisibleCells(),
286
- instance.getState().columnPinning.right,
287
- ],
288
- (allCells, right) => {
289
- const cells = (right ?? [])
290
- .map(columnId => allCells.find(cell => cell.columnId === columnId)!)
291
- .filter(Boolean)
292
-
293
- return cells
294
- },
295
- 'row.getRightVisibleCells',
296
- instance.options.debug
297
- ),
298
- }
299
- }
300
-
301
- export function getInstance<
302
- TData,
303
- TValue,
304
- TFilterFns,
305
- TSortingFns,
306
- TAggregationFns
307
- >(
308
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
309
- ): HeadersInstance<TData, TValue, TFilterFns, TSortingFns, TAggregationFns> {
310
- return {
311
- createHeader: (
312
- column: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
313
- options: {
314
- id?: string
315
- isPlaceholder?: boolean
316
- placeholderId?: string
317
- depth: number
318
- }
319
- ) => {
320
- const id = options.id ?? column.id
321
-
322
- let header: Header<
323
- TData,
324
- TValue,
325
- TFilterFns,
326
- TSortingFns,
327
- TAggregationFns
328
- > = {
329
- id,
330
- column,
331
- isPlaceholder: options.isPlaceholder,
332
- placeholderId: options.placeholderId,
333
- depth: options.depth,
334
- subHeaders: [],
335
- colSpan: 0,
336
- rowSpan: 0,
337
- getWidth: () => {
338
- let sum = 0
339
-
340
- const recurse = (
341
- header: Header<
342
- TData,
343
- TValue,
344
- TFilterFns,
345
- TSortingFns,
346
- TAggregationFns
347
- >
348
- ) => {
349
- if (header.subHeaders.length) {
350
- header.subHeaders.forEach(recurse)
351
- } else {
352
- sum += header.column.getWidth() ?? 0
353
- }
354
- }
355
-
356
- recurse(header)
357
-
358
- return sum
359
- },
360
- getLeafHeaders: (): Header<
361
- TData,
362
- TValue,
363
- TFilterFns,
364
- TSortingFns,
365
- TAggregationFns
366
- >[] => {
367
- const leafHeaders: Header<
368
- TData,
369
- TValue,
370
- TFilterFns,
371
- TSortingFns,
372
- TAggregationFns
373
- >[] = []
374
-
375
- const recurseHeader = (
376
- h: Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>
377
- ) => {
378
- if (h.subHeaders && h.subHeaders.length) {
379
- h.subHeaders.map(recurseHeader)
380
- }
381
- leafHeaders.push(h)
382
- }
383
-
384
- recurseHeader(header)
385
- return leafHeaders
386
- },
387
- getHeaderProps: userProps =>
388
- instance.getHeaderProps(header.id, userProps)!,
389
- getFooterProps: userProps =>
390
- instance.getFooterProps(header.id, userProps)!,
391
- renderHeader: () => flexRender(column.header, { header, column }),
392
- renderFooter: () => flexRender(column.footer, { header, column }),
393
- }
394
-
395
- return header
396
- },
397
-
398
- // Header Groups
399
-
400
- getHeaderGroups: memo(
401
- () => [
402
- instance.getAllColumns(),
403
- instance.getVisibleLeafColumns(),
404
- instance.getState().columnPinning.left,
405
- instance.getState().columnPinning.right,
406
- ],
407
- (allColumns, leafColumns, left, right) => {
408
- const leftColumns = leafColumns.filter(column =>
409
- left?.includes(column.id)
410
- )
411
- const rightColumns = leafColumns.filter(column =>
412
- right?.includes(column.id)
413
- )
414
- const centerColumns = leafColumns.filter(
415
- column => !left?.includes(column.id) && !right?.includes(column.id)
416
- )
417
- const headerGroups = buildHeaderGroups(
418
- allColumns,
419
- [...leftColumns, ...centerColumns, ...rightColumns],
420
- instance
421
- )
422
-
423
- return headerGroups
424
- },
425
- 'getHeaderGroups',
426
- instance.options.debug
427
- ),
428
-
429
- getCenterHeaderGroups: memo(
430
- () => [
431
- instance.getAllColumns(),
432
- instance.getVisibleLeafColumns(),
433
- instance.getState().columnPinning.left,
434
- instance.getState().columnPinning.right,
435
- ],
436
- (allColumns, leafColumns, left, right) => {
437
- leafColumns = leafColumns.filter(
438
- column => !left?.includes(column.id) && !right?.includes(column.id)
439
- )
440
- return buildHeaderGroups(allColumns, leafColumns, instance, 'center')
441
- },
442
- 'getCenterHeaderGroups',
443
- instance.options.debug
444
- ),
445
-
446
- getLeftHeaderGroups: memo(
447
- () => [
448
- instance.getAllColumns(),
449
- instance.getVisibleLeafColumns(),
450
- instance.getState().columnPinning.left,
451
- ],
452
- (allColumns, leafColumns, left) => {
453
- leafColumns = leafColumns.filter(column => left?.includes(column.id))
454
- return buildHeaderGroups(allColumns, leafColumns, instance, 'left')
455
- },
456
- 'getLeftHeaderGroups',
457
- instance.options.debug
458
- ),
459
-
460
- getRightHeaderGroups: memo(
461
- () => [
462
- instance.getAllColumns(),
463
- instance.getVisibleLeafColumns(),
464
- instance.getState().columnPinning.right,
465
- ],
466
- (allColumns, leafColumns, right) => {
467
- leafColumns = leafColumns.filter(column => right?.includes(column.id))
468
- return buildHeaderGroups(allColumns, leafColumns, instance, 'right')
469
- },
470
- 'getRightHeaderGroups',
471
- instance.options.debug
472
- ),
473
-
474
- // Footer Groups
475
-
476
- getFooterGroups: memo(
477
- () => [instance.getHeaderGroups()],
478
- headerGroups => {
479
- return [...headerGroups].reverse()
480
- },
481
- 'getFooterGroups',
482
- instance.options.debug
483
- ),
484
-
485
- getLeftFooterGroups: memo(
486
- () => [instance.getLeftHeaderGroups()],
487
- headerGroups => {
488
- return [...headerGroups].reverse()
489
- },
490
- 'getLeftFooterGroups',
491
- instance.options.debug
492
- ),
493
-
494
- getCenterFooterGroups: memo(
495
- () => [instance.getCenterHeaderGroups()],
496
- headerGroups => {
497
- return [...headerGroups].reverse()
498
- },
499
- 'getCenterFooterGroups',
500
- instance.options.debug
501
- ),
502
-
503
- getRightFooterGroups: memo(
504
- () => [instance.getRightHeaderGroups()],
505
- headerGroups => {
506
- return [...headerGroups].reverse()
507
- },
508
- 'getRightFooterGroups',
509
- instance.options.debug
510
- ),
511
-
512
- // Flat Headers
513
-
514
- getFlatHeaders: memo(
515
- () => [instance.getHeaderGroups()],
516
- headerGroups => {
517
- return headerGroups
518
- .map(headerGroup => {
519
- return headerGroup.headers
520
- })
521
- .flat()
522
- },
523
- 'getFlatHeaders',
524
- instance.options.debug
525
- ),
526
-
527
- getLeftFlatHeaders: memo(
528
- () => [instance.getLeftHeaderGroups()],
529
- left => {
530
- return left
531
- .map(headerGroup => {
532
- return headerGroup.headers
533
- })
534
- .flat()
535
- },
536
- 'getLeftFlatHeaders',
537
- instance.options.debug
538
- ),
539
-
540
- getCenterFlatHeaders: memo(
541
- () => [instance.getCenterHeaderGroups()],
542
- left => {
543
- return left
544
- .map(headerGroup => {
545
- return headerGroup.headers
546
- })
547
- .flat()
548
- },
549
- 'getCenterFlatHeaders',
550
- instance.options.debug
551
- ),
552
-
553
- getRightFlatHeaders: memo(
554
- () => [instance.getRightHeaderGroups()],
555
- left => {
556
- return left
557
- .map(headerGroup => {
558
- return headerGroup.headers
559
- })
560
- .flat()
561
- },
562
- 'getRightFlatHeaders',
563
- instance.options.debug
564
- ),
565
-
566
- // Leaf Headers
567
-
568
- getCenterLeafHeaders: memo(
569
- () => [instance.getCenterFlatHeaders()],
570
- flatHeaders => {
571
- return flatHeaders.filter(header => !header.subHeaders?.length)
572
- },
573
- 'getCenterLeafHeaders',
574
- instance.options.debug
575
- ),
576
-
577
- getLeftLeafHeaders: memo(
578
- () => [instance.getLeftFlatHeaders()],
579
- flatHeaders => {
580
- return flatHeaders.filter(header => !header.subHeaders?.length)
581
- },
582
- 'getLeftLeafHeaders',
583
- instance.options.debug
584
- ),
585
-
586
- getRightLeafHeaders: memo(
587
- () => [instance.getRightFlatHeaders()],
588
- flatHeaders => {
589
- return flatHeaders.filter(header => !header.subHeaders?.length)
590
- },
591
- 'getRightLeafHeaders',
592
- instance.options.debug
593
- ),
594
-
595
- getLeafHeaders: memo(
596
- () => [
597
- instance.getLeftHeaderGroups(),
598
- instance.getCenterHeaderGroups(),
599
- instance.getRightHeaderGroups(),
600
- ],
601
- (left, center, right) => {
602
- return [
603
- ...(left[0]?.headers ?? []),
604
- ...(center[0]?.headers ?? []),
605
- ...(right[0]?.headers ?? []),
606
- ]
607
- .map(header => {
608
- return header.getLeafHeaders()
609
- })
610
- .flat()
611
- },
612
- 'getLeafHeaders',
613
- instance.options.debug
614
- ),
615
-
616
- getHeader: (id: string) => {
617
- const header = [
618
- ...instance.getFlatHeaders(),
619
- ...instance.getCenterFlatHeaders(),
620
- ...instance.getLeftFlatHeaders(),
621
- ...instance.getRightFlatHeaders(),
622
- ].find(d => d.id === id)
623
-
624
- if (!header) {
625
- if (process.env.NODE_ENV !== 'production') {
626
- console.warn(`Could not find header with id: ${id}`)
627
- }
628
- throw new Error()
629
- }
630
-
631
- return header
632
- },
633
-
634
- getHeaderGroupProps: (id, userProps) => {
635
- const headerGroup = instance.getHeaderGroups().find(d => d.id === id)
636
-
637
- if (!headerGroup) {
638
- return
639
- }
640
-
641
- return propGetter(
642
- {
643
- key: headerGroup.id,
644
- role: 'row',
645
- },
646
- userProps
647
- )
648
- },
649
-
650
- getFooterGroupProps: (id, userProps) => {
651
- const headerGroup = instance.getFooterGroups().find(d => d.id === id)
652
-
653
- if (!headerGroup) {
654
- return
655
- }
656
-
657
- const initialProps = {
658
- key: headerGroup.id,
659
- role: 'row',
660
- }
661
-
662
- return propGetter(initialProps, userProps)
663
- },
664
-
665
- getHeaderProps: (id, userProps) => {
666
- const header = instance.getHeader(id)
667
-
668
- if (!header) {
669
- throw new Error()
670
- }
671
-
672
- const initialProps: HeaderProps = {
673
- key: header.id,
674
- role: 'columnheader',
675
- colSpan: header.colSpan,
676
- rowSpan: header.rowSpan,
677
- }
678
-
679
- return propGetter(initialProps, userProps)
680
- },
681
-
682
- getFooterProps: (id, userProps) => {
683
- const header = instance.getHeader(id)
684
- if (!header) {
685
- return
686
- }
687
-
688
- const initialProps: FooterProps = {
689
- key: header.id,
690
- role: 'columnfooter',
691
- colSpan: header.colSpan,
692
- rowSpan: header.rowSpan,
693
- }
694
-
695
- return propGetter(initialProps, userProps)
696
- },
697
-
698
- getTotalWidth: () => {
699
- let width = 0
700
-
701
- instance.getVisibleLeafColumns().forEach(column => {
702
- width += column.getWidth() ?? 0
703
- })
704
-
705
- return width
706
- },
707
- }
708
- }
709
-
710
- export function buildHeaderGroups<
711
- TData,
712
- TValue,
713
- TFilterFns,
714
- TSortingFns,
715
- TAggregationFns
716
- >(
717
- allColumns: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[],
718
- columnsToGroup: Column<
719
- TData,
720
- TValue,
721
- TFilterFns,
722
- TSortingFns,
723
- TAggregationFns
724
- >[],
725
- instance: ReactTable<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>,
726
- headerFamily?: 'center' | 'left' | 'right'
727
- ) {
728
- // Find the max depth of the columns:
729
- // build the leaf column row
730
- // build each buffer row going up
731
- // placeholder for non-existent level
732
- // real column for existing level
733
-
734
- let maxDepth = 0
735
-
736
- const findMaxDepth = (
737
- columns: Column<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[],
738
- depth = 1
739
- ) => {
740
- maxDepth = Math.max(maxDepth, depth)
741
-
742
- columns
743
- .filter(column => column.getIsVisible())
744
- .forEach(column => {
745
- if (column.columns?.length) {
746
- findMaxDepth(column.columns, depth + 1)
747
- }
748
- }, 0)
749
- }
750
-
751
- findMaxDepth(allColumns)
752
-
753
- let headerGroups: HeaderGroup<
754
- TData,
755
- TValue,
756
- TFilterFns,
757
- TSortingFns,
758
- TAggregationFns
759
- >[] = []
760
-
761
- const createHeaderGroup = (
762
- headersToGroup: Header<
763
- TData,
764
- TValue,
765
- TFilterFns,
766
- TSortingFns,
767
- TAggregationFns
768
- >[],
769
- depth: number
770
- ) => {
771
- // The header group we are creating
772
- const headerGroup: HeaderGroup<
773
- TData,
774
- TValue,
775
- TFilterFns,
776
- TSortingFns,
777
- TAggregationFns
778
- > = {
779
- depth,
780
- id: [headerFamily, `${depth}`].filter(Boolean).join('_'),
781
- headers: [],
782
- getHeaderGroupProps: getterValue =>
783
- instance.getHeaderGroupProps(`${depth}`, getterValue)!,
784
- getFooterGroupProps: getterValue =>
785
- instance.getFooterGroupProps(`${depth}`, getterValue)!,
786
- }
787
-
788
- // The parent columns we're going to scan next
789
- const parentHeaders: Header<
790
- TData,
791
- TValue,
792
- TFilterFns,
793
- TSortingFns,
794
- TAggregationFns
795
- >[] = []
796
-
797
- // Scan each column for parents
798
- headersToGroup.forEach(headerToGroup => {
799
- // What is the latest (last) parent column?
800
-
801
- const latestParentHeader = [...parentHeaders].reverse()[0]
802
-
803
- const isLeafHeader = headerToGroup.column.depth === headerGroup.depth
804
-
805
- let column: Column<
806
- TData,
807
- TValue,
808
- TFilterFns,
809
- TSortingFns,
810
- TAggregationFns
811
- >
812
- let isPlaceholder = false
813
-
814
- if (isLeafHeader && headerToGroup.column.parent) {
815
- // The parent header is new
816
- column = headerToGroup.column.parent
817
- } else {
818
- // The parent header is repeated
819
- column = headerToGroup.column
820
- isPlaceholder = true
821
- }
822
-
823
- const header = instance.createHeader(column, {
824
- id: [headerFamily, depth, column.id, headerToGroup?.id]
825
- .filter(Boolean)
826
- .join('_'),
827
- isPlaceholder,
828
- placeholderId: isPlaceholder
829
- ? `${parentHeaders.filter(d => d.column === column).length}`
830
- : undefined,
831
- depth,
832
- })
833
-
834
- if (!latestParentHeader || latestParentHeader.column !== header.column) {
835
- header.subHeaders.push(headerToGroup)
836
- parentHeaders.push(header)
837
- } else {
838
- latestParentHeader.subHeaders.push(headerToGroup)
839
- }
840
-
841
- // if (!headerToGroup.isPlaceholder) {
842
- // headerToGroup.column.header = headerToGroup;
843
- // }
844
-
845
- headerGroup.headers.push(headerToGroup)
846
- })
847
-
848
- headerGroups.push(headerGroup)
849
-
850
- if (depth > 0) {
851
- createHeaderGroup(parentHeaders, depth - 1)
852
- }
853
- }
854
-
855
- const bottomHeaders = columnsToGroup.map(column =>
856
- instance.createHeader(column, {
857
- depth: maxDepth,
858
- })
859
- )
860
-
861
- createHeaderGroup(bottomHeaders, maxDepth - 1)
862
-
863
- headerGroups.reverse()
864
-
865
- // headerGroups = headerGroups.filter(headerGroup => {
866
- // return !headerGroup.headers.every(header => header.isPlaceholder)
867
- // })
868
-
869
- const recurseHeadersForSpans = (
870
- headers: Header<TData, TValue, TFilterFns, TSortingFns, TAggregationFns>[]
871
- ) => {
872
- const filteredHeaders = headers.filter(header =>
873
- header.column.getIsVisible()
874
- )
875
-
876
- return filteredHeaders.map(header => {
877
- let colSpan = 0
878
- let rowSpan = 0
879
- let childRowSpans = [0]
880
-
881
- if (header.subHeaders && header.subHeaders.length) {
882
- childRowSpans = []
883
-
884
- recurseHeadersForSpans(header.subHeaders).forEach(
885
- ({ colSpan: childColSpan, rowSpan: childRowSpan }) => {
886
- colSpan += childColSpan
887
- childRowSpans.push(childRowSpan)
888
- }
889
- )
890
- } else {
891
- colSpan = 1
892
- }
893
-
894
- const minChildRowSpan = Math.min(...childRowSpans)
895
- rowSpan = rowSpan + minChildRowSpan
896
-
897
- header.colSpan = colSpan > 0 ? colSpan : undefined
898
- header.rowSpan = rowSpan > 0 ? rowSpan : undefined
899
-
900
- return { colSpan, rowSpan }
901
- })
902
- }
903
-
904
- recurseHeadersForSpans(headerGroups[0]?.headers ?? [])
905
-
906
- return headerGroups
907
- }