Skip to content

Commit f36f207

Browse files
author
Andrew Ross
committed
Inital checkin
1 parent a6b1932 commit f36f207

12 files changed

+836
-32
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#Typings-React-Router
2+
3+
This is typings for thee [react-router](https://github.com/reactjs/react-router)
4+
5+
It's highly suggested to install with [typings](https://github.com/typings/typings) definition manager.
6+
Use the command `typings install react-router --save`.
7+
8+
You're code should look pretty much the same the javascript with one exception.
9+
When a component is used with a `Route` component it injects several property values.
10+
To type this the `IInjectableProps` interface has all the properties defined that injecting will add.
11+
12+
To use it extend your current Property interface with `IInjectableProps`.
13+
14+
```javascript
15+
import React from 'react';
16+
import { render } from 'react-dom';
17+
import { Link, Router, Route, IInjectedProps } from 'react-router';
18+
19+
class User extends React.Component<IInjectedProps,{}> {
20+
render() {
21+
let { userID } = this.props.params
22+
let { query } = this.props.location
23+
let age = query && query["showAge"] ? '33' : ''
24+
25+
return (
26+
<div className="User">
27+
<h1>User id: {userID}</h1>
28+
{age}
29+
</div>
30+
)
31+
}
32+
}
33+
34+
class App extends React.Component<React.Props<{}>,{}> {
35+
render() {
36+
return (
37+
<div>
38+
<ul>
39+
<li><Link to="/user/bob" activeClassName="active">Bob</Link></li>
40+
<li><Link to={{ pathname: '/user/bob', query: { showAge: true } }} activeClassName="active">Bob With Query Params</Link></li>
41+
<li><Link to="/user/sally" activeClassName="active">Sally</Link></li>
42+
</ul>
43+
{this.props.children}
44+
</div>
45+
)
46+
}
47+
}
48+
49+
render((
50+
<Router history={browserHistory}>
51+
<Route path="/" component={App}>
52+
<Route path="user/:userID" component={User} />
53+
</Route>
54+
</Router>
55+
), document.getElementById('example'));
56+
```
57+
58+
This is quite a large api so I might have missed some of the details.
59+
If there is something that should work or documentation that's incorrect please don't hesistate to post an issue.

examples/active-links.tsx

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/// <reference path="../typings/main.d.ts" />
2+
/// <reference path="../react-router.d.ts" />
3+
4+
import React from 'react';
5+
import { render } from 'react-dom';
6+
import {
7+
IRouterProps,
8+
Router,
9+
ILinkProps,
10+
Link,
11+
IndexLink,
12+
IRouterContext,
13+
IRouter,
14+
RouterContext,
15+
IRouteProps,
16+
Route,
17+
PlainRoute,
18+
IRedirectProps,
19+
Redirect,
20+
IndexRoute,
21+
IIndexRouteProps,
22+
IndexRedirect,
23+
IInjectedProps,
24+
browserHistory,
25+
hashHistory,
26+
createMemoryHistory,
27+
useRouterHistory,
28+
IMatchArgs,
29+
match,
30+
createRoutes
31+
} from '../react-router'
32+
33+
34+
const ACTIVE = { color: 'red' }
35+
36+
class App extends React.Component<React.Props<{}>, {}> {
37+
render() {
38+
return (
39+
<div>
40+
<h1>APP!</h1>
41+
<ul>
42+
<li><Link to="/" activeStyle={ACTIVE}>/</Link></li>
43+
<li><IndexLink to="/" activeStyle={ACTIVE}>/ IndexLink</IndexLink></li>
44+
45+
<li><Link to="/users" activeStyle={ACTIVE}>/users</Link></li>
46+
<li><IndexLink to="/users" activeStyle={ACTIVE}>/users IndexLink</IndexLink></li>
47+
48+
<li><Link to="/users/ryan" activeStyle={ACTIVE}>/users/ryan</Link></li>
49+
<li><Link to={{ pathname: '/users/ryan', query: { foo: 'bar' } }}
50+
activeStyle={ACTIVE}>/users/ryan?foo=bar</Link></li>
51+
52+
<li><Link to="/about" activeStyle={ACTIVE}>/about</Link></li>
53+
</ul>
54+
55+
{this.props.children}
56+
</div>
57+
)
58+
}
59+
}
60+
61+
class Index extends React.Component<IInjectedProps, {}> {
62+
render() {
63+
return (
64+
<div>
65+
<h2>Index!</h2>
66+
</div>
67+
)
68+
}
69+
}
70+
71+
class Users extends React.Component<React.Props<{}> & IInjectedProps, {}> {
72+
render() {
73+
return (
74+
<div>
75+
<h2>Users</h2>
76+
{this.props.children}
77+
</div>
78+
)
79+
}
80+
}
81+
82+
class UsersIndex extends React.Component<IInjectedProps, {}> {
83+
render() {
84+
return (
85+
<div>
86+
<h3>UsersIndex</h3>
87+
</div>
88+
)
89+
}
90+
}
91+
92+
class User extends React.Component<IInjectedProps, {}> {
93+
render() {
94+
return (
95+
<div>
96+
<h3>User {this.props.params['id']}</h3>
97+
</div>
98+
)
99+
}
100+
}
101+
102+
class About extends React.Component<{}, {}> {
103+
render() {
104+
return (
105+
<div>
106+
<h2>About</h2>
107+
</div>
108+
)
109+
}
110+
}
111+
112+
render((
113+
<Router history={browserHistory}>
114+
<Route path="/" component={App}>
115+
<IndexRoute component={Index}/>
116+
<Route path="/about" component={About}/>
117+
<Route path="users" component={Users}>
118+
<IndexRoute component={UsersIndex}/>
119+
<Route path=":id" component={User}/>
120+
</Route>
121+
</Route>
122+
</Router>
123+
), document.getElementById('example'));

examples/auth-flow.tsx

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/// <reference path="../typings/main.d.ts" />
2+
/// <reference path="../react-router.d.ts" />
3+
4+
import React from 'react';
5+
import { render } from 'react-dom';
6+
import {
7+
IRouterProps,
8+
Router,
9+
ILinkProps,
10+
Link,
11+
IndexLink,
12+
IRouterContext,
13+
IRouter,
14+
RouterContext,
15+
IRouteProps,
16+
Route,
17+
PlainRoute,
18+
IRedirectProps,
19+
Redirect,
20+
IndexRoute,
21+
IIndexRouteProps,
22+
IndexRedirect,
23+
IInjectedProps,
24+
browserHistory,
25+
hashHistory,
26+
createMemoryHistory,
27+
useRouterHistory,
28+
IMatchArgs,
29+
match,
30+
createRoutes
31+
} from '../react-router'
32+
import auth = require("./auth");
33+
34+
const App = React.createClass({
35+
getInitialState() {
36+
return {
37+
loggedIn: auth.loggedIn()
38+
}
39+
},
40+
41+
updateAuth(loggedIn) {
42+
this.setState({
43+
loggedIn: loggedIn
44+
})
45+
},
46+
47+
componentWillMount() {
48+
auth.onChange = this.updateAuth
49+
auth.login()
50+
},
51+
52+
render() {
53+
return (
54+
<div>
55+
<ul>
56+
<li>
57+
{this.state.loggedIn ? (
58+
<Link to="/logout">Log out</Link>
59+
) : (
60+
<Link to="/login">Sign in</Link>
61+
) }
62+
</li>
63+
<li><Link to="/about">About</Link></li>
64+
<li><Link to="/dashboard">Dashboard</Link> (authenticated) </li>
65+
</ul>
66+
{this.props.children || <p>You are {!this.state.loggedIn && 'not'} logged in.</p>}
67+
</div>
68+
)
69+
}
70+
})
71+
72+
const Dashboard = React.createClass({
73+
render() {
74+
const token = auth.getToken()
75+
76+
return (
77+
<div>
78+
<h1>Dashboard</h1>
79+
<p>You made it!</p>
80+
<p>{token}</p>
81+
</div>
82+
)
83+
}
84+
})
85+
86+
const Login = React.createClass({
87+
88+
contextTypes: {
89+
router: React.PropTypes.object.isRequired
90+
},
91+
92+
getInitialState() {
93+
return {
94+
error: false
95+
}
96+
},
97+
98+
handleSubmit(event:React.FormEvent) {
99+
event.preventDefault()
100+
101+
const email = this.refs.email.value
102+
const pass = this.refs.pass.value
103+
104+
auth.login(email, pass, (loggedIn) => {
105+
if (!loggedIn)
106+
return this.setState({ error: true })
107+
108+
const { location } = this.props;
109+
110+
if (location.state && location.state.nextPathname) {
111+
this.context.router.replace(location.state.nextPathname)
112+
} else {
113+
this.context.router.replace('/')
114+
}
115+
})
116+
},
117+
118+
render() {
119+
return (
120+
<form onSubmit={this.handleSubmit}>
121+
<label><input ref="email" placeholder="email" defaultValue="[email protected]" /></label>
122+
<label><input ref="pass" placeholder="password" /></label> (hint: password1) <br />
123+
<button type="submit">login</button>
124+
{this.state.error && (
125+
<p>Bad login information</p>
126+
) }
127+
</form>
128+
)
129+
}
130+
})
131+
132+
const About = React.createClass({
133+
render() {
134+
return <h1>About</h1>
135+
}
136+
})
137+
138+
const Logout = React.createClass({
139+
componentDidMount() {
140+
auth.logout()
141+
},
142+
143+
render() {
144+
return <p>You are now logged out</p>
145+
}
146+
})
147+
148+
function requireAuth(nextState, replace) {
149+
if (!auth.loggedIn()) {
150+
replace({
151+
pathname: '/login',
152+
state: { nextPathname: nextState.location.pathname }
153+
})
154+
}
155+
}
156+
157+
render((
158+
<Router history={browserHistory}>
159+
<Route path="/" component={Login} components={{ "login": Login }}>
160+
<Route path="login" component={Login} />
161+
<Route path="logout" component={Logout} />
162+
<Route path="about" component={About} />
163+
<Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
164+
</Route>
165+
</Router>
166+
), document.getElementById('example'))

0 commit comments

Comments
 (0)