Skip to content

Commit c27a121

Browse files
Sean PrashadSean Prashad
authored andcommitted
WIP - Add DataGrid component
1 parent 96ed0d1 commit c27a121

File tree

6 files changed

+159
-0
lines changed

6 files changed

+159
-0
lines changed

web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"react-dom": "^16.12.0",
1717
"react-scripts": "3.3.0",
1818
"react-scroll": "^1.7.15",
19+
"react-table": "^7.0.0-rc.15",
1920
"react-test-renderer": "^16.12.0",
2021
"reactstrap": "^8.2.0"
2122
},

web/src/components/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import React from 'react';
33
import './styles.scss';
44

55
import Navigation from './Navigation';
6+
import DataGrid from './DataGrid';
67

78
function App() {
89
return (
910
<div className="App">
1011
<Navigation />
12+
<DataGrid />
1113
</div>
1214
);
1315
}

web/src/components/DataGrid/index.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import React from 'react';
2+
import { Container, Row, Col } from 'reactstrap';
3+
import { useTable, useSortBy } from 'react-table';
4+
5+
import questions from '../../data';
6+
7+
import './styles.scss';
8+
9+
function Table({ columns, data }) {
10+
const {
11+
getTableProps,
12+
getTableBodyProps,
13+
headerGroups,
14+
rows,
15+
prepareRow,
16+
} = useTable(
17+
{
18+
columns,
19+
data,
20+
},
21+
useSortBy,
22+
);
23+
24+
// We don't want to render all 2000 rows for this example, so cap
25+
// it at 20 for this use case
26+
const firstPageRows = rows.slice(0, 20);
27+
28+
return (
29+
<>
30+
<table {...getTableProps()}>
31+
<thead>
32+
{headerGroups.map(headerGroup => (
33+
<tr {...headerGroup.getHeaderGroupProps()}>
34+
{headerGroup.headers.map(column => (
35+
// Add the sorting props to control sorting. For this example
36+
// we can add them into the header props
37+
<th {...column.getHeaderProps(column.getSortByToggleProps())}>
38+
{column.render('Header')}
39+
{/* Add a sort direction indicator */}
40+
<span>
41+
{column.isSorted
42+
? column.isSortedDesc
43+
? ' 🔽'
44+
: ' 🔼'
45+
: ''}
46+
</span>
47+
</th>
48+
))}
49+
</tr>
50+
))}
51+
</thead>
52+
<tbody {...getTableBodyProps()}>
53+
{firstPageRows.map((row, i) => {
54+
prepareRow(row);
55+
return (
56+
<tr {...row.getRowProps()}>
57+
{row.cells.map(cell => {
58+
return (
59+
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
60+
);
61+
})}
62+
</tr>
63+
);
64+
})}
65+
</tbody>
66+
</table>
67+
<br />
68+
<div>Showing the first 20 results of {rows.length} rows</div>
69+
</>
70+
);
71+
}
72+
73+
// {
74+
// id: 1,
75+
// name: 'My Question',
76+
// url: 'wwww.google.ca',
77+
// pattern: 'Arrays',
78+
// companies: ['abc', 'def', 'ghi'],
79+
// },
80+
81+
const DataGrid = () => {
82+
const data = React.useMemo(() => questions, []);
83+
const columns = React.useMemo(
84+
() => [
85+
{
86+
Header: 'Name',
87+
columns: [
88+
{
89+
Header: '#',
90+
accessor: 'id',
91+
},
92+
{
93+
Header: 'Question Name',
94+
accessor: 'name',
95+
},
96+
{
97+
Header: 'URL',
98+
accessor: 'url',
99+
},
100+
{
101+
Header: 'Pattern',
102+
accessor: 'pattern',
103+
},
104+
{
105+
Header: 'Companies',
106+
accessor: 'companies',
107+
},
108+
],
109+
},
110+
],
111+
[],
112+
);
113+
114+
return (
115+
<Container className="datagrid" fluid>
116+
<Col>
117+
<Row>
118+
<Col>
119+
<Table columns={columns} data={data} />
120+
</Col>
121+
</Row>
122+
</Col>
123+
</Container>
124+
);
125+
};
126+
127+
export default DataGrid;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.datagrid {
2+
display: flex;
3+
align-items: center;
4+
justify-content: center;
5+
6+
> .col {
7+
padding: 100px 0 100px 0;
8+
}
9+
10+
.row {
11+
justify-content: center;
12+
}
13+
}

web/src/data/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const data = [
2+
{
3+
id: 1,
4+
name: 'My Question',
5+
url: 'wwww.google.ca',
6+
pattern: 'Arrays',
7+
companies: ['abc', 'def', 'ghi'],
8+
},
9+
];
10+
11+
export default data;

web/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9383,6 +9383,11 @@ react-scroll@^1.7.15:
93839383
lodash.throttle "^4.1.1"
93849384
prop-types "^15.5.8"
93859385

9386+
react-table@^7.0.0-rc.15:
9387+
version "7.0.0-rc.15"
9388+
resolved "https://registry.yarnpkg.com/react-table/-/react-table-7.0.0-rc.15.tgz#bb855e4e2abbb4aaf0ed2334404a41f3ada8e13a"
9389+
integrity sha512-ofMOlgrioHhhvHjvjsQkxvfQzU98cqwy6BjPGNwhLN1vhgXeWi0mUGreaCPvRenEbTiXsQbMl4k3Xmx3Mut8Rw==
9390+
93869391
react-test-renderer@^16.0.0-0, react-test-renderer@^16.12.0:
93879392
version "16.12.0"
93889393
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-16.12.0.tgz#11417ffda579306d4e841a794d32140f3da1b43f"

0 commit comments

Comments
 (0)