|
1
|
|
|
import React from 'react' |
|
2
|
|
|
import PropTypes from 'prop-types' |
|
3
|
|
|
import { styled } from '@material-ui/core/styles' |
|
4
|
|
|
import TableCell from '@material-ui/core/TableCell' |
|
5
|
|
|
import TableHead from '@material-ui/core/TableHead' |
|
6
|
|
|
import TableRow from '@material-ui/core/TableRow' |
|
7
|
|
|
import TableSortLabel from '@material-ui/core/TableSortLabel' |
|
8
|
|
|
import Checkbox from '@material-ui/core/Checkbox' |
|
9
|
|
|
import { HEAD_CELLS } from 'core/const/posts' |
|
10
|
|
|
|
|
11
|
4 |
|
const EnhancedTableHead = ({ |
|
12
|
|
|
order, |
|
13
|
|
|
orderColumn, |
|
14
|
|
|
numSelected, |
|
15
|
|
|
rowCount, |
|
16
|
|
|
setOrder, |
|
17
|
|
|
setOrderColumn, |
|
18
|
|
|
selectAllPosts, |
|
19
|
|
|
clearSelected |
|
20
|
|
|
}) => { |
|
21
|
25 |
|
const createSortHandler = property => () => { |
|
22
|
2 |
|
const isAsc = orderColumn === property && order === 'asc' |
|
23
|
2 |
|
setOrder(isAsc ? 'desc' : 'asc') |
|
24
|
2 |
|
setOrderColumn(property) |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
5 |
|
return ( |
|
28
|
|
|
<TableHead> |
|
29
|
|
|
<TableRow> |
|
30
|
|
|
<TableCell padding="checkbox"> |
|
31
|
|
|
<Checkbox |
|
32
|
|
|
indeterminate={numSelected > 0 && numSelected < rowCount} |
|
33
|
|
|
checked={rowCount > 0 && numSelected === rowCount} |
|
34
|
|
|
onChange={event => |
|
35
|
2 |
|
event.target.checked ? selectAllPosts() : clearSelected() |
|
36
|
|
|
} |
|
37
|
|
|
inputProps={{ 'aria-label': 'select all posts' }} |
|
38
|
|
|
/> |
|
39
|
|
|
</TableCell> |
|
40
|
|
|
{HEAD_CELLS.map(headCell => { |
|
41
|
25 |
|
const isOrderColumn = orderColumn === headCell.id |
|
42
|
25 |
|
return ( |
|
43
|
|
|
<TableCell |
|
44
|
|
|
key={headCell.id} |
|
45
|
|
|
align={headCell.numeric ? 'right' : 'left'} |
|
46
|
|
|
padding={headCell.disablePadding ? 'none' : 'default'} |
|
47
|
|
|
sortDirection={isOrderColumn ? order : false} |
|
48
|
|
|
> |
|
49
|
|
|
<TableSortLabel |
|
50
|
|
|
active={isOrderColumn} |
|
51
|
|
|
direction={isOrderColumn ? order : 'asc'} |
|
52
|
|
|
data-test-id={`head-sort-${headCell.id}`} |
|
53
|
|
|
onClick={createSortHandler(headCell.id)} |
|
54
|
|
|
> |
|
55
|
|
|
{headCell.label} |
|
56
|
|
|
{isOrderColumn ? ( |
|
57
|
|
|
<VisuallyHiddenSpan> |
|
58
|
|
|
{order === 'desc' |
|
59
|
|
|
? 'sorted descending' |
|
60
|
|
|
: 'sorted ascending'} |
|
61
|
|
|
</VisuallyHiddenSpan> |
|
62
|
|
|
) : null} |
|
63
|
|
|
</TableSortLabel> |
|
64
|
|
|
</TableCell> |
|
65
|
|
|
) |
|
66
|
|
|
})} |
|
67
|
|
|
</TableRow> |
|
68
|
|
|
</TableHead> |
|
69
|
|
|
) |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
EnhancedTableHead.propTypes = { |
|
73
|
|
|
numSelected: PropTypes.number, |
|
74
|
|
|
order: PropTypes.oneOf(['asc', 'desc']).isRequired, |
|
75
|
|
|
orderColumn: PropTypes.string.isRequired, |
|
76
|
|
|
rowCount: PropTypes.number.isRequired, |
|
77
|
|
|
setOrder: PropTypes.func, |
|
78
|
|
|
setOrderColumn: PropTypes.func, |
|
79
|
|
|
selectAllPosts: PropTypes.func, |
|
80
|
|
|
clearSelected: PropTypes.func |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
export default EnhancedTableHead |
|
84
|
|
|
|
|
85
|
4 |
|
const VisuallyHiddenSpan = styled('span')({ |
|
86
|
|
|
border: 0, |
|
87
|
|
|
clip: 'rect(0 0 0 0)', |
|
88
|
|
|
height: 1, |
|
89
|
|
|
margin: -1, |
|
90
|
|
|
overflow: 'hidden', |
|
91
|
|
|
padding: 0, |
|
92
|
|
|
position: 'absolute', |
|
93
|
|
|
top: 20, |
|
94
|
|
|
width: 1 |
|
95
|
|
|
}) |
|
96
|
|
|
|