Skip to content

Commit ca9e3b7

Browse files
taionmjackson
authored andcommitted
[added] IndexRedirect
Closes #1966
1 parent 4446897 commit ca9e3b7

File tree

7 files changed

+98
-63
lines changed

7 files changed

+98
-63
lines changed

docs/API.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
- [Route](#route)
1111
- [PlainRoute](#plainroute)
1212
- [Redirect](#redirect)
13-
- [IndexRoute](#indexroute)
13+
- [IndexRoute](#indexroute)
14+
- [IndexRedirect](#indexredirect)
1415

1516
* [Handler Components](#handler-components)
1617

@@ -344,18 +345,32 @@ Note that the `<Redirect>` can be placed anywhere in the route hierarchy, though
344345
</Route>
345346
```
346347

348+
349+
347350
## IndexRoute
348351
Index Routes allow you to provide a default "child" to a parent
349352
route when visitor is at the url of the parent, they provide convention
350353
for `<IndexLink>` to work.
351354

352-
Please see the [Index Routes guide](/docs/guides/basics/IndexRoutes.md)
355+
Please see the [Index Routes guide](/docs/guides/basics/IndexRoutes.md).
353356

354357
#### Props
355358
All the same props as [Route](#route) except for `path`.
356359

357360

358361

362+
## IndexRedirect
363+
Index Redirects allow you to redirect from the URL of a parent route to another
364+
route. They can be used to allow a child route to serve as the default route
365+
for its parent, while still keeping a distinct URL.
366+
367+
Please see the [Index Routes guide](/docs/guides/basics/IndexRoutes.md).
368+
369+
#### Props
370+
All the same props as [Redirect](./Redirect.md) except for `from`.
371+
372+
373+
359374
## Handler Components
360375
A route's handler component is rendered when that route matches the URL. The router will inject the following properties into your component when it's rendered:
361376

examples/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ <h1>React Router Examples</h1>
1313
<li><a href="pinterest">Pinterest-style UI (location.state)</a></li>
1414
<li><a href="nested-animations">Nested Animations</a></li>
1515
<li><a href="query-params">Query Params</a></li>
16-
<li><a href="redirect-using-index">Redirect using IndexRoute</a></li>
1716
<li><a href="shared-root">Shared Root</a></li>
1817
<li><a href="sidebar">Sidebar</a></li>
1918
<li><a href="transitions">Transitions</a></li>

examples/redirect-using-index/app.js

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

examples/redirect-using-index/index.html

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

modules/IndexRedirect.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react'
2+
import invariant from 'invariant'
3+
import warning from 'warning'
4+
import Redirect from './Redirect'
5+
import { falsy } from './PropTypes'
6+
7+
const { string, object } = React.PropTypes
8+
9+
/**
10+
* An <IndexRedirect> is used to redirect from an indexRoute.
11+
*/
12+
const IndexRedirect = React.createClass({
13+
14+
statics: {
15+
16+
createRouteFromReactElement(element, parentRoute) {
17+
if (parentRoute) {
18+
parentRoute.indexRoute = Redirect.createRouteFromReactElement(element)
19+
} else {
20+
warning(
21+
false,
22+
'An <IndexRedirect> does not make sense at the root of your route config'
23+
)
24+
}
25+
}
26+
27+
},
28+
29+
propTypes: {
30+
to: string.isRequired,
31+
query: object,
32+
state: object,
33+
onEnter: falsy,
34+
children: falsy
35+
},
36+
37+
render() {
38+
invariant(
39+
false,
40+
'<IndexRedirect> elements are for router configuration only and should not be rendered'
41+
)
42+
}
43+
44+
})
45+
46+
export default IndexRedirect
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*eslint-env mocha */
2+
import expect from 'expect'
3+
import React from 'react'
4+
import createHistory from 'history/lib/createMemoryHistory'
5+
import IndexRedirect from '../IndexRedirect'
6+
import Router from '../Router'
7+
import Route from '../Route'
8+
9+
describe('An <IndexRedirect>', function () {
10+
11+
let node
12+
beforeEach(function () {
13+
node = document.createElement('div')
14+
})
15+
16+
afterEach(function () {
17+
React.unmountComponentAtNode(node)
18+
})
19+
20+
it('works', function (done) {
21+
React.render((
22+
<Router history={createHistory('/')}>
23+
<Route path="/">
24+
<IndexRedirect to="/messages" />
25+
<Route path="messages" />
26+
</Route>
27+
</Router>
28+
), node, function () {
29+
expect(this.state.location.pathname).toEqual('/messages')
30+
done()
31+
})
32+
})
33+
34+
})

modules/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export Link from './Link'
44
export IndexLink from './IndexLink'
55

66
/* components (configuration) */
7+
export IndexRedirect from './IndexRedirect'
78
export IndexRoute from './IndexRoute'
89
export Redirect from './Redirect'
910
export Route from './Route'

0 commit comments

Comments
 (0)