()
| 4 | import { GroupingState } from '../features/ColumnGrouping' |
| 5 | |
| 6 | export function getGroupedRowModel<TData extends RowData>(): ( |
| 7 | table: Table<TData> |
| 8 | ) => () => RowModel<TData> { |
| 9 | return table => |
| 10 | memo( |
| 11 | () => [table.getState().grouping, table.getPreGroupedRowModel()], |
| 12 | (grouping, rowModel) => { |
| 13 | if (!rowModel.rows.length || !grouping.length) { |
| 14 | rowModel.rows.forEach(row => { |
| 15 | row.depth = 0 |
| 16 | row.parentId = undefined |
| 17 | }) |
| 18 | return rowModel |
| 19 | } |
| 20 | |
| 21 | // Filter the grouping list down to columns that exist |
| 22 | const existingGrouping = grouping.filter(columnId => |
| 23 | table.getColumn(columnId) |
| 24 | ) |
| 25 | |
| 26 | const groupedFlatRows: Row<TData>[] = [] |
| 27 | const groupedRowsById: Record<string, Row<TData>> = {} |
| 28 | // const onlyGroupedFlatRows: Row[] = []; |
| 29 | // const onlyGroupedRowsById: Record<RowId, Row> = {}; |
| 30 | // const nonGroupedFlatRows: Row[] = []; |
| 31 | // const nonGroupedRowsById: Record<RowId, Row> = {}; |
| 32 | |
| 33 | // Recursively group the data |
| 34 | const groupUpRecursively = ( |
| 35 | rows: Row<TData>[], |
| 36 | depth = 0, |
| 37 | parentId?: string |
| 38 | ) => { |
| 39 | // Grouping depth has been been met |
| 40 | // Stop grouping and simply rewrite thd depth and row relationships |
| 41 | if (depth >= existingGrouping.length) { |
| 42 | return rows.map(row => { |
| 43 | row.depth = depth |
| 44 | |
| 45 | groupedFlatRows.push(row) |
| 46 | groupedRowsById[row.id] = row |
| 47 | |
| 48 | if (row.subRows) { |
| 49 | row.subRows = groupUpRecursively(row.subRows, depth + 1, row.id) |
| 50 | } |
| 51 | |
| 52 | return row |
| 53 | }) |
| 54 | } |
| 55 | |
| 56 | const columnId: string = existingGrouping[depth]! |
| 57 | |
| 58 | // Group the rows together for this level |
| 59 | const rowGroupsMap = groupBy(rows, columnId) |
| 60 | |
| 61 | // Perform aggregations for each group |
| 62 | const aggregatedGroupedRows = Array.from(rowGroupsMap.entries()).map( |
| 63 | ([groupingValue, groupedRows], index) => { |
no test coverage detected
searching dependent graphs…