-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
App.js
159 lines (140 loc) 路 3.01 KB
/
App.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React from "react";
import styled from "styled-components";
import { useTable, useAbsoluteLayout } from "@tanstack/react-table";
import makeData from "./makeData";
const Styles = styled.div`
padding: 1rem;
* {
box-sizing: border-box;
}
.table {
border: 1px solid #000;
max-width: 700px;
overflow-x: auto;
}
.header {
font-weight: bold;
}
.rows {
overflow-y: auto;
}
.row {
border-bottom: 1px solid #000;
height: 32px;
&.body {
:last-child {
border: 0;
}
}
}
.cell {
height: 100%;
line-height: 30px;
border-right: 1px solid #000;
padding-left: 5px;
:last-child {
border: 0;
}
}
`;
function Table({ columns, data }) {
// Use the state and functions returned from useTable to build your UI
const defaultColumn = React.useMemo(
() => ({
width: 150,
}),
[]
);
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
useTable(
{
columns,
data,
defaultColumn,
},
useAbsoluteLayout
);
// Render the UI for your table
return (
<div {...getTableProps()} className="table">
<div>
{headerGroups.map((headerGroup) => (
<div
{...headerGroup.getHeaderGroupProps()}
className="row header-group"
>
{headerGroup.headers.map((column) => (
<div {...column.getHeaderProps()} className="cell header">
{column.render("Header")}
</div>
))}
</div>
))}
</div>
<div className="rows" {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<div {...row.getRowProps()} className="row body">
{row.cells.map((cell, index) => (
<div {...cell.getCellProps()} key={index} className="cell">
{cell.render("Cell")}
</div>
))}
</div>
);
})}
</div>
</div>
);
}
function App() {
const columns = React.useMemo(
() => [
{
Header: "Name",
columns: [
{
Header: "First Name",
accessor: "firstName",
},
{
Header: "Last Name",
accessor: "lastName",
},
],
},
{
Header: "Info",
columns: [
{
Header: "Age",
accessor: "age",
width: 50,
},
{
Header: "Visits",
accessor: "visits",
width: 60,
},
{
Header: "Status",
accessor: "status",
},
{
Header: "Profile Progress",
accessor: "progress",
},
],
},
],
[]
);
const data = React.useMemo(() => makeData(20), []);
return (
<Styles>
<Table columns={columns} data={data} />
</Styles>
);
}
export default App;