/
submenu.js
150 lines (134 loc) · 3.59 KB
/
submenu.js
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
import React from 'react';
import withStyles from '@material-ui/core/styles/withStyles';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import Collapse from '@material-ui/core/Collapse';
import Typography from '@material-ui/core/Typography';
import Icon from './icon';
import each from 'lodash/each';
import registrar from 'mson/lib/compiler/registrar';
const styles = theme => ({
nested: {
paddingLeft: theme.spacing(4)
},
primary: {
color: theme.palette.text.primary
},
secondary: {
color: theme.palette.text.secondary
},
selected: {
fontWeight: 'bold'
}
});
class Submenu extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
open: this.childSelected()
};
}
childSelected() {
const { item, path } = this.props;
let childSelected = false;
each(item.items, item => {
if (path === item.path) {
childSelected = true;
return false; // exit loop
}
});
return childSelected;
}
handleClick = item => {
// Has sub items?
if (item.items) {
this.setState({ open: !this.state.open });
} else {
// Note: the navigation is handled by the parent as we want to be able to prevent the
// navigation if say a form is dirty, etc...
if (this.props.onNavigate) {
this.props.onNavigate(item);
}
if (this.props.onDrawerToggle) {
// Close the drawer
this.props.onDrawerToggle();
}
}
};
items() {
const { classes, item, path } = this.props;
const listItems = [];
item.items.forEach((item, index) => {
const isSelected = path === item.path;
let classNames = [classes.secondary];
if (isSelected) {
classNames.push(classes.selected);
}
if (
(!item.roles ||
(registrar.client && registrar.client.user.hasRole(item.roles))) &&
item.hidden !== true
) {
listItems.push(
<ListItem
button
className={classes.nested}
key={index}
onClick={() => this.handleClick(item)}
>
<ListItemText
disableTypography
primary={
<Typography variant="body2" className={classNames.join(' ')}>
{item.label}
</Typography>
}
/>
</ListItem>
);
}
});
return listItems;
}
render() {
const { item, classes, path } = this.props;
const items = item.items ? this.items() : null;
const isSelected = path === item.path;
let classNames = [classes.primary];
if (isSelected) {
classNames.push(classes.selected);
}
let listItems = null;
if (items) {
listItems = (
<Collapse in={this.state.open} timeout="auto" unmountOnExit>
<List disablePadding>{this.items()}</List>
</Collapse>
);
}
return (
<div>
<ListItem button onClick={() => this.handleClick(item)}>
<ListItemText
disableTypography
primary={
<Typography variant="subtitle1" className={classNames.join(' ')}>
{item.label}
</Typography>
}
/>
{items ? (
this.state.open ? (
<Icon icon="ExpandLess" />
) : (
<Icon icon="ExpandMore" />
)
) : null}
</ListItem>
{listItems}
</div>
);
}
}
export default withStyles(styles)(Submenu);