Browse Source

V2 default options (#2489)

* default options support
* refactored navigation options
yogevbd 6 years ago
parent
commit
5578b9507a
No account linked to committer's email address

+ 16
- 0
e2e/ScreenStyle.test.js View File

@@ -110,4 +110,20 @@ describe('screen style', () => {
110 110
     await expect(elementById(testIDs.FIRST_TAB_BAR_BUTTON)).toBeVisible();
111 111
     await expect(elementById(testIDs.SECOND_TAB_BAR_BUTTON)).toBeVisible();
112 112
   });
113
+
114
+  it('default options should apply to all screens in stack', async () => {
115
+    await elementById(testIDs.PUSH_OPTIONS_BUTTON).tap();
116
+    await elementById(testIDs.PUSH_DEFAULT_OPTIONS_BUTTON).tap();
117
+    await expect(elementById(testIDs.TOP_BAR_ELEMENT)).toBeNotVisible();
118
+    await elementById(testIDs.PUSH_BUTTON).tap();
119
+    await expect(elementById(testIDs.TOP_BAR_ELEMENT)).toBeNotVisible();
120
+  });
121
+
122
+  it('default options should not override static options', async () => {
123
+    await elementById(testIDs.PUSH_OPTIONS_BUTTON).tap();
124
+    await elementById(testIDs.PUSH_DEFAULT_OPTIONS_BUTTON).tap();
125
+    await expect(elementById(testIDs.TOP_BAR_ELEMENT)).toBeNotVisible();
126
+    await elementById(testIDs.POP_BUTTON).tap();
127
+    await expect(elementById(testIDs.TOP_BAR_ELEMENT)).toBeVisible();
128
+  });
113 129
 });

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

@@ -30,6 +30,12 @@ RCT_EXPORT_METHOD(setOptions:(NSString*)componentId options:(NSDictionary*)optio
30 30
 	}];
31 31
 }
32 32
 
33
+RCT_EXPORT_METHOD(setDefaultOptions:(NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
34
+	[_commandsHandler setDefaultOptions:options completion:^{
35
+		resolve(nil);
36
+	}];
37
+}
38
+
33 39
 RCT_EXPORT_METHOD(push:(NSString*)componentId layout:(NSDictionary*)layout resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
34 40
 	[_commandsHandler push:componentId layout:layout completion:^{
35 41
 		resolve(componentId);

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

@@ -12,6 +12,8 @@
12 12
 
13 13
 -(void) setOptions:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion;
14 14
 
15
+-(void) setDefaultOptions:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion;
16
+
15 17
 -(void) push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion;
16 18
 
17 19
 -(void) pop:(NSString*)componentId options:(NSDictionary*)options completion:(RNNTransitionCompletionBlock)completion;

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

@@ -51,6 +51,12 @@
51 51
 	}
52 52
 }
53 53
 
54
+-(void) setDefaultOptions:(NSDictionary*)optionsDict completion:(RNNTransitionCompletionBlock)completion {
55
+	[self assertReady];
56
+	RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:optionsDict];
57
+	[_controllerFactory setDefaultOptions:options];
58
+}
59
+
54 60
 -(void) push:(NSString*)componentId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion {
55 61
 	[self assertReady];
56 62
 	

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

@@ -15,4 +15,6 @@
15 15
 
16 16
 -(UIViewController<RNNRootViewProtocol> *)createLayoutAndSaveToStore:(NSDictionary*)layout;
17 17
 
18
+@property (nonatomic, strong) RNNNavigationOptions* defaultOptions;
19
+
18 20
 @end

+ 1
- 0
lib/ios/RNNControllerFactory.m View File

@@ -89,6 +89,7 @@
89 89
 	NSDictionary* customTransition = node.data[@"customTransition"];
90 90
 	RNNAnimator* animator = [[RNNAnimator alloc] initWithAnimationsDictionary:customTransition];
91 91
 	RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:node.data[@"options"]];
92
+	options.defaultOptions = _defaultOptions;
92 93
 	NSString* componentId = node.nodeId;
93 94
 	RNNRootViewController* component = [[RNNRootViewController alloc] initWithName:name withOptions:options withComponentId:componentId rootViewCreator:_creator eventEmitter:_eventEmitter animator:animator];
94 95
 	CGSize availableSize = UIApplication.sharedApplication.delegate.window.bounds.size;

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

@@ -19,6 +19,8 @@ extern const NSInteger TOP_BAR_TRANSPARENT_TAG;
19 19
 @property (nonatomic, strong) RNNTopTabOptions* topTab;
20 20
 @property (nonatomic, strong) RNNSideMenuOptions* sideMenu;
21 21
 
22
+@property (nonatomic, strong) RNNNavigationOptions* defaultOptions;
23
+
22 24
 @property (nonatomic, strong) NSNumber* statusBarHidden;
23 25
 @property (nonatomic, strong) NSNumber* screenBackgroundColor;
24 26
 @property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;

+ 6
- 1
lib/ios/RNNNavigationOptions.m View File

@@ -46,12 +46,17 @@ const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
46 46
 }
47 47
 
