Przeglądaj źródła

#833 - pop screen by id when not on top

Ran Greenberg 7 lat temu
rodzic
commit
0117b1d61e

+ 8
- 1
ios/RNNBridgeModule.m Wyświetl plik

@@ -40,7 +40,14 @@ RCT_EXPORT_METHOD(pop:(NSString*)containerId)
40 40
 	[self assertReady];
41 41
 	UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
42 42
 	
43
-	[[vc navigationController] popViewControllerAnimated:YES];
43
+	if([[vc navigationController] topViewController] == vc ) {
44
+		[[vc navigationController] popViewControllerAnimated:YES];
45
+	}
46
+	else {
47
+		NSMutableArray * vcs = [vc navigationController].viewControllers.mutableCopy;
48
+		[vcs removeObject:vc];
49
+		[[vc navigationController] setViewControllers:vcs animated:NO];
50
+	}
44 51
 	[[RNN instance].store removeContainer:containerId];
45 52
 }
46 53
 

+ 12
- 0
playground/e2e/app.test.js Wyświetl plik

@@ -38,6 +38,18 @@ describe('app', () => {
38 38
     elementByLabel('Pop').tap();
39 39
     expect(elementByLabel('React Native Navigation!')).toBeVisible();
40 40
   });
41
+  
42
+  it.only('pop screen deep in the stack', () => {
43
+    elementByLabel('Push').tap();
44
+    expect(elementByLabel('Stack Position: 1')).toBeVisible();
45
+    elementByLabel('Push').tap();
46
+    expect(elementByLabel('Stack Position: 2')).toBeVisible();
47
+    elementByLabel('Pop Previous').tap();
48
+    expect(elementByLabel('Stack Position: 2')).toBeVisible();
49
+    elementByLabel('Pop').tap();
50
+    expect(elementByLabel('React Native Navigation!')).toBeVisible();
51
+  
52
+  });
41 53
 
42 54
   it('show modal', () => {
43 55
     elementByLabel('Show Modal').tap();

+ 12
- 4
playground/src/containers/PushedScreen.js Wyświetl plik

@@ -1,3 +1,4 @@
1
+import _ from 'lodash';
1 2
 import React, { Component } from 'react';
2 3
 import {
3 4
   StyleSheet,
@@ -13,6 +14,7 @@ class PushedScreen extends Component {
13 14
     super(props);
14 15
     this.onClickPush = this.onClickPush.bind(this);
15 16
     this.onClickPop = this.onClickPop.bind(this);
17
+    this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
16 18
   }
17 19
   render() {
18 20
     return (
@@ -21,24 +23,30 @@ class PushedScreen extends Component {
21 23
         <Text style={styles.h2}>{`Stack Position: ${this.getStackPosition()}`}</Text>
22 24
         <Button title="Push" onPress={this.onClickPush} />
23 25
         <Button title="Pop" onPress={this.onClickPop} />
26
+        <Button title="Pop Previous" onPress={this.onClickPopPrevious} />
24 27
         <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
25 28
       </View>
26 29
     );
27 30
   }
28
-
31
+  
29 32
   onClickPush() {
30 33
     Navigation.on(this.props.id).push({
31 34
       name: 'navigation.playground.PushedScreen',
32 35
       passProps: {
33
-        stackPosition: this.getStackPosition() + 1
36
+        stackPosition: this.getStackPosition() + 1,
37
+        previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.id)
34 38
       }
35 39
     });
36 40
   }
37
-
41
+  
38 42
   onClickPop() {
39 43
     Navigation.on(this.props.id).pop();
40 44
   }
41
-
45
+  
46
+  onClickPopPrevious() {
47
+    Navigation.on(_.last(this.props.previousScreenIds)).pop();
48
+  }
49
+  
42 50
   getStackPosition() {
43 51
     return this.props.stackPosition || 1;
44 52
   }