react-native-navigation的迁移库

PushedScreen.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. }
  18. };
  19. }
  20. constructor(props) {
  21. super(props);
  22. this.onClickPush = this.onClickPush.bind(this);
  23. this.onClickPop = this.onClickPop.bind(this);
  24. this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
  25. this.onClickPopToFirstPosition = this.onClickPopToFirstPosition.bind(this);
  26. this.onClickPopToRoot = this.onClickPopToRoot.bind(this);
  27. this.onClickSetStackRoot = this.onClickSetStackRoot.bind(this);
  28. this.state = { disabled: false };
  29. }
  30. listeners = [];
  31. componentDidMount() {
  32. this.listeners.push(
  33. Navigation.events().registerNativeEventListener((name, params) => {
  34. if (name === 'previewContext') {
  35. const { previewComponentId } = params;
  36. this.setState({ previewComponentId });
  37. }
  38. }),
  39. Navigation.events().registerComponentDidAppearListener((componentId, componentName) => {
  40. if (this.state.previewComponentId === componentId) {
  41. this.setState({ disabled: true });
  42. }
  43. }),
  44. Navigation.events().registerComponentDidDisappearListener((componentId, componentName) => {
  45. if (this.state.previewComponentId === componentId) {
  46. this.setState({ disabled: false });
  47. }
  48. })
  49. );
  50. }
  51. componentWillUnmount() {
  52. this.listeners.forEach(listener => listener.remove && listener.remove());
  53. }
  54. render() {
  55. const stackPosition = this.getStackPosition();
  56. return (
  57. <View style={styles.root}>
  58. <Text testID={testIDs.PUSHED_SCREEN_HEADER} style={styles.h1}>{`Pushed Screen`}</Text>
  59. <Text style={styles.h2}>{`Stack Position: ${stackPosition}`}</Text>
  60. <Button title='Push' testID={testIDs.PUSH_BUTTON} onPress={this.onClickPush} />
  61. {Platform.OS === 'ios' && (
  62. <Navigation.Element elementId='PreviewElement'>
  63. <Button testID={testIDs.SHOW_PREVIEW_BUTTON} onPress={this.onClickPush} onPressIn={this.onClickShowPreview} title='Push Preview' />
  64. </Navigation.Element>
  65. )}
  66. <Button title='Pop' testID={testIDs.POP_BUTTON} onPress={this.onClickPop} />
  67. <Button title='Pop Previous' testID={testIDs.POP_PREVIOUS_BUTTON} onPress={this.onClickPopPrevious} />
  68. <Button title='Pop To Root' testID={testIDs.POP_TO_ROOT} onPress={this.onClickPopToRoot} />
  69. <Button title='Set Stack Root' testID={testIDs.SET_STACK_ROOT_BUTTON} onPress={this.onClickSetStackRoot} />
  70. {stackPosition > 2 && <Button title='Pop To Stack Position 1' testID={testIDs.POP_STACK_POSITION_ONE_BUTTON} onPress={this.onClickPopToFirstPosition} />}
  71. <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
  72. </View>
  73. );
  74. }
  75. onClickShowPreview = async () => {
  76. await Navigation.push(this.props.componentId, {
  77. component: {
  78. name: 'navigation.playground.PushedScreen',
  79. passProps: {
  80. stackPosition: this.getStackPosition() + 1,
  81. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  82. },
  83. options: {
  84. topBar: {
  85. title: {
  86. text: `Pushed ${this.getStackPosition() + 1}`
  87. }
  88. },
  89. animations: {
  90. push: {
  91. enable: false
  92. }
  93. },
  94. preview: {
  95. elementId: 'PreviewElement',
  96. height: 400,
  97. commit: true,
  98. actions: [{
  99. id: 'action-cancel',
  100. title: 'Cancel'
  101. }]
  102. }
  103. }
  104. }
  105. });
  106. }
  107. async onClickPush() {
  108. if (this.state.disabled) {
  109. return;
  110. }
  111. await Navigation.push(this.props.componentId, {
  112. component: {
  113. name: 'navigation.playground.PushedScreen',
  114. passProps: {
  115. stackPosition: this.getStackPosition() + 1,
  116. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  117. },
  118. options: {
  119. topBar: {
  120. title: {
  121. text: `Pushed ${this.getStackPosition() + 1}`
  122. }
  123. }
  124. }
  125. }
  126. });
  127. }
  128. async onClickPop() {
  129. await Navigation.pop(this.props.componentId);
  130. }
  131. async onClickPopPrevious() {
  132. await Navigation.pop(_.last(this.props.previousScreenIds));
  133. }
  134. async onClickPopToFirstPosition() {
  135. await Navigation.popTo(this.props.previousScreenIds[0]);
  136. }
  137. async onClickPopToRoot() {
  138. await Navigation.popToRoot(this.props.componentId);
  139. }
  140. async onClickSetStackRoot() {
  141. await Navigation.setStackRoot(this.props.componentId, {
  142. component: {
  143. name: 'navigation.playground.PushedScreen',
  144. passProps: {
  145. stackPosition: this.getStackPosition() + 1,
  146. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  147. },
  148. options: {
  149. animated: true,
  150. topBar: {
  151. title: {
  152. text: `Pushed ${this.getStackPosition() + 1}`
  153. }
  154. }
  155. }
  156. }
  157. });
  158. }
  159. getStackPosition() {
  160. return this.props.stackPosition || 1;
  161. }
  162. }
  163. const styles = {
  164. root: {
  165. flexGrow: 1,
  166. justifyContent: 'center',
  167. alignItems: 'center',
  168. backgroundColor: '#f5fcff'
  169. },
  170. h1: {
  171. fontSize: 24,
  172. textAlign: 'center',
  173. margin: 10
  174. },
  175. h2: {
  176. fontSize: 12,
  177. textAlign: 'center',
  178. margin: 10
  179. },
  180. footer: {
  181. fontSize: 10,
  182. color: '#888',
  183. marginTop: 10
  184. }
  185. };
  186. module.exports = PushedScreen;