@tanstack/table-core 8.0.0-alpha.65 → 8.0.0-alpha.69

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 (144) hide show
  1. package/build/cjs/aggregationFns.js +25 -37
  2. package/build/cjs/aggregationFns.js.map +1 -1
  3. package/build/cjs/core.js +167 -48
  4. package/build/cjs/core.js.map +1 -1
  5. package/build/cjs/createTable.js +24 -40
  6. package/build/cjs/createTable.js.map +1 -1
  7. package/build/cjs/features/Cells.js +28 -34
  8. package/build/cjs/features/Cells.js.map +1 -1
  9. package/build/cjs/features/ColumnSizing.js +136 -225
  10. package/build/cjs/features/ColumnSizing.js.map +1 -1
  11. package/build/cjs/features/Columns.js +57 -84
  12. package/build/cjs/features/Columns.js.map +1 -1
  13. package/build/cjs/features/Expanding.js +79 -126
  14. package/build/cjs/features/Expanding.js.map +1 -1
  15. package/build/cjs/features/Filters.js +137 -288
  16. package/build/cjs/features/Filters.js.map +1 -1
  17. package/build/cjs/features/Grouping.js +71 -117
  18. package/build/cjs/features/Grouping.js.map +1 -1
  19. package/build/cjs/features/Headers.js +125 -205
  20. package/build/cjs/features/Headers.js.map +1 -1
  21. package/build/cjs/features/Ordering.js +32 -44
  22. package/build/cjs/features/Ordering.js.map +1 -1
  23. package/build/cjs/features/Pagination.js +79 -82
  24. package/build/cjs/features/Pagination.js.map +1 -1
  25. package/build/cjs/features/Pinning.js +99 -178
  26. package/build/cjs/features/Pinning.js.map +1 -1
  27. package/build/cjs/features/RowSelection.js +133 -204
  28. package/build/cjs/features/RowSelection.js.map +1 -1
  29. package/build/cjs/features/Rows.js +32 -22
  30. package/build/cjs/features/Rows.js.map +1 -1
  31. package/build/cjs/features/Sorting.js +67 -136
  32. package/build/cjs/features/Sorting.js.map +1 -1
  33. package/build/cjs/features/Visibility.js +56 -118
  34. package/build/cjs/features/Visibility.js.map +1 -1
  35. package/build/cjs/filterFns.js +48 -72
  36. package/build/cjs/filterFns.js.map +1 -1
  37. package/build/cjs/index.js +13 -13
  38. package/build/cjs/sortingFns.js +21 -21
  39. package/build/cjs/sortingFns.js.map +1 -1
  40. package/build/cjs/utils/filterRowsUtils.js +49 -55
  41. package/build/cjs/utils/filterRowsUtils.js.map +1 -1
  42. package/build/cjs/utils/getCoreRowModel.js +98 -0
  43. package/build/cjs/utils/getCoreRowModel.js.map +1 -0
  44. package/build/cjs/utils/getExpandedRowModel.js +18 -22
  45. package/build/cjs/utils/getExpandedRowModel.js.map +1 -1
  46. package/build/cjs/utils/getFacetedMinMaxValues.js +48 -0
  47. package/build/cjs/utils/getFacetedMinMaxValues.js.map +1 -0
  48. package/build/cjs/utils/getFacetedRowModel.js +50 -0
  49. package/build/cjs/utils/getFacetedRowModel.js.map +1 -0
  50. package/build/cjs/utils/getFacetedUniqueValues.js +48 -0
  51. package/build/cjs/utils/getFacetedUniqueValues.js.map +1 -0
  52. package/build/cjs/utils/getFilteredRowModel.js +130 -0
  53. package/build/cjs/utils/getFilteredRowModel.js.map +1 -0
  54. package/build/cjs/utils/getGroupedRowModel.js +126 -136
  55. package/build/cjs/utils/getGroupedRowModel.js.map +1 -1
  56. package/build/cjs/utils/getPaginationRowModel.js +39 -39
  57. package/build/cjs/utils/getPaginationRowModel.js.map +1 -1
  58. package/build/cjs/utils/getSortedRowModel.js +112 -0
  59. package/build/cjs/utils/getSortedRowModel.js.map +1 -0
  60. package/build/cjs/utils.js +36 -207
  61. package/build/cjs/utils.js.map +1 -1
  62. package/build/esm/index.js +2024 -2971
  63. package/build/esm/index.js.map +1 -1
  64. package/build/stats-html.html +1 -1
  65. package/build/stats-react.json +421 -481
  66. package/build/types/core.d.ts +3 -5
  67. package/build/types/features/Cells.d.ts +1 -1
  68. package/build/types/features/ColumnSizing.d.ts +4 -15
  69. package/build/types/features/Columns.d.ts +1 -1
  70. package/build/types/features/Expanding.d.ts +2 -7
  71. package/build/types/features/Filters.d.ts +24 -21
  72. package/build/types/features/Grouping.d.ts +5 -11
  73. package/build/types/features/Ordering.d.ts +1 -1
  74. package/build/types/features/Pagination.d.ts +3 -3
  75. package/build/types/features/Pinning.d.ts +1 -5
  76. package/build/types/features/RowSelection.d.ts +2 -8
  77. package/build/types/features/Rows.d.ts +4 -3
  78. package/build/types/features/Sorting.d.ts +5 -15
  79. package/build/types/features/Visibility.d.ts +1 -4
  80. package/build/types/index.d.ts +6 -4
  81. package/build/types/types.d.ts +3 -15
  82. package/build/types/utils/filterRowsUtils.d.ts +3 -3
  83. package/build/types/utils/getCoreRowModel.d.ts +2 -0
  84. package/build/types/utils/getFacetedMinMaxValues.d.ts +2 -0
  85. package/build/types/utils/getFacetedRowModel.d.ts +2 -0
  86. package/build/types/utils/getFacetedUniqueValues.d.ts +2 -0
  87. package/build/types/utils/{getCoreRowModelSync.d.ts → getFilteredRowModel.d.ts} +1 -1
  88. package/build/types/utils/getGroupedRowModel.d.ts +1 -1
  89. package/build/types/utils/getSortedRowModel.d.ts +2 -0
  90. package/build/types/utils.d.ts +1 -17
  91. package/build/umd/index.development.js +2053 -3002
  92. package/build/umd/index.development.js.map +1 -1
  93. package/build/umd/index.production.js +1 -1
  94. package/build/umd/index.production.js.map +1 -1
  95. package/package.json +1 -1
  96. package/src/core.ts +197 -47
  97. package/src/features/Cells.ts +7 -11
  98. package/src/features/ColumnSizing.ts +89 -139
  99. package/src/features/Columns.ts +10 -13
  100. package/src/features/Expanding.ts +62 -107
  101. package/src/features/Filters.ts +201 -308
  102. package/src/features/Grouping.ts +55 -81
  103. package/src/features/Headers.ts +18 -40
  104. package/src/features/Ordering.ts +6 -4
  105. package/src/features/Pagination.ts +31 -17
  106. package/src/features/Pinning.ts +79 -96
  107. package/src/features/RowSelection.ts +68 -118
  108. package/src/features/Rows.ts +21 -6
  109. package/src/features/Sorting.ts +47 -103
  110. package/src/features/Visibility.ts +24 -47
  111. package/src/filterFns.ts +9 -9
  112. package/src/index.ts +12 -4
  113. package/src/sortingFns.ts +11 -11
  114. package/src/types.ts +3 -18
  115. package/src/utils/filterRowsUtils.ts +47 -68
  116. package/src/utils/{getCoreRowModelSync.ts → getCoreRowModel.ts} +3 -9
  117. package/src/utils/getExpandedRowModel.ts +2 -2
  118. package/src/utils/getFacetedMinMaxValues.ts +37 -0
  119. package/src/utils/getFacetedRowModel.ts +50 -0
  120. package/src/utils/getFacetedUniqueValues.ts +37 -0
  121. package/src/utils/getFilteredRowModel.ts +150 -0
  122. package/src/utils/getGroupedRowModel.ts +53 -62
  123. package/src/utils/getPaginationRowModel.ts +1 -1
  124. package/src/utils/{getSortedRowModelSync.ts → getSortedRowModel.ts} +7 -7
  125. package/src/utils.ts +28 -236
  126. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -112
  127. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  128. package/build/cjs/utils/getCoreRowModelAsync.js +0 -125
  129. package/build/cjs/utils/getCoreRowModelAsync.js.map +0 -1
  130. package/build/cjs/utils/getCoreRowModelSync.js +0 -102
  131. package/build/cjs/utils/getCoreRowModelSync.js.map +0 -1
  132. package/build/cjs/utils/getFilteredRowModelSync.js +0 -130
  133. package/build/cjs/utils/getFilteredRowModelSync.js.map +0 -1
  134. package/build/cjs/utils/getSortedRowModelSync.js +0 -118
  135. package/build/cjs/utils/getSortedRowModelSync.js.map +0 -1
  136. package/build/types/utils/getCoreRowModelAsync.d.ts +0 -4
  137. package/build/types/utils/getFilteredRowModelAsync.d.ts +0 -4
  138. package/build/types/utils/getFilteredRowModelSync.d.ts +0 -2
  139. package/build/types/utils/getSortedRowModelAsync.d.ts +0 -13
  140. package/build/types/utils/getSortedRowModelSync.d.ts +0 -2
  141. package/src/utils/getCoreRowModelAsync.ts +0 -104
  142. package/src/utils/getFilteredRowModelAsync.ts +0 -49
  143. package/src/utils/getFilteredRowModelSync.ts +0 -150
  144. package/src/utils/getSortedRowModelAsync.ts +0 -200
