Skip to content

Commit db45c8c

Browse files
Merge pull request #9 from marcoaraujojunior/develop
I put Menu in charge to know which links will be used
2 parents 9d71d91 + 53b627f commit db45c8c

File tree

3 files changed

+126
-46
lines changed

3 files changed

+126
-46
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React from 'react';
2+
import { Link } from 'react-router'
3+
4+
class LinksApp extends React.Component
5+
{
6+
constructor(props, context) {
7+
super(props, context);
8+
this.generate = this.generate.bind(this);
9+
this.handleClick = this.handleClick.bind(this);
10+
this.state = {
11+
links: this.props.links || []
12+
};
13+
}
14+
15+
handleClick(e) {
16+
if (this.context.onClick) {
17+
this.context.onClick();
18+
}
19+
}
20+
21+
generate() {
22+
return this.state.links.map((link, index) => (
23+
<Link to={link[0]}
24+
key={index}
25+
className="nav-item is-tab"
26+
onlyActiveOnIndex={true}
27+
activeClassName="is-active"
28+
onClick={this.handleClick}
29+
>
30+
{link[1]}
31+
</Link>
32+
));
33+
}
34+
35+
render() {
36+
return (
37+
<span className="nav-right nav-menu is-active">
38+
{this.generate()}
39+
</span>
40+
);
41+
}
42+
}
43+
44+
LinksApp.propTypes = {
45+
links: React.PropTypes.array.isRequired
46+
};
47+
48+
LinksApp.contextTypes = {
49+
onClick: React.PropTypes.func
50+
};
51+
52+
export default LinksApp;
53+

app/components/Menu/Menu.js

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import React from 'react';
2-
import { Router, Link } from 'react-router'
2+
import { Router } from 'react-router'
3+
4+
import Nav from 'components/Nav/Nav'
5+
import LinksApp from 'components/LinksApp/LinksApp'
6+
37

48
class Menu extends React.Component{
59
constructor(props, context) {
610
super(props, context);
711
this.handleLogout = this.handleLogout.bind(this);
812
this.handleView = this.handleView.bind(this);
9-
this.toggleNav = this.toggleNav.bind(this);
10-
this.handleLink = this.handleLink.bind(this);
1113
this.state = {
12-
toggleNav: ''
14+
links: [
15+
[ '/', 'Home'],
16+
['/client', 'Client'],
17+
['/area', 'Area']
18+
]
1319
};
1420
}
1521

@@ -18,50 +24,16 @@ class Menu extends React.Component{
1824
this.context.router.push("/");
1925
}
2026

21-
toggleNav() {
22-
let isActive = this.state.toggleNav ? '' : 'is-active';
23-
this.setState({toggleNav: isActive});
24-
}
25-
26-
handleLink(e) {
27-
this.setState({toggleNav: ''});
28-
}
29-
3027
handleView() {
3128
return (
32-
<nav className="nav has-shadow" id="top">
33-
<div className="container">
34-
<div className="nav-left">
35-
<a className="nav-item" href="../index.html">IClient</a>
36-
</div>
37-
<span className={ `nav-toggle ${this.state.toggleNav}` } onClick={this.toggleNav}>
38-
<span></span>
39-
<span></span>
40-
<span></span>
41-
</span>
42-
<div className={ `nav-right nav-menu ${this.state.toggleNav}` }>
43-
<Link to="/"
44-
className="nav-item is-tab"
45-
onlyActiveOnIndex={true}
46-
activeClassName="is-active"
47-
onClick={this.handleLink}>Home
48-
</Link>
49-
<Link to="/client"
50-
className="nav-item is-tab"
51-
activeClassName="is-active"
52-
onClick={this.handleLink}>Client
53-
</Link>
54-
<Link to="/area"
55-
className="nav-item is-tab"
56-
activeClassName="is-active"
57-
onClick={this.handleLink}>Area
58-
</Link>
59-
<span className="nav-item">
60-
<a className="button" onClick={this.handleLogout}>Logout</a>
61-
</span>
62-
</div>
63-
</div>
64-
</nav>
29+
<Nav>
30+
<LinksApp
31+
links={this.state.links}
32+
/>
33+
<span className="nav-item">
34+
<a className="button" onClick={this.handleLogout}>Logout</a>
35+
</span>
36+
</Nav>
6537
);
6638
}
6739

app/components/Nav/Nav.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
3+
class Nav extends React.Component
4+
{
5+
constructor(props, context) {
6+
super(props, context);
7+
this.toggleNavStatus = this.toggleNavStatus.bind(this);
8+
this.hide = this.hide.bind(this);
9+
this.state = {
10+
toggleNavStatus: ''
11+
};
12+
}
13+
14+
getChildContext() {
15+
return {
16+
onClick: this.toggleNavStatus
17+
}
18+
}
19+
20+
hide(e) {
21+
this.setState({toggleNavStatus: ''});
22+
}
23+
24+
toggleNavStatus() {
25+
let isActive = this.state.toggleNavStatus ? '' : 'is-active';
26+
this.setState({toggleNavStatus: isActive});
27+
}
28+
29+
render() {
30+
return (
31+
<nav className="nav has-shadow" id="top">
32+
<div className="container">
33+
<div className="nav-left">
34+
<a className="nav-item" href="../index.html">IClient</a>
35+
</div>
36+
<span className={ `nav-toggle ${this.state.toggleNavStatus}` } onClick={this.toggleNavStatus}>
37+
<span></span>
38+
<span></span>
39+
<span></span>
40+
</span>
41+
<div className={ `nav-right nav-menu ${this.state.toggleNavStatus}` }>
42+
{this.props.children}
43+
</div>
44+
</div>
45+
</nav>
46+
);
47+
}
48+
}
49+
50+
Nav.childContextTypes = {
51+
onClick: React.PropTypes.func
52+
};
53+
54+
export default Nav;
55+

0 commit comments

Comments
 (0)