import React from 'react'; import { Table, TableBody, TableCell, TableHead, TableHeaderCell, TableRow, Text, Badge, Icon, Button, Card, } from "@tremor/react"; import { Tooltip } from "antd"; interface Column { header: string; accessor: string; cellRenderer?: (value: any, row: any) => React.ReactNode; width?: string; style?: React.CSSProperties; } interface Action { icon?: React.ComponentType; onClick: (item: T) => void; condition?: () => boolean; tooltip?: string; } interface DeleteModalProps { isOpen: boolean; onConfirm: () => void; onCancel: () => void; title: string; message: string; } interface DataTableProps { data: any[]; columns: Column[]; actions?: Action[]; emptyMessage?: string; deleteModal?: DeleteModalProps; } const DataTable: React.FC = ({ data, columns, actions, emptyMessage = "No data available", deleteModal }) => { const renderCell = (column: Column, row: any) => { const value = row[column.accessor]; if (column.cellRenderer) { return column.cellRenderer(value, row); } // Default cell rendering based on value type if (Array.isArray(value)) { return (
{value.length === 0 ? ( None ) : ( value.map((item: any, index: number) => ( {String(item).length > 30 ? `${String(item).slice(0, 30)}...` : item} )) )}
); } return value?.toString() || ''; }; return ( {columns.map((column, index) => ( {column.header} ))} {actions && actions.length > 0 && ( Actions )} {data && data.length > 0 ? ( data.map((row, rowIndex) => ( {columns.map((column, colIndex) => ( {column.accessor === 'id' ? ( {renderCell(column, row)} ) : ( renderCell(column, row) )} ))} {actions && actions.length > 0 && ( {actions.map((action, actionIndex) => ( // @ts-ignore action.condition?.(row) !== false && ( action.onClick(row)} className="cursor-pointer mx-1" /> ) ))} )} )) ) : ( {emptyMessage} )}
); }; export default DataTable; export type { Action, Column, DataTableProps, DeleteModalProps };