react-native-navigation的迁移库

PushedScreen.js 4.4KB

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