react-native-navigation的迁移库

TextScreen.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const React = require('react');
  2. const { Component } = require('react');
  3. const { View, Text, Button } = require('react-native');
  4. const { Navigation } = require('react-native-navigation');
  5. const testIDs = require('../testIDs');
  6. let globalFirstComponentID;
  7. class TextScreen extends Component {
  8. static get options() {
  9. return {
  10. bottomTabs: {
  11. testID: testIDs.BOTTOM_TABS_ELEMENT
  12. },
  13. topBar: {
  14. testID: testIDs.TOP_BAR_ELEMENT
  15. }
  16. };
  17. }
  18. constructor(props) {
  19. super(props);
  20. globalFirstComponentID = (props.text === 'This is tab 1') ? props.componentId : globalFirstComponentID;
  21. this.onClickPop = this.onClickPop.bind(this);
  22. }
  23. render() {
  24. return (
  25. <View style={styles.root}>
  26. <Text style={styles.h1} testID={testIDs.CENTERED_TEXT_HEADER}>{this.props.text || 'Text Screen'}</Text>
  27. {this.renderTextFromFunctionInProps()}
  28. <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
  29. <Button title={'Set Tab Badge'} testID={testIDs.SET_TAB_BADGE_BUTTON} onPress={() => this.onButtonPress()} />
  30. <Button title={'Switch To Tab 2'} testID={testIDs.SWITCH_SECOND_TAB_BUTTON} onPress={() => this.onClickSwitchToTab()} />
  31. <Button title={'Switch To Tab 1 by componentID'} testID={testIDs.SWITCH_FIRST_TAB_BUTTON} onPress={() => this.onClickSwitchToTabByComponentID()} />
  32. <Button title='Hide Tab Bar' testID={testIDs.HIDE_BOTTOM_TABS_BUTTON} onPress={() => this.hideTabBar(false)} />
  33. <Button title='Show Tab Bar' testID={testIDs.SHOW_BOTTOM_TABS_BUTTON} onPress={() => this.hideTabBar(true)} />
  34. <Button title='Show Left Side Menu' testID={testIDs.SHOW_LEFT_SIDE_MENU_BUTTON} onPress={() => this.showSideMenu('left')} />
  35. <Button title='Show Right Side Menu' testID={testIDs.SHOW_RIGHT_SIDE_MENU_BUTTON} onPress={() => this.showSideMenu('right')} />
  36. <Button title='Pop' testID={testIDs.POP_BUTTON} onPress={this.onClickPop} />
  37. </View>
  38. );
  39. }
  40. async onClickPop() {
  41. await Navigation.pop(this.props.componentId);
  42. }
  43. renderTextFromFunctionInProps() {
  44. if (!this.props.myFunction) {
  45. return undefined;
  46. }
  47. return (
  48. <Text style={styles.h1}>{this.props.myFunction()}</Text>
  49. );
  50. }
  51. onButtonPress() {
  52. Navigation.mergeOptions(this.props.componentId, {
  53. bottomTab: {
  54. badge: `TeSt`
  55. }
  56. });
  57. }
  58. onClickSwitchToTab() {
  59. Navigation.mergeOptions(this.props.componentId, {
  60. bottomTabs: {
  61. currentTabIndex: 1,
  62. visible: false,
  63. animate: true,
  64. tabColor: 'blue',
  65. selectedTabColor: 'red'
  66. }
  67. });
  68. }
  69. onClickSwitchToTabByComponentID() {
  70. Navigation.mergeOptions(this.props.componentId, {
  71. bottomTabs: {
  72. currentTabId: globalFirstComponentID
  73. }
  74. });
  75. }
  76. hideTabBar(visible) {
  77. Navigation.mergeOptions(this.props.componentId, {
  78. bottomTabs: {
  79. visible,
  80. animate: true
  81. }
  82. });
  83. }
  84. showSideMenu(side) {
  85. Navigation.mergeOptions(this.props.componentId, {
  86. sideMenu: {
  87. [side]: {
  88. visible: true
  89. }
  90. }
  91. });
  92. }
  93. onClickPop() {
  94. Navigation.pop(this.props.componentId);
  95. }
  96. }
  97. module.exports = TextScreen;
  98. const styles = {
  99. root: {
  100. flexGrow: 1,
  101. justifyContent: 'center',
  102. alignItems: 'center',
  103. backgroundColor: '#f5fcff'
  104. },
  105. h1: {
  106. fontSize: 24,
  107. textAlign: 'center',
  108. margin: 10
  109. },
  110. h2: {
  111. fontSize: 12,
  112. textAlign: 'center',
  113. margin: 10
  114. },
  115. footer: {
  116. fontSize: 10,
  117. color: '#888',
  118. marginTop: 10
  119. }
  120. };