You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.6 KiB
74 lines
1.6 KiB
'use strict'; |
|
|
|
var PropTypes = require('./PropTypes'); |
|
|
|
/** |
|
* A mixin for components that need to know the path, routes, URL |
|
* params and query that are currently active. |
|
* |
|
* Example: |
|
* |
|
* var AboutLink = React.createClass({ |
|
* mixins: [ Router.State ], |
|
* render() { |
|
* var className = this.props.className; |
|
* |
|
* if (this.isActive('about')) |
|
* className += ' is-active'; |
|
* |
|
* return React.DOM.a({ className: className }, this.props.children); |
|
* } |
|
* }); |
|
*/ |
|
var State = { |
|
|
|
contextTypes: { |
|
router: PropTypes.router.isRequired |
|
}, |
|
|
|
/** |
|
* Returns the current URL path. |
|
*/ |
|
getPath: function getPath() { |
|
return this.context.router.getCurrentPath(); |
|
}, |
|
|
|
/** |
|
* Returns the current URL path without the query string. |
|
*/ |
|
getPathname: function getPathname() { |
|
return this.context.router.getCurrentPathname(); |
|
}, |
|
|
|
/** |
|
* Returns an object of the URL params that are currently active. |
|
*/ |
|
getParams: function getParams() { |
|
return this.context.router.getCurrentParams(); |
|
}, |
|
|
|
/** |
|
* Returns an object of the query params that are currently active. |
|
*/ |
|
getQuery: function getQuery() { |
|
return this.context.router.getCurrentQuery(); |
|
}, |
|
|
|
/** |
|
* Returns an array of the routes that are currently active. |
|
*/ |
|
getRoutes: function getRoutes() { |
|
return this.context.router.getCurrentRoutes(); |
|
}, |
|
|
|
/** |
|
* A helper method to determine if a given route, params, and query |
|
* are active. |
|
*/ |
|
isActive: function isActive(to, params, query) { |
|
return this.context.router.isActive(to, params, query); |
|
} |
|
|
|
}; |
|
|
|
module.exports = State; |