forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[changed] isActive is an instance method
[removed] <Routes onActiveStateChange> This commit removes ActiveStore (yay!). Instead, <Routes> components now store their own active state and emit active state change events to ActiveState descendants that are interested.
- Loading branch information
1 parent
af1fb6e
commit a4ce7c8
Showing
10 changed files
with
236 additions
and
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
var React = require('react'); | ||
var ChangeEmitter = require('./ChangeEmitter'); | ||
|
||
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 store the active state of routes, URL | ||
* parameters, and query. | ||
*/ | ||
var ActiveDelegate = { | ||
|
||
mixins: [ ChangeEmitter ], | ||
|
||
childContextTypes: { | ||
activeDelegate: React.PropTypes.any.isRequired | ||
}, | ||
|
||
getChildContext: function () { | ||
return { | ||
activeDelegate: this | ||
}; | ||
}, | ||
|
||
/** | ||
* Returns true if the route with the given name, URL parameters, and | ||
* query are all currently active. | ||
*/ | ||
isActive: function (routeName, params, query) { | ||
var activeRoutes = this.state.activeRoutes || []; | ||
var activeParams = this.state.activeParams || {}; | ||
var activeQuery = this.state.activeQuery || {}; | ||
|
||
var isActive = routeIsActive(activeRoutes, routeName) && paramsAreActive(activeParams, params); | ||
|
||
if (query) | ||
return isActive && queryIsActive(activeQuery, query); | ||
|
||
return isActive; | ||
} | ||
|
||
}; | ||
|
||
module.exports = ActiveDelegate; |
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,50 @@ | ||
var React = require('react'); | ||
var EventEmitter = require('events').EventEmitter; | ||
|
||
var CHANGE_EVENT = 'change'; | ||
|
||
/** | ||
* A mixin for components that emit change events. ActiveDelegate uses | ||
* this mixin to notify descendant ActiveState components when the | ||
* active state changes. | ||
*/ | ||
var ChangeEmitter = { | ||
|
||
propTypes: { | ||
maxChangeListeners: React.PropTypes.number.isRequired | ||
}, | ||
|
||
getDefaultProps: function () { | ||
return { | ||
maxChangeListeners: 0 | ||
}; | ||
}, | ||
|
||
componentWillMount: function () { | ||
this._events = new EventEmitter; | ||
this._events.setMaxListeners(this.props.maxChangeListeners); | ||
}, | ||
|
||
componentWillReceiveProps: function (nextProps) { | ||
this._events.setMaxListeners(nextProps.maxChangeListeners); | ||
}, | ||
|
||
componentWillUnmount: function () { | ||
this._events.removeAllListeners(); | ||
}, | ||
|
||
addChangeListener: function (listener) { | ||
this._events.addListener(CHANGE_EVENT, listener); | ||
}, | ||
|
||
removeChangeListener: function (listener) { | ||
this._events.removeListener(CHANGE_EVENT, listener); | ||
}, | ||
|
||
emitChange: function () { | ||
this._events.emit(CHANGE_EVENT); | ||
} | ||
|
||
}; | ||
|
||
module.exports = ChangeEmitter; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.