Skip to content

Commit ed0cf62

Browse files
committed
Yet another large refactoring (hooray!)
[added] Navigation mixin for components that need to modify the URL [added] CurrentPath mixin for components that need to know the current URL path [added] getActiveRoutes, getActiveParams, and getActiveQuery methods to ActiveState mixin [removed] Awkward updateActiveState callback from ActiveState mixin [removed] Router.PathState (use Router.CurrentPath instead) [removed] Router.Transitions (use Router.Navigation instead) [removed] Router.RouteLookup (because it was useless) [added] <Routes scrollBehavior="browser"> as an alternative to scrollBehavior="imitateBrowser" [changed] <Routes fixedPath> => <Routes initialPath>. Currently only used in testing, but will be useful for SSR [added] <Routes onTransition> but starting to think the <Routes on*> hooks should be private
1 parent 931daf4 commit ed0cf62

34 files changed

+1146
-1110
lines changed

modules/actions/LocationActions.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ var LocationActions = {
2121
/**
2222
* Indicates the most recent entry should be removed from the history stack.
2323
*/
24-
POP: 'pop'
24+
POP: 'pop',
25+
26+
/**
27+
* Indicates that a route transition is finished.
28+
*/
29+
FINISHED_TRANSITION: 'finished-transition'
2530

2631
};
2732

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var LocationActions = require('../actions/LocationActions');
2+
3+
/**
4+
* A scroll behavior that attempts to imitate the default behavior
5+
* of modern browsers.
6+
*/
7+
var ImitateBrowserBehavior = {
8+
9+
updateScrollPosition: function (position, actionType) {
10+
switch (actionType) {
11+
case LocationActions.PUSH:
12+
case LocationActions.REPLACE:
13+
window.scrollTo(0, 0);
14+
break;
15+
case LocationActions.POP:
16+
window.scrollTo(position.x, position.y);
17+
break;
18+
}
19+
}
20+
21+
};
22+
23+
module.exports = ImitateBrowserBehavior;
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* A scroll behavior that always scrolls to the top of the page
3+
* after a transition.
4+
*/
5+
var ScrollToTopBehavior = {
6+
7+
updateScrollPosition: function () {
8+
window.scrollTo(0, 0);
9+
}
10+
11+
};
12+
13+
module.exports = ScrollToTopBehavior;

modules/components/Link.js

+7-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var React = require('react');
22
var merge = require('react/lib/merge');
33
var ActiveState = require('../mixins/ActiveState');
4-
var Transitions = require('../mixins/Transitions');
4+
var Navigation = require('../mixins/Navigation');
55

66
function isLeftClickEvent(event) {
77
return event.button === 0;
@@ -33,11 +33,11 @@ var Link = React.createClass({
3333

3434
displayName: 'Link',
3535

36-
mixins: [ ActiveState, Transitions ],
36+
mixins: [ ActiveState, Navigation ],
3737

3838
propTypes: {
39-
to: React.PropTypes.string.isRequired,
4039
activeClassName: React.PropTypes.string.isRequired,
40+
to: React.PropTypes.string.isRequired,
4141
params: React.PropTypes.object,
4242
query: React.PropTypes.object,
4343
onClick: React.PropTypes.func
@@ -49,35 +49,17 @@ var Link = React.createClass({
4949
};
5050
},
5151

52-
getInitialState: function () {
53-
return {
54-
isActive: false
55-
};
56-
},
57-
58-
updateActiveState: function () {
59-
this.setState({
60-
isActive: this.isActive(this.props.to, this.props.params, this.props.query)
61-
});
62-
},
63-
64-
componentWillReceiveProps: function (nextProps) {
65-
this.setState({
66-
isActive: this.isActive(nextProps.to, nextProps.params, nextProps.query)
67-
});
68-
},
69-
7052
handleClick: function (event) {
7153
var allowTransition = true;
72-
var onClickResult;
54+
var clickResult;
7355

7456
if (this.props.onClick)
75-
onClickResult = this.props.onClick(event);
57+
clickResult = this.props.onClick(event);
7658

7759
if (isModifiedEvent(event) || !isLeftClickEvent(event))
7860
return;
7961

80-
if (onClickResult === false || event.defaultPrevented === true)
62+
if (clickResult === false || event.defaultPrevented === true)
8163
allowTransition = false;
8264

8365
event.preventDefault();
@@ -100,7 +82,7 @@ var Link = React.createClass({
10082
getClassName: function () {
10183
var className = this.props.className || '';
10284

103-
if (this.state.isActive)
85+
if (this.isActive(this.props.to, this.props.params, this.props.query))
10486
className += ' ' + this.props.activeClassName;
10587

10688
return className;

0 commit comments

Comments
 (0)