Skip to content

Commit c85e63b

Browse files
committed
Merge pull request #47 from react-native-simple-router-community/0.7.0-rc
0.7.0
2 parents 1c5c161 + d6269bb commit c85e63b

File tree

3 files changed

+127
-8
lines changed

3 files changed

+127
-8
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ The **`<Router />`** object used to initialize the navigation can take the follo
100100
- `firstRoute` (required): A React class corresponding to the first page of your navigation
101101
- `headerStyle`: Apply a StyleSheet to the navigation bar. You'll probably want to change the backgroundColor for example.
102102
- `titleStyle`: Apply a StyleSheet to the navigation bar titles. Useful for changing the font or text color.
103+
- `bgStyle` Apply a StyleSheet to the background of all routes.
104+
- `statusBarColor`: Specify the string `black` if you want the statusbar to be dark in color, or leave unspecified for a `light-content` style. Refer to StatusBarIOS for details.
103105
- `borderBottomWidth`: Apply a bottom border to your navbar.
104106
- `borderColor`: Apply a border color to your bottom border.
105107
- `backButtonComponent`: By default, the navigation bar will display a simple "Back" text for the back button. To change this, you can specify your own backButton component (like in the Twitter app).
@@ -108,6 +110,7 @@ The **`<Router />`** object used to initialize the navigation can take the follo
108110
- `hideNavigationBar`: Hide the navigation bar, always
109111
- `handleBackAndroid` (Boolean value): Apply a listener to the native back button on Android. On click, it will go to the previous route until it reach the first scene, then it will exit the app.
110112

113+
111114
The **`this.props.toRoute()`** callback prop takes one parameter (a JavaScript object) which can have the following keys:
112115
- `name`: The name of your route, which will be shown as the title of the navigation bar unless it is changed.
113116
- `component` (required): The React class corresponding to the view you want to render.
@@ -145,13 +148,26 @@ The functions **`this.props.setRightProps`**, **`this.props.setLeftProps`** and
145148
- This allows you to talk directly to your navbar, because previously you could only talk to it when navigating forward or backward.
146149

147150

148-
Events emitted by the router:
149-
`didFocus`, emits route name
150-
You can add a listener to a component as such:
151+
As of 0.7.0 the router acts as a relay for events emitted by the navigator, and extends these to the following list:
152+
153+
`willFocus`: Emitted when a route will focus. Emits the route name as a string.
154+
`didFocus`: Emitted when a route did focus. Emits the route name as a string.
155+
`willPop`: Emitted when a route stack will be popped. Triggered by `Navigator.pop();`
156+
`didPop`: Emitted when a route stack did pop. Triggered by `Navigator.pop();`
157+
`willPush`: Emitted when a new route will be pushed to the route stack. Emits the new route object. Triggered by `Navigator.push(route);`
158+
`didPush`: Emitted when a new route has been pushed to the route stack. Emits the new route object. Triggered by `Navigator.push(route);`
159+
`willResetTo`: Emitted when the route stack will be reset to a given route. Emits the route object. Triggered by `Navigator.resetTo(route);`
160+
`didResetTo`: Emitted when the route stack has been reset to a given route. Emits the route object. Triggered by `Navigator.resetTo(route);`
161+
`willReplace`: Emitted when a route will replace the current one in the route stack. Emits the new route object. Triggered by `Navigator.reset(route);`
162+
`didReplace`: Emitted when a route has replaced the current one in the route stack. Emits the new route object. Triggered by `Navigator.reset(route);`
163+
`willPopToTop`: Emitted when the route stack will be popped to the top. Triggered by `Navigator.popToTop();`
164+
`didPopToTop`: Emitted when the route stack has been popped to the top. Triggered by `Navigator.popToTop();`
165+
166+
You can listen to these events by adding an event listener as such:
151167

152168
```javascript
153169
this.props.routeEmitter.addListener('didFocus', (name) => {
154-
//Check if name is the current component, and if it is, act on this focus event.
170+
//Do something with name..
155171
});
156172
```
157173

index.js

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter';
1212

1313
import NavBarContainer from './components/NavBarContainer';
1414
import * as Styles from './styles';
15+
import aspect from 'aspect-js';
1516

