Browse Source

added switchToTab on iOS (#1862)

yogevbd 6 years ago
parent
commit
32dcdc78ee

+ 8
- 0
e2e/ScreenStack.test.js View File

@@ -41,4 +41,12 @@ describe('screen stack', () => {
41 41
     await elementByLabel('Pop To Root').tap();
42 42
     await expect(elementByLabel('React Native Navigation!')).toBeVisible();
43 43
   });
44
+
45
+  it('switch to tab', async () => {
46
+    await elementByLabel('Switch to tab based app').tap();
47
+    await expect(elementByLabel('This is tab 1')).toBeVisible();
48
+    await elementByLabel('Switch To Tab 2').tap();
49
+    await expect(elementByLabel('This is tab 1')).toBeNotVisible();
50
+    await expect(elementByLabel('This is tab 2')).toBeVisible();
51
+  });
44 52
 });

+ 4
- 0
lib/ios/RNNBridgeModule.m View File

@@ -54,5 +54,9 @@ RCT_EXPORT_METHOD(dismissAllModals) {
54 54
 	[_commandsHandler dismissAllModals];
55 55
 }
56 56
 
57
+RCT_EXPORT_METHOD(switchToTab:(NSString*)containerId tabIndex:(nonnull NSNumber*)tabIndex) {
58
+	[_commandsHandler switchToTab:containerId tabIndex:tabIndex];
59
+}
60
+
57 61
 @end
58 62
 

+ 2
- 0
lib/ios/RNNCommandsHandler.h View File

@@ -26,4 +26,6 @@
26 26
 
27 27
 -(void) dismissAllModals;
28 28
 
29
+-(void) switchToTab:(NSString*)containerId tabIndex:(NSNumber*)tabIndex;
30
+
29 31
 @end

+ 7
- 0
lib/ios/RNNCommandsHandler.m View File

@@ -91,6 +91,13 @@
91 91
 	[_modalManager dismissAllModals];
92 92
 }
93 93
 
94
+-(void) switchToTab:(NSString *)containerId tabIndex:(NSNumber *)tabIndex {
95
+	[self assertReady];
96
+	
97
+	UIViewController* vc = [_store findContainerForId:containerId];
98
+	[vc.tabBarController setSelectedIndex:[tabIndex unsignedIntegerValue]];
99
+}
100
+
94 101
 #pragma mark - private
95 102
 
96 103
 -(void) assertReady {

+ 4
- 0
lib/src/Navigation.js View File

@@ -64,6 +64,10 @@ class Navigation {
64 64
     return this.commands.popToRoot(containerId);
65 65
   }
66 66
 
67
+  switchToTab(onContainerId, tabIndex) {
68
+    return this.commands.switchToTab(onContainerId, tabIndex);
69
+  }
70
+
67 71
   events() {
68 72
     return this.publicEventsRegistry;
69 73
   }

+ 5
- 0
lib/src/adapters/NativeCommandsSender.js View File

@@ -48,6 +48,11 @@ class NativeCommandsSender {
48 48
     this.nativeCommandsModule.dismissAllModals();
49 49
     return Promise.resolve(true);
50 50
   }
51
+
52
+  switchToTab(containerId, tabIndex) {
53
+    this.nativeCommandsModule.switchToTab(containerId, tabIndex);
54
+    return Promise.resolve(containerId);
55
+  }
51 56
 }
52 57
 
53 58
 module.exports = NativeCommandsSender;

+ 4
- 0
lib/src/commands/Commands.js View File

@@ -54,6 +54,10 @@ class Commands {
54 54
   popToRoot(containerId) {
55 55
     return this.nativeCommandsSender.popToRoot(containerId);
56 56
   }
57
+
58
+  switchToTab(containerId, tabIndex) {
59
+    return this.nativeCommandsSender.switchToTab(containerId, tabIndex);
60
+  }
57 61
 }
58 62
 
59 63
 module.exports = Commands;

+ 15
- 0
lib/src/commands/Commands.test.js View File

@@ -222,4 +222,19 @@ describe('Commands', () => {
222 222
       expect(result).toEqual('theContainerId');
223 223
     });
224 224
   });
225
+
226
+  describe('switchToTab', () => {
227
+    it('switch tab in tabs controller', () => {
228
+      const tabIndex = 1;
229
+      uut.switchToTab('theContainerId', tabIndex);
230
+      expect(mockCommandsSender.switchToTab).toHaveBeenCalledTimes(1);
231
+      expect(mockCommandsSender.switchToTab).toHaveBeenCalledWith('theContainerId', tabIndex);
232
+    });
233
+
234
+    it('returns a promise that resolves to targetId', async () => {
235
+      mockCommandsSender.switchToTab.mockReturnValue(Promise.resolve('theContainerId', 1));
236
+      const result = await uut.switchToTab('theContainerId');
237
+      expect(result).toEqual('theContainerId');
238
+    });
239
+  });
225 240
 });

+ 5
- 0
playground/src/containers/TextScreen.js View File

@@ -14,6 +14,7 @@ class TextScreen extends Component {
14 14
         {this.renderTextFromFunctionInProps()}
15 15
         <Text style={styles.footer}>{`this.props.containerId = ${this.props.containerId}`}</Text>
16 16
         <Button title={'Set Tab Badge'} onPress={() => this.onButtonPress()} />
17
+        <Button title="Switch To Tab 2" onPress={() => this.onClickSwitchToTab()} />
17 18
       </View>
18 19
     );
19 20
   }
@@ -32,6 +33,10 @@ class TextScreen extends Component {
32 33
       tabBadge: `EnCyClOpEdIa`
33 34
     });
34 35
   }
36
+
37
+  onClickSwitchToTab() {
38
+    Navigation.switchToTab(this.props.containerId, 1);
39
+  }
35 40
 }
36 41
 module.exports = TextScreen;
37 42