( table: Table<TData>, rowModel: RowModel<TData> )
| 579 | } |
| 580 | |
| 581 | export function selectRowsFn<TData extends RowData>( |
| 582 | table: Table<TData>, |
| 583 | rowModel: RowModel<TData> |
| 584 | ): RowModel<TData> { |
| 585 | const rowSelection = table.getState().rowSelection |
| 586 | |
| 587 | const newSelectedFlatRows: Row<TData>[] = [] |
| 588 | const newSelectedRowsById: Record<string, Row<TData>> = {} |
| 589 | |
| 590 | // Filters top level and nested rows |
| 591 | const recurseRows = (rows: Row<TData>[], depth = 0): Row<TData>[] => { |
| 592 | return rows |
| 593 | .map(row => { |
| 594 | const isSelected = isRowSelected(row, rowSelection) |
| 595 | |
| 596 | if (isSelected) { |
| 597 | newSelectedFlatRows.push(row) |
| 598 | newSelectedRowsById[row.id] = row |
| 599 | } |
| 600 | |
| 601 | if (row.subRows?.length) { |
| 602 | row = { |
| 603 | ...row, |
| 604 | subRows: recurseRows(row.subRows, depth + 1), |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (isSelected) { |
| 609 | return row |
| 610 | } |
| 611 | }) |
| 612 | .filter(Boolean) as Row<TData>[] |
| 613 | } |
| 614 | |
| 615 | return { |
| 616 | rows: recurseRows(rowModel.rows), |
| 617 | flatRows: newSelectedFlatRows, |
| 618 | rowsById: newSelectedRowsById, |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | export function isRowSelected<TData extends RowData>( |
| 623 | row: Row<TData>, |
no test coverage detected
searching dependent graphs…