Skip to content

Commit f602d91

Browse files
committed
Add router functions to corners and title
1 parent 3d021d5 commit f602d91

File tree

3 files changed

+87
-31
lines changed

3 files changed

+87
-31
lines changed

components/NavBarContainer.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ const propTypes = {
1010
currentRoute: PropTypes.object.isRequired,
1111
customAction: PropTypes.func,
1212
handleBackAndroid: PropTypes.bool,
13+
goToFirstRoute: PropTypes.func.isRequired,
1314
leftProps: PropTypes.object,
1415
navigator: PropTypes.object.isRequired,
16+
replaceRoute: PropTypes.func.isRequired,
17+
resetToRoute: PropTypes.func.isRequired,
1518
rightCorner: PropTypes.func,
1619
rightProps: PropTypes.object,
1720
style: View.propTypes.style,
@@ -44,6 +47,9 @@ class NavBarContainer extends React.Component {
4447

4548
this.goBack = this.goBack.bind(this);
4649
this.goForward = this.goForward.bind(this);
50+
this.replaceRoute = this.replaceRoute.bind(this);
51+
this.resetToRoute = this.resetToRoute.bind(this);
52+
this.goToFirstRoute = this.goToFirstRoute.bind(this);
4753
this.customAction = this.customAction.bind(this);
4854

4955
this.state = {
@@ -87,6 +93,18 @@ class NavBarContainer extends React.Component {
8793
this.props.toRoute(route, this.props.navigator);
8894
}
8995

96+
replaceRoute(nextRoute) {
97+
this.props.replaceRoute(nextRoute, this.props.navigator);
98+
}
99+
100+
resetToRoute(nextRoute) {
101+
this.props.resetToRoute(nextRoute, this.props.navigator);
102+
}
103+
104+
goToFirstRoute(nextRoute) {
105+
this.props.goToFirstRoute(nextRoute, this.props.navigator);
106+
}
107+
90108
customAction(opts) {
91109
this.props.customAction(opts);
92110
}
@@ -119,23 +137,6 @@ class NavBarContainer extends React.Component {
119137
willDisappear
120138
/>
121139
);
122-
} else if (this.props.currentRoute.hideNavigationBar) {
123-
navbarContent = (
124-
<NavBarContent
125-
route={this.props.currentRoute}
126-
backButtonComponent={this.props.backButtonComponent}
127-
rightCorner={this.props.rightCorner}
128-
titleStyle={this.props.titleStyle}
129-
borderBottomWidth={this.props.borderBottomWidth}
130-
borderColor={this.props.borderColor}
131-
goBack={this.goBack}
132-
goForward={this.goForward}
133-
leftProps={this.props.leftProps}
134-
rightProps={this.props.rightProps}
135-
titleProps={this.props.titleProps}
136-
customAction={this.customAction}
137-
/>
138-
);
139140
} else {
140141
navbarContent = (
141142
<NavBarContent
@@ -147,6 +148,9 @@ class NavBarContainer extends React.Component {
147148
borderColor={this.props.borderColor}
148149
goBack={this.goBack}
149150
goForward={this.goForward}
151+
replaceRoute={this.replaceRoute}
152+
resetToRoute={this.resetToRoute}
153+
goToFirstRoute={this.goToFirstRoute}
150154
leftProps={this.props.leftProps}
151155
rightProps={this.props.rightProps}
152156
titleProps={this.props.titleProps}

components/NavBarContent.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const propTypes = {
99
customAction: PropTypes.func,
1010
goBack: PropTypes.func,
1111
goForward: PropTypes.func,
12+
goToFirstRoute: PropTypes.func.isRequired,
1213
leftProps: PropTypes.object,
14+
replaceRoute: PropTypes.func.isRequired,
15+
resetToRoute: PropTypes.func.isRequired,
1316
rightCorner: PropTypes.func,
1417
rightProps: PropTypes.object,
1518
route: PropTypes.object.isRequired,
@@ -65,6 +68,9 @@ class NavBarContent extends React.Component {
6568

6669
this.goBack = this.goBack.bind(this);
6770
this.goForward = this.goForward.bind(this);
71+
this.replaceRoute = this.replaceRoute.bind(this);
72+
this.resetToRoute = this.resetToRoute.bind(this);
73+
this.goToFirstRoute = this.goToFirstRoute.bind(this);
6874
this.customAction = this.customAction.bind(this);
6975

7076
this.state = {
@@ -112,6 +118,18 @@ class NavBarContent extends React.Component {
112118
this.props.goForward(route);
113119
}
114120

121+
replaceRoute(nextRoute) {
122+
this.props.replaceRoute(nextRoute);
123+
}
124+
125+
resetToRoute(nextRoute) {
126+
this.props.resetToRoute(nextRoute);
127+
}
128+
129+
goToFirstRoute(nextRoute) {
130+
this.props.goToFirstRoute(nextRoute);
131+
}
132+
115133
customAction(opts) {
116134
this.props.customAction(opts);
117135
}
@@ -144,6 +162,10 @@ class NavBarContent extends React.Component {
144162
leftCornerContent = (
145163
<LeftCorner
146164
toRoute={this.goForward}
165+
toBack={this.goBack}
166+
replaceRoute={this.replaceRoute}
167+
resetToRoute={this.resetToRoute}
168+
goToFirstRoute={this.goToFirstRoute}
147169
customAction={this.customAction}
148170
{...this.props.leftProps}
149171
{...this.props.route.leftCornerProps}
@@ -172,6 +194,10 @@ class NavBarContent extends React.Component {
172194
rightCornerContent = (
173195
<RightCorner
174196
toRoute={this.goForward}
197+
toBack={this.goBack}
198+
replaceRoute={this.replaceRoute}
199+
resetToRoute={this.resetToRoute}
200+
goToFirstRoute={this.goToFirstRoute}
175201
customAction={this.customAction}
176202
{...this.props.rightProps}
177203
{...this.props.route.rightCornerProps}
@@ -193,7 +219,17 @@ class NavBarContent extends React.Component {
193219

194220
if (this.props.route.titleComponent) {
195221
TitleComponent = this.props.route.titleComponent;
196-
titleContent = <TitleComponent {...this.props.titleProps} />;
222+
titleContent = (
223+
<TitleComponent
224+
toRoute={this.goForward}
225+
toBack={this.goBack}
226+
replaceRoute={this.replaceRoute}
227+
resetToRoute={this.resetToRoute}
228+
goToFirstRoute={this.goToFirstRoute}
229+
customAction={this.customAction}
230+
{...this.props.titleProps}
231+
/>
232+
);
197233
} else {
198234
titleContent = (
199235
<Text style={[styles.navbarText, this.props.titleStyle]} numberOfLines={1}>

index.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class Router extends React.Component {
4545

4646
this.onForward = this.onForward.bind(this);
4747
this.onBack = this.onBack.bind(this);
48+
this.onReplaceRoute = this.onReplaceRoute.bind(this);
49+
this.onResetToRoute = this.onResetToRoute.bind(this);
50+
this.onToFirstRoute = this.onToFirstRoute.bind(this);
4851
this.customAction = this.customAction.bind(this);
4952
this.renderScene = this.renderScene.bind(this);
5053

@@ -127,18 +130,34 @@ class Router extends React.Component {
127130
});
128131
}
129132

133+
onForward(nextRoute, navigator) {
134+
navigator.push(
135+
Object.assign(nextRoute, { index: this.state.route.index + 1 || 1 })
136+
);
137+
}
138+
130139
onBack(navigator) {
131140
if (this.state.route.index > 0) {
132141
navigator.pop();
133142
}
134143
}
135144

136-
onForward(nextRoute, navigator) {
137-
navigator.push(
138-
Object.assign(nextRoute, { index: this.state.route.index + 1 || 1 })
145+
onReplaceRoute(nextRoute, navigator) {
146+
navigator.replace(
147+
Object.assign(nextRoute, { index: this.state.route.index || 0 })
139148
);
140149
}
141150

151+
onResetToRoute(nextRoute, navigator) {
152+
navigator.resetTo(
153+
Object.assign(nextRoute, { index: 0 })
154+
);
155+
}
156+
157+
onToFirstRoute(navigator) {
158+
navigator.popToTop();
159+
}
160+
142161
onWillPop() {
143162
this.emitter.emit('willPop');
144163
}
@@ -201,23 +220,17 @@ class Router extends React.Component {
201220

202221
renderScene(route, navigator) {
203222
const goForward = (nextRoute) => {
204-
navigator.push(
205-
Object.assign(nextRoute, { index: this.state.route.index + 1 || 1 })
206-
);
223+
this.onForward(nextRoute, navigator);
207224
this.emitter.emit('push', nextRoute);
208225
};
209226

210227
const replaceRoute = (nextRoute) => {
211-
navigator.replace(
212-
Object.assign(nextRoute, { index: this.state.route.index || 0 })
213-
);
228+
this.replaceRoute(nextRoute, navigator);
214229
this.emitter.emit('replace', nextRoute);
215230
};
216231

217232
const resetToRoute = (nextRoute) => {
218-
navigator.resetTo(
219-
Object.assign(nextRoute, { index: 0 })
220-
);
233+
this.onResetToRoute(nextRoute, navigator);
221234
this.emitter.emit('resetTo', nextRoute);
222235
};
223236

@@ -226,7 +239,7 @@ class Router extends React.Component {
226239
};
227240

228241
const goToFirstRoute = () => {
229-
navigator.popToTop();
242+
this.onToFirstRoute(navigator);
230243
this.emitter.emit('popToTop');
231244
};
232245

@@ -305,6 +318,9 @@ class Router extends React.Component {
305318
borderColor={this.props.borderColor}
306319
toRoute={this.onForward}
307320
toBack={this.onBack}
321+
replaceRoute={this.onReplaceRoute}
322+
resetToRoute={this.onResetToRoute}
323+
goToFirstRoute={this.onToFirstRoute}
308324
leftProps={this.state.leftProps}
309325
rightProps={this.state.rightProps}
310326
titleProps={this.state.titleProps}

0 commit comments

Comments
 (0)