Skip to content

Commit 498d31e

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents ed0cf62 + 0a6e9dc commit 498d31e

File tree

10 files changed

+261
-9
lines changed

10 files changed

+261
-9
lines changed

docs/api/mixins/RouteLookup.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Instance Methods
99

1010
### `getRoutes()`
1111

12-
Returns an array of the active routes in order of their hierarchy.
12+
Returns an array of all routes.
1313

1414
```js
1515
[route, route, ...]
1616
```
1717

1818
### `getNamedRoutes()`
1919

20-
Returns an object hash of active routes by name.
20+
Returns an object hash of all routes by name.
2121

2222
```js
2323
{user: route, users: route, ...}
@@ -31,13 +31,11 @@ Example
3131
-------
3232

3333
```js
34-
var TopLevelApp = React.createClass({
35-
mixins: [RouteLookup, PathState],
34+
React.createClass({
35+
mixins: [RouteLookup],
3636

37-
// `updatePath` is from `PathState`
38-
updatePath: function() {
39-
var deepestRoute = this.getRoutes().reverse()[0];
40-
document.title = deepestRoute.props.title;
37+
componentDidMount: function() {
38+
console.log(this.getRoutes())
4139
}
4240
});
4341
```

docs/guides/overview.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,15 @@ redirecting transitions, query parameters and more.
371371
[API]:../api/
372372
[path-matching]:./path-matching.md
373373

374+
CommonJS Guide
375+
--------------
376+
377+
In order for the above examples to work in a CommonJS environment you'll need to `require` the following:
378+
379+
```
380+
var Router = require('react-router');
381+
var Route = Router.Route;
382+
var Routes = Router.Routes;
383+
var DefaultRoute = Router.DefaultRoute;
384+
var Link = Router.Link;
385+
```

examples/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ <h1>React Router Examples</h1>
1212
<li><a href="partial-app-loading/index.html">Partial App Loading</a></li>
1313
<li><a href="query-params/index.html">Query Params</a></li>
1414
<li><a href="shared-root/index.html">Shared Root</a></li>
15+
<li><a href="sidebar/index.html">Sidebar</a></li>
1516
<li><a href="simple-master-detail/index.html">Simple Master Detail</a></li>
1617
<li><a href="transitions/index.html">Transitions</a></li>
1718
</ul>

examples/sidebar/app.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.Sidebar {
2+
float: left;
3+
background: #eee;
4+
padding: 20px;
5+
margin: 0 20px 20px 20px;
6+
width: 200px;
7+
cursor: pointer;
8+
}
9+
10+
.Content {
11+
padding: 20px 20px 20px 300px;
12+
}
13+
14+
.CategoryNav__Toggle:before {
15+
display: inline-block;
16+
width: 1em;
17+
content: '▸';
18+
}
19+
20+
.CategoryNav__Toggle--is-open:before {
21+
content: '▾';
22+
}
23+

examples/sidebar/app.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/** @jsx React.DOM */
2+
var React = require('react');
3+
var Router = require('react-router');
4+
var Routes = Router.Routes;
5+
var Route = Router.Route;
6+
var DefaultRoute = Router.DefaultRoute;
7+
var Link = Router.Link;
8+
var RouteLookup = Router.RouteLookup;
9+
var data = require('./data');
10+
11+
var CategoryNav = React.createClass({
12+
getInitialState: function() {
13+
return { isOpen: this.props.defaultIsOpen};
14+
},
15+
16+
getDefaultProps: function() {
17+
return { isOpen: false };
18+
},
19+
20+
componentWillReceiveProps: function(newProps) {
21+
if (!this.state.isOpen)
22+
this.setState({isOpen: newProps.defaultIsOpen});
23+
},
24+
25+
toggle: function() {
26+
this.setState({isOpen: !this.state.isOpen});
27+
},
28+
29+
buildToggleClassName: function() {
30+
var toggleClassName = 'CategoryNav__Toggle';
31+
if (this.state.isOpen)
32+
toggleClassName += ' CategoryNav__Toggle--is-open';
33+
return toggleClassName;
34+
},
35+
36+
renderItems: function() {
37+
var category = this.props.category;
38+
return this.state.isOpen ? category.items.map(function(item) {
39+
var params = { name: item.name, category: category.name };
40+
return (
41+
<li key={item.name}>
42+
<Link to="item" params={params}>{item.name}</Link>
43+
</li>
44+
);
45+
}) : null;
46+
},
47+
48+
render: function() {
49+
var category = this.props.category;
50+
return (
51+
<div className="CategoryNav">
52+
<h3
53+
className={this.buildToggleClassName()}
54+
onClick={this.toggle}
55+
>{category.name}</h3>
56+
<ul>{this.renderItems()}</ul>
57+
</div>
58+
);
59+
}
60+
});
61+
62+
var Sidebar = React.createClass({
63+
renderCategory: function(category) {
64+
return <CategoryNav
65+
key={category.name}
66+
defaultIsOpen={category.name === this.props.activeCategory}
67+
category={category}
68+
/>;
69+
},
70+
71+
render: function() {
72+
return (
73+
<div className="Sidebar">
74+
{this.props.categories.map(this.renderCategory)}
75+
</div>
76+
);
77+
}
78+
});
79+
80+
var App = React.createClass({
81+
render: function() {
82+
var activeRouteHandler = this.props.activeRouteHandler();
83+
var activeCategory = activeRouteHandler ?
84+
activeRouteHandler.props.params.category : null;
85+
return (
86+
<div>
87+
<Sidebar activeCategory={activeCategory} categories={data.getAll()}/>
88+
<div className="Content">
89+
{activeRouteHandler}
90+
</div>
91+
</div>
92+
);
93+
}
94+
});
95+
96+
var Item = React.createClass({
97+
render: function() {
98+
var params = this.props.params;
99+
var category = data.lookupCategory(params.category);
100+
var item = data.lookupItem(params.category, params.name);
101+
return (
102+
<div>
103+
<h2>{category.name} / {item.name}</h2>
104+
<p>Price: ${item.price}</p>
105+
</div>
106+
);
107+
}
108+
});
109+
110+
var Index = React.createClass({
111+
render: function() {
112+
return (
113+
<div>
114+
<p>Sidebar features:</p>
115+
<ul style={{maxWidth: '400px'}}>
116+
<li>User can open/close categories</li>
117+
<li>
118+
Visiting an item on first page load will automatically open
119+
the correct category. (Click an item, then reload the
120+
browser.)
121+
</li>
122+
<li>
123+
Navigating with forward/back buttons will open an active
124+
category if it is not already open. (Navigate to several
125+
items, close all the categories, then use back/forward
126+
buttons.)
127+
</li>
128+
<li>
129+
Only the user can close a category. (Navigating from an
130+
active category will not close it.)
131+
</li>
132+
</ul>
133+
</div>
134+
);
135+
}
136+
});
137+
138+
var routes = (
139+
<Routes>
140+
<Route handler={App}>
141+
<DefaultRoute handler={Index}/>
142+
<Route name="item" path=":category/:name" handler={Item} />
143+
</Route>
144+
</Routes>
145+
);
146+
147+
React.renderComponent(routes, document.getElementById('example'));
148+

examples/sidebar/data.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var data = [
2+
{
3+
name: 'Tacos',
4+
items: [
5+
{ name: 'Carne Asada', price: 7 },
6+
{ name: 'Pollo', price: 6 },
7+
{ name: 'Carnitas', price: 6 }
8+
]
9+
},
10+
{
11+
name: 'Burgers',
12+
items: [
13+
{ name: 'Buffalo Bleu', price: 8 },
14+
{ name: 'Bacon', price: 8 },
15+
{ name: 'Mushroom and Swiss', price: 6 }
16+
]
17+
},
18+
{
19+
name: 'Drinks',
20+
items: [
21+
{ name: 'Lemonade', price: 3 },
22+
{ name: 'Root Beer', price: 4 },
23+
{ name: 'Iron Port', price: 5 }
24+
]
25+
}
26+
];
27+
28+
var dataMap = data.reduce(function(map, category) {
29+
category.itemsMap = category.items.reduce(function(itemsMap, item) {
30+
itemsMap[item.name] = item;
31+
return itemsMap;
32+
}, {});
33+
map[category.name] = category;
34+
return map;
35+
}, {});
36+
37+
exports.getAll = function() {
38+
return data;
39+
};
40+
41+
exports.lookupCategory = function(name) {
42+
return dataMap[name];
43+
};
44+
45+
exports.lookupItem = function(category, item) {
46+
return dataMap[category].itemsMap[item];
47+
};
48+

examples/sidebar/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html public "restroom">
2+
<title>Sidebar Example</title>
3+
<link href="../global.css" rel="stylesheet"/>
4+
<link href="app.css" rel="stylesheet"/>
5+
<body>
6+
<h1 class="breadcrumbs"><a href="../index.html">React Router Examples</a> / Sidebar</h1>
7+
<div id="example"/>
8+
<script src="../global-bundle.js"></script>
9+
<script src="app-bundle.js"></script>

examples/simple-master-detail/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var App = React.createClass({
1616

1717
render: function() {
1818
var links = this.state.states.map(function(state) {
19-
return <li><Link to="state" abbr={state.abbr}>{state.name}</Link></li>
19+
return <li><Link to="state" params={{ abbr: state.abbr }}>{state.name}</Link></li>
2020
});
2121
return (
2222
<div className="App">

modules/utils/__tests__/Path-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,22 @@ describe('Path.injectParams', function () {
195195
});
196196
});
197197

198+
describe('when a pattern has one splat', function () {
199+
it('returns the correct path', function () {
200+
expect(Path.injectParams('/a/*/d', { splat: 'b/c' })).toEqual('/a/b/c/d');
201+
});
202+
});
203+
198204
describe('when a pattern has multiple splats', function () {
199205
it('returns the correct path', function () {
200206
expect(Path.injectParams('/a/*/c/*', { splat: [ 'b', 'd' ] })).toEqual('/a/b/c/d');
201207
});
208+
209+
it('complains if not given enough splat values', function () {
210+
expect(function() {
211+
Path.injectParams('/a/*/c/*', { splat: [ 'b' ] });
212+
}).toThrow(Error);
213+
});
202214
});
203215
});
204216

scripts/build-examples

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ $BUNDLE_EXAMPLE examples/query-params/app.js > examples/query-params/app-bundle.
1616
$BUNDLE_EXAMPLE examples/shared-root/app.js > examples/shared-root/app-bundle.js
1717
$BUNDLE_EXAMPLE examples/simple-master-detail/app.js > examples/simple-master-detail/app-bundle.js
1818
$BUNDLE_EXAMPLE examples/transitions/app.js > examples/transitions/app-bundle.js
19+
$BUNDLE_EXAMPLE examples/sidebar/app.js > examples/sidebar/app-bundle.js

0 commit comments

Comments
 (0)