Browse Source

Nav bar save props (#2211)

This commit saves navBar component props in propsRegistry and restores the saved props
when the component is mounted, allowing us to pass unserializable props to custom navBar component.
Guy Carmeli 7 years ago
parent
commit
6170e70346
No account linked to committer's email address
2 changed files with 22 additions and 8 deletions
  1. 1
    1
      docs/styling-the-navigator.md
  2. 21
    7
      src/deprecated/platformSpecificDeprecated.ios.js

+ 1
- 1
docs/styling-the-navigator.md View File

53
   navBarBackgroundColor: '#f7f7f7', // change the background color of the nav bar (remembered across pushes)
53
   navBarBackgroundColor: '#f7f7f7', // change the background color of the nav bar (remembered across pushes)
54
   navBarCustomView: 'example.CustomTopBar', // registered component name
54
   navBarCustomView: 'example.CustomTopBar', // registered component name
55
   navBarComponentAlignment: 'center', // center/fill
55
   navBarComponentAlignment: 'center', // center/fill
56
-  navBarCustomViewInitialProps: {}, // Serializable JSON passed as props
56
+  navBarCustomViewInitialProps: {}, // navBar custom component props
57
   navBarButtonColor: '#007aff', // Change color of nav bar buttons (eg. the back button) (remembered across pushes)
57
   navBarButtonColor: '#007aff', // Change color of nav bar buttons (eg. the back button) (remembered across pushes)
58
 
58
 
59
   navBarHidden: false, // make the nav bar hidden
59
   navBarHidden: false, // make the nav bar hidden

+ 21
- 7
src/deprecated/platformSpecificDeprecated.ios.js View File

31
       navigatorButtons,
31
       navigatorButtons,
32
       navigatorEventID
32
       navigatorEventID
33
     } = _mergeScreenSpecificSettings(tab.screen, screenInstanceID, tab);
33
     } = _mergeScreenSpecificSettings(tab.screen, screenInstanceID, tab);
34
-    _processNavigatorButtons(navigatorButtons);
34
+    _saveNavigatorButtonsProps(navigatorButtons);
35
+    _saveNavBarComponentProps(navigatorStyle);
35
     tab.navigationParams = {
36
     tab.navigationParams = {
36
       screenInstanceID,
37
       screenInstanceID,
37
       navigatorStyle,
38
       navigatorStyle,
139
     navigatorButtons,
140
     navigatorButtons,
140
     navigatorEventID
141
     navigatorEventID
141
   } = _mergeScreenSpecificSettings(screen.screen, screenInstanceID, screen);
142
   } = _mergeScreenSpecificSettings(screen.screen, screenInstanceID, screen);
142
-  _processNavigatorButtons(navigatorButtons);
143
+  _saveNavigatorButtonsProps(navigatorButtons);
144
+  _saveNavBarComponentProps(navigatorStyle);
143
   params.navigationParams = {
145
   params.navigationParams = {
144
     screenInstanceID,
146
     screenInstanceID,
145
     navigatorStyle,
147
     navigatorStyle,
238
     navigatorButtons,
240
     navigatorButtons,
239
     navigatorEventID
241
     navigatorEventID
240
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
242
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
241
-  _processNavigatorButtons(navigatorButtons);
243
+  _saveNavigatorButtonsProps(navigatorButtons);
244
+  _saveNavBarComponentProps(navigatorStyle);
242
   const passProps = Object.assign({}, params.passProps);
245
   const passProps = Object.assign({}, params.passProps);
243
   passProps.navigatorID = navigator.navigatorID;
246
   passProps.navigatorID = navigator.navigatorID;
244
   passProps.screenInstanceID = screenInstanceID;
247
   passProps.screenInstanceID = screenInstanceID;
297
     navigatorButtons,
300
     navigatorButtons,
298
     navigatorEventID
301
     navigatorEventID
299
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
302
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
300
-  _processNavigatorButtons(navigatorButtons);
303
+  _saveNavigatorButtonsProps(navigatorButtons);
304
+  _saveNavBarComponentProps(navigatorStyle);
301
   const passProps = Object.assign({}, params.passProps);
305
   const passProps = Object.assign({}, params.passProps);
302
   passProps.navigatorID = navigator.navigatorID;
306
   passProps.navigatorID = navigator.navigatorID;
303
   passProps.screenInstanceID = screenInstanceID;
307
   passProps.screenInstanceID = screenInstanceID;
366
 }
370
 }
367
 
371
 
368
 function navigatorSetStyle(navigator, params) {
372
 function navigatorSetStyle(navigator, params) {
373
+  _saveNavBarComponentProps(params);
369
   Controllers.NavigationControllerIOS(navigator.navigatorID).setStyle(params)
374
   Controllers.NavigationControllerIOS(navigator.navigatorID).setStyle(params)
370
 }
375
 }
371
 
376
 
447
 }
452
 }
448
 
453
 
449
 function navigatorSetButtons(navigator, navigatorEventID, params) {
454
 function navigatorSetButtons(navigator, navigatorEventID, params) {
450
-  _processNavigatorButtons(params);
455
+  _saveNavigatorButtonsProps(params);
451
   if (params.leftButtons) {
456
   if (params.leftButtons) {
452
     const buttons = params.leftButtons.slice(); // clone
457
     const buttons = params.leftButtons.slice(); // clone
453
     for (let i = 0; i < buttons.length; i++) {
458
     for (let i = 0; i < buttons.length; i++) {
477
     navigatorButtons,
482
     navigatorButtons,
478
     navigatorEventID
483
     navigatorEventID
479
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
484
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
480
-  _processNavigatorButtons(navigatorButtons);
485
+  _saveNavigatorButtonsProps(navigatorButtons);
486
+  _saveNavBarComponentProps(navigatorStyle);
481
   const passProps = Object.assign({}, params.passProps);
487
   const passProps = Object.assign({}, params.passProps);
482
   passProps.navigatorID = navigatorID;
488
   passProps.navigatorID = navigatorID;
483
   passProps.screenInstanceID = screenInstanceID;
489
   passProps.screenInstanceID = screenInstanceID;
646
   return await ScreenUtils.getCurrentlyVisibleScreenId();
652
   return await ScreenUtils.getCurrentlyVisibleScreenId();
647
 }
653
 }
648
 
654
 
649
-function _processNavigatorButtons({rightButtons, leftButtons}) {
655
+function _saveNavBarComponentProps(navigatorStyle) {
656
+  if (navigatorStyle.navBarCustomViewInitialProps) {
657
+    const passPropsKey = _.uniqueId('navBarComponent');
658
+    PropRegistry.save(passPropsKey, navigatorStyle.navBarCustomViewInitialProps);
659
+    navigatorStyle.navBarCustomViewInitialProps = {passPropsKey};
660
+  }
661
+}
662
+
663
+function _saveNavigatorButtonsProps({rightButtons, leftButtons}) {
650
   _saveNavigatorButtonsPassProps(rightButtons);
664
   _saveNavigatorButtonsPassProps(rightButtons);
651
   _saveNavigatorButtonsPassProps(leftButtons);
665
   _saveNavigatorButtonsPassProps(leftButtons);
652
 }
666
 }