@@ -2,21 +2,9 @@ import { TableGenerics, Row, RowModel, TableInstance } from '../types'
2
2
 
3
3
  export function filterRows<TGenerics extends TableGenerics>(
4
4
  rows: Row<TGenerics>[],
5
- columnIds: string[],
5
+ filterRowImpl: (row: Row<TGenerics>) => any,
6
6
  instance: TableInstance<TGenerics>
7
7
  ) {
8
- const filterRowImpl = (rowsToFilter: Row<TGenerics>[]) => {
9
- // Horizontally filter rows through each column
10
- return rowsToFilter.filter(row => {
11
- for (let i = 0; i < columnIds.length; i++) {
12
- if (row.columnFilterMap[columnIds[i]] === false) {
13
- return false
14
- }
15
- }
16
- return true
17
- })
18
- }
19
-
20
8
  if (instance.options.filterFromLeafRows) {
21
9
  return filterRowModelFromLeafs(rows, filterRowImpl, instance)
22
10
  }
@@ -26,51 +14,40 @@ export function filterRows<TGenerics extends TableGenerics>(
26
14
 
27
15
  export function filterRowModelFromLeafs<TGenerics extends TableGenerics>(
28
16
  rowsToFilter: Row<TGenerics>[],
29
- filterRows: (
30
- rowsToFilter: Row<TGenerics>[],
31
- depth: number
32
- ) => Row<TGenerics>[],
17
+ filterRow: (row: Row<TGenerics>) => Row<TGenerics>[],
33
18
  instance: TableInstance<TGenerics>
34
19
  ): RowModel<TGenerics> {
35
20
  const newFilteredFlatRows: Row<TGenerics>[] = []
36
21
  const newFilteredRowsById: Record<string, Row<TGenerics>> = {}
37
22
 
23
+ let row
24
+ let newRow
25
+
38
26
  const recurseFilterRows = (rowsToFilter: Row<TGenerics>[], depth = 0) => {
39
- rowsToFilter = rowsToFilter.slice()
27
+ const rows: Row<TGenerics>[] = []
40
28
 
41
29
  // Filter from children up first
42
30
  for (let i = 0; i < rowsToFilter.length; i++) {
43
- const row = rowsToFilter[i]
44
-
45
- if (!row.subRows?.length) {
46
- continue
31
+ row = rowsToFilter[i]!
32
+
33
+ if (row.subRows?.length) {
34
+ newRow = instance.createRow(row.id, row.original, row.index, row.depth)
35
+ newRow.columnFilterMap = row.columnFilterMap
36
+ newRow.subRows = recurseFilterRows(row.subRows, depth + 1)
37
+ if (!newRow.subRows.length) {
38
+ continue
39
+ }
40
+ row = newRow
47
41
  }
48
42
 
49
- rowsToFilter[i] = instance.createRow(
50
- row.id,
51
- row.original,
52
- row.index,
53
- row.depth,
54
- row.values
55
- )
56
-
57
- rowsToFilter[i].subRows = recurseFilterRows(row.subRows, depth + 1)
58
-
59
- if (!rowsToFilter[i].subRows.length) {
60
- rowsToFilter.splice(i, 1)
61
- i--
43
+ if (filterRow(row)) {
44
+ rows.push(row)
45
+ newFilteredRowsById[row.id] = row
46
+ newFilteredRowsById[i] = row
62
47
  }
63
48
  }
64
49
 
65
- rowsToFilter = filterRows(rowsToFilter, depth)
66
-
67
- // Apply the filter to any subRows
68
- rowsToFilter.forEach(row => {
69
- newFilteredFlatRows.push(row)
70
- newFilteredRowsById[row.id] = row
71
- })
72
-
73
- return rowsToFilter
50
+ return rows
74
51
  }
75
52
 
76
53
  return {
@@ -82,45 +59,47 @@ export function filterRowModelFromLeafs<TGenerics extends TableGenerics>(
82
59
 
83
60
  export function filterRowModelFromRoot<TGenerics extends TableGenerics>(
84
61
  rowsToFilter: Row<TGenerics>[],
85
- filterRows: (
86
- rowsToFilter: Row<TGenerics>[],
87
- depth: number
88
- ) => Row<TGenerics>[],
62
+ filterRow: (row: Row<TGenerics>) => any,
89
63
  instance: TableInstance<TGenerics>
90
64
  ): RowModel<TGenerics> {
91
65
  const newFilteredFlatRows: Row<TGenerics>[] = []
92
66
  const newFilteredRowsById: Record<string, Row<TGenerics>> = {}
93
67
 
68
+ let rows
69
+ let row
70
+ let newRow
71
+
94
72
  // Filters top level and nested rows
95
73
  const recurseFilterRows = (rowsToFilter: Row<TGenerics>[], depth = 0) => {
96
74
  // Filter from parents downward first
97
- rowsToFilter = filterRows(rowsToFilter, depth)
75
+
76
+ rows = []
98
77
 
99
78
  // Apply the filter to any subRows
100
79
  for (let i = 0; i < rowsToFilter.length; i++) {
101
- const row = rowsToFilter[i]
102
-
103
- newFilteredFlatRows.push(row)
104
- newFilteredRowsById[row.id] = row
80
+ row = rowsToFilter[i]!
81
+
82
+ const pass = filterRow(row)
83
+
84
+ if (pass) {
85
+ if (row.subRows?.length) {
86
+ newRow = instance.createRow(
87
+ row.id,
88
+ row.original,
89
+ row.index,
90
+ row.depth
91
+ )
92
+ newRow.subRows = recurseFilterRows(row.subRows, depth + 1)
93
+ row = newRow
94
+ }
105
95
 
106
- if (!row.subRows?.length) {
107
- rowsToFilter.splice(i, 1)
108
- i--
109
- continue
96
+ rows.push(row)
97
+ newFilteredFlatRows.push(row)
98
+ newFilteredRowsById[row.id] = row
110
99
  }
111
-
112
- rowsToFilter[i] = instance.createRow(
113
- row.id,
114
- row.original,
115
- row.index,
116
- row.depth,
117
- row.values
118
- )
119
-
120
- rowsToFilter[i].subRows = recurseFilterRows(row.subRows, depth + 1)
121
100
  }
122
101
 
123
- return rowsToFilter
102
+ return rows
124
103
  }
125
104
 
126
105
  return {
@@ -1,7 +1,7 @@
1
1
  import { TableInstance, Row, RowModel, TableGenerics } from '../types'
2
2
  import { memo } from '../utils'
3
3
 
4
- export function getCoreRowModelSync<TGenerics extends TableGenerics>(): (
4
+ export function getCoreRowModel<TGenerics extends TableGenerics>(): (
5
5
  instance: TableInstance<TGenerics>
6
6
  ) => () => RowModel<TGenerics> {
7
7
  return instance =>
@@ -46,13 +46,7 @@ export function getCoreRowModelSync<TGenerics extends TableGenerics>(): (
46
46
  }
47
47
 
48
48
  // Make the row
49
- const row = instance.createRow(
50
- id,
51
- originalRow,
52
- rowIndex,
53
- depth,
54
- values
55
- )
49
+ const row = instance.createRow(id, originalRow, rowIndex, depth)
56
50
 
57
51
  // Push instance row into the parentRows array
58
52
  parentRows.push(row)
@@ -94,7 +88,7 @@ export function getCoreRowModelSync<TGenerics extends TableGenerics>(): (
94
88
  return { rows, flatRows, rowsById }
95
89
  },
96
90
  {
97
- key: process.env.NODE_ENV === 'production' && 'getRowModel',
91
+ key: process.env.NODE_ENV === 'development' && 'getRowModel',
98
92
  debug: () => instance.options.debugAll ?? instance.options.debugTable,
99
93
  onChange: () => {
100
94
  instance.queue(() => {
@@ -24,7 +24,7 @@ export function getExpandedRowModel<TGenerics extends TableGenerics>(): (
24
24
  return expandRows(rowModel, instance)
25
25
  },
26
26
  {
27
- key: process.env.NODE_ENV === 'production' && 'getExpandedRowModel',
27
+ key: process.env.NODE_ENV === 'development' && 'getExpandedRowModel',
28
28
  debug: () => instance.options.debugAll ?? instance.options.debugTable,
29
29
  }
30
30
  )
@@ -42,7 +42,7 @@ export function expandRows<TGenerics extends TableGenerics>(
42
42
  if (
43
43
  instance.options.expandSubRows &&
44
44
  row.subRows?.length &&
45
- instance.getIsRowExpanded(row.id)
45
+ row.getIsExpanded()
46
46
  ) {
47
47
  row.subRows.forEach(handleRow)
48
48
  }
@@ -0,0 +1,37 @@
1
+ import { TableInstance, TableGenerics } from '../types'
2
+ import { memo } from '../utils'
3
+
4
+ export function getFacetedMinMaxValues<TGenerics extends TableGenerics>(): (
5
+ instance: TableInstance<TGenerics>,
6
+ columnId: string
7
+ ) => () => [number, number] {
8
+ return (instance, columnId) =>
9
+ memo(
10
+ () => [instance.getColumn(columnId).getFacetedRowModel()],
11
+ facetedRowModel => {
12
+ let facetedMinMaxValues: [any, any] = [
13
+ facetedRowModel.flatRows[0]?.getValue(columnId) ?? null,
14
+ facetedRowModel.flatRows[0]?.getValue(columnId) ?? null,
15
+ ]
16
+
17
+ for (let i = 0; i < facetedRowModel.flatRows.length; i++) {
18
+ const value = facetedRowModel.flatRows[i]?.getValue(columnId)
19
+
20
+ if (value < facetedMinMaxValues[0]) {
21
+ facetedMinMaxValues[0] = value
22
+ } else if (value > facetedMinMaxValues[1]) {
23
+ facetedMinMaxValues[1] = value
24
+ }
25
+ }
26
+
27
+ return facetedMinMaxValues
28
+ },
29
+ {
30
+ key:
31
+ process.env.NODE_ENV === 'development' &&
32
+ 'getFacetedMinMaxValues_' + columnId,
33
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
34
+ onChange: () => {},
35
+ }
36
+ )
37
+ }
@@ -0,0 +1,50 @@
1
+ import { TableInstance, RowModel, TableGenerics, Row } from '../types'
2
+ import { memo } from '../utils'
3
+ import { filterRows } from './filterRowsUtils'
4
+
5
+ export function getFacetedRowModel<TGenerics extends TableGenerics>(): (
6
+ instance: TableInstance<TGenerics>,
7
+ columnId: string
8
+ ) => () => RowModel<TGenerics> {
9
+ return (instance, columnId) =>
10
+ memo(
11
+ () => [
12
+ instance.getPreFilteredRowModel(),
13
+ instance.getState().columnFilters,
14
+ instance.getState().globalFilter,
15
+ instance.getFilteredRowModel(),
16
+ ],
17
+ (preRowModel, columnFilters, globalFilter) => {
18
+ if (
19
+ !preRowModel.rows.length ||
20
+ (!columnFilters?.length && !globalFilter)
21
+ ) {
22
+ return preRowModel
23
+ }
24
+
25
+ const filterableIds = [
26
+ ...columnFilters.map(d => d.id).filter(d => d !== columnId),
27
+ globalFilter ? '__global__' : undefined,
28
+ ].filter(Boolean) as string[]
29
+
30
+ const filterRowsImpl = (row: Row<TGenerics>) => {
31
+ // Horizontally filter rows through each column
32
+ for (let i = 0; i < filterableIds.length; i++) {
33
+ if (row.columnFilterMap[filterableIds[i]!] === false) {
34
+ return false
35
+ }
36
+ }
37
+ return true
38
+ }
39
+
40
+ return filterRows(preRowModel.rows, filterRowsImpl, instance)
41
+ },
42
+ {
43
+ key:
44
+ process.env.NODE_ENV === 'development' &&
45
+ 'getFacetedRowModel_' + columnId,
46
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
47
+ onChange: () => {},
48
+ }
49
+ )
50
+ }
@@ -0,0 +1,37 @@
1
+ import { TableInstance, TableGenerics } from '../types'
2
+ import { memo } from '../utils'
3
+
4
+ export function getFacetedUniqueValues<TGenerics extends TableGenerics>(): (
5
+ instance: TableInstance<TGenerics>,
6
+ columnId: string
7
+ ) => () => Map<any, number> {
8
+ return (instance, columnId) =>
9
+ memo(
10
+ () => [instance.getColumn(columnId).getFacetedRowModel()],
11
+ facetedRowModel => {
12
+ let facetedUniqueValues = new Map<any, number>()
13
+
14
+ for (let i = 0; i < facetedRowModel.flatRows.length; i++) {
15
+ const value = facetedRowModel.flatRows[i]?.getValue(columnId)
16
+
17
+ if (facetedUniqueValues.has(value)) {
18
+ facetedUniqueValues.set(
19
+ value,
20
+ (facetedUniqueValues.get(value) ?? 0) + 1
21
+ )
22
+ } else {
23
+ facetedUniqueValues.set(value, 1)
24
+ }
25
+ }
26
+
27
+ return facetedUniqueValues
28
+ },
29
+ {
30
+ key:
31
+ process.env.NODE_ENV === 'development' &&
32
+ 'getFacetedUniqueValues_' + columnId,
33
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
34
+ onChange: () => {},
35
+ }
36
+ )
37
+ }
@@ -0,0 +1,150 @@
1
+ import { ResolvedColumnFilter } from '../features/Filters'
2
+ import { TableInstance, RowModel, TableGenerics, Row } from '../types'
3
+ import { memo } from '../utils'
4
+ import { filterRows } from './filterRowsUtils'
5
+
6
+ export function getFilteredRowModel<TGenerics extends TableGenerics>(): (
7
+ instance: TableInstance<TGenerics>
8
+ ) => () => RowModel<TGenerics> {
9
+ return instance =>
10
+ memo(
11
+ () => [
12
+ instance.getPreFilteredRowModel(),
13
+ instance.getState().columnFilters,
14
+ instance.getState().globalFilter,
15
+ ],
16
+ (rowModel, columnFilters, globalFilter) => {
17
+ if (
18
+ !rowModel.rows.length ||
19
+ (!columnFilters?.length && !globalFilter)
20
+ ) {
21
+ return rowModel
22
+ }
23
+
24
+ const resolvedColumnFilters: ResolvedColumnFilter<TGenerics>[] = []
25
+ const resolvedGlobalFilters: ResolvedColumnFilter<TGenerics>[] = []
26
+
27
+ ;(columnFilters ?? []).forEach(d => {
28
+ const column = instance.getColumn(d.id)
29
+
30
+ if (!column) {
31
+ if (process.env.NODE_ENV !== 'production') {
32
+ console.warn(
33
+ `Table: Could not find a column to filter with columnId: ${d.id}`
34
+ )
35
+ }
36
+ }
37
+
38
+ const filterFn = column.getFilterFn()
39
+
40
+ if (!filterFn) {
41
+ if (process.env.NODE_ENV !== 'production') {
42
+ console.warn(
43
+ `Could not find a valid 'column.filterFn' for column with the ID: ${column.id}.`
44
+ )
45
+ }
46
+ return
47
+ }
48
+
49
+ resolvedColumnFilters.push({
50
+ id: d.id,
51
+ filterFn,
52
+ resolvedValue: filterFn.resolveFilterValue?.(d.value) ?? d.value,
53
+ })
54
+ })
55
+
56
+ const filterableIds = columnFilters.map(d => d.id)
57
+
58
+ const globalFilterFn = instance.getGlobalFilterFn()
59
+
60
+ const globallyFilterableColumns = instance
61
+ .getAllLeafColumns()
62
+ .filter(column => column.getCanGlobalFilter())
63
+
64
+ if (
65
+ globalFilter &&
66
+ globalFilterFn &&
67
+ globallyFilterableColumns.length
68
+ ) {
69
+ filterableIds.push('__global__')
70
+
71
+ globallyFilterableColumns.forEach(column => {
72
+ resolvedGlobalFilters.push({
73
+ id: column.id,
74
+ filterFn: globalFilterFn,
75
+ resolvedValue:
76
+ globalFilterFn.resolveFilterValue?.(globalFilter) ??
77
+ globalFilter,
78
+ })
79
+ })
80
+ }
81
+
82
+ let currentColumnFilter
83
+ let currentGlobalFilter
84
+
85
+ // Flag the prefiltered row model with each filter state
86
+ for (let j = 0; j < rowModel.flatRows.length; j++) {
87
+ const row = rowModel.flatRows[j]!
88
+
89
+ row.columnFilterMap = {}
90
+
91
+ if (resolvedColumnFilters.length) {
92
+ for (let i = 0; i < resolvedColumnFilters.length; i++) {
93
+ currentColumnFilter = resolvedColumnFilters[i]!
94
+
95
+ // Tag the row with the column filter state
96
+ row.columnFilterMap[currentColumnFilter.id] =
97
+ currentColumnFilter.filterFn(
98
+ row,
99
+ currentColumnFilter.id,
100
+ currentColumnFilter.resolvedValue
101
+ )
102
+ }
103
+ }
104
+
105
+ if (resolvedGlobalFilters.length) {
106
+ for (let i = 0; i < resolvedGlobalFilters.length; i++) {
107
+ currentGlobalFilter = resolvedGlobalFilters[i]!
108
+ // Tag the row with the first truthy global filter state
109
+ if (
110
+ currentGlobalFilter.filterFn(
111
+ row,
112
+ currentGlobalFilter.id,
113
+ currentGlobalFilter.resolvedValue
114
+ )
115
+ ) {
116
+ row.columnFilterMap.__global__ = true
117
+ break
118
+ }
119
+ }
120
+
121
+ if (row.columnFilterMap.__global__ !== true) {
122
+ row.columnFilterMap.__global__ = false
123
+ }
124
+ }
125
+ }
126
+
127
+ const filterRowsImpl = (row: Row<TGenerics>) => {
128
+ // Horizontally filter rows through each column
129
+ for (let i = 0; i < filterableIds.length; i++) {
130
+ if (row.columnFilterMap[filterableIds[i]!] === false) {
131
+ return false
132
+ }
133
+ }
134
+ return true
135
+ }
136
+
137
+ // Filter final rows using all of the active filters
138
+ return filterRows(rowModel.rows, filterRowsImpl, instance)
139
+ },
140
+ {
141
+ key: process.env.NODE_ENV === 'development' && 'getFilteredRowModel',
142
+ debug: () => instance.options.debugAll ?? instance.options.debugTable,
143
+ onChange: () => {
144
+ instance.queue(() => {
145
+ instance._autoResetPageIndex()
146
+ })
147
+ },
148
+ }
149
+ )
150
+ }
@@ -1,7 +1,8 @@
1
+ import { CoreRow } from '../features/Rows'
1
2
  import { TableInstance, Row, RowModel, TableGenerics } from '../types'
2
3
  import { flattenBy, memo } from '../utils'
3
4
 
4
- export function getGroupedRowModelSync<TGenerics extends TableGenerics>(): (
5
+ export function getGroupedRowModel<TGenerics extends TableGenerics>(): (
5
6
  instance: TableInstance<TGenerics>
6
7
  ) => () => RowModel<TGenerics> {
7
8
  return instance =>
@@ -17,56 +18,6 @@ export function getGroupedRowModelSync<TGenerics extends TableGenerics>(): (
17
18
  instance.getColumn(columnId)
18
19
  )
19
20
 
20
- // Find the columns that can or are aggregating
21
- // Uses each column to aggregate rows into a single value
22
- const aggregateRowsToValues = (
23
- leafRows: Row<TGenerics>[],
24
- groupedRows: Row<TGenerics>[],
25
- depth: number
26
- ) => {
27
- const values: Record<string, unknown> = {}
28
-
29
- instance.getAllLeafColumns().forEach(column => {
30
- // Don't aggregate columns that are in the grouping
31
- if (existingGrouping.includes(column.id)) {
32
- values[column.id] = groupedRows[0]
33
- ? groupedRows[0].values[column.id]
34
- : null
35
- return
36
- }
37
-
38
- // Aggregate the values
39
- const aggregateFn = instance.getColumnAggregationFn(column.id)
40
-
41
- if (aggregateFn) {
42
- values[column.id] = aggregateFn(
43
- () =>
44
- leafRows.map(row => {
45
- let columnValue = row.values[column.id]
46
-
47
- if (!depth && column.aggregateValue) {
48
- columnValue = column.aggregateValue(columnValue)
49
- }
50
-
51
- return columnValue
52
- }),
53
- () => groupedRows.map(row => row.values[column.id])
54
- )
55
- } else if (column.aggregationFn) {
56
- console.info({ column })
57
- throw new Error(
58
- process.env.NODE_ENV !== 'production'
59
- ? `Table: Invalid column.aggregateType option for column listed above`
60
- : ''
61
- )
62
- } else {
63
- values[column.id] = null
64
- }
65
- })
66
-
67
- return values
68
- }
69
-
70
21
  const groupedFlatRows: Row<TGenerics>[] = []
71
22
  const groupedRowsById: Record<string, Row<TGenerics>> = {}
72
23
  // const onlyGroupedFlatRows: Row[] = [];
@@ -104,21 +55,61 @@ export function getGroupedRowModelSync<TGenerics extends TableGenerics>(): (
104
55
  ? flattenBy(groupedRows, row => row.subRows)
105
56
  : groupedRows
106
57
 
107
- const values = aggregateRowsToValues(leafRows, groupedRows, depth)
108
-
109
- const row = instance.createRow(
110
- id,
111
- undefined,
112
- index,
113
- depth,
114
- values
115
- )
58
+ const row = instance.createRow(id, undefined, index, depth)
116
59
 
117
60
  Object.assign(row, {
118
61
  groupingColumnId: columnId,
119
62
  groupingValue,
120
63
  subRows,
121
64
  leafRows,
65
+ getValue: (columnId: string) => {
66
+ // Don't aggregate columns that are in the grouping
67
+ if (existingGrouping.includes(columnId)) {
68
+ if (row.valuesCache.hasOwnProperty(columnId)) {
69
+ return row.valuesCache[columnId]
70
+ }
71
+
72
+ if (groupedRows[0]) {
73
+ row.valuesCache[columnId] =
74
+ groupedRows[0].getValue(columnId) ?? undefined
75
+ }
76
+
77
+ return row.valuesCache[columnId]
78
+ }
79
+
80
+ if (row.groupingValuesCache.hasOwnProperty(columnId)) {
81
+ return row.groupingValuesCache[columnId]
82
+ }
83
+
84
+ // Aggregate the values
85
+ const column = instance.getColumn(columnId)
86
+ const aggregateFn = column.getColumnAggregationFn()
87
+
88
+ if (aggregateFn) {
89
+ row.groupingValuesCache[columnId] = aggregateFn(
90
+ () =>
91
+ leafRows.map(row => {
92
+ let columnValue = row.getValue(columnId)
93
+
94
+ if (!depth && column.aggregateValue) {
95
+ columnValue = column.aggregateValue(columnValue)
96
+ }
97
+
98
+ return columnValue
99
+ }),
100
+ () => groupedRows.map(row => row.getValue(columnId))
101
+ )
102
+
103
+ return row.groupingValuesCache[columnId]
104
+ } else if (column.aggregationFn) {
105
+ console.info({ column })
106
+ throw new Error(
107
+ process.env.NODE_ENV !== 'production'
108
+ ? `Table: Invalid column.aggregateType option for column listed above`
109
+ : ''
110
+ )
111
+ }
112
+ },
122
113
  })
123
114
 
124
115
  subRows.forEach(subRow => {
@@ -161,7 +152,7 @@ export function getGroupedRowModelSync<TGenerics extends TableGenerics>(): (
161
152
  }
162
153
  },
163
154
  {
164
- key: process.env.NODE_ENV === 'production' && 'getGroupedRowModel',
155
+ key: process.env.NODE_ENV === 'development' && 'getGroupedRowModel',
165
156
  debug: () => instance.options.debugAll ?? instance.options.debugTable,
166
157
  onChange: () => {
167
158
  instance.queue(() => {
@@ -180,7 +171,7 @@ function groupBy<TGenerics extends TableGenerics>(
180
171
  const groupMap = new Map<any, Row<TGenerics>[]>()
181
172
 
182
173
  return rows.reduce((map, row) => {
183
- const resKey = `${row.values[columnId]}`
174
+ const resKey = `${row.getValue(columnId)}`
184
175
  const previous = map.get(resKey)
185
176
  if (!previous) {
186
177
  map.set(resKey, [row])
@@ -41,7 +41,7 @@ export function getPaginationRowModel<TGenerics extends TableGenerics>(opts?: {
41
41
  }
42
42
  },
43
43
  {
44
- key: process.env.NODE_ENV === 'production' && 'getPaginationRowModel',
44
+ key: process.env.NODE_ENV === 'development' && 'getPaginationRowModel',
45
45
  debug: () => instance.options.debugAll ?? instance.options.debugTable,
46
46
  }
47
47
  )