48 48
 -(void)applyOn:(UIViewController*)viewController {
49
+	[_defaultOptions applyOn:viewController];
50
+	
49 51
 	[self.topBar applyOn:viewController];
50 52
 	[self.bottomTabs applyOn:viewController];
51 53
 	[self.topTab applyOn:viewController];
52 54
 	[self.bottomTab applyOn:viewController];
53 55
 	[self.sideMenu applyOn:viewController];
54
-	
56
+	[self applyOtherOptionsOn:viewController];
57
+}
58
+
59
+- (void)applyOtherOptionsOn:(UIViewController*)viewController {
55 60
 	if (self.popGesture) {
56 61
 		viewController.navigationController.interactivePopGestureRecognizer.enabled = [self.popGesture boolValue];
57 62
 	}

+ 8
- 4
lib/ios/RNNOptions.h View File

@@ -1,17 +1,21 @@
1 1
 #import <Foundation/Foundation.h>
2 2
 #import <UIKit/UIKit.h>
3 3
 #import <React/RCTConvert.h>
4
+
5
+@class RNNOptions;
6
+
4 7
 @protocol RNNOptionsProtocol <NSObject>
5 8
 
6 9
 @optional
7
--(void)resetOptions;
8
--(void)applyOn:(UIViewController*)viewController;
10
+- (void)resetOptions;
11
+- (void)applyOn:(UIViewController *)viewController;
9 12
 
10 13
 @end
11 14
 
12 15
 @interface RNNOptions : NSObject <RNNOptionsProtocol>
13 16
 
14
--(instancetype)initWithDict:(NSDictionary*)dict;
15
--(void)mergeWith:(NSDictionary*)otherOptions;
17
+- (instancetype)initWithDict:(NSDictionary*)dict;
18
+- (void)mergeWith:(NSDictionary*)otherOptions;
19
+- (void)applyOn:(UIViewController *)viewController defaultOptions:(RNNOptions*)defaultOptions;
16 20
 
17 21
 @end

+ 5
- 0
lib/ios/RNNOptions.m View File

@@ -9,6 +9,11 @@
9 9
 	return self;
10 10
 }
11 11
 
12
+- (void)applyOn:(UIViewController *)viewController defaultOptions:(RNNOptions *)defaultOptions {
13
+	[defaultOptions applyOn:viewController];
14
+	[self applyOn:viewController];
15
+}
16
+
12 17
 -(void)mergeWith:(NSDictionary *)otherOptions {
13 18
 	for (id key in otherOptions) {
14 19
 		[self setValue:[otherOptions objectForKey:key] forKey:key];

+ 17
- 1
playground/src/screens/OptionsScreen.js View File

@@ -48,6 +48,7 @@ class OptionsScreen extends Component {
48 48
     this.onClickTopBarTransparent = this.onClickTopBarTransparent.bind(this);
49 49
     this.onClickTopBarOpaque = this.onClickTopBarOpaque.bind(this);
50 50
     this.onClickCustomTranstition = this.onClickCustomTranstition.bind(this);
51
+    this.onClickPushDefaultOptionsScreen = this.onClickPushDefaultOptionsScreen.bind(this);
51 52
   }
52 53
 
53 54
   render() {
@@ -63,7 +64,8 @@ class OptionsScreen extends Component {
63 64
         <Button title="Custom Transition" onPress={this.onClickCustomTranstition} />
64 65
         <Button title="Show custom alert" testID={testIDs.SHOW_CUSTOM_ALERT_BUTTON} onPress={this.onClickAlert} />
65 66
         <Button title="Show snackbar" testID={testIDs.SHOW_SNACKBAR_BUTTON} onPress={this.onClickSnackbar} />
66
-        <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
67
+        <Button title="Push Default Options Screen" testID={testIDs.PUSH_DEFAULT_OPTIONS_BUTTON} onPress={this.onClickPushDefaultOptionsScreen} />
68
+        <Text style={styles.footer}>{`this.props.containerId = ${this.props.containerId}`}</Text>
67 69
       </View>
68 70
     );
69 71
   }
@@ -187,6 +189,20 @@ class OptionsScreen extends Component {
187 189
       }
188 190
     });
189 191
   }
192
+
193
+  onClickPushDefaultOptionsScreen() {
194
+    Navigation.setDefaultOptions({
195
+      topBar: {
196
+        hidden: true
197
+      }
198
+    });
199
+
200
+    Navigation.push(this.props.componentId, {
201
+      component: {
202
+        name: 'navigation.playground.PushedScreen'
203
+      }
204
+    });
205
+  }
190 206
 }
191 207
 
192 208
 const styles = {

+ 8
- 0
playground/src/screens/PushedScreen.js View File

@@ -9,6 +9,14 @@ const Navigation = require('react-native-navigation');
9 9
 const testIDs = require('../testIDs');
10 10
 
11 11
 class PushedScreen extends Component {
12
+  static get navigationOptions() {
13
+    return {
14
+      topBar: {
15
+        testID: testIDs.TOP_BAR_ELEMENT
16
+      }
17
+    };
18
+  }
19
+
12 20
   constructor(props) {
13 21
     super(props);
14 22
     this.onClickPush = this.onClickPush.bind(this);

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

@@ -7,6 +7,7 @@ module.exports = {
7 7
   PUSH_LIFECYCLE_BUTTON: `PUSH_LIFECYCLE_BUTTON`,
8 8
   PUSH_BUTTON: `PUSH_BUTTON`,
9 9
   PUSH_OPTIONS_BUTTON: `PUSH_OPTIONS_BUTTON`,
10
+  PUSH_DEFAULT_OPTIONS_BUTTON: `PUSH_DEFAULT_OPTIONS_BUTTON`,
10 11
   BACK_HANDLER_BUTTON: `BACK_HANDLER_BUTTON`,
11 12
   SHOW_MODAL_BUTTON: `SHOW_MODAL_BUTTON`,
12 13
   SHOW_REDBOX_BUTTON: `SHOW_REDBOX_BUTTON`,