Browse Source

V2 Current tab by container ID (#2264)

* set current tab by container id

* currentTabByContainerID e2e

* lint fix

* e2e test
yogevbd 7 years ago
parent
commit
79daa62e3c

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

50
     await expect(elementByLabel('This is tab 1')).toBeNotVisible();
50
     await expect(elementByLabel('This is tab 1')).toBeNotVisible();
51
     await expect(elementByLabel('This is tab 2')).toBeVisible();
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
 		if (self.bottomTabs.currentTabIndex) {
205
 		if (self.bottomTabs.currentTabIndex) {
206
 			[viewController.tabBarController setSelectedIndex:[self.bottomTabs.currentTabIndex unsignedIntegerValue]];
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
 		if (self.bottomTabs.hidden) {
213
 		if (self.bottomTabs.hidden) {
209
 			[((RNNTabBarController *)viewController.tabBarController) setTabBarHidden:[self.bottomTabs.hidden boolValue] animated:[self.bottomTabs.animateHide boolValue]];
214
 			[((RNNTabBarController *)viewController.tabBarController) setTabBarHidden:[self.bottomTabs.hidden boolValue] animated:[self.bottomTabs.animateHide boolValue]];
210
 		}
215
 		}
220
 				viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
225
 				viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
221
 			}
226
 			}
222
 		}
227
 		}
228
+		
229
+		[self.bottomTabs resetOptions];
223
 	}
230
 	}
224
 	
231
 	
225
 	if (self.statusBarBlur) {
232
 	if (self.statusBarBlur) {

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

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

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

1
 
1
 
2
 #import "RNNTabBarController.h"
2
 #import "RNNTabBarController.h"
3
+#import "RNNRootViewController.h"
3
 #define kTabBarHiddenDuration 0.3
4
 #define kTabBarHiddenDuration 0.3
4
 
5
 
5
 @implementation RNNTabBarController
6
 @implementation RNNTabBarController
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
 @end
36
 @end

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

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

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

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

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

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

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

27
   POP_TO_ROOT: `POP_TO_ROOT`,
27
   POP_TO_ROOT: `POP_TO_ROOT`,
28
   POP_STACK_POSITION_ONE_BUTTON: `POP_STACK_POSITION_ONE_BUTTON`,
28
   POP_STACK_POSITION_ONE_BUTTON: `POP_STACK_POSITION_ONE_BUTTON`,
29
   SWITCH_SECOND_TAB_BUTTON: `SWITCH_SECOND_TAB_BUTTON`,
29
   SWITCH_SECOND_TAB_BUTTON: `SWITCH_SECOND_TAB_BUTTON`,
30
+  SWITCH_FIRST_TAB_BUTTON: `SWITCH_FIRST_TAB_BUTTON`,
30
   DYNAMIC_OPTIONS_BUTTON: `DYNAMIC_OPTIONS_BUTTON`,
31
   DYNAMIC_OPTIONS_BUTTON: `DYNAMIC_OPTIONS_BUTTON`,
31
   SHOW_TOP_BAR_BUTTON: `SHOW_TOP_BAR_BUTTON`,
32
   SHOW_TOP_BAR_BUTTON: `SHOW_TOP_BAR_BUTTON`,
32
   HIDE_TOP_BAR_BUTTON: `HIDE_TOP_BAR_BUTTON`,
33
   HIDE_TOP_BAR_BUTTON: `HIDE_TOP_BAR_BUTTON`,