Skip to content
This repository was archived by the owner on May 23, 2018. It is now read-only.

Commit 55db083

Browse files
committed
feat: add listeners and state to link component, fix mixin
1 parent fb9fcd5 commit 55db083

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

modules/link-factory.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,69 @@
11
import React from 'react'
22

3-
export default router5LinkFactory
3+
export default linkFactory
44

5-
let router5LinkFactory = (router) => {
5+
let linkFactory = (router) => {
66
return React.createClass({
77
propTypes: {
88
routeName: React.PropTypes.string.isRequired
9-
routeParams: React.PropTypes.object
10-
routeOptions: React.PropTypes.object
9+
routeParams: React.PropTypes.object,
10+
routeOptions: React.PropTypes.object,
11+
activeClass: React.PropTypes.string,
12+
onClick: React.PropTypes.func
1113
},
1214

15+
getDefaultProps() ({
16+
className: '',
17+
activeClass: 'active',
18+
routeParams: {},
19+
routeOptions: {},
20+
onClick: this.clickHandler
21+
}),
22+
23+
getInitialState() ({
24+
// Initialise state
25+
// Not an anti-pattern
26+
// https://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html
27+
active: router.isActive(this.props.routeName, this.prop.routeParams)
28+
}),
29+
30+
// Is it overkill?
1331
shouldComponentUpdate(nextProps, nextState) {
1432
return !router.areStatesEqual(
1533
{name: nextProps.routeName, params: nextProps.routeParams},
1634
{name: this.props.routeName, params: this.props.routeParams}
17-
)
35+
) || this.state.active !== nextState.active;
1836
},
1937

2038
clickHandler(evt) {
2139
evt.preventDefault()
2240
router.navigate(this.props.routeName, this.props.routeParams, this.props.options)
2341
},
2442

43+
// Is it overkill?
44+
// Should it be an option to observe state in Links?
45+
// Should we add a GroupLink component for menus?
46+
routeChangeHandler(toState, fromState) {
47+
this.setState({active: router.isActive(this.props.routeName, this.prop.routeParams)})
48+
},
49+
50+
componentDidMount() {
51+
router.addListener(this.routeChangeHandler)
52+
},
53+
54+
componentWillUnmount() {
55+
router.removeListener(this.routeChangeHandler)
56+
}
57+
2558
render() {
26-
let path = router.buildPath(this.props.routeName, this.props.routeParams)
27-
return <a href={path} onClick={clickHandler}></a>
59+
let props = this.props
60+
let active = this.state.active
61+
62+
let path = router.buildPath(this.props.routeName, this.props.routeParams);
63+
let className = props.className.split(' ')
64+
.concat(active ? [activeClassName] : []).join(' ')
65+
66+
return <a href={path} className={className} onClick={props.onClick}></a>
2867
}
2968
})
3069
}

modules/segment-mixin-factory.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ export default segmentMixinFactory
22

33
let segmentMixinFactory = (router) => {
44
return (routeName, listener) => ({
5+
nodeListener: () {
6+
listener.bind(this)()
7+
},
8+
59
componentDidMount() {
6-
router.addNodeListener(routeName, listener.bind(this))
10+
router.addNodeListener(routeName, this.nodeListener)
711
router.registerComponent(routeName, this)
812
},
913

1014
componentWillUnmount() {
11-
router.addremoveNodeListener(routeName, listener.bind(this))
15+
router.addremoveNodeListener(routeName, this.nodeListener)
1216
router.deregisterComponent(routeName, this)
1317
}
1418
})

0 commit comments

Comments
 (0)