1617
const propTypes = {
1718
backButtonComponent: PropTypes.func,
@@ -45,6 +46,24 @@ class Router extends React.Component {
4546
this.customAction = this.customAction.bind(this);
4647
this.renderScene = this.renderScene.bind(this);
4748

49+
this.onDidFocus = this.onDidFocus.bind(this);
50+
this.onWillFocus = this.onWillFocus.bind(this);
51+
52+
this.onWillPop = this.onWillPop.bind(this);
53+
this.onDidPop = this.onDidPop.bind(this);
54+
55+
this.onWillPush = this.onWillPush.bind(this);
56+
this.onDidPush = this.onDidPush.bind(this);
57+
58+
this.onWillResetTo = this.onWillResetTo.bind(this);
59+
this.onDidResetTo = this.onDidResetTo.bind(this);
60+
61+
this.onWillReplace = this.onWillReplace.bind(this);
62+
this.onDidReplace = this.onDidReplace.bind(this);
63+
64+
this.onWillPopToTop = this.onWillPopToTop.bind(this);
65+
this.onDidPopToTop = this.onDidPopToTop.bind(this);
66+
4867
this.state = {
4968
route: {
5069
name: null,
@@ -54,6 +73,10 @@ class Router extends React.Component {
5473
this.emitter = new EventEmitter();
5574
}
5675

76+
componentWillMount() {
77+
this.state.route = this.props.firstRoute;
78+
}
79+
5780
componentDidMount() {
5881
this.refs.navigator.navigationContext.addListener('willfocus', (event) => {
5982
const route = event.data.route;
@@ -65,20 +88,96 @@ class Router extends React.Component {
6588
const route = event.data.route;
6689
this.emitter.emit('didFocus', route.name);
6790
});
91+
92+
aspect.before(this.refs.navigator, 'pop', () => {
93+
this.onWillPop();
94+
});
95+
aspect.after(this.refs.navigator, 'pop', () => {
96+
this.onDidPop();
97+
});
98+
99+
aspect.before(this.refs.navigator, 'push', (route) => {
100+
this.onWillPush(route);
101+
});
102+
aspect.after(this.refs.navigator, 'push', (route) => {
103+
// temporary hack to fix bug in aspect library
104+
this.onDidPush(route || arguments[1]);
105+
});
106+
107+
aspect.before(this.refs.navigator, 'resetTo', (route) => {
108+
this.onWillResetTo(route);
109+
});
110+
aspect.after(this.refs.navigator, 'resetTo', (route) => {
111+
// temporary hack to fix bug in aspect library
112+
this.onDidResetTo(route || arguments[1]);
113+
});
114+
115+
aspect.before(this.refs.navigator, 'replace', (route) => {
116+
this.onWillReplace(route);
117+
});
118+
aspect.after(this.refs.navigator, 'replace', (route) => {
119+
// temporary hack to fix bug in aspect library
120+
this.onDidReplace(route || arguments[1]);
121+
});
122+
123+
aspect.before(this.refs.navigator, 'popToTop', () => {
124+
this.onWillPopToTop();
125+
});
126+
aspect.after(this.refs.navigator, 'popToTop', () => {
127+
this.onDidPopToTop();
128+
});
68129
}
69130

70131
onBack(navigator) {
71132
if (this.state.route.index > 0) {
72133
navigator.pop();
73-
this.emitter.emit('pop');
74134
}
75135
}
76136

77137
onForward(nextRoute, navigator) {
78138
navigator.push(
79139
Object.assign(nextRoute, { index: this.state.route.index + 1 || 1 })
80140
);
81-
this.emitter.emit('push', nextRoute);
141+
}
142+
143+
onWillPop() {
144+
this.emitter.emit('willPop');
145+
}
146+
147+
onDidPop() {
148+
this.emitter.emit('didPop');
149+
}
150+
151+
onWillPush(route) {
152+
this.emitter.emit('willPush', route);
153+
}
154+
155+
onDidPush(route) {
156+
this.emitter.emit('didPush', route);
157+
}
158+
159+
onWillResetTo(route) {
160+
this.emitter.emit('willResetTo', route);
161+
}
162+
163+
onDidResetTo(route) {
164+
this.emitter.emit('didResetTo', route);
165+
}
166+
167+
onWillReplace(route) {
168+
this.emitter.emit('willReplace', route);
169+
}
170+
171+
onDidReplace(route) {
172+
this.emitter.emit('didReplace', route);
173+
}
174+
175+
onWillPopToTop() {
176+
this.emitter.emit('willPopToTop');
177+
}
178+
179+
onDidPopToTop() {
180+
this.emitter.emit('didPopToTop');
82181
}
83182

84183
setRightProps(props) {
@@ -194,9 +293,9 @@ class Router extends React.Component {
194293
// Status bar color
195294
if (Platform.OS === 'ios') {
196295
if (this.props.statusBarColor === 'black') {
197-
StatusBarIOS.setStyle(0);
296+
StatusBarIOS.setStyle(0); // "Default" style according to StatusBarIOS.js
198297
} else {
199-
StatusBarIOS.setStyle(1);
298+
StatusBarIOS.setStyle(1); // "light-content" style according to StatusBarIOS.js
200299
}
201300
} else if (Platform.OS === 'android') {
202301
// no android version yet

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"author": "Tristan Edwards <tristan.edwards@me.com> (http://tristanedwards.me)",
66
"contributors": [
77
"Mikael Carpenter",
8+
"Nicolas Charpentier",
89
"David Leonardi"
910
],
1011
"main": "index.js",
@@ -31,6 +32,9 @@
3132
"url": "https://github.com/react-native-simple-router-community/react-native-simple-router/issues"
3233
},
3334
"homepage": "https://github.com/react-native-simple-router-community/react-native-simple-router",
35+
"dependencies": {
36+
"aspect-js": "^1.0.3"
37+
},
3438
"peerDependencies": {
3539
"react-native": ">=0.12.0 || 0.5.0-rc1 || 0.6.0-rc || 0.7.0-rc || 0.7.0-rc.2 || 0.8.0-rc || 0.8.0-rc.2 || 0.9.0-rc || 0.10.0-rc || 0.11.0-rc || 0.12.0-rc || 0.13.0-rc || 0.14.0-rc || 0.15.0-rc || 0.16.0-rc || 0.17.0-rc || 0.18.0-rc || 0.19.0-rc || 0.20.0-rc || 0.20.0-rc1"
3640
},

0 commit comments

Comments
 (0)