This repository was archived by the owner on Feb 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathApp.js
More file actions
152 lines (140 loc) · 3.92 KB
/
Copy pathApp.js
File metadata and controls
152 lines (140 loc) · 3.92 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
import React from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
import { Route, Redirect, Switch } from 'react-router-dom';
import {
VerticalNav,
VerticalNavItem,
VerticalNavSecondaryItem,
Masthead,
Icon,
MenuItem
} from 'patternfly-react';
import pfLogo from 'patternfly/dist/img/logo-alt.svg';
import pfBrand from 'patternfly/dist/img/brand-alt.svg';
import { routes } from './routes';
import './App.css';
class App extends React.Component {
state = {
collapse: false
};
constructor() {
super();
this.menu = routes();
}
handleNavClick = (event: Event) => {
event.preventDefault();
const target = (event.currentTarget: any);
const { history } = this.props;
if (target.getAttribute) {
const href = target.getAttribute('href');
history.push(href);
}
};
renderContent = () => {
const allRoutes = [];
this.menu.map((item, index) => {
allRoutes.push(
<Route key={index} exact path={item.to} component={item.component} />
);
if (item.subItems) {
item.subItems.map((secondaryItem, subIndex) =>
allRoutes.push(
<Route
key={subIndex}
exact
path={secondaryItem.to}
component={secondaryItem.component}
/>
)
);
}
return allRoutes;
});
return (
<Switch>
{allRoutes}
<Redirect from="*" to="/" key="default-route" />
</Switch>
);
};
onCollapse = () => {
this.setState({ collapse: !this.state.collapse });
};
navigateTo = path => {
const { history } = this.props;
history.push(path);
};
render() {
const { location } = this.props;
const activeItem = this.menu.find(
item => location.pathname.indexOf(item.to) > -1
);
const vertNavItems = this.menu.map(item => (
<VerticalNavItem
key={item.to}
title={item.title}
iconClass={item.iconClass}
active={item === activeItem}
onClick={() => this.navigateTo(item.to)}
>
{item.subItems &&
item.subItems.map(secondaryItem => (
<VerticalNavSecondaryItem
key={secondaryItem.to}
title={secondaryItem.title}
iconClass={secondaryItem.iconClass}
active={secondaryItem.to === location.pathname}
onClick={() => this.navigateTo(secondaryItem.to)}
/>
))}
</VerticalNavItem>
));
return (
<React.Fragment>
<Masthead
iconImg={pfLogo}
titleImg={pfBrand}
title="PatternFly React Demo App"
onNavToggleClick={() => this.onCollapse()}
>
<Masthead.Collapse>
<Masthead.Dropdown
id="app-help-dropdown"
noCaret
title={<span title="Help" className="pficon pficon-help" />}
>
<MenuItem eventKey="1">Help</MenuItem>
<MenuItem eventKey="2">About</MenuItem>
</Masthead.Dropdown>
<Masthead.Dropdown
id="app-user-dropdown"
title={[
<Icon type="pf" name="user" key="user-icon" />,
<span className="dropdown-title" key="dropdown-title">
Brian Johnson
</span>
]}
>
<MenuItem eventKey="1">User Preferences</MenuItem>
<MenuItem eventKey="2">Logout</MenuItem>
</Masthead.Dropdown>
</Masthead.Collapse>
</Masthead>
<VerticalNav
persistentSecondary={false}
hideMasthead
navCollapsed={this.state.collapse}
>
{vertNavItems}
</VerticalNav>
{this.renderContent()}
</React.Fragment>
);
}
}
App.propTypes = {
history: PropTypes.object.isRequired,
location: PropTypes.object.isRequired
};
export default withRouter(App);