-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTable.tsx
More file actions
65 lines (61 loc) · 1.73 KB
/
Copy pathDataTable.tsx
File metadata and controls
65 lines (61 loc) · 1.73 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
import { useMemo, useState } from "react";
interface DataTableProps {
records: Record<string, unknown>[];
}
export function DataTable({ records }: DataTableProps) {
const [query, setQuery] = useState("");
const normalizedQuery = query.trim().toLowerCase();
const columns = useMemo(
() => Array.from(new Set(records.flatMap((record) => Object.keys(record)))),
[records],
);
const filteredRecords = normalizedQuery
? records.filter((record) =>
Object.values(record).some((value) =>
String(value ?? "")
.toLowerCase()
.includes(normalizedQuery),
),
)
: records;
if (records.length === 0) {
return (
<div className="empty-panel" role="status">
No records loaded yet.
</div>
);
}
return (
<div className="data-table">
<label className="d-block mb8">
<span className="d-block fs-caption tt-uppercase fc-light mb4">Search</span>
<input
className="s-input"
type="search"
value={query}
onChange={(event) => setQuery(event.target.value)}
/>
</label>
<div className="s-table-container data-table-container">
<table className="s-table s-table__striped">
<thead>
<tr>
{columns.map((column) => (
<th key={column}>{column}</th>
))}
</tr>
</thead>
<tbody>
{filteredRecords.map((record, index) => (
<tr key={index}>
{columns.map((column) => (
<td key={column}>{String(record[column] ?? "")}</td>
))}
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}