react-native-navigation的迁移库

PushedScreen.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. noBorder: true
  24. },
  25. bottomTabs: {
  26. visible: false
  27. }
  28. };
  29. }
  30. constructor(props) {
  31. super(props);
  32. if (this.props.simulateLongRunningTask) {
  33. this.simulateLongRunningTask();
  34. }
  35. this.onClickPush = this.onClickPush.bind(this);
  36. this.onClickPushBottomTabs = this.onClickPushBottomTabs.bind(this);
  37. this.onClickPop = this.onClickPop.bind(this);
  38. this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
  39. this.onClickPopToFirstPosition = this.onClickPopToFirstPosition.bind(this);
  40. this.onClickPopToRoot = this.onClickPopToRoot.bind(this);
  41. this.onClickSetStackRoot = this.onClickSetStackRoot.bind(this);
  42. this.state = {disabled: false};
  43. }
  44. simulateLongRunningTask() {
  45. // tslint:disable-next-line
  46. for (let i = 0; i < Math.pow(2, 25); i++);
  47. }
  48. listeners = [];
  49. componentDidMount() {
  50. this.listeners.push(
  51. Navigation.events().registerComponentDidAppearListener((event) => {
  52. if (this.state.previewComponentId === event.componentId) {
  53. this.setState({disabled: event.type === 'ComponentDidAppear'});
  54. }
  55. })
  56. );
  57. if (Platform.OS === 'ios') {
  58. // this.listeners.push(
  59. // Navigation.events().registerNativeEventListener((name, params) => {
  60. // if (name === 'previewContext') {
  61. // const { previewComponentId } = params;
  62. // this.setState({ previewComponentId });
  63. // }
  64. // })
  65. // );
  66. }
  67. }
  68. componentWillUnmount() {
  69. this.listeners.forEach(listener => listener.remove && listener.remove());
  70. }
  71. render() {
  72. const stackPosition = this.getStackPosition();
  73. return (
  74. <View style={styles.root}>
  75. <Text testID={testIDs.PUSHED_SCREEN_HEADER} style={styles.h1}>{`Pushed Screen`}</Text>
  76. <Text style={styles.h2}>{`Stack Position: ${stackPosition}`}</Text>
  77. <Button title='Push' testID={testIDs.PUSH_BUTTON} onPress={this.onClickPush} />
  78. <Button title='Push bottomTabs' testID={testIDs.PUSH_BOTTOM_TABS_BUTTON} onPress={this.onClickPushBottomTabs} />
  79. {Platform.OS === 'ios' && <Button testID={testIDs.SHOW_PREVIEW_BUTTON} onPress={this.onClickPush} onPressIn={this.onClickShowPreview} title='Push Preview' />}
  80. <Button title='Pop' testID={testIDs.POP_BUTTON} onPress={this.onClickPop} />
  81. <Button title='Pop Previous' testID={testIDs.POP_PREVIOUS_BUTTON} onPress={this.onClickPopPrevious} />
  82. <Button title='Pop To Root' testID={testIDs.POP_TO_ROOT} onPress={this.onClickPopToRoot} />
  83. <Button title='Set Stack Root' testID={testIDs.SET_STACK_ROOT_BUTTON} onPress={this.onClickSetStackRoot} />
  84. <Button title='Push and Wait for Render' testID={testIDs.PUSH_BUTTON_WAIT_FOR_RENDER} onPress={this.onClickPushWaitForRender} />
  85. {stackPosition > 2 && <Button title='Pop To Stack Position 1' testID={testIDs.POP_STACK_POSITION_ONE_BUTTON} onPress={this.onClickPopToFirstPosition} />}
  86. <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
  87. </View>
  88. );
  89. }
  90. onClickShowPreview = async ({reactTag}) => {
  91. await Navigation.push(this.props.componentId, {
  92. component: {
  93. name: 'navigation.playground.PushedScreen',
  94. passProps: {
  95. stackPosition: this.getStackPosition() + 1,
  96. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  97. },
  98. options: {
  99. topBar: {
  100. title: {
  101. text: `Pushed ${this.getStackPosition() + 1}`
  102. }
  103. },
  104. animations: {
  105. push: {
  106. enable: false
  107. }
  108. },
  109. preview: {
  110. reactTag,
  111. height: 400,
  112. commit: true,
  113. actions: [{
  114. id: 'action-cancel',
  115. title: 'Cancel'
  116. }]
  117. }
  118. }
  119. }
  120. });
  121. }
  122. async onClickPush() {
  123. if (this.state.disabled) {
  124. return;
  125. }
  126. await Navigation.push(this.props.componentId, {
  127. component: {
  128. name: 'navigation.playground.PushedScreen',
  129. passProps: {
  130. stackPosition: this.getStackPosition() + 1,
  131. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  132. },
  133. options: {
  134. topBar: {
  135. title: {
  136. text: `Pushed ${this.getStackPosition() + 1}`
  137. }
  138. }
  139. }
  140. }
  141. });
  142. }
  143. async onClickPop() {
  144. await Navigation.pop(this.props.componentId);
  145. }
  146. async onClickPopPrevious() {
  147. await Navigation.pop(_.last(this.props.previousScreenIds));
  148. }
  149. async onClickPopToFirstPosition() {
  150. await Navigation.popTo(this.props.previousScreenIds[0]);
  151. }
  152. async onClickPopToRoot() {
  153. await Navigation.popToRoot(this.props.componentId);
  154. }
  155. async onClickSetStackRoot() {
  156. await Navigation.setStackRoot(this.props.componentId, {
  157. component: {
  158. name: 'navigation.playground.PushedScreen',
  159. passProps: {
  160. stackPosition: this.getStackPosition() + 1,
  161. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  162. },
  163. options: {
  164. animations: {
  165. setStackRoot: {
  166. enable: false
  167. }
  168. },
  169. topBar: {
  170. title: {
  171. text: `Pushed ${this.getStackPosition() + 1}`
  172. }
  173. }
  174. }
  175. }
  176. });
  177. }
  178. onClickPushWaitForRender = async () => {
  179. await Navigation.push(this.props.componentId, {
  180. component: {
  181. name: 'navigation.playground.PushedScreen',
  182. passProps: {
  183. stackPosition: this.getStackPosition() + 1,
  184. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId),
  185. simulateLongRunningTask: true
  186. },
  187. options: {
  188. layout: {
  189. backgroundColor: 'transparent'
  190. },
  191. topBar: {
  192. title: {
  193. text: `Pushed ${this.getStackPosition() + 1}`
  194. }
  195. },
  196. animations: {
  197. push: {
  198. waitForRender: true
  199. }
  200. }
  201. }
  202. }
  203. });
  204. }
  205. async onClickPushBottomTabs() {
  206. await Navigation.push(this.props.componentId, {
  207. bottomTabs: {
  208. id: 'BottomTabs',
  209. children: [
  210. {
  211. stack: {
  212. id: 'TAB1_ID',
  213. children: [
  214. {
  215. component: {
  216. name: 'navigation.playground.TextScreen',
  217. passProps: {
  218. text: 'This is tab 1',
  219. myFunction: () => 'Hello from a function!'
  220. },
  221. options: {
  222. topBar: {
  223. visible: true,
  224. animate: false,
  225. title: {
  226. text: 'React Native Navigation!'
  227. }
  228. },
  229. bottomTab: {
  230. text: 'Tab 1',
  231. icon: require('../images/one.png'),
  232. selectedIcon: require('../images/one.png'),
  233. testID: testIDs.FIRST_TAB_BAR_BUTTON
  234. }
  235. }
  236. }
  237. }
  238. ],
  239. options: {
  240. topBar: {
  241. visible: false
  242. }
  243. }
  244. }
  245. },
  246. {
  247. stack: {
  248. children: [
  249. {
  250. component: {
  251. name: 'navigation.playground.TextScreen',
  252. passProps: {
  253. text: 'This is tab 2'
  254. }
  255. }
  256. }
  257. ],
  258. options: {
  259. bottomTab: {
  260. text: 'Tab 2',
  261. icon: require('../images/two.png'),
  262. testID: testIDs.SECOND_TAB_BAR_BUTTON
  263. }
  264. }
  265. }
  266. },
  267. {
  268. component: {
  269. name: 'navigation.playground.TextScreen',
  270. passProps: {
  271. text: 'This is tab 3',
  272. myFunction: () => 'Hello from a function!'
  273. },
  274. options: {
  275. topBar: {
  276. visible: true,
  277. animate: false
  278. },
  279. bottomTab: {
  280. text: 'Tab 3',
  281. icon: require('../images/one.png'),
  282. selectedIcon: require('../images/one.png')
  283. }
  284. }
  285. }
  286. }
  287. ],
  288. options: {
  289. bottomTabs: {
  290. titleDisplayMode: 'alwaysShow',
  291. testID: testIDs.BOTTOM_TABS_ELEMENT
  292. }
  293. }
  294. }
  295. });
  296. }
  297. getStackPosition() {
  298. return this.props.stackPosition || 1;
  299. }
  300. }
  301. const styles = {
  302. root: {
  303. flexGrow: 1,
  304. justifyContent: 'center',
  305. alignItems: 'center',
  306. backgroundColor: '#f5fcff'
  307. },
  308. h1: {
  309. fontSize: 24,
  310. textAlign: 'center',
  311. margin: 10
  312. },
  313. h2: {
  314. fontSize: 12,
  315. textAlign: 'center',
  316. margin: 10
  317. },
  318. footer: {
  319. fontSize: 10,
  320. color: '#888',
  321. marginTop: 10
  322. }
  323. };
  324. module.exports = PushedScreen;