import { type ReactNode, type JSX } from 'react'; import { createRow as _createRow, flexRender as _flexRender, type Renderable, } from '@tanstack/react-table'; import { type MRT_ColumnHelper, type MRT_DisplayColumnDef, type MRT_GroupColumnDef, type MRT_Row, type MRT_RowData, type MRT_TableInstance, } from '../types'; import { getAllLeafColumnDefs, getColumnId } from './column.utils'; export const flexRender = _flexRender as ( Comp: Renderable, props: any, ) => JSX.Element | ReactNode; export function createMRTColumnHelper< TData extends MRT_RowData, >(): MRT_ColumnHelper { return { accessor: (accessor, column) => { return typeof accessor === 'function' ? ({ ...column, accessorFn: accessor, } as any) : { ...column, accessorKey: accessor, }; }, display: (column) => column as MRT_DisplayColumnDef, group: (column) => column as MRT_GroupColumnDef, }; } export const createRow = ( table: MRT_TableInstance, originalRow?: TData, rowIndex = -1, depth = 0, subRows?: MRT_Row[], parentId?: string, ): MRT_Row => _createRow( table as any, 'mrt-row-create', originalRow ?? Object.assign( {}, ...getAllLeafColumnDefs(table.options.columns).map((col) => ({ [getColumnId(col)]: '', })), ), rowIndex, depth, subRows as any, parentId, ) as MRT_Row;