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

Function createHeaderGroup

packages/table-core/src/core/headers.ts:514–584  ·  view source on GitHub ↗
(
    headersToGroup: Header<TData, unknown>[],
    depth: number,
  )

Source from the content-addressed store, hash-verified

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
542 } else {
543 // The parent header is repeated
544 column = headerToGroup.column
545 isPlaceholder = true
546 }
547
548 if (
549 latestPendingParentHeader &&
550 latestPendingParentHeader?.column === column
551 ) {
552 // This column is repeated. Add it as a sub header to the next batch
553 latestPendingParentHeader.subHeaders.push(headerToGroup)
554 } else {
555 // This is a new header. Let's create it
556 const header = createHeader(table, column, {
557 id: [headerFamily, depth, column.id, headerToGroup?.id]
558 .filter(Boolean)
559 .join('_'),
560 isPlaceholder,
561 placeholderId: isPlaceholder
562 ? `${pendingParentHeaders.filter((d) => d.column === column).length}`
563 : undefined,
564 depth,
565 index: pendingParentHeaders.length,
566 })
567
568 // Add the headerToGroup as a subHeader of the new header
569 header.subHeaders.push(headerToGroup)
570 // Add the new header to the pendingParentHeaders to get grouped
571 // in the next batch

Callers 1

buildHeaderGroupsFunction · 0.85

Calls 1

createHeaderFunction · 0.85

Tested by

no test coverage detected