-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarsTable.tsx
89 lines (85 loc) · 2.49 KB
/
CarsTable.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
import {
createColumnHelper,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
import { Table } from "react-bootstrap";
import { SampleCars } from "../data";
import { Car } from "../types/Car";
type CarsTableProps = {
selectedCar: Car | null;
onCarSelected: (car: Car) => void;
};
export const CarsTable = (props: CarsTableProps) => {
const columnHelper = createColumnHelper<Car>();
const columns = [
columnHelper.accessor((row) => row.brand, {
id: "brand",
cell: (info) => info.getValue(),
header: () => "Brand",
}),
columnHelper.accessor((row) => row.model, {
id: "model",
cell: (info) => info.getValue(),
header: () => "Model",
}),
columnHelper.accessor((row) => row.productionYear, {
id: "productionYear",
cell: (info) => info.getValue(),
header: () => "Production year",
}),
columnHelper.accessor((row) => row.isAvailable, {
id: "isAvailable",
cell: (info) => (info.getValue().valueOf() === true ? "Yes" : "No"),
header: () => "Available?",
}),
];
const table = useReactTable({
data: SampleCars,
columns,
getCoreRowModel: getCoreRowModel(),
});
return (
<Table striped bordered hover>
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<th key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => {
const isActive = row.original.id === props.selectedCar?.id;
return (
<tr
key={row.id}
style={
isActive === true ? { backgroundColor: "#3a7a11" } : undefined
}
onClick={() => {
props.onCarSelected(row.original);
}}
>
{row.getVisibleCells().map((cell) => (
<td key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
);
})}
</tbody>
</Table>
);
};