74 lines
1.6 KiB
JavaScript
Raw Normal View History

2015-04-29 11:02:32 +02:00
'use strict';
2015-04-21 19:38:17 +02:00
2015-04-29 11:02:32 +02:00
var PropTypes = require('./PropTypes');
2015-04-21 19:38:17 +02:00
/**
* 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;
2015-04-29 11:02:32 +02:00
*
2015-04-21 19:38:17 +02:00
* if (this.isActive('about'))
* className += ' is-active';
2015-04-29 11:02:32 +02:00
*
2015-04-21 19:38:17 +02:00
* return React.DOM.a({ className: className }, this.props.children);
* }
* });
*/
var State = {
contextTypes: {
router: PropTypes.router.isRequired
},
/**
* Returns the current URL path.
*/
2015-04-29 11:02:32 +02:00
getPath: function getPath() {
2015-04-21 19:38:17 +02:00
return this.context.router.getCurrentPath();
2015-04-29 11:02:32 +02:00
},
2015-04-21 19:38:17 +02:00
/**
* Returns the current URL path without the query string.
*/
2015-04-29 11:02:32 +02:00
getPathname: function getPathname() {
2015-04-21 19:38:17 +02:00
return this.context.router.getCurrentPathname();
2015-04-29 11:02:32 +02:00
},
2015-04-21 19:38:17 +02:00
/**
* Returns an object of the URL params that are currently active.
*/
2015-04-29 11:02:32 +02:00
getParams: function getParams() {
2015-04-21 19:38:17 +02:00
return this.context.router.getCurrentParams();
2015-04-29 11:02:32 +02:00
},
2015-04-21 19:38:17 +02:00
/**
* Returns an object of the query params that are currently active.
*/
2015-04-29 11:02:32 +02:00
getQuery: function getQuery() {
2015-04-21 19:38:17 +02:00
return this.context.router.getCurrentQuery();
2015-04-29 11:02:32 +02:00
},
2015-04-21 19:38:17 +02:00
/**
* Returns an array of the routes that are currently active.
*/
2015-04-29 11:02:32 +02:00
getRoutes: function getRoutes() {
2015-04-21 19:38:17 +02:00
return this.context.router.getCurrentRoutes();
2015-04-29 11:02:32 +02:00
},
2015-04-21 19:38:17 +02:00
/**
* A helper method to determine if a given route, params, and query
* are active.
*/
2015-04-29 11:02:32 +02:00
isActive: function isActive(to, params, query) {
2015-04-21 19:38:17 +02:00
return this.context.router.isActive(to, params, query);
2015-04-29 11:02:32 +02:00
}
2015-04-21 19:38:17 +02:00
};
module.exports = State;