-
Notifications
You must be signed in to change notification settings - Fork 5
/
TableBuilder.tsx
78 lines (71 loc) · 2.01 KB
/
TableBuilder.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
import { useState } from 'react'
import {
useReactTable,
getCoreRowModel,
getSortedRowModel,
getFilteredRowModel,
ColumnFiltersState,
} from '@tanstack/react-table'
import { Button } from 'react-bootstrap'
import Table from 'react-bootstrap/Table'
import TableHeader from './TableHeader'
import TableRow from './TableRow'
/*
This component uses TanStack Table to add filtering
and sorting functionality.
For documentation, visit: https://tanstack.com/table/v8/docs/guide/introduction
*/
function TableBuilder({ data, columns, filterableInputs = [] }) {
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([])
const [sorting, setSorting] = useState([])
const table = useReactTable({
data,
columns: columns,
state: {
sorting,
columnFilters,
},
onColumnFiltersChange: setColumnFilters,
getSortedRowModel: getSortedRowModel(),
onSortingChange: setSorting,
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
})
const resetColumnFilters = () => {
table.resetColumnFilters()
}
return (
<div className="p-2">
<Table striped borderless>
<thead>
{!!filterableInputs.length && (
<tr className="border">
<th className="text-end" colSpan={100}>
<Button
className="btn btn-secondary"
size="sm"
onClick={resetColumnFilters}
>
Reset filters
</Button>
</th>
</tr>
)}
{table.getHeaderGroups().map((headerGroup) => (
<TableHeader
key={headerGroup.id}
headerGroup={headerGroup}
filterableInputs={filterableInputs}
/>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<TableRow row={row} key={row.id} />
))}
</tbody>
</Table>
</div>
)
}
export default TableBuilder