-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataTable.tsx
More file actions
108 lines (106 loc) · 3.56 KB
/
Copy pathDataTable.tsx
File metadata and controls
108 lines (106 loc) · 3.56 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Fragment, type ReactNode } from "react";
import { flexRender, type RowData } from "@tanstack/react-table";
import type {
ComicarrRow,
ComicarrTable,
} from "@/components/data-table/useTableState";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { cn } from "@/lib/utils";
interface DataTableProps<TData extends RowData> {
table: ComicarrTable<TData>;
onRowClick?: (row: TData) => void;
renderSubRow?: (row: ComicarrRow<TData>, colSpan: number) => ReactNode;
className?: string;
}
export function DataTable<TData extends RowData>({
table,
onRowClick,
renderSubRow,
className,
}: DataTableProps<TData>) {
return (
<div className={cn("min-w-0", className)}>
<Table>
<TableHeader className="bg-muted/30">
{table.getHeaderGroups().map((headerGroup) => (
<TableRow
key={headerGroup.id}
className="border-b border-border hover:bg-transparent"
>
{headerGroup.headers.map((header) => (
<TableHead
key={header.id}
// Sticky per-cell rather than on <thead>: the page scrolls
// the table body now, and an opaque background is needed so
// rows do not show through the header as they pass under it.
className="sticky top-0 z-10 bg-background px-5 py-2 font-mono text-[10px] font-normal text-muted-foreground/70 uppercase tracking-wider"
style={
header.column.getSize() !== 150
? { width: header.column.getSize() }
: undefined
}
>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</TableHead>
))}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows.length ? (
table.getRowModel().rows.map((row) => {
const colSpan = table.getAllColumns().length;
return (
<Fragment key={row.id}>
<TableRow
data-state={row.getIsSelected() && "selected"}
className={cn(
"border-b border-border/50",
onRowClick && "cursor-pointer",
)}
onClick={
onRowClick ? () => onRowClick(row.original) : undefined
}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id} className="px-5 py-2">
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</TableCell>
))}
</TableRow>
{renderSubRow &&
row.getIsExpanded() &&
renderSubRow(row, colSpan)}
</Fragment>
);
})
) : (
<TableRow>
<TableCell
colSpan={table.getAllColumns().length}
className="h-24 text-center text-muted-foreground"
>
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
);
}