1
1
import React , { useState , useEffect } from 'react' ;
2
- import { Container , Row , Col , Badge , NavLink } from 'reactstrap' ;
2
+ import {
3
+ Table as ReactTable ,
4
+ Container ,
5
+ Row ,
6
+ Badge ,
7
+ NavLink ,
8
+ } from 'reactstrap' ;
3
9
import ReactTooltip from 'react-tooltip' ;
4
- import TableView from './TableView' ;
10
+ import { useTable , useFilters } from 'react-table' ;
11
+ import { FaSortAlphaUp , FaSortAlphaDown } from 'react-icons/fa' ;
5
12
import { Event } from '../Shared/Tracking' ;
6
13
7
14
import questionList from '../../data' ;
@@ -10,6 +17,22 @@ import './styles.scss';
10
17
11
18
const images = require . context ( '../../icons' , true ) ;
12
19
20
+ function DefaultColumnFilter ( {
21
+ column : { filterValue, preFilteredRows, setFilter } ,
22
+ } ) {
23
+ const count = preFilteredRows . length ;
24
+
25
+ return (
26
+ < input
27
+ value = { filterValue || '' }
28
+ onChange = { e => {
29
+ setFilter ( e . target . value || undefined ) ; // Set undefined to remove the filter entirely
30
+ } }
31
+ placeholder = { `Search ${ count } records...` }
32
+ />
33
+ ) ;
34
+ }
35
+
13
36
const Table = ( ) => {
14
37
const [ checked , setChecked ] = useState (
15
38
JSON . parse ( localStorage . getItem ( 'checked' ) ) ||
@@ -20,6 +43,15 @@ const Table = () => {
20
43
window . localStorage . setItem ( 'checked' , JSON . stringify ( checked ) ) ;
21
44
} , [ checked ] ) ;
22
45
46
+ const defaultColumn = React . useMemo (
47
+ ( ) => ( {
48
+ Filter : DefaultColumnFilter ,
49
+ minWidth : 30 ,
50
+ maxWidth : 10 ,
51
+ } ) ,
52
+ [ ] ,
53
+ ) ;
54
+
23
55
const data = React . useMemo ( ( ) => questionList , [ ] ) ;
24
56
25
57
const columns = React . useMemo (
@@ -68,7 +100,8 @@ const Table = () => {
68
100
{ cellInfo . row . original . url }
69
101
</ NavLink >
70
102
) ,
71
- disableSortBy : true ,
103
+ disableFilters : true ,
104
+ maxWidth : 2 ,
72
105
} ,
73
106
{
74
107
Header : 'Pattern' ,
@@ -77,7 +110,6 @@ const Table = () => {
77
110
{
78
111
Header : 'Difficulty' ,
79
112
accessor : 'difficulty' ,
80
- disableSortBy : true ,
81
113
Cell : cellInfo => (
82
114
< Badge
83
115
className = { cellInfo . row . original . difficulty . toLowerCase ( ) }
@@ -105,7 +137,7 @@ const Table = () => {
105
137
106
138
return < Row className = "companies" > { companies } </ Row > ;
107
139
} ,
108
- disableSortBy : true ,
140
+ disableFilters : true ,
109
141
} ,
110
142
] ,
111
143
} ,
@@ -114,16 +146,65 @@ const Table = () => {
114
146
[ ] ,
115
147
) ;
116
148
149
+ const {
150
+ getTableProps,
151
+ getTableBodyProps,
152
+ headerGroups,
153
+ rows,
154
+ prepareRow,
155
+ } = useTable (
156
+ {
157
+ columns,
158
+ data,
159
+ defaultColumn,
160
+ } ,
161
+ useFilters ,
162
+ ) ;
163
+
117
164
return (
118
165
< Container className = "table" >
119
- < Col >
120
- < Row >
121
- < Col >
122
- < ReactTooltip />
123
- < TableView columns = { columns } data = { data } />
124
- </ Col >
125
- </ Row >
126
- </ Col >
166
+ < ReactTooltip />
167
+ < ReactTable align = "center" striped hover { ...getTableProps ( ) } >
168
+ < thead >
169
+ { headerGroups . map ( headerGroup => (
170
+ < tr { ...headerGroup . getHeaderGroupProps ( ) } >
171
+ { headerGroup . headers . map ( column => (
172
+ < th { ...column . getHeaderProps ( ) } >
173
+ { column . render ( 'Header' ) }
174
+ < span >
175
+ { ' ' }
176
+ { column . isSorted ? (
177
+ column . isSortedDesc ? (
178
+ < FaSortAlphaUp />
179
+ ) : (
180
+ < FaSortAlphaDown />
181
+ )
182
+ ) : (
183
+ ''
184
+ ) }
185
+ </ span >
186
+ < div > { column . canFilter ? column . render ( 'Filter' ) : null } </ div >
187
+ </ th >
188
+ ) ) }
189
+ </ tr >
190
+ ) ) }
191
+ </ thead >
192
+
193
+ < tbody { ...getTableBodyProps ( ) } >
194
+ { rows . map ( row => {
195
+ prepareRow ( row ) ;
196
+ return (
197
+ < tr { ...row . getRowProps ( ) } >
198
+ { row . cells . map ( cell => {
199
+ return (
200
+ < td { ...cell . getCellProps ( ) } > { cell . render ( 'Cell' ) } </ td >
201
+ ) ;
202
+ } ) }
203
+ </ tr >
204
+ ) ;
205
+ } ) }
206
+ </ tbody >
207
+ </ ReactTable >
127
208
</ Container >
128
209
) ;
129
210
} ;
0 commit comments