1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/test/integration/with-router/components/header-nav.js

73 lines
2.1 KiB
JavaScript

import * as React from 'react'
import Router, { withRouter } from 'next/router'
import Link from 'next/link'
const pages = {
'/a': 'Foo',
'/b': 'Bar'
}
class HeaderNav extends React.Component {
constructor ({ router }) {
super()
this.state = {
activeURL: router.asPath,
activeURLTopLevelRouterDeprecatedBehavior: router.asPath,
activeURLTopLevelRouter: router.asPath
}
this.handleRouteChange = this.handleRouteChange.bind(this)
this.handleRouteChangeTopLevelRouter = this.handleRouteChangeTopLevelRouter.bind(this)
this.handleRouteChangeTopLevelRouterDeprecatedBehavior = this.handleRouteChangeTopLevelRouterDeprecatedBehavior.bind(this)
}
componentDidMount () {
Router.onRouteChangeComplete = this.handleRouteChangeTopLevelRouterDeprecatedBehavior
Router.events.on('routeChangeComplete', this.handleRouteChangeTopLevelRouter)
this.props.router.events.on('routeChangeComplete', this.handleRouteChange)
}
componentWillUnmount () {
Router.onRouteChangeComplete = null
Router.events.on('routeChangeComplete', this.handleRouteChangeTopLevelRouter)
this.props.router.events.off('routeChangeComplete', this.handleRouteChange)
}
handleRouteChangeTopLevelRouterDeprecatedBehavior (url) {
this.setState({
activeURLTopLevelRouterDeprecatedBehavior: url
})
}
handleRouteChangeTopLevelRouter (url) {
this.setState({
activeURLTopLevelRouter: url
})
}
handleRouteChange (url) {
this.setState({
activeURL: url
})
}
render () {
return (
<nav>
{
Object.keys(pages).map(url => (
<Link href={url} key={url} prefetch>
<a className={`${this.state.activeURL === url ? 'active' : ''} ${this.state.activeURLTopLevelRouter === url ? 'active-top-level-router' : ''} ${this.state.activeURLTopLevelRouterDeprecatedBehavior === url ? 'active-top-level-router-deprecated-behavior' : ''}`}>
{ pages[url] }
</a>
</Link>
))
}
</nav>
)
}
}
export default withRouter(HeaderNav)