Browse Source

V2 Current tab by container ID (#2264)

* set current tab by container id

* currentTabByContainerID e2e

* lint fix

* e2e test
yogevbd 6 years ago
parent
commit
79daa62e3c

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

@@ -50,4 +50,13 @@ describe('screen stack', () => {
50 50
     await expect(elementByLabel('This is tab 1')).toBeNotVisible();
51 51
     await expect(elementByLabel('This is tab 2')).toBeVisible();
52 52
   });
53
+
54
+  it('switch to tab by cotnainerId', async () => {
55
+    await elementById(testIDs.TAB_BASED_APP_BUTTON).tap();
56
+    await expect(elementByLabel('This is tab 1')).toBeVisible();
57
+    await elementById(testIDs.SWITCH_SECOND_TAB_BUTTON).tap();
58
+    await expect(elementByLabel('This is tab 2')).toBeVisible();
59
+    await elementById(testIDs.SWITCH_FIRST_TAB_BUTTON).tap();
60
+    await expect(elementByLabel('This is tab 1')).toBeVisible();
61
+  });
53 62
 });

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

@@ -205,6 +205,11 @@ const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
205 205
 		if (self.bottomTabs.currentTabIndex) {
206 206
 			[viewController.tabBarController setSelectedIndex:[self.bottomTabs.currentTabIndex unsignedIntegerValue]];
207 207
 		}
208
+		
209
+		if (self.bottomTabs.currentTabId) {
210
+			[(RNNTabBarController*)viewController.tabBarController setSelectedIndexByContainerID:self.bottomTabs.currentTabId];
211
+		}
212
+		
208 213
 		if (self.bottomTabs.hidden) {
209 214
 			[((RNNTabBarController *)viewController.tabBarController) setTabBarHidden:[self.bottomTabs.hidden boolValue] animated:[self.bottomTabs.animateHide boolValue]];
210 215
 		}
@@ -220,6 +225,8 @@ const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
220 225
 				viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
221 226
 			}
222 227
 		}
228
+		
229
+		[self.bottomTabs resetOptions];
223 230
 	}
224 231
 	
225 232
 	if (self.statusBarBlur) {

+ 1
- 0
lib/ios/RNNTabBarController.h View File

@@ -4,5 +4,6 @@
4 4
 @interface RNNTabBarController : UITabBarController
5 5
 
6 6
 - (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;
7
+- (void)setSelectedIndexByContainerID:(NSString *)containerID;
7 8
 
8 9
 @end

+ 13
- 0
lib/ios/RNNTabBarController.m View File

@@ -1,5 +1,6 @@
1 1
 
2 2
 #import "RNNTabBarController.h"
3
+#import "RNNRootViewController.h"
3 4
 #define kTabBarHiddenDuration 0.3
4 5
 
5 6
 @implementation RNNTabBarController
@@ -20,4 +21,16 @@
20 21
 	}];
21 22
 }
22 23
 
24
+- (void)setSelectedIndexByContainerID:(NSString *)containerID {
25
+	for (id child in self.childViewControllers) {
26
+		RNNRootViewController* vc = child;
27
+		if ([child isKindOfClass:[UINavigationController class]]) {
28
+			vc = ((UINavigationController *)child).childViewControllers.firstObject;
29
+		}
30
+		if ([vc.containerId isEqualToString:containerID]) {
31
+			[self setSelectedIndex:[self.childViewControllers indexOfObject:child]];
32
+		}
33
+	}
34
+}
35
+
23 36
 @end

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

@@ -9,9 +9,11 @@ extern const NSInteger BLUR_TOPBAR_TAG;
9 9
 @property (nonatomic, strong) NSNumber* currentTabIndex;
10 10
 @property (nonatomic, strong) NSString* testID;
11 11
 @property (nonatomic, strong) NSNumber* drawUnder;
12
+@property (nonatomic, strong) NSString* currentTabId;
12 13
 
13 14
 -(instancetype)init;
14 15
 -(instancetype)initWithDict:(NSDictionary *)topBarOptions;
15 16
 -(void)mergeWith:(NSDictionary*)otherOptions;
17
+- (void)resetOptions;
16 18
 
17 19
 @end

+ 6
- 0
lib/ios/RNNTabBarOptions.m View File

