Parcourir la source

added switchToTab on iOS (#1862)

yogevbd il y a 6 ans
Parent
révision
32dcdc78ee

+ 8
- 0
e2e/ScreenStack.test.js Voir le fichier

41
     await elementByLabel('Pop To Root').tap();
41
     await elementByLabel('Pop To Root').tap();
42
     await expect(elementByLabel('React Native Navigation!')).toBeVisible();
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 Voir le fichier

54
 	[_commandsHandler dismissAllModals];
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
 @end
61
 @end
58
 
62
 

+ 2
- 0
lib/ios/RNNCommandsHandler.h Voir le fichier

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

+ 7
- 0
lib/ios/RNNCommandsHandler.m Voir le fichier

91
 	[_modalManager dismissAllModals];
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
 #pragma mark - private
101
 #pragma mark - private
95
 
102
 
96
 -(void) assertReady {
103
 -(void) assertReady {

+ 4
- 0
lib/src/Navigation.js Voir le fichier

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

+ 5
- 0
lib/src/adapters/NativeCommandsSender.js Voir le fichier

48
     this.nativeCommandsModule.dismissAllModals();
48
     this.nativeCommandsModule.dismissAllModals();
49
     return Promise.resolve(true);
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
 module.exports = NativeCommandsSender;
58
 module.exports = NativeCommandsSender;

+ 4
- 0
lib/src/commands/Commands.js Voir le fichier

54
   popToRoot(containerId) {
54
   popToRoot(containerId) {
55
     return this.nativeCommandsSender.popToRoot(containerId);
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
 module.exports = Commands;
63
 module.exports = Commands;

+ 15
- 0
lib/src/commands/Commands.test.js Voir le fichier

222
       expect(result).toEqual('theContainerId');
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 Voir le fichier

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