import * as React from "react" import { Badge } from "@/components/ui/badge" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { cn } from "@/lib/utils" export type DataGridColumn = { key: keyof TData | string header: React.ReactNode render?: (row: TData) => React.ReactNode className?: string } export type DataGridProps> = React.ComponentProps<"div"> & { columns: DataGridColumn[] rows: TData[] rowKey?: keyof TData | ((row: TData, index: number) => React.Key) emptyLabel?: React.ReactNode title?: React.ReactNode description?: React.ReactNode } function DataGrid>({ columns, rows, rowKey, emptyLabel = "No rows found.", title, description, className, ...props }: DataGridProps) { return (
{(title || description) && (
{title &&

{title}

} {description &&

{description}

}
{rows.length} rows
)} {columns.map((column) => ( {column.header} ))} {rows.length > 0 ? ( rows.map((row, rowIndex) => ( {columns.map((column) => ( {column.render ? column.render(row) : renderCell(row[column.key])} ))} )) ) : ( {emptyLabel} )}
) } function resolveRowKey>( row: TData, index: number, rowKey?: keyof TData | ((row: TData, index: number) => React.Key) ) { if (typeof rowKey === "function") return rowKey(row, index) if (rowKey && row[rowKey] != null) return String(row[rowKey]) return index } function renderCell(value: unknown) { if (React.isValidElement(value)) return value if (value == null) return null if (typeof value === "boolean") return value ? "Yes" : "No" return String(value) } export { DataGrid }