forked from newrelic/docs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableHeader.js
More file actions
95 lines (89 loc) · 2.41 KB
/
Copy pathTableHeader.js
File metadata and controls
95 lines (89 loc) · 2.41 KB
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
import PropTypes from 'prop-types';
import { css } from '@emotion/react';
import { Button, Icon } from '@newrelic/gatsby-theme-newrelic';
const TableHeader = ({
headers,
setSortDirection,
setSortField,
sortDirection,
sortField,
}) => {
return (
<thead
css={css`
margin-bottom: 2rem;
th {
text-align: left;
}
`}
>
<tr>
{headers.map(({ label, contentId, sort = false }) => {
const isActiveSort = sortField === contentId;
return (
<th key={contentId}>
{sort ? (
<Button
variant={Button.VARIANT.PLAIN}
onClick={() => {
if (sortField === contentId) {
setSortDirection(DIRECTION.opposite(sortDirection));
} else {
setSortField(contentId);
setSortDirection(DIRECTION.ASC);
}
}}
css={css`
background: none;
`}
>
<b
css={css`
margin: 0 0.25rem;
`}
>
{label}
</b>
<Icon
name={
isActiveSort && sortDirection === DIRECTION.DESC
? 'fe-arrow-up'
: 'fe-arrow-down'
}
size="1rem"
css={css`
position: relative;
top: 1px;
stroke: ${isActiveSort ? '#00ac69' : 'grey'};
stroke-width: 4px;
`}
/>
</Button>
) : (
<b>{label}</b>
)}
</th>
);
})}
</tr>
</thead>
);
};
TableHeader.propTypes = {
headers: PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.string.isRequired,
contentId: PropTypes.string.isRequired,
sort: PropTypes.bool,
})
).isRequired,
sortField: PropTypes.string.isRequired,
setSortField: PropTypes.func.isRequired,
};
export const DIRECTION = {
ASC: 'asc',
DESC: 'desc',
};
DIRECTION.opposite = (direction) =>
direction === DIRECTION.ASC ? DIRECTION.DESC : DIRECTION.ASC;
export default TableHeader;