react-native-navigation的迁移库

PushedScreen.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. const _ = require('lodash');
  2. const React = require('react');
  3. const { Component } = require('react');
  4. const { View, Text, Platform } = require('react-native');
  5. const { Navigation } = require('react-native-navigation');
  6. const Button = require('./Button');
  7. const testIDs = require('../testIDs');
  8. class PushedScreen extends Component {
  9. static get options() {
  10. return {
  11. _statusBar: {
  12. visible: false,
  13. drawBehind: true
  14. },
  15. topBar: {
  16. testID: testIDs.TOP_BAR_ELEMENT,
  17. rightButtons: {
  18. id: 'singleBtn',
  19. text: 'single',
  20. testID: testIDs.TOP_BAR_BUTTON
  21. },
  22. rightButtonColor: 'red',
  23. },
  24. layout: {
  25. backgroundColor: '#f5fcff'
  26. }
  27. };
  28. }
  29. constructor(props) {
  30. super(props);
  31. if (this.props.simulateLongRunningTask) {
  32. this.simulateLongRunningTask();
  33. }
  34. this.onClickPush = this.onClickPush.bind(this);
  35. this.onClickPop = this.onClickPop.bind(this);
  36. this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
  37. this.onClickPopToFirstPosition = this.onClickPopToFirstPosition.bind(this);
  38. this.onClickPopToRoot = this.onClickPopToRoot.bind(this);
  39. this.onClickSetStackRoot = this.onClickSetStackRoot.bind(this);
  40. this.state = { disabled: false };
  41. }
  42. simulateLongRunningTask() {
  43. // tslint:disable-next-line
  44. for (let i = 0; i < Math.pow(2, 25); i++);
  45. }
  46. listeners = [];
  47. componentDidMount() {
  48. this.listeners.push(
  49. Navigation.events().registerComponentDidAppearListener((event) => {
  50. if (this.state.previewComponentId === event.componentId) {
  51. this.setState({ disabled: event.type === 'ComponentDidAppear' });
  52. }
  53. })
  54. );
  55. if (Platform.OS === 'ios') {
  56. // this.listeners.push(
  57. // Navigation.events().registerNativeEventListener((name, params) => {
  58. // if (name === 'previewContext') {
  59. // const { previewComponentId } = params;
  60. // this.setState({ previewComponentId });
  61. // }
  62. // })
  63. // );
  64. }
  65. }
  66. componentWillUnmount() {
  67. this.listeners.forEach(listener => listener.remove && listener.remove());
  68. }
  69. render() {
  70. const stackPosition = this.getStackPosition();
  71. return (
  72. <View style={styles.root}>
  73. <Text testID={testIDs.PUSHED_SCREEN_HEADER} style={styles.h1}>{`Pushed Screen`}</Text>
  74. <Text style={styles.h2}>{`Stack Position: ${stackPosition}`}</Text>
  75. <Button title='Push' testID={testIDs.PUSH_BUTTON} onPress={this.onClickPush} />
  76. {Platform.OS === 'ios' && (
  77. <Navigation.Element elementId='PreviewElement'>
  78. <Button testID={testIDs.SHOW_PREVIEW_BUTTON} onPress={this.onClickPush} onPressIn={this.onClickShowPreview} title='Push Preview' />
  79. </Navigation.Element>
  80. )}
  81. <Button title='Pop' testID={testIDs.POP_BUTTON} onPress={this.onClickPop} />
  82. <Button title='Pop Previous' testID={testIDs.POP_PREVIOUS_BUTTON} onPress={this.onClickPopPrevious} />
  83. <Button title='Pop To Root' testID={testIDs.POP_TO_ROOT} onPress={this.onClickPopToRoot} />
  84. <Button title='Set Stack Root' testID={testIDs.SET_STACK_ROOT_BUTTON} onPress={this.onClickSetStackRoot} />
  85. <Button title='Push and Wait for Render' testID={testIDs.PUSH_BUTTON_WAIT_FOR_RENDER} onPress={this.onClickPushWaitForRender} />
  86. {stackPosition > 2 && <Button title='Pop To Stack Position 1' testID={testIDs.POP_STACK_POSITION_ONE_BUTTON} onPress={this.onClickPopToFirstPosition} />}
  87. <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
  88. </View>
  89. );
  90. }
  91. onClickShowPreview = async () => {
  92. await Navigation.push(this.props.componentId, {
  93. component: {
  94. name: 'navigation.playground.PushedScreen',
  95. passProps: {
  96. stackPosition: this.getStackPosition() + 1,
  97. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  98. },
  99. options: {
  100. topBar: {
  101. title: {
  102. text: `Pushed ${this.getStackPosition() + 1}`
  103. }
  104. },
  105. animations: {
  106. push: {
  107. enable: false
  108. }
  109. },
  110. preview: {
  111. elementId: 'PreviewElement',
  112. height: 400,
  113. commit: true,
  114. actions: [{
  115. id: 'action-cancel',
  116. title: 'Cancel'
  117. }]
  118. }
  119. }
  120. }
  121. });
  122. }
  123. async onClickPush() {
  124. if (this.state.disabled) {
  125. return;
  126. }
  127. await Navigation.push(this.props.componentId, {
  128. component: {
  129. name: 'navigation.playground.PushedScreen',
  130. passProps: {
  131. stackPosition: this.getStackPosition() + 1,
  132. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  133. },
  134. options: {
  135. topBar: {
  136. title: {
  137. text: `Pushed ${this.getStackPosition() + 1}`
  138. }
  139. }
  140. }
  141. }
  142. });
  143. }
  144. async onClickPop() {
  145. await Navigation.pop(this.props.componentId);
  146. }
  147. async onClickPopPrevious() {
  148. await Navigation.pop(_.last(this.props.previousScreenIds));
  149. }
  150. async onClickPopToFirstPosition() {
  151. await Navigation.popTo(this.props.previousScreenIds[0]);
  152. }
  153. async onClickPopToRoot() {
  154. await Navigation.popToRoot(this.props.componentId);
  155. }
  156. async onClickSetStackRoot() {
  157. await Navigation.setStackRoot(this.props.componentId, {
  158. component: {
  159. name: 'navigation.playground.PushedScreen',
  160. passProps: {
  161. stackPosition: this.getStackPosition() + 1,
  162. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  163. },
  164. options: {
  165. animations: {
  166. setStackRoot: {
  167. enable: false
  168. }
  169. },
  170. topBar: {
  171. title: {
  172. text: `Pushed ${this.getStackPosition() + 1}`
  173. }
  174. }
  175. }
  176. }
  177. });
  178. }
  179. onClickPushWaitForRender = async () => {
  180. await Navigation.push(this.props.componentId, {
  181. component: {
  182. name: 'navigation.playground.PushedScreen',
  183. passProps: {
  184. stackPosition: this.getStackPosition() + 1,
  185. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId),
  186. simulateLongRunningTask: true
  187. },
  188. options: {
  189. layout: {
  190. backgroundColor: 'transparent'
  191. },
  192. topBar: {
  193. title: {
  194. text: `Pushed ${this.getStackPosition() + 1}`
  195. }
  196. },
  197. animations: {
  198. push: {
  199. waitForRender: true
  200. }
  201. }
  202. }
  203. }
  204. });
  205. }
  206. getStackPosition() {
  207. return this.props.stackPosition || 1;
  208. }
  209. }
  210. const styles = {
  211. root: {
  212. flexGrow: 1,
  213. justifyContent: 'center',
  214. alignItems: 'center',
  215. backgroundColor: '#f5fcff'
  216. },
  217. h1: {
  218. fontSize: 24,
  219. textAlign: 'center',
  220. margin: 10
  221. },
  222. h2: {
  223. fontSize: 12,
  224. textAlign: 'center',
  225. margin: 10
  226. },
  227. footer: {
  228. fontSize: 10,
  229. color: '#888',
  230. marginTop: 10
  231. }
  232. };
  233. module.exports = PushedScreen;