Skip to content

Commit 042cffc

Browse files
committed
[changed] Removed histories/added history dep
Also, fixed path matching.
1 parent e25d468 commit 042cffc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1026
-2494
lines changed

examples/animations/app.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import React, { cloneElement } from 'react/addons';
2-
import { history } from 'react-router/lib/HashHistory';
1+
import React from 'react/addons';
2+
import createHistory from 'history/lib/createHashHistory';
33
import { Router, Route, Link } from 'react-router';
44

55
var { CSSTransitionGroup } = React.addons;
66

7+
var history = createHistory();
8+
79
var App = React.createClass({
810
render() {
911
var key = this.props.location.pathname;
@@ -15,7 +17,7 @@ var App = React.createClass({
1517
<li><Link to="/page2">Page 2</Link></li>
1618
</ul>
1719
<CSSTransitionGroup component="div" transitionName="example">
18-
{cloneElement(this.props.children || <div/>, { key: key })}
20+
{React.cloneElement(this.props.children || <div/>, { key: key })}
1921
</CSSTransitionGroup>
2022
</div>
2123
);
@@ -44,7 +46,6 @@ var Page2 = React.createClass({
4446
}
4547
});
4648

47-
4849
React.render((
4950
<Router history={history}>
5051
<Route path="/" component={App}>

examples/auth-flow/app.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, { findDOMNode } from 'react';
2+
import createHistory from 'history/lib/createHashHistory';
23
import { Router, Route, Link, Navigation } from 'react-router';
3-
import HashHistory from 'react-router/lib/HashHistory';
44
import auth from './auth';
5-
var history = new HashHistory({ queryKey: true });
5+
6+
var history = createHistory();
67

78
var App = React.createClass({
89
getInitialState() {
@@ -11,14 +12,14 @@ var App = React.createClass({
1112
};
1213
},
1314

14-
setStateOnAuth(loggedIn) {
15+
updateAuth(loggedIn) {
1516
this.setState({
16-
loggedIn: loggedIn
17+
loggedIn: !!loggedIn
1718
});
1819
},
1920

2021
componentWillMount() {
21-
auth.onChange = this.setStateOnAuth;
22+
auth.onChange = this.updateAuth;
2223
auth.login();
2324
},
2425

@@ -45,6 +46,7 @@ var App = React.createClass({
4546
var Dashboard = React.createClass({
4647
render() {
4748
var token = auth.getToken();
49+
4850
return (
4951
<div>
5052
<h1>Dashboard</h1>
@@ -56,7 +58,6 @@ var Dashboard = React.createClass({
5658
});
5759

5860
var Login = React.createClass({
59-
6061
mixins: [ Navigation ],
6162

6263
getInitialState() {
@@ -115,9 +116,9 @@ var Logout = React.createClass({
115116
}
116117
});
117118

118-
function requireAuth(nextState, transition) {
119+
function requireAuth(nextState, redirectTo) {
119120
if (!auth.loggedIn())
120-
transition.to('/login', null, { nextPathname: nextState.location.pathname });
121+
redirectTo('/login', null, { nextPathname: nextState.location.pathname });
121122
}
122123

123124
React.render((

examples/dynamic-segments/app.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from 'react';
2-
import { history } from 'react-router/lib/HashHistory';
2+
import createHistory from 'history/lib/createHashHistory';
33
import { Router, Route, Link, Redirect } from 'react-router';
44

5+
var history = createHistory();
6+
57
var App = React.createClass({
68
render() {
79
return (
@@ -50,8 +52,8 @@ React.render((
5052
<Router history={history}>
5153
<Route path="/" component={App}>
5254
<Route path="user/:userID" component={User}>
53-
<Route path="tasks/:taskID" component={Task}/>
54-
<Redirect from="todos/:taskID" to="task"/>
55+
<Route path="tasks/:taskID" component={Task} />
56+
<Redirect from="todos/:taskID" to="/user/:userID/tasks/:taskID" />
5557
</Route>
5658
</Route>
5759
</Router>

examples/transitions/app.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import React, { findDOMNode } from 'react';
2-
import { Router, Route, Link, Navigation, TransitionHook } from 'react-router';
3-
import { history } from 'react-router/lib/HashHistory';
1+
import React from 'react';
2+
import createHistory from 'history/lib/createHashHistory';
3+
import { Router, Route, Link, Navigation } from 'react-router';
4+
5+
var history = createHistory();
46

57
var App = React.createClass({
68
render() {
@@ -29,26 +31,51 @@ var Dashboard = React.createClass({
2931
});
3032

3133
var Form = React.createClass({
32-
mixins: [ Navigation, TransitionHook ],
34+
mixins: [ Navigation ],
35+
36+
getInitialState() {
37+
return {
38+
textValue: 'ohai'
39+
};
40+
},
3341

34-
routerWillLeave(nextState, transition) {
35-
if (findDOMNode(this.refs.userInput).value !== '')
36-
if (!confirm('You have unsaved information, are you sure you want to leave this page?'))
37-
transition.abort();
42+
transitionHook() {
43+
if (this.state.textValue)
44+
return 'You have unsaved information, are you sure you want to leave this page?';
45+
},
46+
47+
componentDidMount() {
48+
history.registerTransitionHook(this.transitionHook);
49+
},
50+
51+
componentWillUnmount() {
52+
history.unregisterTransitionHook(this.transitionHook);
53+
},
54+
55+
handleChange(event) {
56+
var { value } = event.target;
57+
58+
this.setState({
59+
textValue: value
60+
});
3861
},
3962

4063
handleSubmit(event) {
4164
event.preventDefault();
42-
findDOMNode(this.refs.userInput).value = '';
43-
this.transitionTo('/');
65+
66+
this.setState({
67+
textValue: ''
68+
}, () => {
69+
this.transitionTo('/');
70+
});
4471
},
4572

4673
render() {
4774
return (
4875
<div>
4976
<form onSubmit={this.handleSubmit}>
5077
<p>Click the dashboard link with text in the input.</p>
51-
<input type="text" ref="userInput" defaultValue="ohai" />
78+
<input type="text" ref="userInput" value={this.state.textValue} onChange={this.handleChange} />
5279
<button type="submit">Go</button>
5380
</form>
5481
</div>

modules/ActiveMixin.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { matchPattern } from './PatternUtils';
2+
3+
/**
4+
* Returns true if a route and params that match the given
5+
* pathname are currently active.
6+
*/
7+
function pathnameIsActive(pathname, activePathname, activeRoutes, activeParams) {
8+
if (pathname === activePathname || activePathname.indexOf(pathname + '/') === 0)
9+
return true;
10+
11+
var route, pattern, basename;
12+
for (var i = 0, len = activeRoutes.length; i < len; ++i) {
13+
route = activeRoutes[i];
14+
pattern = route.path || '';
15+
16+
if (pattern.indexOf('/') !== 0)
17+
pattern = basename.replace(/\/*$/, '/') + pattern; // Relative paths build on the parent's path.
18+
19+
var { remainingPathname, paramNames, paramValues } = matchPattern(pattern, pathname);
20+
21+
if (remainingPathname === '') {
22+
return paramNames.every(function (paramName, index) {
23+
return String(paramValues[index]) === String(activeParams[paramName]);
24+
});
25+
}
26+
27+
basename = pattern;
28+
}
29+
30+
return false;
31+
}
32+
33+
/**
34+
* Returns true if all key/value pairs in the given query are
35+
* currently active.
36+
*/
37+
function queryIsActive(query, activeQuery) {
38+
if (activeQuery == null)
39+
return query == null;
40+
41+
if (query == null)
42+
return true;
43+
44+
for (var p in query)
45+
if (query.hasOwnProperty(p) && String(query[p]) !== String(activeQuery[p]))
46+
return false;
47+
48+
return true;
49+
}
50+
51+
var ActiveMixin = {
52+
53+
/**
54+
* Returns true if a <Link> to the given pathname/query combination is
55+
* currently active.
56+
*/
57+
isActive(pathname, query) {
58+
var { location, routes, params } = this.state;
59+
60+
if (location == null)
61+
return false;
62+
63+
return pathnameIsActive(pathname, location.pathname, routes, params) &&
64+
queryIsActive(query, location.query);
65+
}
66+
67+
};
68+
69+
export default ActiveMixin;

modules/BrowserHistory.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

modules/DOMHistory.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)