react-native-navigation的迁移库

ModalScreen.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. const _ = require('lodash');
  2. const React = require('react');
  3. const { Component } = require('react');
  4. const { View, Text, Button } = require('react-native');
  5. const { Navigation } = require('react-native-navigation');
  6. const testIDs = require('../testIDs');
  7. class ModalScreen extends Component {
  8. static get options() {
  9. return {
  10. statusBar: {
  11. visible: false,
  12. drawBehind: true,
  13. backgroundColor: 'transparent'
  14. },
  15. layout: {
  16. orientation: ['portrait'],
  17. backgroundColor: '#f5fcff'
  18. },
  19. _animations: {
  20. showModal: {
  21. waitForRender: true
  22. }
  23. }
  24. };
  25. }
  26. constructor(props) {
  27. super(props);
  28. // this.simulateLongRunningTask();
  29. this.onClickShowModal = this.onClickShowModal.bind(this);
  30. this.onClickDismissModal = this.onClickDismissModal.bind(this);
  31. this.onClickDismissPreviousModal = this.onClickDismissPreviousModal.bind(this);
  32. this.onClickDismissUnknownModal = this.onClickDismissUnknownModal.bind(this);
  33. this.onClickDismissAllPreviousModals = this.onClickDismissAllPreviousModals.bind(this);
  34. this.onClickDismissFirstInStack = this.onClickDismissFirstInStack.bind(this);
  35. this.onClickDismissAllModals = this.onClickDismissAllModals.bind(this);
  36. this.onClickPushScreen = this.onClickPushScreen.bind(this);
  37. this.onShowModalWithDeepStack = this.onShowModalWithDeepStack.bind(this);
  38. this.onClickModalLifecycle = this.onClickModalLifecycle.bind(this);
  39. }
  40. simulateLongRunningTask = () => {
  41. // tslint:disable-next-line
  42. for (let i = 0; i < Math.pow(2, 25); i++);
  43. }
  44. render() {
  45. return (
  46. <View style={styles.root}>
  47. <Text style={styles.h1} testID={testIDs.MODAL_SCREEN}>{`Modal Screen`}</Text>
  48. <Text style={styles.footer}>{`Modal Stack Position: ${this.getModalPosition()}`}</Text>
  49. <Button title='Show Modal' testID={testIDs.SHOW_MODAL_BUTTON} onPress={this.onClickShowModal} />
  50. <Button title='Dismiss Modal' testID={testIDs.DISMISS_MODAL_BUTTON} onPress={this.onClickDismissModal} />
  51. <Button title='Dismiss Unknown Modal' testID={testIDs.DISMISS_UNKNOWN_MODAL_BUTTON} onPress={this.onClickDismissUnknownModal} />
  52. <Button title='Dismiss All Modals' testID={testIDs.DISMISS_ALL_MODALS_BUTTON} onPress={this.onClickDismissAllModals} />
  53. <Button title='Test Modal Lifecycle' testID={testIDs.MODAL_LIFECYCLE_BUTTON} onPress={this.onClickModalLifecycle} />
  54. <Button title='Push screen' testID={testIDs.PUSH_BUTTON} onPress={this.onClickPushScreen} />
  55. <Button title='Show Modal With Stack' testID={testIDs.MODAL_WITH_STACK_BUTTON} onPress={this.onShowModalWithDeepStack} />
  56. {this.getPreviousModalId() ? (<Button title='Dismiss Previous Modal' testID={testIDs.DISMISS_PREVIOUS_MODAL_BUTTON}
  57. onPress={this.onClickDismissPreviousModal} />) : undefined}
  58. {this.props.previousModalIds ? (<Button title='Dismiss ALL Previous Modals' testID={testIDs.DISMISS_ALL_PREVIOUS_MODAL_BUTTON}
  59. onPress={this.onClickDismissAllPreviousModals} />) : undefined}
  60. {this.props.previousModalIds ? (<Button title='Dismiss First In Stack' testID={testIDs.DISMISS_FIRST_MODAL_BUTTON}
  61. onPress={this.onClickDismissFirstInStack} />) : undefined}
  62. <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
  63. </View>
  64. );
  65. }
  66. onClickShowModal() {
  67. Navigation.showModal({
  68. component: {
  69. name: 'navigation.playground.ModalScreen',
  70. passProps: {
  71. modalPosition: this.getModalPosition() + 1,
  72. previousModalIds: _.concat([], this.props.previousModalIds || [], this.props.componentId)
  73. },
  74. options: {
  75. animated: false
  76. }
  77. }
  78. });
  79. }
  80. async onClickDismissModal() {
  81. await Navigation.dismissModal(this.props.componentId);
  82. }
  83. onClickDismissPreviousModal() {
  84. Navigation.dismissModal(this.getPreviousModalId());
  85. }
  86. onClickDismissUnknownModal() {
  87. Navigation.dismissModal('unknown');
  88. }
  89. onClickDismissAllPreviousModals() {
  90. _.forEach(this.props.previousModalIds, (id) => Navigation.dismissModal(id));
  91. }
  92. onClickDismissFirstInStack() {
  93. Navigation.dismissModal(_.head(this.props.previousModalIds));
  94. }
  95. onClickDismissAllModals() {
  96. Navigation.dismissAllModals();
  97. }
  98. onClickModalLifecycle() {
  99. Navigation.showModal({
  100. component: {
  101. name: 'navigation.playground.LifecycleScreen',
  102. passProps: {
  103. isModal: true,
  104. },
  105. options: {
  106. animated: false
  107. }
  108. }
  109. });
  110. }
  111. onClickPushScreen() {
  112. Navigation.push(this.props.componentId, {
  113. component: {
  114. name: `navigation.playground.PushedScreen`,
  115. passProps: {
  116. text: 'Pushed from modal'
  117. }
  118. }
  119. });
  120. }
  121. onShowModalWithDeepStack() {
  122. Navigation.showModal({
  123. stack: {
  124. children: [
  125. {
  126. component: {
  127. name: `navigation.playground.TextScreen`,
  128. passProps: {
  129. text: 'Screen 1'
  130. }
  131. }
  132. },
  133. {
  134. component: {
  135. name: `navigation.playground.TextScreen`,
  136. passProps: {
  137. text: 'Screen 2'
  138. }
  139. }
  140. }
  141. ]
  142. }
  143. });
  144. }
  145. getModalPosition() {
  146. return (this.props.modalPosition || 1);
  147. }
  148. getPreviousModalId() {
  149. return _.last(this.props.previousModalIds);
  150. }
  151. }
  152. const styles = {
  153. root: {
  154. flexGrow: 1,
  155. justifyContent: 'center',
  156. alignItems: 'center',
  157. backgroundColor: '#f5fcff'
  158. },
  159. h1: {
  160. fontSize: 24,
  161. textAlign: 'center',
  162. margin: 10
  163. },
  164. h2: {
  165. fontSize: 12,
  166. textAlign: 'center',
  167. margin: 10
  168. },
  169. footer: {
  170. fontSize: 10,
  171. color: '#888',
  172. marginTop: 10
  173. }
  174. };
  175. module.exports = ModalScreen;