瀏覽代碼

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 年之前
父節點
當前提交
6170e70346
No account linked to committer's email address
共有 2 個文件被更改,包括 22 次插入8 次删除
  1. 1
    1
      docs/styling-the-navigator.md
  2. 21
    7
      src/deprecated/platformSpecificDeprecated.ios.js

+ 1
- 1
docs/styling-the-navigator.md 查看文件

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

+ 21
- 7
src/deprecated/platformSpecificDeprecated.ios.js 查看文件

@@ -31,7 +31,8 @@ function startTabBasedApp(params) {
31 31
       navigatorButtons,
32 32
       navigatorEventID
33 33
     } = _mergeScreenSpecificSettings(tab.screen, screenInstanceID, tab);
34
-    _processNavigatorButtons(navigatorButtons);
34
+    _saveNavigatorButtonsProps(navigatorButtons);
35
+    _saveNavBarComponentProps(navigatorStyle);
35 36
     tab.navigationParams = {
36 37
       screenInstanceID,
37 38
       navigatorStyle,
@@ -139,7 +140,8 @@ function startSingleScreenApp(params) {
139 140
     navigatorButtons,
140 141
     navigatorEventID
141 142
   } = _mergeScreenSpecificSettings(screen.screen, screenInstanceID, screen);
142
-  _processNavigatorButtons(navigatorButtons);
143
+  _saveNavigatorButtonsProps(navigatorButtons);
144
+  _saveNavBarComponentProps(navigatorStyle);
143 145
   params.navigationParams = {
144 146
     screenInstanceID,
145 147
     navigatorStyle,
@@ -238,7 +240,8 @@ function navigatorPush(navigator, params) {
238 240
     navigatorButtons,
239 241
     navigatorEventID
240 242
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
241
-  _processNavigatorButtons(navigatorButtons);
243
+  _saveNavigatorButtonsProps(navigatorButtons);
244
+  _saveNavBarComponentProps(navigatorStyle);
242 245
   const passProps = Object.assign({}, params.passProps);
243 246
   passProps.navigatorID = navigator.navigatorID;
244 247
   passProps.screenInstanceID = screenInstanceID;
@@ -297,7 +300,8 @@ function navigatorResetTo(navigator, params) {
297 300
     navigatorButtons,
298 301
     navigatorEventID
299 302
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
300
-  _processNavigatorButtons(navigatorButtons);
303
+  _saveNavigatorButtonsProps(navigatorButtons);
304
+  _saveNavBarComponentProps(navigatorStyle);
301 305
   const passProps = Object.assign({}, params.passProps);
302 306
   passProps.navigatorID = navigator.navigatorID;
303 307
   passProps.screenInstanceID = screenInstanceID;
@@ -366,6 +370,7 @@ function navigatorToggleNavBar(navigator, params) {
366 370
 }
367 371
 
368 372
 function navigatorSetStyle(navigator, params) {
373
+  _saveNavBarComponentProps(params);
369 374
   Controllers.NavigationControllerIOS(navigator.navigatorID).setStyle(params)
370 375
 }
371 376
 
@@ -447,7 +452,7 @@ function navigatorSwitchToTab(navigator, params) {
447 452
 }
448 453
 
449 454
 function navigatorSetButtons(navigator, navigatorEventID, params) {
450
-  _processNavigatorButtons(params);
455
+  _saveNavigatorButtonsProps(params);
451 456
   if (params.leftButtons) {
452 457
     const buttons = params.leftButtons.slice(); // clone
453 458
     for (let i = 0; i < buttons.length; i++) {
@@ -477,7 +482,8 @@ function showModal(params) {
477 482
     navigatorButtons,
478 483
     navigatorEventID
479 484
   } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
480
-  _processNavigatorButtons(navigatorButtons);
485
+  _saveNavigatorButtonsProps(navigatorButtons);
486
+  _saveNavBarComponentProps(navigatorStyle);
481 487
   const passProps = Object.assign({}, params.passProps);
482 488
   passProps.navigatorID = navigatorID;
483 489
   passProps.screenInstanceID = screenInstanceID;
@@ -646,7 +652,15 @@ async function getCurrentlyVisibleScreenId() {
646 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 664
   _saveNavigatorButtonsPassProps(rightButtons);
651 665
   _saveNavigatorButtonsPassProps(leftButtons);
652 666
 }