Skip to content

Commit 066cad6

Browse files
committed
Set nesting depth for leaf rows when rows are grouped
useGroupBy doesn't set nesting depth for leaf rows like in v6, so leaf rows all had depth = 0 previously.
1 parent 580c1c3 commit 066cad6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

srcjs/__tests__/Reactable.v2.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5676,6 +5676,28 @@ describe('grouping and aggregation', () => {
56765676
])
56775677
})
56785678

5679+
it('leaf rows should have a nesting depth > 0', () => {
5680+
const props = {
5681+
data: { a: ['a', 'a', 'b'], b: [1, 2, 3], c: ['x', 'x', 'x'], d: [1, 2, 3] },
5682+
columns: [
5683+
{ name: 'col-a', accessor: 'a' },
5684+
{
5685+
name: 'col-b',
5686+
accessor: 'b',
5687+
cell: cellInfo => cellInfo.depth,
5688+
aggregated: cellInfo => cellInfo.depth,
5689+
className: 'col-b'
5690+
},
5691+
{ name: 'col-c', accessor: 'c' },
5692+
{ name: 'col-d', accessor: 'd' }
5693+
],
5694+
pivotBy: ['c', 'a'],
5695+
defaultExpanded: true
5696+
}
5697+
const { container } = render(<Reactable {...props} />)
5698+
expect(getCellsText(container, '.col-b')).toEqual(['0', '1', '2', '2', '1', '2'])
5699+
})
5700+
56795701
it('aggregated cell formatting', () => {
56805702
const props = {
56815703
data: { a: ['a', 'a', 'b'], b: [1, 2, 3], c: ['x', 'x', 'x'], d: [1, 2, 3] },

srcjs/useGroupBy.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// useGroupBy hook modified to:
22
// - Pass row objects and aggregated row objects to aggregate functions
33
// - Include groupBy columns in aggregations
4+
// - Set nesting depth for leaf rows
45

56
import React from 'react'
67
import {
@@ -285,6 +286,10 @@ function useInstance(instance) {
285286
const groupUpRecursively = (rows, depth = 0, parentId) => {
286287
// This is the last level, just return the rows
287288
if (depth === existingGroupBy.length) {
289+
// Set nesting depth for leaf rows
290+
rows.forEach(row => {
291+
row.depth = depth
292+
})
288293
return rows
289294
}
290295

0 commit comments

Comments
 (0)