Skip to content

Commit 923908d

Browse files
author
vikasrohit
authored
Merge pull request #272 from appirio-tech/feature/connectv2-router-upgrade
Fixed our custom NavLink to work as expected for selected link after react router upgrade
2 parents f2c12c7 + 0cddacf commit 923908d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

components/NavLink/NavLink.jsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import _ from 'lodash'
12
import React, { PropTypes, Component } from 'react'
2-
import { NavLink as RNavLink} from 'react-router-dom'
3+
import { NavLink as RNavLink, matchPath} from 'react-router-dom'
34

45
class NavLink extends Component {
56
constructor(props) {
@@ -8,8 +9,16 @@ class NavLink extends Component {
89

910
render() {
1011
const { to, content, target } = this.props
12+
const { router } = this.context
1113
let classes = this.props.classes
1214
const attrs = { to }
15+
if (to && router) {
16+
const pathname = _.get(router, 'route.location.pathname', '')
17+
const active = matchPath(pathname, { path: to }) !== null
18+
if (active) {
19+
classes += ' selected'
20+
}
21+
}
1322
if (target || target !== '_self') {
1423
attrs.target = target
1524
if (attrs.target === '_blank') {
@@ -18,17 +27,21 @@ class NavLink extends Component {
1827
}
1928
return (
2029
<li className={classes}>
21-
<RNavLink activeClassName="selected" {...attrs}>{content}</RNavLink>
30+
<RNavLink activeClassName="active" {...attrs}>{content}</RNavLink>
2231
</li>
2332
)
2433
}
2534
}
2635

36+
NavLink.contextTypes ={
37+
router: PropTypes.object
38+
}
39+
2740
NavLink.propTypes = {
2841
to: PropTypes.string.isRequired,
2942
target: PropTypes.string,
3043
content: PropTypes.string.isRequired,
3144
classes: PropTypes.string.isRequired
3245
}
3346

34-
export default NavLink
47+
export default NavLink

0 commit comments

Comments
 (0)