import React from 'react' import type { Cell, CellData, Header, RowData, TableFeatures, } from '@tanstack/table-core' import type { ComponentType, JSX, ReactNode } from 'react' export type Renderable = ReactNode | ComponentType function isReactComponent( component: unknown, ): component is ComponentType { return ( isClassComponent(component) || typeof component === 'function' || isExoticComponent(component) ) } function isClassComponent(component: any) { return ( typeof component === 'function' && (() => { const proto = Object.getPrototypeOf(component) return proto.prototype && proto.prototype.isReactComponent })() ) } function isExoticComponent(component: any) { return ( typeof component === 'object' && typeof component.$$typeof === 'symbol' && ['react.memo', 'react.forward_ref'].includes(component.$$typeof.description) ) } /** * If rendering headers, cells, or footers with custom markup, use flexRender instead of `cell.getValue()` or `cell.renderValue()`. * @example flexRender(cell.column.columnDef.cell, cell.getContext()) */ export function flexRender( Comp: Renderable, props: TProps, ): ReactNode | JSX.Element { if (Comp === null || Comp === undefined) { return null } return isReactComponent(Comp) ? : Comp } /** * Simplified component wrapper of `flexRender`. Use this utility component to render headers, cells, or footers with custom markup. * Only one prop (`cell`, `header`, or `footer`) may be passed. * @example * @example * @example */ export type FlexRenderProps< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, > = | { cell: Cell; header?: never; footer?: never } | { header: Header cell?: never footer?: never } | { footer: Header cell?: never header?: never } /** * Simplified component wrapper of `flexRender`. Use this utility component to render headers, cells, or footers with custom markup. * Only one prop (`cell`, `header`, or `footer`) may be passed. * @example * ```tsx * * * * ``` * * This replaces calling `flexRender` directly like this: * ```tsx * flexRender(cell.column.columnDef.cell, cell.getContext()) * flexRender(header.column.columnDef.header, header.getContext()) * flexRender(footer.column.columnDef.footer, footer.getContext()) * ``` */ export function FlexRender< TFeatures extends TableFeatures, TData extends RowData, TValue extends CellData = CellData, >(props: FlexRenderProps) { if ('cell' in props && props.cell) { const cell = props.cell const def = cell.column.columnDef const groupingCell = cell as typeof cell & { getIsAggregated?: () => boolean getIsPlaceholder?: () => boolean } const groupingDef = def as typeof def & { aggregatedCell?: typeof def.cell } if (groupingCell.getIsAggregated?.()) { return flexRender( groupingDef.aggregatedCell ?? def.cell, cell.getContext(), ) } if (groupingCell.getIsPlaceholder?.()) { return null } return flexRender(def.cell, cell.getContext()) } if ('header' in props && props.header) { return flexRender( props.header.column.columnDef.header, props.header.getContext(), ) } if ('footer' in props && props.footer) { return flexRender( props.footer.column.columnDef.footer, props.footer.getContext(), ) } return null }