( allColumns: Column<TData, unknown>[], columnsToGroup: Column<TData, unknown>[], table: Table<TData>, headerFamily?: 'center' | 'left' | 'right' )
| 482 | } |
| 483 | |
| 484 | export function buildHeaderGroups<TData extends RowData>( |
| 485 | allColumns: Column<TData, unknown>[], |
| 486 | columnsToGroup: Column<TData, unknown>[], |
| 487 | table: Table<TData>, |
| 488 | headerFamily?: 'center' | 'left' | 'right' |
| 489 | ) { |
| 490 | // Find the max depth of the columns: |
| 491 | // build the leaf column row |
| 492 | // build each buffer row going up |
| 493 | // placeholder for non-existent level |
| 494 | // real column for existing level |
| 495 | |
| 496 | let maxDepth = 0 |
| 497 | |
| 498 | const findMaxDepth = (columns: Column<TData, unknown>[], depth = 1) => { |
| 499 | maxDepth = Math.max(maxDepth, depth) |
| 500 | |
| 501 | columns |
| 502 | .filter(column => column.getIsVisible()) |
| 503 | .forEach(column => { |
| 504 | if (column.columns?.length) { |
| 505 | findMaxDepth(column.columns, depth + 1) |
| 506 | } |
| 507 | }, 0) |
| 508 | } |
| 509 | |
| 510 | findMaxDepth(allColumns) |
| 511 | |
| 512 | let headerGroups: HeaderGroup<TData>[] = [] |
| 513 | |
| 514 | const createHeaderGroup = ( |
| 515 | headersToGroup: Header<TData, unknown>[], |
| 516 | depth: number |
| 517 | ) => { |
| 518 | // The header group we are creating |
| 519 | const headerGroup: HeaderGroup<TData> = { |
| 520 | depth, |
| 521 | id: [headerFamily, `${depth}`].filter(Boolean).join('_'), |
| 522 | headers: [], |
| 523 | } |
| 524 | |
| 525 | // The parent columns we're going to scan next |
| 526 | const pendingParentHeaders: Header<TData, unknown>[] = [] |
| 527 | |
| 528 | // Scan each column for parents |
| 529 | headersToGroup.forEach(headerToGroup => { |
| 530 | // What is the latest (last) parent column? |
| 531 | |
| 532 | const latestPendingParentHeader = [...pendingParentHeaders].reverse()[0] |
| 533 | |
| 534 | const isLeafHeader = headerToGroup.column.depth === headerGroup.depth |
| 535 | |
| 536 | let column: Column<TData, unknown> |
| 537 | let isPlaceholder = false |
| 538 | |
| 539 | if (isLeafHeader && headerToGroup.column.parent) { |
| 540 | // The parent header is new |
| 541 | column = headerToGroup.column.parent |
no test coverage detected
searching dependent graphs…