MCPcopy Index your code
hub / github.com/TanStack/table / buildHeaderGroups

Function buildHeaderGroups

packages/table-core/src/core/headers.ts:484–639  ·  view source on GitHub ↗
(
  allColumns: Column<TData, unknown>[],
  columnsToGroup: Column<TData, unknown>[],
  table: Table<TData>,
  headerFamily?: 'center' | 'left' | 'right',
)

Source from the content-addressed store, hash-verified

482}
483
484export 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

Callers 1

headers.tsFile · 0.85

Calls 4

findMaxDepthFunction · 0.85
createHeaderFunction · 0.85
createHeaderGroupFunction · 0.85
recurseHeadersForSpansFunction · 0.85

Tested by

no test coverage detected