react-native-navigation的迁移库

Navigation.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*eslint-disable*/
  2. import React from 'react';
  3. import {AppRegistry} from 'react-native';
  4. import platformSpecific from './deprecated/platformSpecificDeprecated';
  5. import Screen from './Screen';
  6. import PropRegistry from './PropRegistry';
  7. const registeredScreens = {};
  8. const _allNavigatorEventHandlers = {};
  9. function registerScreen(screenID, generator) {
  10. registeredScreens[screenID] = generator;
  11. AppRegistry.registerComponent(screenID, generator);
  12. }
  13. function registerComponent(screenID, generator, store = undefined, Provider = undefined, options = {}) {
  14. if (store && Provider) {
  15. return _registerComponentRedux(screenID, generator, store, Provider, options);
  16. } else {
  17. return _registerComponentNoRedux(screenID, generator);
  18. }
  19. }
  20. function _registerComponentNoRedux(screenID, generator) {
  21. const generatorWrapper = function() {
  22. const InternalComponent = generator();
  23. if (!InternalComponent) {
  24. console.error(`Navigation: ${screenID} registration result is 'undefined'`);
  25. }
  26. return class extends Screen {
  27. static navigatorStyle = InternalComponent.navigatorStyle || {};
  28. static navigatorButtons = InternalComponent.navigatorButtons || {};
  29. constructor(props) {
  30. super(props);
  31. this.state = {
  32. internalProps: {...props, ...PropRegistry.load(props.screenInstanceID)}
  33. }
  34. }
  35. componentWillReceiveProps(nextProps) {
  36. this.setState({
  37. internalProps: {...PropRegistry.load(this.props.screenInstanceID), ...nextProps}
  38. })
  39. }
  40. render() {
  41. return (
  42. <InternalComponent testID={screenID} navigator={this.navigator} {...this.state.internalProps} />
  43. );
  44. }
  45. };
  46. };
  47. registerScreen(screenID, generatorWrapper);
  48. return generatorWrapper;
  49. }
  50. function _registerComponentRedux(screenID, generator, store, Provider, options) {
  51. const generatorWrapper = function() {
  52. const InternalComponent = generator();
  53. return class extends Screen {
  54. static navigatorStyle = InternalComponent.navigatorStyle || {};
  55. static navigatorButtons = InternalComponent.navigatorButtons || {};
  56. constructor(props) {
  57. super(props);
  58. this.state = {
  59. internalProps: {...props, ...PropRegistry.load(props.screenInstanceID)}
  60. }
  61. }
  62. componentWillReceiveProps(nextProps) {
  63. this.setState({
  64. internalProps: {...PropRegistry.load(this.props.screenInstanceID), ...nextProps}
  65. })
  66. }
  67. render() {
  68. return (
  69. <Provider store={store} {...options}>
  70. <InternalComponent testID={screenID} navigator={this.navigator} {...this.state.internalProps} />
  71. </Provider>
  72. );
  73. }
  74. };
  75. };
  76. registerScreen(screenID, generatorWrapper);
  77. return generatorWrapper;
  78. }
  79. function getRegisteredScreen(screenID) {
  80. const generator = registeredScreens[screenID];
  81. if (!generator) {
  82. console.error(`Navigation.getRegisteredScreen: ${screenID} used but not yet registered`);
  83. return undefined;
  84. }
  85. return generator();
  86. }
  87. function showModal(params = {}) {
  88. return platformSpecific.showModal(params);
  89. }
  90. function dismissModal(params = {}) {
  91. return platformSpecific.dismissModal(params);
  92. }
  93. function dismissAllModals(params = {}) {
  94. return platformSpecific.dismissAllModals(params);
  95. }
  96. function showSnackbar(params = {}) {
  97. return platformSpecific.showSnackbar(params);
  98. }
  99. function showLightBox(params = {}) {
  100. return platformSpecific.showLightBox(params);
  101. }
  102. function dismissLightBox(params = {}) {
  103. return platformSpecific.dismissLightBox(params);
  104. }
  105. function showInAppNotification(params = {}) {
  106. return platformSpecific.showInAppNotification(params);
  107. }
  108. function dismissInAppNotification(params = {}) {
  109. return platformSpecific.dismissInAppNotification(params);
  110. }
  111. function startTabBasedApp(params) {
  112. return platformSpecific.startTabBasedApp(params);
  113. }
  114. function startSingleScreenApp(params) {
  115. return platformSpecific.startSingleScreenApp(params);
  116. }
  117. function setEventHandler(navigatorEventID, eventHandler) {
  118. _allNavigatorEventHandlers[navigatorEventID] = eventHandler;
  119. }
  120. function clearEventHandler(navigatorEventID) {
  121. delete _allNavigatorEventHandlers[navigatorEventID];
  122. }
  123. function handleDeepLink(params = {}) {
  124. const { link, payload } = params;
  125. if (!link) return;
  126. const event = {
  127. type: 'DeepLink',
  128. link,
  129. ...(payload ? { payload } : {})
  130. };
  131. for (let i in _allNavigatorEventHandlers) {
  132. _allNavigatorEventHandlers[i](event);
  133. }
  134. }
  135. export default {
  136. getRegisteredScreen,
  137. registerComponent,
  138. showModal: showModal,
  139. dismissModal: dismissModal,
  140. dismissAllModals: dismissAllModals,
  141. showSnackbar: showSnackbar,
  142. showLightBox: showLightBox,
  143. dismissLightBox: dismissLightBox,
  144. showInAppNotification: showInAppNotification,
  145. dismissInAppNotification: dismissInAppNotification,
  146. startTabBasedApp: startTabBasedApp,
  147. startSingleScreenApp: startSingleScreenApp,
  148. setEventHandler: setEventHandler,
  149. clearEventHandler: clearEventHandler,
  150. handleDeepLink: handleDeepLink
  151. };