forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSidebarItem.jsx
More file actions
145 lines (131 loc) · 3.72 KB
/
SidebarItem.jsx
File metadata and controls
145 lines (131 loc) · 3.72 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
import PropTypes from "prop-types";
import { Component } from "react";
import "./SidebarItem.scss";
import { NavLink } from "react-router-dom";
import GraphIcon from "../../styles/icons/graph.svg";
import BarIcon from "../../styles/icons/vertical-bar.svg";
import list2Tree from "../../utilities/list2Tree/index.js";
const block = "sidebar-item";
export default class SidebarItem extends Component {
static propTypes = {
title: PropTypes.string,
anchors: PropTypes.array,
url: PropTypes.string,
currentPage: PropTypes.string,
};
state = {
open: this._isOpen(this.props),
};
scrollTop(event) {
// there're two cases
// 1. location.pathname or location.hash changes which will be handled by useEffect in Page.jsx
// 2. location.pathname and location.hash doesn't change at all
if (window.location.hash !== "") {
// case 1
return;
}
if (!event.metaKey && !event.ctrlKey) {
// case 2
window.scrollTo(0, 0);
}
}
renderAnchors(anchors) {
return (
<ul className={`${block}__anchors`}>
{anchors.map((anchor) => (
<li
key={this._generateAnchorURL(anchor)}
className={`${block}__anchor`}
title={anchor.title}
>
<NavLink to={this._generateAnchorURL(anchor)}>
{anchor.title2}
</NavLink>
{anchor.children && this.renderAnchors(anchor.children)}
</li>
))}
</ul>
);
}
render() {
const { title, anchors = [] } = this.props;
const openMod = this.state.open ? `${block}--open` : "";
const disabledMod = anchors.length === 0 ? `${block}--disabled` : "";
const isDependencyGraph = title === "Dependency Graph";
const filteredAnchors = anchors.filter((anchor) => anchor.level > 1);
const tree = list2Tree(title, filteredAnchors);
return (
<div className={`${block} ${openMod} ${disabledMod}`}>
{anchors.length > 0 ? (
<GraphIcon
width={15}
height={17}
fill="#175d96"
className={`${block}__toggle ${block}__toggle--graph`}
onClick={this._toggle.bind(this)}
/>
) : isDependencyGraph ? (
<GraphIcon
className={`${block}__toggle ${block}__toggle--static`}
width={15}
height={17}
fill="#175d96"
/>
) : (
<BarIcon
className={`${block}__toggle`}
width={15}
height={17}
fill="#175d96"
/>
)}
<NavLink
end
key={this.props.url}
className={`${block}__title`}
to={this.props.url}
onClick={this.scrollTop}
>
{title}
</NavLink>
{anchors.length > 0 ? this.renderAnchors(tree) : null}
</div>
);
}
// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.currentPage !== this.props.currentPage) {
this.setState({
open: this._isOpen(nextProps),
});
}
}
/**
* Checks whether the item should be expanded
*
* @param {object} props - The current props
*/
_isOpen(props) {
return new RegExp(`${props.currentPage}/?$`).test(props.url);
}
/**
* Toggles the open state (expanded/collapsed)
*
* @param {object} e - Click event
*/
_toggle() {
this.setState({
open: !this.state.open,
});
}
/**
* Generate the url for the given [anchor] depending on the current page
*
* @param {object} anchor - The anchor object containing its id
* @returns {string}
*/
_generateAnchorURL(anchor) {
const { url } = this.props;
return anchor.id ? `${url}#${anchor.id}` : url;
}
}