-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataTable.tsx
91 lines (86 loc) · 2.25 KB
/
DataTable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
export const DataTable = <T = any,>({
data,
noDataMessage = "No data present",
columnDef,
}: {
data: Array<{ [key: string]: any }>;
noDataMessage?: string;
columnDef?: ColumnDef<T>[];
}) => {
if (data === undefined || data.length === 0) {
return <div className="text-center">{noDataMessage}</div>;
}
const keys = Object.keys(data[0]);
const columns: ColumnDef<any>[] =
columnDef ||
keys.map((key) => ({
accessorKey: key,
header: key,
}));
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
manualSorting: true,
});
return (
<div className="rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
);
};