import { type ReactNode } from 'react'; import { Skeleton } from '@patternfly/react-core'; import { Tbody, Tr, Td, ExpandableRowContent } from '@patternfly/react-table'; import { type Row } from '@tanstack/react-table'; import { type VirtualItem } from '@tanstack/react-virtual'; import { TableRow } from '~/shared/components/TableV2/TableRow'; /** Props for the {@link TableBody} component. */ interface TableBodyProps { /** All rows from the table model. */ rows: Row[]; /** Virtual items from the virtualizer — only these are rendered in the DOM. */ virtualRows: VirtualItem[]; /** Total pixel height of all rows (used for spacer calculation). */ totalSize: number; /** Callback from virtualizer to measure a row element for dynamic height. */ measureElement: (el: HTMLElement | null) => void; /** Returns a unique, stable string ID for a given row. */ getRowId: (row: TData) => string; /** Whether rows are expandable. */ enableExpansion?: boolean; /** Whether rows are selectable. */ enableRowSelection?: boolean; /** Render function for expanded row content. */ expandedContent?: (row: TData) => ReactNode; /** Number of visible columns, used for `colSpan` on expanded content and loading rows. */ visibleColumnCount: number; /** Whether the next page is being fetched. Shows skeleton rows when `true`. */ isFetchingNextPage?: boolean; /** Scroll margin from the virtualizer, subtracted from spacer calculations. */ scrollMargin?: number; } /** * Renders the virtualized table body. * * Only rows visible in the viewport (plus overscan) are rendered. Top and * bottom spacer `` elements maintain correct scroll height. When a row * is expanded, its expanded content is rendered in a full-width row below it. * * Shows skeleton loading rows when `isFetchingNextPage` is `true`. * * @typeParam TData - The row data type */ export const TableBody = ({ rows, virtualRows, totalSize, measureElement, getRowId, enableExpansion, enableRowSelection, expandedContent, visibleColumnCount, isFetchingNextPage, scrollMargin = 0, }: TableBodyProps) => { const lastVirtualRow = virtualRows[virtualRows.length - 1]; const bottomSpacerHeight = lastVirtualRow ? totalSize - (lastVirtualRow.start + lastVirtualRow.size) : 0; const topSpacerHeight = virtualRows.length > 0 ? virtualRows[0].start - scrollMargin : 0; const leadingCellCount = Number(Boolean(enableExpansion)) + Number(Boolean(enableRowSelection)); const expandedContentColumnCount = visibleColumnCount - leadingCellCount; return ( // Multiple siblings (not nested) — each becomes a direct child of the // element. Multiple per
is valid HTML5 (same pattern // used by ConformaGroupedTable). Per-row wrappers let TanStack Virtual's // measureElement capture the combined height of the main row + expanded content // as a single virtual item, preventing spacer miscalculations on expand/collapse. <> {topSpacerHeight > 0 && } {virtualRows.map((virtualRow) => { const row = rows[virtualRow.index]; if (!row) return null; const rowId = getRowId(row.original); const isExpanded = Boolean(enableExpansion && row.getIsExpanded() && expandedContent); return ( // No explicit role needed: renders a native whose implicit // ARIA role is "rowgroup", correctly grouping the main row with its optional // expanded content for screen readers. {isExpanded && ( {enableRowSelection && )} {enableExpansion && ( )} ); })} {bottomSpacerHeight > 0 && } {isFetchingNextPage && Array.from({ length: 3 }, (_, rowIdx) => ( {Array.from({ length: visibleColumnCount }, (__, colIdx) => ( ))} ))} ); };
} {enableExpansion && } {expandedContent(row.original)}