Skip to content

Commit 302e9d4

Browse files
Pavlo AksonovPavlo Aksonov
authored andcommitted
add pop action for back button
1 parent 2a6d191 commit 302e9d4

File tree

5 files changed

+68
-11
lines changed

5 files changed

+68
-11
lines changed

Actions.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Route from './Route';
22
import Router from './Router';
3+
import debug from './debug';
34

45
function isNumeric(n){
56
return !isNaN(parseFloat(n)) && isFinite(n);
@@ -40,16 +41,21 @@ class Actions {
4041
props = filterParam(props);
4142
// check if route is in children, current or parent routers
4243
let router: Router = this.currentRouter;
44+
45+
debug("Route to "+name+" current router="+this.currentRouter.name+ " current route="+this.currentRouter.currentRoute.name);
4346
// deep into child router
47+
4448
while (router.currentRoute.childRouter){
4549
router = router.currentRoute.childRouter;
50+
debug("Switching to child router="+router.name);
4651
}
4752
while (!router.routes[name]){
4853
const route = router.parentRoute;
4954
if (!route || !route.parent){
50-
throw new Error("Cannot find router for route="+name);
55+
throw new Error("Cannot find router for route="+name+" current router="+router.name);
5156
}
5257
router = route.parent;
58+
debug("Switching to router="+router.name);
5359
}
5460
if (router.route(name, props)){
5561
this.currentRouter = router;
@@ -73,8 +79,10 @@ class Actions {
7379
return true;
7480
} else {
7581
let router: Router = this.currentRouter;
82+
debug("Pop, router="+router.name);
7683
while (router.stack.length <= 1 || router.currentRoute.type === 'switch'){
7784
router = router.parentRoute.parent;
85+
debug("Switching to parent router="+router.name);
7886
}
7987
if (router.pop()){
8088
this.currentRouter = router;

Animations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ var FromTheRight = {
124124
const Animations = {
125125
FlatFloatFromRight: {
126126
...Navigator.SceneConfigs.FloatFromRight,
127+
gestures: null,
127128
// Animation interpolators for horizontal transitioning:
128129
animationInterpolators: {
129130
into: buildStyleInterpolator(FromTheRight),

ExRouter.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import Router from './Router';
33
import Route from './Route';
44
import * as Components from './Common';
55
import ExNavigator from '@exponent/react-native-navigator';
6+
import ExNavigatorStyles from '@exponent/react-native-navigator/ExNavigatorStyles';
7+
import { BackIcon } from '@exponent/react-native-navigator/ExNavigatorIcons';
68
import Animations from './Animations';
79
const {TouchableOpacity, StyleSheet, View, Text} = React;
810
import ReactRouter from './ReactRouter';
11+
import Actions from './Actions';
12+
import debug from './debug';
913

10-
export class ExRoute {
14+
export class ExRouteAdapter {
1115
name: string;
1216
navigator: ExNavigator;
1317
route: Route;
@@ -31,6 +35,7 @@ export class ExRoute {
3135
}
3236

3337
renderScene(navigator) {
38+
debug("RENDER SCENE:", this.route.name, Object.keys(this.route.props));
3439
const Component = this.route.component;
3540
const child = Component ?
3641
!this.route.wrapRouter ? <Component key={this.route.name} name={this.route.name} {...this.route.props} {...this.props} route={this.route}/>:
@@ -57,6 +62,49 @@ export class ExRoute {
5762
return title.length>10 ? null : title;
5863
}
5964

65+
renderLeftButton(navigator, index, state){
66+
if (index === 0) {
67+
return null;
68+
}
69+
70+
let previousIndex = index - 1;
71+
let previousRoute = state.routeStack[previousIndex];
72+
if (previousRoute.renderBackButton) {
73+
return previousRoute.renderBackButton(navigator, previousIndex, state);
74+
}
75+
76+
let title = this.getBackButtonTitle(navigator, index, state);
77+
78+
if (title) {
79+
var buttonText =
80+
<Text
81+
numberOfLines={1}
82+
style={[
83+
ExNavigatorStyles.barButtonText,
84+
ExNavigatorStyles.barBackButtonText,
85+
this._barButtonTextStyle,
86+
]}
87+
>
88+
{title}
89+
</Text>;
90+
}
91+
92+
return (
93+
<TouchableOpacity
94+
pressRetentionOffset={ExNavigatorStyles.barButtonPressRetentionOffset}
95+
onPress={() => Actions.pop()}
96+
style={[ExNavigatorStyles.barBackButton, styles.backButtonStyle]}>
97+
<BackIcon
98+
style={[
99+
ExNavigatorStyles.barButtonIcon,
100+
this._barButtonIconStyle,
101+
]}
102+
/>
103+
{buttonText}
104+
</TouchableOpacity>
105+
);
106+
}
107+
60108
renderRightButton() {
61109
if (this.route.onRight && this.route.rightTitle){
62110
return (<TouchableOpacity
@@ -71,10 +119,6 @@ export class ExRoute {
71119
}
72120
}
73121

74-
const defaultCreateRoute = function(route, data){
75-
return new ExRoute(route, data);
76-
};
77-
78122
export default class ExRouter extends React.Component {
79123
router: Router;
80124

@@ -95,7 +139,7 @@ export default class ExRouter extends React.Component {
95139
return false;
96140
}
97141
}
98-
this.refs.nav.push(new ExRoute(route, props));
142+
this.refs.nav.push(new ExRouteAdapter(route, props));
99143
return true;
100144
}
101145

@@ -106,7 +150,7 @@ export default class ExRouter extends React.Component {
106150
return false;
107151
}
108152
}
109-
this.refs.nav.replace(new ExRoute(route, props));
153+
this.refs.nav.replace(new ExRouteAdapter(route, props));
110154
return true;
111155
}
112156

@@ -123,7 +167,7 @@ export default class ExRouter extends React.Component {
123167
if (exist.length){
124168
navigator.jumpTo(exist[0]);
125169
} else {
126-
navigator.push(new ExRoute(route, props));
170+
navigator.push(new ExRouteAdapter(route, props));
127171

128172
}
129173
this.setState({selected: route.name});
@@ -151,11 +195,12 @@ export default class ExRouter extends React.Component {
151195

152196
const Footer = this.props.footer;
153197
const footer = Footer ? <Footer {...this.props} {...this.state}/> : null;
198+
debug("RENDER ROUTER:", router.name, Object.keys(this.props), Object.keys(this.state || {}));
154199

155200
return (
156201
<View style={styles.transparent}>
157202
{header}
158-
<ExNavigator ref="nav" initialRouteStack={router.stack.map(route => new ExRoute(router.routes[route]))}
203+
<ExNavigator ref="nav" initialRouteStack={router.stack.map(route => new ExRouteAdapter(router.routes[route]))}
159204
style={styles.transparent}
160205
sceneStyle={{ paddingTop: 0 }}
161206
showNavigationBar={!this.props.hideNavBar}

debug.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function debug(msg){
2+
//console.log(msg);
3+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-router-flux",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "React Native Router using Flux architecture",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)