1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import find from 'lodash/find' ;
4
+ import { withRouter } from 'next/router' ;
4
5
import { Provider } from 'react-redux' ;
5
6
import AppWrapper from 'docs/src/modules/components/AppWrapper' ;
6
7
import initRedux from 'docs/src/modules/redux/initRedux' ;
@@ -229,23 +230,23 @@ const pages = [
229
230
} ,
230
231
] ;
231
232
232
- function findActivePage ( currentPages , url ) {
233
+ function findActivePage ( currentPages , router ) {
233
234
const activePage = find ( currentPages , page => {
234
235
if ( page . children ) {
235
- return url . pathname . indexOf ( page . pathname ) === 0 ;
236
+ return router . pathname . indexOf ( page . pathname ) === 0 ;
236
237
}
237
238
238
239
// Should be an exact match if no children
239
- return url . pathname === page . pathname ;
240
+ return router . pathname === page . pathname ;
240
241
} ) ;
241
242
242
243
if ( ! activePage ) {
243
244
return null ;
244
245
}
245
246
246
247
// We need to drill down
247
- if ( activePage . pathname !== url . pathname ) {
248
- return findActivePage ( activePage . children , url ) ;
248
+ if ( activePage . pathname !== router . pathname ) {
249
+ return findActivePage ( activePage . children , router ) ;
249
250
}
250
251
251
252
return activePage ;
@@ -260,19 +261,18 @@ function withRoot(Component) {
260
261
}
261
262
262
263
getChildContext ( ) {
263
- const url = this . props . url ;
264
+ const { router } = this . props ;
264
265
265
- let pathname = url . pathname ;
266
+ let pathname = router . pathname ;
266
267
if ( pathname !== '/' ) {
267
268
// The leading / is only added to support static hosting (resolve /index.html).
268
269
// We remove it to normalize the pathname.
269
270
pathname = pathname . replace ( / \/ $ / , '' ) ;
270
271
}
271
272
272
273
return {
273
- url,
274
274
pages,
275
- activePage : findActivePage ( pages , { ...url , pathname } ) ,
275
+ activePage : findActivePage ( pages , { ...router , pathname } ) ,
276
276
} ;
277
277
}
278
278
@@ -296,11 +296,10 @@ function withRoot(Component) {
296
296
WithRoot . propTypes = {
297
297
pageContext : PropTypes . object ,
298
298
reduxServerState : PropTypes . object ,
299
- url : PropTypes . object ,
299
+ router : PropTypes . object . isRequired ,
300
300
} ;
301
301
302
302
WithRoot . childContextTypes = {
303
- url : PropTypes . object ,
304
303
pages : PropTypes . array ,
305
304
activePage : PropTypes . object ,
306
305
} ;
@@ -329,7 +328,7 @@ function withRoot(Component) {
329
328
} ;
330
329
} ;
331
330
332
- return WithRoot ;
331
+ return withRouter ( WithRoot ) ;
333
332
}
334
333
335
334
export default withRoot ;
0 commit comments