Skip to content

Commit 0e44015

Browse files
committed
Created context that inform how onClick works
1 parent 4a3a4b7 commit 0e44015

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

app/components/LinksApp/LinksApp.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class LinksApp extends React.Component
77
super(props, context);
88
this.generate = this.generate.bind(this);
99
this.handleLogout = this.handleLogout.bind(this);
10+
this.handleClick = this.handleClick.bind(this);
1011
this.state = {
1112
links: [
1213
[ '/', 'Home'],
@@ -17,19 +18,26 @@ class LinksApp extends React.Component
1718
}
1819

1920
handleLogout() {
20-
console.log('Marco', this.context, this.context.router);
2121
delete localStorage.token;
2222
this.context.router.push("/");
2323
}
2424

25+
handleClick(e) {
26+
if (this.context.onClick) {
27+
this.context.onClick();
28+
}
29+
}
30+
2531
generate() {
2632
return this.state.links.map((link, index) => (
2733
<Link to={link[0]}
2834
key={index}
2935
className="nav-item is-tab"
3036
onlyActiveOnIndex={true}
3137
activeClassName="is-active"
32-
onClick={this.props.hide}>{link[1]}
38+
onClick={this.handleClick}
39+
>
40+
{link[1]}
3341
</Link>
3442
));
3543
}
@@ -46,12 +54,9 @@ class LinksApp extends React.Component
4654
}
4755
}
4856

49-
LinksApp.propTypes = {
50-
hide: React.PropTypes.func.isRequired
51-
};
52-
5357
LinksApp.contextTypes = {
54-
router: React.PropTypes.object.isRequired
58+
router: React.PropTypes.object.isRequired,
59+
onClick: React.PropTypes.func
5560
};
5661

5762
export default LinksApp;

app/components/Menu/Menu.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react';
22

33
import Nav from 'components/Nav/Nav'
4+
import LinksApp from 'components/LinksApp/LinksApp'
5+
46

57
class Menu extends React.Component{
68
constructor(props, context) {
@@ -10,7 +12,9 @@ class Menu extends React.Component{
1012

1113
handleView() {
1214
return (
13-
<Nav />
15+
<Nav>
16+
<LinksApp />
17+
</Nav>
1418
);
1519
}
1620

app/components/Nav/Nav.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import React from 'react';
22

3-
import LinksApp from 'components/LinksApp/LinksApp'
4-
53
class Nav extends React.Component
64
{
75
constructor(props, context) {
86
super(props, context);
9-
this.toggleNav = this.toggleNav.bind(this);
7+
this.toggleNavStatus = this.toggleNavStatus.bind(this);
108
this.hide = this.hide.bind(this);
119
this.state = {
12-
toggleNav: ''
10+
toggleNavStatus: ''
1311
};
1412
}
1513

14+
getChildContext() {
15+
return {
16+
onClick: this.toggleNavStatus
17+
}
18+
}
19+
1620
hide(e) {
17-
this.setState({toggleNav: ''});
21+
this.setState({toggleNavStatus: ''});
1822
}
1923

20-
toggleNav() {
21-
let isActive = this.state.toggleNav ? '' : 'is-active';
22-
this.setState({toggleNav: isActive});
24+
toggleNavStatus() {
25+
let isActive = this.state.toggleNavStatus ? '' : 'is-active';
26+
this.setState({toggleNavStatus: isActive});
2327
}
2428

2529
render() {
@@ -29,13 +33,12 @@ class Nav extends React.Component
2933
<div className="nav-left">
3034
<a className="nav-item" href="../index.html">IClient</a>
3135
</div>
32-
<span className={ `nav-toggle ${this.state.toggleNav}` } onClick={this.toggleNav}>
36+
<span className={ `nav-toggle ${this.state.toggleNavStatus}` } onClick={this.toggleNavStatus}>
3337
<span></span>
3438
<span></span>
3539
<span></span>
3640
</span>
37-
<div className={ `nav-right nav-menu ${this.state.toggleNav}` }>
38-
<LinksApp hide={this.hide} />
41+
<div className={ `nav-right nav-menu ${this.state.toggleNavStatus}` }>
3942
{this.props.children}
4043
</div>
4144
</div>
@@ -44,5 +47,9 @@ class Nav extends React.Component
4447
}
4548
}
4649

50+
Nav.childContextTypes = {
51+
onClick: React.PropTypes.func
52+
};
53+
4754
export default Nav;
4855

0 commit comments

Comments
 (0)