import Component from '@glimmer/component' import { cached } from '@glimmer/tracking' import { FlexRenderComponentConfig } from './flex-render-helpers.ts' import { flexRender } from '@tanstack/table-core/flex-render' import type { Cell_Core, CellContext, CellData, Header_Core, HeaderContext, RowData, TableFeatures, } from '@tanstack/table-core' import type { ComponentLike, ContentValue } from '@glint/template' type RenderOptions = Record | undefined type CellRenderResult< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > = | string | number | null | FlexRenderComponentConfig type HeaderRenderResult< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > = | string | number | null | FlexRenderComponentConfig interface CellRenderSignature< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData, > { Args: { ctx: CellContext options?: Record } } interface HeaderRenderSignature< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData, > { Args: { ctx: HeaderContext options?: Record } } // --- FlexRenderCell --- export interface FlexRenderCellSignature< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > { Args: { cell: Cell_Core } Element: null } export class FlexRenderCell< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > extends Component> { @cached get result(): CellRenderResult { const cell = this.args.cell const definition = cell.column.columnDef const groupingCell = cell as typeof cell & { getIsAggregated?: () => boolean getIsPlaceholder?: () => boolean } const groupingDefinition = definition as typeof definition & { aggregatedCell?: typeof definition.cell } if (groupingCell.getIsAggregated?.()) { return flexRender( groupingDefinition.aggregatedCell ?? definition.cell, cell.getContext(), ) as CellRenderResult } if (groupingCell.getIsPlaceholder?.()) { return null } return flexRender(definition.cell, cell.getContext()) as CellRenderResult< TFeatures, TData, TValue > } get resolvedContext(): CellContext { return this.args.cell.getContext() } get isComponent(): boolean { return this.result instanceof FlexRenderComponentConfig } get componentToRender(): ComponentLike> | undefined { const result = this.result if (result instanceof FlexRenderComponentConfig) { return result.component } return undefined } get componentOptions(): RenderOptions { const result = this.result if (result instanceof FlexRenderComponentConfig) { return result.options } return undefined } get content(): ContentValue { return this.result as ContentValue } } // --- FlexRenderHeader --- export interface FlexRenderHeaderSignature< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > { Args: { header: Header_Core } Element: null } export class FlexRenderHeader< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > extends Component> { @cached get result(): HeaderRenderResult { const header = this.args.header // Placeholder headers are not skipped here. Whether a placeholder renders // is the template's decision: the usual pattern wraps this component in // `{{#unless header.isPlaceholder}}`, but merging headers vertically with // `header.rowSpan` requires the spanning placeholder to render its // column's header content. This matches every other framework adapter. return flexRender( header.column.columnDef.header, header.getContext(), ) as HeaderRenderResult } get resolvedContext(): HeaderContext { return this.args.header.getContext() } get isComponent(): boolean { return this.result instanceof FlexRenderComponentConfig } get componentToRender(): ComponentLike> | undefined { const result = this.result if (result instanceof FlexRenderComponentConfig) { return result.component as unknown as ComponentLike< HeaderRenderSignature > } return undefined } get componentOptions(): RenderOptions { const result = this.result if (result instanceof FlexRenderComponentConfig) { return result.options } return undefined } get content(): ContentValue { return this.result as ContentValue } } // --- FlexRenderFooter --- export interface FlexRenderFooterSignature< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > { Args: { footer: Header_Core } Element: null } export class FlexRenderFooter< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > extends Component> { @cached get result(): HeaderRenderResult { const footer = this.args.footer // Placeholder footers are not skipped here; see `FlexRenderHeader`. The // template decides, which matches every other framework adapter. return flexRender( footer.column.columnDef.footer, footer.getContext(), ) as HeaderRenderResult } get resolvedContext(): HeaderContext { return this.args.footer.getContext() } get isComponent(): boolean { return this.result instanceof FlexRenderComponentConfig } get componentToRender(): ComponentLike> | undefined { const result = this.result if (result instanceof FlexRenderComponentConfig) { return result.component as unknown as ComponentLike< HeaderRenderSignature > } return undefined } get componentOptions(): RenderOptions { const result = this.result if (result instanceof FlexRenderComponentConfig) { return result.options } return undefined } get content(): ContentValue { return this.result as ContentValue } }