'use client'; import { Text, View, StyleSheet } from '@react-pdf/renderer'; import { ReactNode } from 'react'; import type { PdfColumn } from './types'; const styles = StyleSheet.create({ tableRow: { flexDirection: 'row', }, tableBorderBottom: { borderBottomWidth: 1, borderBottomColor: '#000000', borderBottomStyle: 'solid', }, tableCell: { flex: 1, borderRightWidth: 1, borderRightColor: '#000000', borderRightStyle: 'solid', padding: 4, fontSize: 7, textAlign: 'left', }, tableCellLast: { flex: 1, padding: 4, fontSize: 7, borderRightWidth: 0, }, tableCellRight: { flex: 1, borderRightWidth: 1, borderRightColor: '#000000', borderRightStyle: 'solid', padding: 4, fontSize: 7, textAlign: 'right', }, tableCellCenter: { flex: 1, borderRightWidth: 1, borderRightColor: '#000000', borderRightStyle: 'solid', padding: 4, fontSize: 7, textAlign: 'center', }, tableCellNo: { flex: 0.5, borderRightWidth: 1, borderRightColor: '#000000', borderRightStyle: 'solid', padding: 4, fontSize: 7, textAlign: 'center', }, }); interface PdfTbodyProps> { columns: PdfColumn[]; data: TData[]; firstRow?: { valueKey: string; value: number; align?: 'right'; color?: string; }; } export const PdfTbody = ,>({ columns, data, firstRow, }: PdfTbodyProps) => { return ( <> {/* First Row */} {firstRow && ( {columns.map((column, index) => { const isLastColumn = index === columns.length - 1; const isFirstRowColumn = column.key === firstRow.valueKey; const align = column.align || 'left'; const cellStyle = column.key === 'no' ? [styles.tableCellNo, { flex: column.flex || 1 }] : isFirstRowColumn ? [ styles.tableCellRight, { flex: column.flex || 1, color: firstRow.color || 'black', borderRightWidth: isLastColumn ? 0 : 1, }, ] : align === 'right' ? [ styles.tableCellRight, { flex: column.flex || 1, borderRightWidth: isLastColumn ? 0 : 1, }, ] : align === 'center' ? [ styles.tableCellCenter, { flex: column.flex || 1, borderRightWidth: isLastColumn ? 0 : 1, }, ] : isLastColumn ? [ styles.tableCellLast, { flex: column.flex || 1, borderRightWidth: 0, }, ] : [styles.tableCell, { flex: column.flex || 1 }]; return ( {isFirstRowColumn ? firstRow.value : ''} ); })} )} {/* Data Rows */} {data.map((row, rowIndex) => { const isLastRow = rowIndex === data.length - 1; return ( {columns.map((column, colIndex) => { const isLastColumn = colIndex === columns.length - 1; const align = column.align || 'left'; // Get cell content from column.cell function or fallback to row value let cellContent: ReactNode; if (column.cell) { cellContent = column.cell({ row, index: rowIndex }); } else { cellContent = ((row as Record)[column.key] as ReactNode) ?? '-'; } const cellStyle = column.key === 'no' ? [styles.tableCellNo, { flex: column.flex || 1 }] : align === 'right' ? [ styles.tableCellRight, { flex: column.flex || 1, borderRightWidth: isLastColumn ? 0 : 1, }, ] : align === 'center' ? [ styles.tableCellCenter, { flex: column.flex || 1, borderRightWidth: isLastColumn ? 0 : 1, }, ] : isLastColumn ? [ styles.tableCellLast, { flex: column.flex || 1, borderRightWidth: 0 }, ] : [ styles.tableCell, { flex: column.flex || 1, borderRightWidth: isLastColumn ? 0 : 1, }, ]; return ( {typeof cellContent === 'string' || typeof cellContent === 'number' ? ( {String(cellContent)} ) : ( cellContent )} ); })} ); })} ); }; export type { PdfColumn };