-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components can mixin Router.ActiveState to receive notifications when the active routes, params, and query change. Fixes #85
- Loading branch information
Showing
5 changed files
with
135 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
var ActiveStore = require('../stores/ActiveStore'); | ||
|
||
function routeIsActive(activeRoutes, routeName) { | ||
return activeRoutes.some(function (route) { | ||
return route.props.name === routeName; | ||
}); | ||
} | ||
|
||
function paramsAreActive(activeParams, params) { | ||
for (var property in params) { | ||
if (activeParams[property] !== String(params[property])) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function queryIsActive(activeQuery, query) { | ||
for (var property in query) { | ||
if (activeQuery[property] !== String(query[property])) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* A mixin for components that need to know about the routes, params, | ||
* and query that are currently active. Components that use it get two | ||
* things: | ||
* | ||
* 1. An `isActive` static method they can use to check if a route, | ||
* params, and query are active. | ||
* 2. An `updateActiveState` instance method that is called when the | ||
* active state changes. | ||
* | ||
* Example: | ||
* | ||
* var Tab = React.createClass({ | ||
* | ||
* mixins: [ Router.ActiveState ], | ||
* | ||
* getInitialState: function () { | ||
* return { | ||
* isActive: false | ||
* }; | ||
* }, | ||
* | ||
* updateActiveState: function () { | ||
* this.setState({ | ||
* isActive: Tab.isActive(routeName, params, query) | ||
* }) | ||
* } | ||
* | ||
* }); | ||
*/ | ||
var ActiveState = { | ||
|
||
statics: { | ||
|
||
/** | ||
* Returns true if the route with the given name, URL parameters, and query | ||
* are all currently active. | ||
*/ | ||
isActive: function (routeName, params, query) { | ||
var state = ActiveStore.getState(); | ||
var isActive = routeIsActive(state.routes, routeName) && paramsAreActive(state.params, params); | ||
|
||
if (query) | ||
return isActive && queryIsActive(state.query, query); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ryanflorence
Member
|
||
|
||
return isActive; | ||
} | ||
|
||
}, | ||
|
||
componentWillMount: function () { | ||
ActiveStore.addChangeListener(this.handleActiveStateChange); | ||
}, | ||
|
||
componentDidMount: function () { | ||
if (this.updateActiveState) | ||
this.updateActiveState(); | ||
}, | ||
|
||
componentWillUnmount: function () { | ||
ActiveStore.removeChangeListener(this.handleActiveStateChange); | ||
}, | ||
|
||
handleActiveStateChange: function () { | ||
if (this.isMounted() && this.updateActiveState) | ||
this.updateActiveState(); | ||
} | ||
|
||
}; | ||
|
||
module.exports = ActiveState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit 8562482
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
I don't think we want this do we? If I'm at
/users?sortBy=name
and/users?sortBy=age
I probably still want<Link to="users"/>
to be active.But ... I'd probably also want
<Link to="users" query={{sortBy: 'status'}}/>
to not be active ... hmmmmmmmmmm