@@ -13,10 +13,16 @@
13 13
 	self.animateHide = [tabBarOptions valueForKey:@"animateHide"];
14 14
 	self.currentTabIndex = [tabBarOptions valueForKey:@"currentTabIndex"];
15 15
 	self.testID = [tabBarOptions valueForKey:@"testID"];
16
+	self.currentTabId = [tabBarOptions valueForKey:@"currentTabId"];
16 17
 	
17 18
 	return self;
18 19
 }
19 20
 
21
+- (void)resetOptions {
22
+	self.currentTabId = nil;
23
+	self.currentTabIndex = nil;
24
+}
25
+
20 26
 -(void)mergeWith:(NSDictionary *)otherOptions {
21 27
 	for (id key in otherOptions) {
22 28
 		[self setValue:[otherOptions objectForKey:key] forKey:key];

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

@@ -6,6 +6,8 @@ const { View, Text, Button } = require('react-native');
6 6
 const Navigation = require('react-native-navigation');
7 7
 const testIDs = require('../testIDs');
8 8
 
9
+let globalFirstContainerID;
10
+
9 11
 class TextScreen extends Component {
10 12
   static get navigationOptions() {
11 13
     return {
@@ -15,6 +17,11 @@ class TextScreen extends Component {
15 17
     };
16 18
   }
17 19
 
20
+  constructor(props) {
21
+    super(props);
22
+    globalFirstContainerID = (props.text === 'This is tab 1') ? props.containerId : globalFirstContainerID;
23
+  }
24
+
18 25
   render() {
19 26
     return (
20 27
       <View style={styles.root}>
@@ -23,6 +30,7 @@ class TextScreen extends Component {
23 30
         <Text style={styles.footer}>{`this.props.containerId = ${this.props.containerId}`}</Text>
24 31
         <Button title={'Set Tab Badge'} testID={testIDs.SET_TAB_BADGE_BUTTON} onPress={() => this.onButtonPress()} />
25 32
         <Button title={'Switch To Tab 2'} testID={testIDs.SWITCH_SECOND_TAB_BUTTON} onPress={() => this.onClickSwitchToTab()} />
33
+        <Button title={'Switch To Tab 1 by containerID'} testID={testIDs.SWITCH_FIRST_TAB_BUTTON} onPress={() => this.onClickSwitchToTabByContainerID()} />
26 34
         <Button title="Hide Tab Bar" testID={testIDs.HIDE_BOTTOM_TABS_BUTTON} onPress={() => this.hideTabBar(true)} />
27 35
         <Button title="Show Tab Bar" testID={testIDs.SHOW_BOTTOM_TABS_BUTTON} onPress={() => this.hideTabBar(false)} />
28 36
         <Button title="Show Left Side Menu" testID={testIDs.SHOW_LEFT_SIDE_MENU_BUTTON} onPress={() => this.showSideMenu('left')} />
@@ -58,6 +66,14 @@ class TextScreen extends Component {
58 66
     });
59 67
   }
60 68
 
69
+  onClickSwitchToTabByContainerID() {
70
+    Navigation.setOptions(this.props.containerId, {
71
+      bottomTabs: {
72
+        currentTabId: globalFirstContainerID
73
+      }
74
+    });
75
+  }
76
+
61 77
   hideTabBar(hidden) {
62 78
     Navigation.setOptions(this.props.containerId, {
63 79
       bottomTabs: {

+ 1
- 0
playground/src/testIDs.js View File

@@ -27,6 +27,7 @@ module.exports = {
27 27
   POP_TO_ROOT: `POP_TO_ROOT`,
28 28
   POP_STACK_POSITION_ONE_BUTTON: `POP_STACK_POSITION_ONE_BUTTON`,
29 29
   SWITCH_SECOND_TAB_BUTTON: `SWITCH_SECOND_TAB_BUTTON`,
30
+  SWITCH_FIRST_TAB_BUTTON: `SWITCH_FIRST_TAB_BUTTON`,
30 31
   DYNAMIC_OPTIONS_BUTTON: `DYNAMIC_OPTIONS_BUTTON`,
31 32
   SHOW_TOP_BAR_BUTTON: `SHOW_TOP_BAR_BUTTON`,
32 33
   HIDE_TOP_BAR_BUTTON: `HIDE_TOP_BAR_BUTTON`,