Skip to content

Commit 9b59d68

Browse files
committed
add(UIRouter): root router component
add(UIRouter): the component handles the router initialization and requires a history implementation and an array with states definitions as props. A config function can also be provided that will be called with the new router instance as argument. chore(example): update example with new syntax
1 parent 9745555 commit 9b59d68

File tree

4 files changed

+68
-20
lines changed

4 files changed

+68
-20
lines changed

example/app.tsx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import * as ReactDOM from 'react-dom';
3-
import UIRouterReact, { UIView, UISrefActive, UISref, ReactStateDeclaration, trace, browserHistory } from '../src/index';
3+
import { UIRouter, UIRouterReact, UIView, UISrefActive, UISref, ReactStateDeclaration, trace, browserHistory } from '../src/index';
44

55
import {Home} from './home';
66
import {Child} from './child';
@@ -28,25 +28,21 @@ let nest = {
2828
}]
2929
} as ReactStateDeclaration;
3030

31-
// create new instance of UIRouterReact
32-
const Router = new UIRouterReact(browserHistory);
33-
// register states
34-
[home, child, nest].forEach(state => {
35-
Router.stateRegistry.register(state);
36-
});
3731

38-
39-
Router.urlRouterProvider.otherwise("/home");
40-
trace.enable(1);
41-
Router.start();
32+
const routerConfig = (router: UIRouterReact) => {
33+
router.urlRouterProvider.otherwise("/home");
34+
trace.enable(1);
35+
}
4236

4337
let el = document.getElementById("react-app");
4438
let app = (
45-
<div>
46-
<UISrefActive class="active">
47-
<UISref to="home"><a>Home</a></UISref>
48-
</UISrefActive>
49-
<UIView><p>Content will load here</p></UIView>
50-
</div>
39+
<UIRouter history={browserHistory} states={[home, child, nest]} config={routerConfig}>
40+
<div>
41+
<UISrefActive class="active">
42+
<UISref to="home"><a>Home</a></UISref>
43+
</UISrefActive>
44+
<UIView><p>Content will load here</p></UIView>
45+
</div>
46+
</UIRouter>
5147
);
5248
ReactDOM.render(app, el);

src/components/UIRouter.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as React from 'react';
2+
import {Component, PropTypes, Children} from 'react';
3+
import {UIRouterReact, ReactStateDeclaration} from '../index';
4+
import {HistoryImplementation} from "../history/interface";
5+
6+
export interface IProps {
7+
history?: HistoryImplementation;
8+
states?: ReactStateDeclaration[];
9+
config?: (router: UIRouterReact) => void;
10+
}
11+
12+
export interface IState {
13+
id?: number;
14+
loaded?: boolean;
15+
component?: string;
16+
props?: any;
17+
}
18+
19+
export class UIRouter extends Component<IProps, IState> {
20+
static propTypes = {
21+
history: PropTypes.object.isRequired,
22+
states: PropTypes.arrayOf(PropTypes.object).isRequired,
23+
config: PropTypes.func,
24+
children: PropTypes.element.isRequired
25+
}
26+
27+
static childContextTypes = {
28+
router: PropTypes.object.isRequired
29+
}
30+
31+
router: UIRouterReact;
32+
33+
constructor (props, context) {
34+
super(props, context);
35+
this.router = new UIRouterReact(props.history);
36+
props.states.forEach(state => {
37+
this.router.stateRegistry.register(state);
38+
});
39+
if (props.config) {
40+
props.config(this.router)
41+
}
42+
this.router.start();
43+
}
44+
45+
getChildContext() {
46+
return { router: this.router }
47+
}
48+
49+
render() {
50+
return Children.only(this.props.children);
51+
}
52+
}

src/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {hashHistory} from './history/hashHistory';
88
let viewConfigFactory = (node: [PathNode], config: ReactViewDeclaration) =>
99
new ReactViewConfig(node, config);
1010

11-
export default class UIRouterReact extends UIRouter {
11+
export class UIRouterReact extends UIRouter {
1212
static instance;
1313
constructor(history: HistoryImplementation = hashHistory) {
1414
super();

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import './services';
2-
import UIRouterReact from "./core";
32

43
export {trace} from "ui-router-core";
54
export {ReactStateDeclaration} from "./interface";
@@ -10,5 +9,6 @@ export {hashHistory} from "./history/hashHistory";
109
export {UIView, InjectedProps, Resolves} from "./components/UIView";
1110
export {UISref} from "./components/UISref";
1211
export {UISrefActive} from "./components/UISrefActive";
12+
export {UIRouter} from "./components/UIRouter";
1313

14-
export default UIRouterReact;
14+
export {UIRouterReact} from "./core";

0 commit comments

Comments
 (0)