import React, { DetailedHTMLProps, ReactNode, TableHTMLAttributes, } from "react"; import classNames from "./lib/classNames"; export type TableRow = Record; export interface TableColumn { renderItem?: (row: TableRow) => ReactNode; dataIndex?: string; heading?: string; } export interface CustomTableProps { columns: TableColumn[]; rows: TableRow[]; } export interface TableProps extends DetailedHTMLProps< TableHTMLAttributes, HTMLTableElement >, CustomTableProps {} export function getTableStyles() { const tableStyles = { base: "min-w-full divide-y divide-gray-300/50 shadow rounded-lg ring-1 ring-black ring-opacity-5", }; return classNames(tableStyles.base); } export default function Table({ className, columns, rows, ...rest }: TableProps) { return ( {columns.map((column, columnIndex) => { const isLast = columnIndex === columns.length - 1; const isFirst = !columnIndex; return ( ); })} {rows.map((row, rowIndex) => { const isRowLast = rowIndex === rows.length - 1; return ( {columns.map((column, columnIndex) => { const isColumnLast = columnIndex === columns.length - 1; const isColumnFirst = !columnIndex; const value = column.dataIndex && (row[column.dataIndex] as ReactNode); return ( ); })} ); })}
{column.heading}
{column.renderItem ? column.renderItem(row) : value}
); }