@tanstack/table-core 8.0.0-alpha.64 → 8.0.0-alpha.68

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 (149) 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 -47
  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 +29 -35
  8. package/build/cjs/features/Cells.js.map +1 -1
  9. package/build/cjs/features/ColumnSizing.js +126 -218
  10. package/build/cjs/features/ColumnSizing.js.map +1 -1
  11. package/build/cjs/features/Columns.js +59 -86
  12. package/build/cjs/features/Columns.js.map +1 -1
  13. package/build/cjs/features/Expanding.js +78 -125
  14. package/build/cjs/features/Expanding.js.map +1 -1
  15. package/build/cjs/features/Filters.js +143 -218
  16. package/build/cjs/features/Filters.js.map +1 -1
  17. package/build/cjs/features/Grouping.js +70 -116
  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 +31 -43
  22. package/build/cjs/features/Ordering.js.map +1 -1
  23. package/build/cjs/features/Pagination.js +64 -72
  24. package/build/cjs/features/Pagination.js.map +1 -1
  25. package/build/cjs/features/Pinning.js +96 -177
  26. package/build/cjs/features/Pinning.js.map +1 -1
  27. package/build/cjs/features/RowSelection.js +134 -205
  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 +62 -131
  32. package/build/cjs/features/Sorting.js.map +1 -1
  33. package/build/cjs/features/Visibility.js +53 -120
  34. package/build/cjs/features/Visibility.js.map +1 -1
  35. package/build/cjs/filterFns.js +52 -133
  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 +56 -41
  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 -211
  61. package/build/cjs/utils.js.map +1 -1
  62. package/build/esm/index.js +1970 -2956
  63. package/build/esm/index.js.map +1 -1
  64. package/build/stats-html.html +1 -1
  65. package/build/stats-react.json +390 -482
  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 +2 -13
  69. package/build/types/features/Columns.d.ts +1 -1
  70. package/build/types/features/Expanding.d.ts +1 -6
  71. package/build/types/features/Filters.d.ts +42 -30
  72. package/build/types/features/Grouping.d.ts +4 -10
  73. package/build/types/features/Pinning.d.ts +0 -4
  74. package/build/types/features/RowSelection.d.ts +1 -7
  75. package/build/types/features/Rows.d.ts +4 -3
  76. package/build/types/features/Sorting.d.ts +3 -14
  77. package/build/types/features/Visibility.d.ts +0 -4
  78. package/build/types/filterFns.d.ts +10 -52
  79. package/build/types/index.d.ts +6 -5
  80. package/build/types/types.d.ts +5 -17
  81. package/build/types/utils/filterRowsUtils.d.ts +3 -2
  82. package/build/types/utils/getCoreRowModel.d.ts +2 -0
  83. package/build/types/utils/getFacetedMinMaxValues.d.ts +2 -0
  84. package/build/types/utils/getFacetedRowModel.d.ts +2 -0
  85. package/build/types/utils/getFacetedUniqueValues.d.ts +2 -0
  86. package/build/types/utils/{getCoreRowModelSync.d.ts → getFilteredRowModel.d.ts} +1 -1
  87. package/build/types/utils/getGroupedRowModel.d.ts +1 -1
  88. package/build/types/utils/getSortedRowModel.d.ts +2 -0
  89. package/build/types/utils.d.ts +2 -18
  90. package/build/umd/index.development.js +1948 -2935
  91. package/build/umd/index.development.js.map +1 -1
  92. package/build/umd/index.production.js +1 -1
  93. package/build/umd/index.production.js.map +1 -1
  94. package/package.json +2 -1
  95. package/src/core.ts +197 -46
  96. package/src/features/Cells.ts +10 -12
  97. package/src/features/ColumnSizing.ts +72 -129
  98. package/src/features/Columns.ts +16 -15
  99. package/src/features/Expanding.ts +58 -105
  100. package/src/features/Filters.ts +223 -226
  101. package/src/features/Grouping.ts +51 -79
  102. package/src/features/Headers.ts +18 -40
  103. package/src/features/Ordering.ts +1 -1
  104. package/src/features/Pagination.ts +1 -1
  105. package/src/features/Pinning.ts +73 -92
  106. package/src/features/RowSelection.ts +71 -120
  107. package/src/features/Rows.ts +21 -6
  108. package/src/features/Sorting.ts +39 -99
  109. package/src/features/Visibility.ts +21 -50
  110. package/src/filterFns.ts +74 -147
  111. package/src/index.ts +12 -5
  112. package/src/sortingFns.ts +11 -11
  113. package/src/types.ts +5 -18
  114. package/src/utils/filterRowsUtils.ts +59 -56
  115. package/src/utils/{getCoreRowModelSync.ts → getCoreRowModel.ts} +3 -9
  116. package/src/utils/getExpandedRowModel.ts +2 -2
  117. package/src/utils/getFacetedMinMaxValues.ts +37 -0
  118. package/src/utils/getFacetedRowModel.ts +50 -0
  119. package/src/utils/getFacetedUniqueValues.ts +37 -0
  120. package/src/utils/getFilteredRowModel.ts +150 -0
  121. package/src/utils/getGroupedRowModel.ts +53 -62
  122. package/src/utils/getPaginationRowModel.ts +1 -1
  123. package/src/utils/{getSortedRowModelSync.ts → getSortedRowModel.ts} +7 -7
  124. package/src/utils.ts +29 -239
  125. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -112
  126. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  127. package/build/cjs/utils/getColumnFilteredRowModelSync.js +0 -115
  128. package/build/cjs/utils/getColumnFilteredRowModelSync.js.map +0 -1
  129. package/build/cjs/utils/getCoreRowModelAsync.js +0 -125
  130. package/build/cjs/utils/getCoreRowModelAsync.js.map +0 -1
  131. package/build/cjs/utils/getCoreRowModelSync.js +0 -102
  132. package/build/cjs/utils/getCoreRowModelSync.js.map +0 -1
  133. package/build/cjs/utils/getGlobalFilteredRowModelSync.js +0 -91
  134. package/build/cjs/utils/getGlobalFilteredRowModelSync.js.map +0 -1
  135. package/build/cjs/utils/getSortedRowModelSync.js +0 -118
  136. package/build/cjs/utils/getSortedRowModelSync.js.map +0 -1
  137. package/build/types/utils/getColumnFilteredRowModelAsync.d.ts +0 -4
  138. package/build/types/utils/getColumnFilteredRowModelSync.d.ts +0 -2
  139. package/build/types/utils/getCoreRowModelAsync.d.ts +0 -4
  140. package/build/types/utils/getGlobalFilteredRowModelAsync.d.ts +0 -4
  141. package/build/types/utils/getGlobalFilteredRowModelSync.d.ts +0 -2
  142. package/build/types/utils/getSortedRowModelAsync.d.ts +0 -13
  143. package/build/types/utils/getSortedRowModelSync.d.ts +0 -2
  144. package/src/utils/getColumnFilteredRowModelAsync.ts +0 -118
  145. package/src/utils/getColumnFilteredRowModelSync.ts +0 -102
  146. package/src/utils/getCoreRowModelAsync.ts +0 -104
  147. package/src/utils/getGlobalFilteredRowModelAsync.ts +0 -96
  148. package/src/utils/getGlobalFilteredRowModelSync.ts +0 -79
  149. package/src/utils/getSortedRowModelAsync.ts +0 -200
