Skip to content

Commit 197fd75

Browse files
authored
feat: improve getGroupingValue (#6351)
1 parent 4be62f6 commit 197fd75

3 files changed

Lines changed: 102 additions & 3 deletions

File tree

‎packages/table-core/src/features/column-grouping/columnGroupingFeature.types.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,16 @@ export interface ColumnDef_ColumnGrouping<
8787
*/
8888
enableGrouping?: boolean
8989
/**
90-
* Specify a value to be used for grouping rows on this column. If this option is not specified, the value derived from `accessorKey` / `accessorFn` will be used instead.
91-
*/
92-
getGroupingValue?: (row: TData) => any
90+
* Returns the value used to group rows for this column.
91+
*
92+
* When omitted, grouping uses the value derived from this column's
93+
* `accessorKey` or `accessorFn`.
94+
*/
95+
getGroupingValue?: (
96+
originalRow: TData,
97+
index: number,
98+
row: Row<TFeatures, TData>,
99+
) => any
93100
}
94101

95102
export interface Column_ColumnGrouping<

‎packages/table-core/src/features/column-grouping/columnGroupingFeature.utils.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ export function row_getGroupingValue<
284284
if (row._groupingValuesCache) {
285285
row._groupingValuesCache[columnId] = column.columnDef.getGroupingValue(
286286
row.original,
287+
row.index,
288+
row,
287289
)
288290
}
289291

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { describe, expect, it } from 'vitest'
2+
import {
3+
aggregationFns,
4+
columnGroupingFeature,
5+
constructTable,
6+
coreFeatures,
7+
createGroupedRowModel,
8+
} from '../../../../src'
9+
import { storeReactivityBindings } from '../../../../src/store-reactivity-bindings'
10+
import type { ColumnDef } from '../../../../src'
11+
12+
const features = {
13+
...coreFeatures,
14+
columnGroupingFeature,
15+
coreReactivityFeature: storeReactivityBindings(),
16+
groupedRowModel: createGroupedRowModel(),
17+
aggregationFns,
18+
}
19+
20+
interface TestRow {
21+
bucket: string
22+
label: string
23+
}
24+
25+
describe('columnGroupingFeature', () => {
26+
it('passes the original row, row index, and row instance to getGroupingValue', () => {
27+
const data: Array<TestRow> = [
28+
{ bucket: 'alpha', label: 'first' },
29+
{ bucket: 'alpha', label: 'second' },
30+
]
31+
const calls: Array<{
32+
originalRow: TestRow
33+
index: number
34+
rowId: string
35+
rowIndex: number
36+
rowOriginal: TestRow
37+
}> = []
38+
const columns: Array<ColumnDef<typeof features, TestRow, any>> = [
39+
{
40+
id: 'bucket',
41+
accessorKey: 'bucket',
42+
getGroupingValue: (originalRow, index, row) => {
43+
calls.push({
44+
originalRow,
45+
index,
46+
rowId: row.id,
47+
rowIndex: row.index,
48+
rowOriginal: row.original,
49+
})
50+
51+
return `${originalRow.bucket}-${index}`
52+
},
53+
},
54+
{
55+
id: 'label',
56+
accessorKey: 'label',
57+
},
58+
]
59+
60+
const table = constructTable<typeof features, TestRow>({
61+
features,
62+
renderFallbackValue: '',
63+
data,
64+
columns,
65+
initialState: {
66+
grouping: ['bucket'],
67+
},
68+
})
69+
70+
expect(
71+
table.getGroupedRowModel().rows.map((row) => row.groupingValue),
72+
).toEqual(['alpha-0', 'alpha-1'])
73+
expect(calls).toEqual([
74+
{
75+
originalRow: data[0],
76+
index: 0,
77+
rowId: '0',
78+
rowIndex: 0,
79+
rowOriginal: data[0],
80+
},
81+
{
82+
originalRow: data[1],
83+
index: 1,
84+
rowId: '1',
85+
rowIndex: 1,
86+
rowOriginal: data[1],
87+
},
88+
])
89+
})
90+
})

0 commit comments

Comments
 (0)