import React from 'react'; import { Skeleton } from './Skeleton'; import { cn } from '../utils/cn'; export interface Column { key: keyof T; header: string; render?: (value: any, row: T, index: number) => React.ReactNode; className?: string; } export interface TableProps { columns: Column[]; data: T[]; loading?: boolean; emptyMessage?: string; rowKey?: keyof T | ((row: T) => string); } export function Table>({ columns, data, loading = false, emptyMessage = 'No Results', rowKey, }: TableProps) { const getRowKey = (row: T, index: number): string => { if (typeof rowKey === 'function') { return rowKey(row); } if (rowKey) { return String(row[rowKey] ?? index); } return `row-${index}`; }; // Skeleton table component for loading state function SkeletonTable({ rows = 5, columns = 4, className = '' }: { rows?: number; columns?: number; className?: string }) { return (
{Array.from({ length: columns }).map((_, i) => ( ))} {Array.from({ length: rows }).map((_, rowIndex) => ( {Array.from({ length: columns }).map((_, colIndex) => ( ))} ))}
); } if (loading) { return ( ); } return (
{columns.map((column) => ( ))} {data.length === 0 ? ( ) : ( data.map((row, index) => ( {columns.map((column) => ( ))} )) )}
{column.header}
{emptyMessage}
{column.render ? column.render(row[column.key], row, index) : row[column.key]}
); }