forked from rage/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeViewItem.js
More file actions
211 lines (191 loc) · 5.58 KB
/
Copy pathTreeViewItem.js
File metadata and controls
211 lines (191 loc) · 5.58 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React from "react"
import styled from "styled-components"
import { Motion, spring } from "react-motion"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faCaretRight } from "@fortawesome/free-solid-svg-icons"
import { trackElementHeight } from "../../util/trackHeight"
import GatsbyLink from "gatsby-link"
import { Location } from "@reach/router"
import Avatar from "@material-ui/core/Avatar"
import Chip from "@material-ui/core/Chip"
import Divider from "@material-ui/core/Divider"
import { faCalendarAlt as icon } from "@fortawesome/free-regular-svg-icons"
import withSimpleErrorBoundary from "../../util/withSimpleErrorBoundary"
const ChildrenList = styled.ul`
height: calc(var(--open-ratio) * var(--calculated-height) * 1px);
overflow: hidden;
margin-left: 0;
`
const ListItem = styled.li`
list-style-type: none;
margin-bottom: 0;
padding: 0.75em;
display: flex;
align-items: center;
`
const ListItemLabel = styled.div`
flex: 1;
padding: 0.3em;
`
const NavigationLink = styled(GatsbyLink)`
border-left: 0.5em solid white;
width: 100%;
background-color: white;
${(props) =>
props.active === "t" &&
`
border-color: #f75b4b !important;
background-color: #edeaea;
`}
:hover {
text-decoration: none;
color: black;
background-color: #f5ebeb;
border-color: #f5ebeb;
//filter: brightness(0.5);
}
`
const DisabledItem = styled.div`
opacity: 0.5;
width: 100%;
cursor: default !important;
border-left: 0.5em solid white;
`
const ItemTitleWrapper = styled.div`
display: flex;
align-items: center;
a {
color: black;
text-decoration: none;
}
&.active-section {
background-color: #ffdfdf;
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
a {
color: black;
}
}
`
const StyledIcon = styled(FontAwesomeIcon)`
vertical-align: middle;
margin-right: 0.5em;
margin-left: 0.5em;
transform: rotate(calc(var(--open-ratio) * 90deg));
`
const StyledChip = styled(Chip)`
span {
width: 6em;
}
`
const StyledDivider = styled(Divider)`
margin: 1em 16px !important;
`
const Centered = styled.div`
text-align: center;
margin-bottom: 0.5rem;
`
class TreeViewItem extends React.Component {
constructor(props) {
super(props)
this.state = {
childrenVisible: props.item.childrenVisibleByDefault || false,
}
this.childrenListRef = React.createRef()
}
onClick = () => {
this.setState((prev) => ({
childrenVisible: !prev.childrenVisible,
}))
}
componentDidMount() {
if (this.props.item.children) {
trackElementHeight(this.childrenListRef.current)
}
}
render() {
if (this.props.item.separator) {
return (
<Centered>
<StyledDivider />
<b>{this.props.item.title}</b>
</Centered>
)
}
return (
<React.Fragment>
<Location>
{({ navigate, location }) => {
let active =
location.pathname === this.props.item.path ||
location.pathname.includes(this.props.item.path + "/")
if (this.props.item.path === "/") {
active = location.pathname === this.props.item.path
}
return (
<Motion
style={{
openRatio: spring(this.state.childrenVisible ? 1 : 0),
}}
>
{({ openRatio }) => (
<React.Fragment>
<ItemTitleWrapper
className={`nav-item-${this.props.item.title
.toLowerCase()
.replace(/ /g, "-")}`}
>
{this.props.item.children && (
<StyledIcon
style={{ "--open-ratio": `${openRatio}` }}
icon={faCaretRight}
size="1x"
/>
)}
<LinkWrapper
to={this.props.item.path || "#"}
active={active ? "t" : "f"}
disabled={this.props.item.tba}
>
<ListItem onClick={this.onClick}>
<ListItemLabel>{this.props.item.title}</ListItemLabel>
{this.props.item.tba && (
<StyledChip
avatar={
<Avatar>
<FontAwesomeIcon icon={icon} />
</Avatar>
}
label={this.props.item.tba}
/>
)}
</ListItem>
</LinkWrapper>
</ItemTitleWrapper>
{this.props.item.children && (
<ChildrenList
innerRef={this.childrenListRef}
style={{ "--open-ratio": `${openRatio}` }}
>
{this.props.item.children.map((i) => (
<TreeViewItem key={i.title} item={i} />
))}
</ChildrenList>
)}
</React.Fragment>
)}
</Motion>
)
}}
</Location>
</React.Fragment>
)
}
}
function LinkWrapper(props) {
if (props.disabled) {
return <DisabledItem {...props} />
}
return <NavigationLink {...props} />
}
export default withSimpleErrorBoundary(TreeViewItem)