Files
co-lite/solvway-admin-portal/app/components/dashboard/DashTable.tsx
T

61 lines
2.3 KiB
TypeScript

import type { ReactNode } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/app/components/ui/card";
import { EmptyState } from "@/app/components/ui/states";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/app/components/ui/table";
import { cn } from "@/app/lib/utils";
export interface DashColumn<R> {
label: string;
numeric?: boolean;
cell: (row: R) => ReactNode;
}
/** A titled table that stacks into label/value cards below md (one DOM, CSS only). */
export function DashTable<R>({ title, empty, columns, rows, rowKey }: {
title: string;
empty: string;
columns: DashColumn<R>[];
rows: R[];
rowKey: (row: R) => string;
}) {
return (
<Card>
<CardHeader><CardTitle className="text-base">{title}</CardTitle></CardHeader>
<CardContent>
{rows.length === 0 ? (
<EmptyState title={empty} className="py-6" />
) : (
<Table aria-label={title} className="max-md:block">
<TableHeader className="max-md:hidden">
<TableRow>
{columns.map((c) => (
<TableHead key={c.label} className={cn(c.numeric && "text-right")}>{c.label}</TableHead>
))}
</TableRow>
</TableHeader>
<TableBody className="max-md:block max-md:[&_tr:last-child]:border">
{rows.map((row) => (
<TableRow key={rowKey(row)} className="max-md:mb-3 max-md:block max-md:rounded-lg max-md:border max-md:p-2">
{columns.map((c) => (
<TableCell
key={c.label}
data-label={c.label}
className={cn(
"max-md:flex max-md:justify-between max-md:gap-4 max-md:whitespace-normal max-md:px-1 max-md:py-1",
"max-md:before:font-medium max-md:before:text-muted-foreground max-md:before:content-[attr(data-label)]",
c.numeric && "text-right tabular max-md:text-left",
)}
>
{c.cell(row)}
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
)}
</CardContent>
</Card>
);
}