@@ -1,52 +1,53 @@
1
1
  import { TableGenerics, Row, RowModel, TableInstance } from '../types'
2
2
 
3
+ export function filterRows<TGenerics extends TableGenerics>(
4
+ rows: Row<TGenerics>[],
5
+ filterRowImpl: (row: Row<TGenerics>) => any,
6
+ instance: TableInstance<TGenerics>
7
+ ) {
8
+ if (instance.options.filterFromLeafRows) {
9
+ return filterRowModelFromLeafs(rows, filterRowImpl, instance)
10
+ }
11
+
12
+ return filterRowModelFromRoot(rows, filterRowImpl, instance)
13
+ }
14
+
3
15
  export function filterRowModelFromLeafs<TGenerics extends TableGenerics>(
4
16
  rowsToFilter: Row<TGenerics>[],
5
- filterRows: (
6
- rowsToFilter: Row<TGenerics>[],
7
- depth: number
8
- ) => Row<TGenerics>[],
17
+ filterRow: (row: Row<TGenerics>) => Row<TGenerics>[],
9
18
  instance: TableInstance<TGenerics>
10
19
  ): RowModel<TGenerics> {
11
20
  const newFilteredFlatRows: Row<TGenerics>[] = []
12
21
  const newFilteredRowsById: Record<string, Row<TGenerics>> = {}
13
22
 
23
+ let row
24
+ let newRow
25
+
14
26
  const recurseFilterRows = (rowsToFilter: Row<TGenerics>[], depth = 0) => {
15
- rowsToFilter = rowsToFilter.slice()
27
+ const rows: Row<TGenerics>[] = []
16
28
 
17
29
  // Filter from children up first
18
30
  for (let i = 0; i < rowsToFilter.length; i++) {
19
- const row = rowsToFilter[i]
20
-
21
- if (!row.subRows?.length) {
22
- 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
23
41
  }
24
42
 
25
- rowsToFilter[i] = instance.createRow(
26
- row.id,
27
- row.original,
28
- row.index,
29
- row.depth,
30
- row.values
31
- )
32
-
33
- rowsToFilter[i].subRows = recurseFilterRows(row.subRows, depth + 1)
34
-
35
- if (!rowsToFilter[i].subRows.length) {
36
- rowsToFilter.splice(i, 1)
37
- i--
43
+ if (filterRow(row)) {
44
+ rows.push(row)
45
+ newFilteredRowsById[row.id] = row
46
+ newFilteredRowsById[i] = row
38
47
  }
39
48
  }
40
49
 
41
- rowsToFilter = filterRows(rowsToFilter, depth)
42
-
43
- // Apply the filter to any subRows
44
- rowsToFilter.forEach(row => {
45
- newFilteredFlatRows.push(row)
46
- newFilteredRowsById[row.id] = row
47
- })
48
-
49
- return rowsToFilter
50
+ return rows
50
51
  }
51
52
 
52
53
  return {
@@ -58,45 +59,47 @@ export function filterRowModelFromLeafs<TGenerics extends TableGenerics>(
58
59
 
59
60
  export function filterRowModelFromRoot<TGenerics extends TableGenerics>(
60
61
  rowsToFilter: Row<TGenerics>[],
61
- filterRows: (
62
- rowsToFilter: Row<TGenerics>[],
63
- depth: number
64
- ) => Row<TGenerics>[],
62
+ filterRow: (row: Row<TGenerics>) => any,
65
63
  instance: TableInstance<TGenerics>
66
64
  ): RowModel<TGenerics> {
67
65
  const newFilteredFlatRows: Row<TGenerics>[] = []
68
66
  const newFilteredRowsById: Record<string, Row<TGenerics>> = {}
69
67
 
68
+ let rows
69
+ let row
70
+ let newRow
71
+
70
72
  // Filters top level and nested rows
71
73
  const recurseFilterRows = (rowsToFilter: Row<TGenerics>[], depth = 0) => {
72
74
  // Filter from parents downward first
73
- rowsToFilter = filterRows(rowsToFilter, depth)
75
+
76
+ rows = []
74
77
 
75
78
  // Apply the filter to any subRows
76
79
  for (let i = 0; i < rowsToFilter.length; i++) {
77
- const row = rowsToFilter[i]
78
-
79
- newFilteredFlatRows.push(row)
80
- newFilteredRowsById[row.id] = row
81
-
82
- if (!row.subRows?.length) {
83
- rowsToFilter.splice(i, 1)
84
- i--
85
- continue
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
+ }
95
+
96
+ rows.push(row)
97
+ newFilteredFlatRows.push(row)
98
+ newFilteredRowsById[row.id] = row
86
99
  }
87
-
88
- rowsToFilter[i] = instance.createRow(
89
- row.id,
90
- row.original,
91
- row.index,
92
- row.depth,
93
- row.values
94
- )
95
-
96
- rowsToFilter[i].subRows = recurseFilterRows(row.subRows, depth + 1)
97
100
  }
98
101
 
99
- return rowsToFilter
102
+ return rows
100
103
  }
101
104
 
102
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: '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: '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: '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: 'getPaginationRowModel',
44
+ key: process.env.NODE_ENV === 'development' && 'getPaginationRowModel',
45
45
  debug: () => instance.options.debugAll ?? instance.options.debugTable,
46
46
  }
47
47
  )