react-native-navigation的迁移库

Navigation.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React from 'react';
  2. import {AppRegistry} from 'react-native';
  3. import platformSpecific from './deprecated/platformSpecificDeprecated';
  4. import Screen from './Screen';
  5. import PropRegistry from './PropRegistry';
  6. const registeredScreens = {};
  7. function registerScreen(screenID, generator) {
  8. registeredScreens[screenID] = generator;
  9. AppRegistry.registerComponent(screenID, generator);
  10. }
  11. function registerComponent(screenID, generator, store = undefined, Provider = undefined) {
  12. if (store && Provider) {
  13. return _registerComponentRedux(screenID, generator, store, Provider);
  14. } else {
  15. return _registerComponentNoRedux(screenID, generator);
  16. }
  17. }
  18. function _registerComponentNoRedux(screenID, generator) {
  19. const generatorWrapper = function() {
  20. const InternalComponent = generator();
  21. return class extends Screen {
  22. static navigatorStyle = InternalComponent.navigatorStyle || {};
  23. static navigatorButtons = InternalComponent.navigatorButtons || {};
  24. constructor(props) {
  25. super(props);
  26. this.allProps = {...props, ...PropRegistry.load(props.screenInstanceID)};
  27. }
  28. render() {
  29. return (
  30. <InternalComponent navigator={this.navigator} {...this.allProps} />
  31. );
  32. }
  33. };
  34. };
  35. registerScreen(screenID, generatorWrapper);
  36. return generatorWrapper;
  37. }
  38. function _registerComponentRedux(screenID, generator, store, Provider) {
  39. const generatorWrapper = function() {
  40. const InternalComponent = generator();
  41. return class extends Screen {
  42. static navigatorStyle = InternalComponent.navigatorStyle || {};
  43. static navigatorButtons = InternalComponent.navigatorButtons || {};
  44. constructor(props) {
  45. super(props);
  46. this.allProps = {...props, ...PropRegistry.load(props.screenInstanceID)};
  47. }
  48. render() {
  49. return (
  50. <Provider store={store}>
  51. <InternalComponent navigator={this.navigator} {...this.allProps} />
  52. </Provider>
  53. );
  54. }
  55. };
  56. };
  57. registerScreen(screenID, generatorWrapper);
  58. return generatorWrapper;
  59. }
  60. function getRegisteredScreen(screenID) {
  61. const generator = registeredScreens[screenID];
  62. if (!generator) {
  63. console.error(`Navigation.getRegisteredScreen: ${screenID} used but not yet registered`);
  64. return undefined;
  65. }
  66. return generator();
  67. }
  68. function showModal(params = {}) {
  69. return platformSpecific.showModal(params);
  70. }
  71. function dismissModal(params = {}) {
  72. return platformSpecific.dismissModal(params);
  73. }
  74. function dismissAllModals(params = {}) {
  75. return platformSpecific.dismissAllModals(params);
  76. }
  77. function showLightBox(params = {}) {
  78. return platformSpecific.showLightBox(params);
  79. }
  80. function dismissLightBox(params = {}) {
  81. return platformSpecific.dismissLightBox(params);
  82. }
  83. function showInAppNotification(params = {}) {
  84. return platformSpecific.showInAppNotification(params);
  85. }
  86. function dismissInAppNotification(params = {}) {
  87. return platformSpecific.dismissInAppNotification(params);
  88. }
  89. function startTabBasedApp(params) {
  90. return platformSpecific.startTabBasedApp(params);
  91. }
  92. function startSingleScreenApp(params) {
  93. return platformSpecific.startSingleScreenApp(params);
  94. }
  95. export default {
  96. getRegisteredScreen,
  97. registerComponent,
  98. showModal: showModal,
  99. dismissModal: dismissModal,
  100. dismissAllModals: dismissAllModals,
  101. showLightBox: showLightBox,
  102. dismissLightBox: dismissLightBox,
  103. showInAppNotification: showInAppNotification,
  104. dismissInAppNotification: dismissInAppNotification,
  105. startTabBasedApp: startTabBasedApp,
  106. startSingleScreenApp: startSingleScreenApp
  107. };