@tanstack/table-core 8.0.0-alpha.84 → 8.0.0-alpha.85
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.
- package/build/cjs/aggregationFns.js +50 -51
- package/build/cjs/aggregationFns.js.map +1 -1
- package/build/cjs/features/Filters.js +1 -2
- package/build/cjs/features/Filters.js.map +1 -1
- package/build/cjs/features/Grouping.js +5 -5
- package/build/cjs/features/Grouping.js.map +1 -1
- package/build/cjs/index.js +0 -1
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/utils/getGroupedRowModel.js +5 -13
- package/build/cjs/utils/getGroupedRowModel.js.map +1 -1
- package/build/esm/index.js +62 -71
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +314 -314
- package/build/types/aggregationFns.d.ts +10 -19
- package/build/types/features/Filters.d.ts +0 -2
- package/build/types/features/Grouping.d.ts +6 -8
- package/build/umd/index.development.js +61 -71
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/aggregationFns.ts +44 -40
- package/src/features/Filters.ts +0 -5
- package/src/features/Grouping.ts +15 -18
- package/src/utils/getGroupedRowModel.ts +8 -17
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/table-core",
|
|
3
3
|
"author": "Tanner Linsley",
|
|
4
|
-
"version": "8.0.0-alpha.
|
|
4
|
+
"version": "8.0.0-alpha.85",
|
|
5
5
|
"description": "Hooks for building lightweight, fast and extendable datagrids for React",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/tanstack/react-table#readme",
|
package/src/aggregationFns.ts
CHANGED
|
@@ -1,64 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
sum,
|
|
3
|
-
min,
|
|
4
|
-
max,
|
|
5
|
-
extent,
|
|
6
|
-
mean,
|
|
7
|
-
median,
|
|
8
|
-
unique,
|
|
9
|
-
uniqueCount,
|
|
10
|
-
count,
|
|
11
|
-
}
|
|
1
|
+
import { AggregationFn } from './features/Grouping'
|
|
12
2
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function sum(_getLeafValues: () => unknown[], getChildValues: () => unknown[]) {
|
|
3
|
+
const sum: AggregationFn<any> = (columnId, _leafRows, childRows) => {
|
|
16
4
|
// It's faster to just add the aggregations together instead of
|
|
17
5
|
// process leaf nodes individually
|
|
18
|
-
return
|
|
6
|
+
return childRows.reduce(
|
|
19
7
|
(sum: number, next: unknown) => sum + (typeof next === 'number' ? next : 0),
|
|
20
8
|
0
|
|
21
9
|
)
|
|
22
10
|
}
|
|
23
11
|
|
|
24
|
-
|
|
12
|
+
const min: AggregationFn<any> = (columnId, _leafRows, childRows) => {
|
|
25
13
|
let min: number | undefined
|
|
26
14
|
|
|
27
|
-
|
|
15
|
+
childRows.forEach(row => {
|
|
16
|
+
const value = row.getValue(columnId)
|
|
17
|
+
|
|
28
18
|
if (
|
|
29
19
|
value != null &&
|
|
30
20
|
(min! > value || (min === undefined && value >= value))
|
|
31
21
|
) {
|
|
32
22
|
min = value
|
|
33
23
|
}
|
|
34
|
-
}
|
|
24
|
+
})
|
|
35
25
|
|
|
36
26
|
return min
|
|
37
27
|
}
|
|
38
28
|
|
|
39
|
-
|
|
29
|
+
const max: AggregationFn<any> = (columnId, _leafRows, childRows) => {
|
|
40
30
|
let max: number | undefined
|
|
41
31
|
|
|
42
|
-
|
|
32
|
+
childRows.forEach(row => {
|
|
33
|
+
const value = row.getValue(columnId)
|
|
43
34
|
if (
|
|
44
35
|
value != null &&
|
|
45
36
|
(max! < value || (max === undefined && value >= value))
|
|
46
37
|
) {
|
|
47
38
|
max = value
|
|
48
39
|
}
|
|
49
|
-
}
|
|
40
|
+
})
|
|
50
41
|
|
|
51
42
|
return max
|
|
52
43
|
}
|
|
53
44
|
|
|
54
|
-
|
|
55
|
-
_getLeafValues: () => unknown[],
|
|
56
|
-
getChildValues: () => unknown[]
|
|
57
|
-
) {
|
|
45
|
+
const extent: AggregationFn<any> = (columnId, _leafRows, childRows) => {
|
|
58
46
|
let min: number | undefined
|
|
59
47
|
let max: number | undefined
|
|
60
48
|
|
|
61
|
-
|
|
49
|
+
childRows.forEach(row => {
|
|
50
|
+
const value = row.getValue(columnId)
|
|
62
51
|
if (value != null) {
|
|
63
52
|
if (min === undefined) {
|
|
64
53
|
if (value >= value) min = max = value
|
|
@@ -67,36 +56,37 @@ function extent(
|
|
|
67
56
|
if (max! < value) max = value
|
|
68
57
|
}
|
|
69
58
|
}
|
|
70
|
-
}
|
|
59
|
+
})
|
|
71
60
|
|
|
72
61
|
return [min, max]
|
|
73
62
|
}
|
|
74
63
|
|
|
75
|
-
|
|
64
|
+
const mean: AggregationFn<any> = (columnId, leafRows) => {
|
|
76
65
|
let count = 0
|
|
77
66
|
let sum = 0
|
|
78
67
|
|
|
79
|
-
|
|
68
|
+
leafRows.forEach(row => {
|
|
69
|
+
let value = row.getValue(columnId)
|
|
80
70
|
if (value != null && (value = +value) >= value) {
|
|
81
71
|
++count, (sum += value)
|
|
82
72
|
}
|
|
83
|
-
}
|
|
73
|
+
})
|
|
84
74
|
|
|
85
75
|
if (count) return sum / count
|
|
86
76
|
|
|
87
77
|
return
|
|
88
78
|
}
|
|
89
79
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (!leafValues.length) {
|
|
80
|
+
const median: AggregationFn<any> = (columnId, leafRows) => {
|
|
81
|
+
if (!leafRows.length) {
|
|
93
82
|
return
|
|
94
83
|
}
|
|
95
84
|
|
|
96
85
|
let min = 0
|
|
97
86
|
let max = 0
|
|
98
87
|
|
|
99
|
-
|
|
88
|
+
leafRows.forEach(row => {
|
|
89
|
+
let value = row.getValue(columnId)
|
|
100
90
|
if (typeof value === 'number') {
|
|
101
91
|
min = Math.min(min, value)
|
|
102
92
|
max = Math.max(max, value)
|
|
@@ -106,14 +96,28 @@ function median(getLeafValues: () => unknown[]) {
|
|
|
106
96
|
return (min + max) / 2
|
|
107
97
|
}
|
|
108
98
|
|
|
109
|
-
|
|
110
|
-
return Array.from(new Set(
|
|
99
|
+
const unique: AggregationFn<any> = (columnId, leafRows) => {
|
|
100
|
+
return Array.from(new Set(leafRows.map(d => d.getValue(columnId))).values())
|
|
111
101
|
}
|
|
112
102
|
|
|
113
|
-
|
|
114
|
-
return new Set(
|
|
103
|
+
const uniqueCount: AggregationFn<any> = (columnId, leafRows) => {
|
|
104
|
+
return new Set(leafRows.map(d => d.getValue(columnId))).size
|
|
115
105
|
}
|
|
116
106
|
|
|
117
|
-
|
|
118
|
-
return
|
|
107
|
+
const count: AggregationFn<any> = (_columnId, leafRows) => {
|
|
108
|
+
return leafRows.length
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const aggregationFns = {
|
|
112
|
+
sum,
|
|
113
|
+
min,
|
|
114
|
+
max,
|
|
115
|
+
extent,
|
|
116
|
+
mean,
|
|
117
|
+
median,
|
|
118
|
+
unique,
|
|
119
|
+
uniqueCount,
|
|
120
|
+
count,
|
|
119
121
|
}
|
|
122
|
+
|
|
123
|
+
export type BuiltInAggregationFn = keyof typeof aggregationFns
|
package/src/features/Filters.ts
CHANGED
|
@@ -20,8 +20,6 @@ import {
|
|
|
20
20
|
export type FiltersTableState = {
|
|
21
21
|
columnFilters: ColumnFiltersState
|
|
22
22
|
globalFilter: any
|
|
23
|
-
// filtersProgress: number
|
|
24
|
-
// facetProgress: Record<string, number>
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
export type ColumnFiltersState = ColumnFilter[]
|
|
@@ -74,7 +72,6 @@ export type FiltersColumnDef<TGenerics extends TableGenerics> = {
|
|
|
74
72
|
filterFn?: FilterFnOption<Overwrite<TGenerics, { Value: any }>>
|
|
75
73
|
enableColumnFilter?: boolean
|
|
76
74
|
enableGlobalFilter?: boolean
|
|
77
|
-
enableFaceting?: boolean
|
|
78
75
|
}
|
|
79
76
|
|
|
80
77
|
export type FiltersColumn<TGenerics extends TableGenerics> = {
|
|
@@ -98,7 +95,6 @@ export type FiltersColumn<TGenerics extends TableGenerics> = {
|
|
|
98
95
|
export type FiltersRow<TGenerics extends TableGenerics> = {
|
|
99
96
|
columnFilters: Record<string, boolean>
|
|
100
97
|
columnFiltersMeta: Record<string, TGenerics['FilterMeta']>
|
|
101
|
-
subRowsByFacetId: Record<string, Row<TGenerics>[]>
|
|
102
98
|
}
|
|
103
99
|
|
|
104
100
|
export type FiltersOptions<TGenerics extends TableGenerics> = {
|
|
@@ -356,7 +352,6 @@ export const Filters: TableFeature = {
|
|
|
356
352
|
return {
|
|
357
353
|
columnFilters: {},
|
|
358
354
|
columnFiltersMeta: {},
|
|
359
|
-
subRowsByFacetId: {},
|
|
360
355
|
}
|
|
361
356
|
},
|
|
362
357
|
|
package/src/features/Grouping.ts
CHANGED
|
@@ -15,9 +15,14 @@ import { isFunction, makeStateUpdater, Overwrite } from '../utils'
|
|
|
15
15
|
|
|
16
16
|
export type GroupingState = string[]
|
|
17
17
|
|
|
18
|
+
export type GroupingTableState = {
|
|
19
|
+
grouping: GroupingState
|
|
20
|
+
}
|
|
21
|
+
|
|
18
22
|
export type AggregationFn<TGenerics extends TableGenerics> = (
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
columnId: string,
|
|
24
|
+
leafRows: Row<TGenerics>[],
|
|
25
|
+
childRows: Row<TGenerics>[]
|
|
21
26
|
) => any
|
|
22
27
|
|
|
23
28
|
export type CustomAggregationFns<TGenerics extends TableGenerics> = Record<
|
|
@@ -31,13 +36,8 @@ export type AggregationFnOption<TGenerics extends TableGenerics> =
|
|
|
31
36
|
| keyof TGenerics['AggregationFns']
|
|
32
37
|
| AggregationFn<TGenerics>
|
|
33
38
|
|
|
34
|
-
export type GroupingTableState = {
|
|
35
|
-
grouping: GroupingState
|
|
36
|
-
}
|
|
37
|
-
|
|
38
39
|
export type GroupingColumnDef<TGenerics extends TableGenerics> = {
|
|
39
40
|
aggregationFn?: AggregationFnOption<Overwrite<TGenerics, { Value: any }>>
|
|
40
|
-
aggregateValue?: (columnValue: unknown) => any
|
|
41
41
|
aggregatedCell?: Renderable<
|
|
42
42
|
TGenerics,
|
|
43
43
|
{
|
|
@@ -58,15 +58,15 @@ export type GroupingColumn<TGenerics extends TableGenerics> = {
|
|
|
58
58
|
getGroupedIndex: () => number
|
|
59
59
|
toggleGrouping: () => void
|
|
60
60
|
getToggleGroupingHandler: () => () => void
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
getAutoAggregationFn: () => AggregationFn<TGenerics> | undefined
|
|
62
|
+
getAggregationFn: () => AggregationFn<TGenerics> | undefined
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export type GroupingRow = {
|
|
66
66
|
groupingColumnId?: string
|
|
67
67
|
groupingValue?: any
|
|
68
68
|
getIsGrouped: () => boolean
|
|
69
|
-
|
|
69
|
+
_groupingValuesCache: Record<string, any>
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
export type GroupingCell<TGenerics extends TableGenerics> = {
|
|
@@ -87,11 +87,9 @@ export type GroupingOptions<TGenerics extends TableGenerics> = {
|
|
|
87
87
|
aggregationFns?: TGenerics['AggregationFns']
|
|
88
88
|
onGroupingChange?: OnChangeFn<GroupingState>
|
|
89
89
|
enableGrouping?: boolean
|
|
90
|
-
enableGroupingRemoval?: boolean
|
|
91
90
|
getGroupedRowModel?: (
|
|
92
91
|
instance: TableInstance<TGenerics>
|
|
93
92
|
) => () => RowModel<TGenerics>
|
|
94
|
-
|
|
95
93
|
groupedColumnMode?: false | 'reorder' | 'remove'
|
|
96
94
|
}
|
|
97
95
|
|
|
@@ -172,7 +170,7 @@ export const Grouping: TableFeature = {
|
|
|
172
170
|
column.toggleGrouping()
|
|
173
171
|
}
|
|
174
172
|
},
|
|
175
|
-
|
|
173
|
+
getAutoAggregationFn: () => {
|
|
176
174
|
const firstRow = instance.getCoreRowModel().flatRows[0]
|
|
177
175
|
|
|
178
176
|
const value = firstRow?.getValue(column.id)
|
|
@@ -187,7 +185,7 @@ export const Grouping: TableFeature = {
|
|
|
187
185
|
|
|
188
186
|
return aggregationFns.count
|
|
189
187
|
},
|
|
190
|
-
|
|
188
|
+
getAggregationFn: () => {
|
|
191
189
|
const userAggregationFns = instance.options.aggregationFns
|
|
192
190
|
|
|
193
191
|
if (!column) {
|
|
@@ -197,7 +195,7 @@ export const Grouping: TableFeature = {
|
|
|
197
195
|
return isFunction(column.aggregationFn)
|
|
198
196
|
? column.aggregationFn
|
|
199
197
|
: column.aggregationFn === 'auto'
|
|
200
|
-
? column.
|
|
198
|
+
? column.getAutoAggregationFn()
|
|
201
199
|
: (userAggregationFns as Record<string, any>)?.[
|
|
202
200
|
column.aggregationFn as string
|
|
203
201
|
] ??
|
|
@@ -240,12 +238,11 @@ export const Grouping: TableFeature = {
|
|
|
240
238
|
},
|
|
241
239
|
|
|
242
240
|
createRow: <TGenerics extends TableGenerics>(
|
|
243
|
-
row: Row<TGenerics
|
|
244
|
-
instance: TableInstance<TGenerics>
|
|
241
|
+
row: Row<TGenerics>
|
|
245
242
|
): GroupingRow => {
|
|
246
243
|
return {
|
|
247
244
|
getIsGrouped: () => !!row.groupingColumnId,
|
|
248
|
-
|
|
245
|
+
_groupingValuesCache: {},
|
|
249
246
|
}
|
|
250
247
|
},
|
|
251
248
|
|
|
@@ -77,31 +77,22 @@ export function getGroupedRowModel<TGenerics extends TableGenerics>(): (
|
|
|
77
77
|
return row._valuesCache[columnId]
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
if (row.
|
|
81
|
-
return row.
|
|
80
|
+
if (row._groupingValuesCache.hasOwnProperty(columnId)) {
|
|
81
|
+
return row._groupingValuesCache[columnId]
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
// Aggregate the values
|
|
85
85
|
const column = instance.getColumn(columnId)
|
|
86
|
-
const aggregateFn = column.
|
|
86
|
+
const aggregateFn = column.getAggregationFn()
|
|
87
87
|
|
|
88
88
|
if (aggregateFn) {
|
|
89
|
-
row.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (!depth && column.columnDef.aggregateValue) {
|
|
95
|
-
columnValue =
|
|
96
|
-
column.columnDef.aggregateValue(columnValue)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return columnValue
|
|
100
|
-
}),
|
|
101
|
-
() => groupedRows.map(row => row.getValue(columnId))
|
|
89
|
+
row._groupingValuesCache[columnId] = aggregateFn(
|
|
90
|
+
columnId,
|
|
91
|
+
leafRows,
|
|
92
|
+
groupedRows
|
|
102
93
|
)
|
|
103
94
|
|
|
104
|
-
return row.
|
|
95
|
+
return row._groupingValuesCache[columnId]
|
|
105
96
|
} else if (column.aggregationFn) {
|
|
106
97
|
console.info({ column })
|
|
107
98
|
throw new Error(
|