react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const concat = require('lodash/concat');
  2. const React = require('react');
  3. const { BackHandler } = require('react-native');
  4. const Navigation = require('../services/Navigation');
  5. const Root = require('../components/Root');
  6. const Button = require('../components/Button');
  7. const {
  8. PUSHED_SCREEN_HEADER,
  9. TOP_BAR_BTN,
  10. PUSH_BTN,
  11. POP_BTN,
  12. PUSH_NO_ANIM_BTN,
  13. POP_TO_FIRST_SCREEN_BTN,
  14. POP_TO_ROOT_BTN,
  15. ADD_BACK_HANDLER,
  16. REMOVE_BACK_HANDLER,
  17. SET_STACK_ROOT_BUTTON,
  18. PUSH_OPTIONS_BUTTON,
  19. HIDE_PREVIOUS_SCREEN_TOP_BAR
  20. } = require('../testIDs');
  21. const Screens = require('./Screens');
  22. class PushedScreen extends React.Component {
  23. static options() {
  24. return {
  25. topBar: {
  26. testID: PUSHED_SCREEN_HEADER,
  27. title: {
  28. text: 'Pushed Screen'
  29. },
  30. rightButtons: {
  31. id: 'singleBtn',
  32. text: 'single',
  33. testID: TOP_BAR_BTN
  34. }
  35. }
  36. };
  37. }
  38. constructor(props) {
  39. super(props);
  40. Navigation.events().bindComponent(this);
  41. }
  42. navigationButtonPressed({ buttonId }) {
  43. if (buttonId === 'backPress') alert('back button clicked')
  44. }
  45. render() {
  46. const stackPosition = this.getStackPosition();
  47. return (
  48. <Root componentId={this.props.componentId} footer={`Stack Position: ${stackPosition}`}>
  49. <Button label='Push' testID={PUSH_BTN} onPress={this.push} />
  50. <Button label='Pop' testID={POP_BTN} onPress={this.pop} />
  51. <Button label='Push Without Animation' testID={PUSH_NO_ANIM_BTN} onPress={this.pushWithoutAnimations} />
  52. {stackPosition > 2 && <Button label='Pop to First Screen' testID={POP_TO_FIRST_SCREEN_BTN} onPress={this.popToFirstScreen} />}
  53. <Button label='Pop to Root' testID={POP_TO_ROOT_BTN} onPress={this.popToRoot} />
  54. <Button label='Add BackHandler' testID={ADD_BACK_HANDLER} onPress={this.addBackHandler} />
  55. <Button label='Remove BackHandler' testID={REMOVE_BACK_HANDLER} onPress={this.removeBackHandler} />
  56. <Button label='Set Stack Root' testID={SET_STACK_ROOT_BUTTON} onPress={this.setStackRoot} />
  57. <Button label='Push Options Screen' testID={PUSH_OPTIONS_BUTTON} onPress={this.pushOptionsScreen} />
  58. <Button label='Hide previous screen top bar' testID={HIDE_PREVIOUS_SCREEN_TOP_BAR} onPress={this.hidePreviousScreenTopBar} />
  59. </Root>
  60. );
  61. }
  62. push = () => Navigation.push(this, {
  63. component: {
  64. name: Screens.Pushed,
  65. passProps: this.createPassProps(),
  66. options: {
  67. topBar: {
  68. title: {
  69. text: `Pushed ${this.getStackPosition() + 1}`
  70. }
  71. }
  72. }
  73. }
  74. });
  75. pop = () => Navigation.pop(this);
  76. pushWithoutAnimations = () => Navigation.push(this, {
  77. component: {
  78. name: Screens.Pushed,
  79. passProps: this.createPassProps(),
  80. options: {
  81. animations: {
  82. push: { enabled: false },
  83. pop: { enabled: false }
  84. }
  85. }
  86. }
  87. });
  88. pushOptionsScreen = () => Navigation.push(this, {
  89. component: {
  90. name: Screens.Options
  91. }
  92. });
  93. popToFirstScreen = () => Navigation.popTo(this.props.previousScreenIds[0]);
  94. popToRoot = () => Navigation.popToRoot(this);
  95. hidePreviousScreenTopBar = () => Navigation.mergeOptions(this.props.previousScreenIds[this.getStackPosition() - 1], {
  96. topBar: {
  97. visible: false
  98. }
  99. });
  100. setStackRoot = () => Navigation.setStackRoot(this, [
  101. {
  102. component: {
  103. name: Screens.Pushed,
  104. passProps: {
  105. stackPosition: this.getStackPosition() + 1,
  106. previousScreenIds: concat([], this.props.previousScreenIds || [], this.props.componentId)
  107. },
  108. options: {
  109. animations: {
  110. setStackRoot: {
  111. enabled: false
  112. }
  113. },
  114. topBar: {
  115. title: {
  116. text: `Pushed ${this.getStackPosition() + 1} a`
  117. }
  118. }
  119. }
  120. }
  121. },
  122. {
  123. component: {
  124. name: Screens.Pushed,
  125. passProps: {
  126. stackPosition: this.getStackPosition() + 1,
  127. previousScreenIds: concat([], this.props.previousScreenIds || [], this.props.componentId)
  128. },
  129. options: {
  130. animations: {
  131. setStackRoot: {
  132. enabled: false
  133. }
  134. },
  135. topBar: {
  136. title: {
  137. text: `Pushed ${this.getStackPosition() + 1} b`
  138. }
  139. }
  140. }
  141. }
  142. }
  143. ]);
  144. addBackHandler = () => BackHandler.addEventListener('hardwareBackPress', this.backHandler);
  145. removeBackHandler = () => BackHandler.removeEventListener('hardwareBackPress', this.backHandler);
  146. backHandler = () => {
  147. this.setState({
  148. backPress: 'Back button pressed!'
  149. });
  150. return true;
  151. };
  152. createPassProps = () => {
  153. return {
  154. stackPosition: this.getStackPosition() + 1,
  155. previousScreenIds: concat([], this.props.previousScreenIds || [], this.props.componentId)
  156. }
  157. };
  158. getStackPosition = () => this.props.stackPosition || 1;
  159. }
  160. module.exports = PushedScreen;