Skip to content

Commit

Permalink
fix(table-core): assign unassign parentId in grouped row model (#5657)
Browse files Browse the repository at this point in the history
* fix(getGroupedRowModel): assign parentId & unassign on ungroup (including depth = 0 on ungroup)

* assign parent id before flattening rows

* simplify ungrouping

* remove nx token again

* don't remove nx token again

---------

Co-authored-by: Kevin Van Cott <[email protected]>
  • Loading branch information
PiR1 and KevinVandy authored Jul 14, 2024
1 parent bb0a536 commit a71ce10
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/table-core/src/utils/getGroupedRowModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRow } from '../core/row'
import { Table, Row, RowModel, RowData } from '../types'
import { Row, RowData, RowModel, Table } from '../types'
import { flattenBy, getMemoOptions, memo } from '../utils'
import { GroupingState } from '../features/ColumnGrouping'

export function getGroupedRowModel<TData extends RowData>(): (
table: Table<TData>
Expand All @@ -10,6 +11,10 @@ export function getGroupedRowModel<TData extends RowData>(): (
() => [table.getState().grouping, table.getPreGroupedRowModel()],
(grouping, rowModel) => {
if (!rowModel.rows.length || !grouping.length) {
rowModel.rows.forEach(row => {
row.depth = 0
row.parentId = undefined
})
return rowModel
}

Expand Down Expand Up @@ -53,7 +58,7 @@ export function getGroupedRowModel<TData extends RowData>(): (
// Group the rows together for this level
const rowGroupsMap = groupBy(rows, columnId)

// Peform aggregations for each group
// Perform aggregations for each group
const aggregatedGroupedRows = Array.from(rowGroupsMap.entries()).map(
([groupingValue, groupedRows], index) => {
let id = `${columnId}:${groupingValue}`
Expand All @@ -62,6 +67,10 @@ export function getGroupedRowModel<TData extends RowData>(): (
// First, Recurse to group sub rows before aggregation
const subRows = groupUpRecursively(groupedRows, depth + 1, id)

subRows.forEach(subRow => {
subRow.parentId = id
})

// Flatten the leaf rows of the rows in this group
const leafRows = depth
? flattenBy(groupedRows, row => row.subRows)
Expand Down

0 comments on commit a71ce10

Please sign in to comment.