Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions components/NavLink/NavLink.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash'
import React, { PropTypes, Component } from 'react'
import { NavLink as RNavLink} from 'react-router-dom'
import { NavLink as RNavLink, matchPath} from 'react-router-dom'

class NavLink extends Component {
constructor(props) {
Expand All @@ -8,8 +9,16 @@ class NavLink extends Component {

render() {
const { to, content, target } = this.props
const { router } = this.context
let classes = this.props.classes
const attrs = { to }
if (to && router) {
const pathname = _.get(router, 'route.location.pathname', '')
const active = matchPath(pathname, { path: to }) !== null
if (active) {
classes += ' selected'
}
}
if (target || target !== '_self') {
attrs.target = target
if (attrs.target === '_blank') {
Expand All @@ -18,17 +27,21 @@ class NavLink extends Component {
}
return (
<li className={classes}>
<RNavLink activeClassName="selected" {...attrs}>{content}</RNavLink>
<RNavLink activeClassName="active" {...attrs}>{content}</RNavLink>
</li>
)
}
}

NavLink.contextTypes ={
router: PropTypes.object
}

NavLink.propTypes = {
to: PropTypes.string.isRequired,
target: PropTypes.string,
content: PropTypes.string.isRequired,
classes: PropTypes.string.isRequired
}

export default NavLink
export default NavLink