mirror of
https://github.com/YuzuZensai/Minikura.git
synced 2026-09-13 18:59:25 +00:00
51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
import type { ReactNode } from "react";
|
|||
|
|
import {
|
||
|
|
Table,
|
||
|
|
TableBody,
|
||
|
|
TableCell,
|
||
|
|
TableHead,
|
||
|
|
TableHeader,
|
||
|
|
TableRow,
|
||
|
|
} from "@/components/ui/table";
|
||
|
|
|
||
|
|
export type DataTableColumn<T> = {
|
||
|
|
id: string;
|
||
|
|
header: ReactNode;
|
||
|
|
cell: (item: T) => ReactNode;
|
||
|
|
className?: string;
|
||
|
|
headerClassName?: string;
|
||
|
|
};
|
||
|
|
|
||
|
|
type DataTableProps<T> = {
|
||
|
|
data: readonly T[];
|
||
|
|
columns: readonly DataTableColumn<T>[];
|
||
|
|
getRowKey: (item: T, index: number) => string | number;
|
||
|
|
};
|
||
|
|
|
||
|
|
export function DataTable<T>({ data, columns, getRowKey }: DataTableProps<T>) {
|
||
|
|
return (
|
||
|
|
<Table>
|
||
|
|
<TableHeader>
|
||
|
|
<TableRow>
|
||
|
|
{columns.map((column) => (
|
||
|
|
<TableHead key={column.id} className={column.headerClassName}>
|
||
|
|
{column.header}
|
||
|
|
</TableHead>
|
||
|
|
))}
|
||
|
|
</TableRow>
|
||
|
|
</TableHeader>
|
||
|
|
<TableBody>
|
||
|
|
{data.map((item, index) => (
|
||
|
|
<TableRow key={getRowKey(item, index)}>
|
||
|
|
{columns.map((column) => (
|
||
|
|
<TableCell key={column.id} className={column.className}>
|
||
|
|
{column.cell(item)}
|
||
|
|
</TableCell>
|
||
|
|
))}
|
||
|
|
</TableRow>
|
||
|
|
))}
|
||
|
|
</TableBody>
|
||
|
|
</Table>
|
||
|
|
);
|
||
|
|
}
|