forked from forthfate/openorbit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-table.tsx
More file actions
58 lines (58 loc) · 1.56 KB
/
Copy pathdata-table.tsx
File metadata and controls
58 lines (58 loc) · 1.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
import type { ReactNode } from "react";
export type Column<Row> = {
id: string;
header: ReactNode;
render: (row: Row) => ReactNode;
};
import { locales, resolveLocale } from "../../locales";
export function DataTable<Row extends { id: string }>({
columns,
rows,
empty,
className = "",
gridTemplateColumns,
onRowClick,
}: {
columns: Column<Row>[];
rows: Row[];
empty?: string;
className?: string;
gridTemplateColumns?: string;
onRowClick?: (row: Row) => void;
}) {
const template =
gridTemplateColumns ?? `repeat(${columns.length},minmax(120px,1fr))`,
locale = resolveLocale(localStorage.getItem("orbit.locale"));
return (
<div className={`table ${className}`}>
<div className="tr th" style={{ gridTemplateColumns: template }}>
{columns.map((c) => (
<span key={c.id}>{c.header}</span>
))}
</div>
{rows.length ? (
rows.map((row) => (
<div
className={`tr${onRowClick ? " tr--interactive" : ""}`}
style={{ gridTemplateColumns: template }}
key={row.id}
onClick={(event) => {
if (
!(event.target as HTMLElement).closest(
"button,input,select,textarea",
)
)
onRowClick?.(row);
}}
>
{columns.map((c) => (
<span key={c.id}>{c.render(row)}</span>
))}
</div>
))
) : (
<div className="empty">{empty ?? locales[locale].ui.emptyEntries}</div>
)}
</div>
);
}