react-native-navigation的迁移库

PushedScreen.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 PushedScreen extends Component {
  8. static get options() {
  9. return {
  10. topBar: {
  11. testID: testIDs.TOP_BAR_ELEMENT
  12. }
  13. };
  14. }
  15. constructor(props) {
  16. super(props);
  17. this.onClickPush = this.onClickPush.bind(this);
  18. this.onClickPop = this.onClickPop.bind(this);
  19. this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
  20. this.onClickPopToFirstPosition = this.onClickPopToFirstPosition.bind(this);
  21. this.onClickPopToRoot = this.onClickPopToRoot.bind(this);
  22. }
  23. render() {
  24. const stackPosition = this.getStackPosition();
  25. return (
  26. <View style={styles.root}>
  27. <Text testID={testIDs.PUSHED_SCREEN_HEADER} style={styles.h1}>{`Pushed Screen`}</Text>
  28. <Text style={styles.h2}>{`Stack Position: ${stackPosition}`}</Text>
  29. <Button title='Push' testID={testIDs.PUSH_BUTTON} onPress={this.onClickPush} />
  30. <Button title='Pop' testID={testIDs.POP_BUTTON} onPress={this.onClickPop} />
  31. <Button title='Pop Previous' testID={testIDs.POP_PREVIOUS_BUTTON} onPress={this.onClickPopPrevious} />
  32. <Button title='Pop To Root' testID={testIDs.POP_TO_ROOT} onPress={this.onClickPopToRoot} />
  33. {stackPosition > 2 && <Button title='Pop To Stack Position 1' testID={testIDs.POP_STACK_POSITION_ONE_BUTTON} onPress={this.onClickPopToFirstPosition} />}
  34. <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
  35. </View>
  36. );
  37. }
  38. async onClickPush() {
  39. await Navigation.push(this.props.componentId, {
  40. component: {
  41. name: 'navigation.playground.PushedScreen',
  42. passProps: {
  43. stackPosition: this.getStackPosition() + 1,
  44. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.componentId)
  45. },
  46. options: {
  47. topBar: {
  48. title: {
  49. text: `Pushed ${this.getStackPosition() + 1}`
  50. }
  51. }
  52. }
  53. }
  54. });
  55. }
  56. async onClickPop() {
  57. await Navigation.pop(this.props.componentId);
  58. }
  59. async onClickPopPrevious() {
  60. await Navigation.pop(_.last(this.props.previousScreenIds));
  61. }
  62. async onClickPopToFirstPosition() {
  63. await Navigation.popTo(this.props.previousScreenIds[0]);
  64. }
  65. async onClickPopToRoot() {
  66. await Navigation.popToRoot(this.props.componentId);
  67. }
  68. getStackPosition() {
  69. return this.props.stackPosition || 1;
  70. }
  71. }
  72. const styles = {
  73. root: {
  74. flexGrow: 1,
  75. justifyContent: 'center',
  76. alignItems: 'center',
  77. backgroundColor: '#f5fcff'
  78. },
  79. h1: {
  80. fontSize: 24,
  81. textAlign: 'center',
  82. margin: 10
  83. },
  84. h2: {
  85. fontSize: 12,
  86. textAlign: 'center',
  87. margin: 10
  88. },
  89. footer: {
  90. fontSize: 10,
  91. color: '#888',
  92. marginTop: 10
  93. }
  94. };
  95. module.exports = PushedScreen;