"use client"; import type React from "react"; import { cn, parseNumber } from "@/lib/utils"; import BaseTableRenderer from "./core/BaseTableRenderer"; import { appendCellMarker, getColumnAlign, getLeafColumns, toCssSize, toRoundedNumber, toTableCellContent, } from "./core/table-core.helpers"; import {DataColumn} from "@/components/table/types"; import {isGapRow} from "@/lib/kfpa-utill"; export type MergedTableCell = { key: keyof T; content: React.ReactNode; startRowIndex?: number; rowSpan?: number; className?: string; }; export type PreAppendTableCell = | React.ReactNode | { content: React.ReactNode; className?: string; colSpan?: number; rowSpan?: number; width?: number | string; }; type Props> = { columns: DataColumn[]; rows: T[]; title?: string; wrapperClassName?: string; tableClassName?: string; subheadRowClassName?: string; minWidth?: number | string; fillGapRow?: boolean; mergedCell?: MergedTableCell; getCellClassName?: (row: T, column: DataColumn, rowIndex: number) => string | undefined; }; const getPreAppendCells = (row: Record): PreAppendTableCell[] => { const cells = row.preAppendCells; return Array.isArray(cells) ? (cells as PreAppendTableCell[]) : []; }; const renderPreAppendCell = (cell: PreAppendTableCell, index: number) => { const isCellConfig = typeof cell === "object" && cell !== null && !Array.isArray(cell) && "content" in cell; if (!isCellConfig) { return {cell}; } return ( {cell.content} ); }; export default function BaseDataTable>({ columns, rows, title, wrapperClassName, tableClassName, subheadRowClassName, minWidth, fillGapRow = false, mergedCell, getCellClassName, }: Readonly>) { const mergedCellStart = mergedCell?.startRowIndex ?? 0; const mergedCellSpan = mergedCell?.rowSpan ?? Math.max(0, rows.length - mergedCellStart); const leafColumns = getLeafColumns(columns); return ( {rows.map((row, rowIndex) => { const rowTotal = parseNumber(row.total); const isFillGapRow = fillGapRow && isGapRow((row.rowLabel as string) ?? "") const fillGapClass = isFillGapRow ? "bg-gray-100" : ""; const preAppendCells = getPreAppendCells(row); return ( {preAppendCells.map(renderPreAppendCell)} {leafColumns.map((column) => { const dynamicCellClassName = getCellClassName?.(row, column, rowIndex); if (mergedCell && column.key === mergedCell.key && rowIndex >= mergedCellStart) { if (rowIndex > mergedCellStart) { return null; } return ( {mergedCell.content} ); } const rawValue = row[column.key]; const align = getColumnAlign(column); const defaultContent = column.render ? column.render(row) : toTableCellContent(rawValue, column.decimalPlaces); const numericValue = "numeric" in column && column.numeric ? parseNumber(rawValue ?? defaultContent) ?? 0 : 0; if (column.rowHead) { const rowHeaderClassName = row.headerClassName ?? ""; return ( {defaultContent} ); } return ( {defaultContent} ); })} ); })} ); }