Skip to content

Commit

Permalink
fix(getGroupedRowModel): assign parentId & unassign on ungroup (inclu…
Browse files Browse the repository at this point in the history
…ding depth = 0 on ungroup)
  • Loading branch information
PiR1 committed Jul 12, 2024
1 parent 0054eb3 commit 8654474
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/table-core/src/utils/getGroupedRowModel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
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>
) => () => RowModel<TData> {
let lastGrouping: GroupingState

return table =>
memo(
() => [table.getState().grouping, table.getPreGroupedRowModel()],
(grouping, rowModel) => {
//ungrouping
if (lastGrouping?.length > 0 && grouping.length === 0) {
rowModel.rows.forEach(row => {
row.depth = 0
delete row.parentId
})
}
lastGrouping = grouping

if (!rowModel.rows.length || !grouping.length) {
return rowModel
}
Expand Down Expand Up @@ -118,6 +130,10 @@ export function getGroupedRowModel<TData extends RowData>(): (
})

subRows.forEach(subRow => {
if (!subRow.parentId) {
Object.assign(subRow, { parentId: id })
}

groupedFlatRows.push(subRow)
groupedRowsById[subRow.id] = subRow
// if (subRow.getIsGrouped?.()) {
Expand Down

0 comments on commit 8654474

Please sign in to comment.