react-native-navigation的迁移库

TextScreen.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const React = require('react');
  2. const { Component } = require('react');
  3. const { View, Text, Button, Platform } = require('react-native');
  4. const { Navigation } = require('react-native-navigation');
  5. const testIDs = require('../testIDs');
  6. const Bounds = require('../components/Bounds');
  7. class TextScreen extends Component {
  8. static get options() {
  9. return {
  10. bottomTabs: {
  11. drawBehind: true,
  12. testID: testIDs.BOTTOM_TABS_ELEMENT
  13. },
  14. topBar: {
  15. testID: testIDs.TOP_BAR_ELEMENT
  16. }
  17. };
  18. }
  19. render() {
  20. return (
  21. <Bounds>
  22. <View style={styles.root}>
  23. <Text style={styles.h1} testID={testIDs.CENTERED_TEXT_HEADER}>{this.props.text || 'Text Screen'}</Text>
  24. {this.renderTextFromFunctionInProps()}
  25. <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
  26. <Button title={'Set Tab Badge'} testID={testIDs.SET_TAB_BADGE_BUTTON} onPress={() => this.onClickSetBadge()} />
  27. <Button title={'Switch To Tab 2'} testID={testIDs.SWITCH_SECOND_TAB_BUTTON} onPress={() => this.onClickSwitchToTab()} />
  28. <Button title={'Switch To Tab 1 by componentID'} testID={testIDs.SWITCH_FIRST_TAB_BUTTON} onPress={() => this.onClickSwitchToTabByComponentID()} />
  29. {/* tslint:disable-next-line:max-line-length */}
  30. { Platform.OS === 'android' && <Button title='Hide Tab Bar' testID={testIDs.HIDE_BOTTOM_TABS_BUTTON} onPress={() => this.toggleTabBarVisibility(this.props.componentId, false)} /> }
  31. { Platform.OS === 'android' && <Button title='Show Tab Bar' testID={testIDs.SHOW_BOTTOM_TABS_BUTTON} onPress={() => this.toggleTabBarVisibility('BottomTabs', true)} /> }
  32. <Button title='Hide Tab Bar on Push' testID={testIDs.HIDE_BOTTOM_TABS_ON_PUSH_BUTTON} onPress={() => this.hideTabBarOnPush()} />
  33. <Button title='Show Left Side Menu' testID={testIDs.SHOW_LEFT_SIDE_MENU_BUTTON} onPress={() => this.showSideMenu('left')} />
  34. <Button title='Show Right Side Menu' testID={testIDs.SHOW_RIGHT_SIDE_MENU_BUTTON} onPress={() => this.showSideMenu('right')} />
  35. <Button title='Push' testID={testIDs.PUSH_BUTTON} onPress={this.onClickPush} />
  36. <Button title='Pop' testID={testIDs.POP_BUTTON} onPress={this.onClickPop} />
  37. </View>
  38. </Bounds>
  39. );
  40. }
  41. onClickPush = async () => {
  42. await Navigation.push(this.props.componentId, {
  43. component: {
  44. name: 'navigation.playground.PushedScreen'
  45. }
  46. });
  47. }
  48. onClickPop = async () => {
  49. await Navigation.pop(this.props.componentId);
  50. }
  51. renderTextFromFunctionInProps() {
  52. if (!this.props.myFunction) {
  53. return undefined;
  54. }
  55. return (
  56. <Text style={styles.h1}>{this.props.myFunction()}</Text>
  57. );
  58. }
  59. onClickSetBadge() {
  60. Navigation.mergeOptions(this.props.componentId, {
  61. bottomTab: {
  62. badge: `TeSt`
  63. }
  64. });
  65. }
  66. onClickSwitchToTab() {
  67. Navigation.mergeOptions(this.props.componentId, {
  68. bottomTabs: {
  69. currentTabIndex: 1,
  70. visible: false,
  71. drawBehind: true,
  72. animate: true
  73. }
  74. });
  75. }
  76. onClickSwitchToTabByComponentID() {
  77. Navigation.mergeOptions(this.props.componentId, {
  78. bottomTabs: {
  79. currentTabId: 'TAB1_ID'
  80. }
  81. });
  82. }
  83. toggleTabBarVisibility(componentId, visible) {
  84. Navigation.mergeOptions(componentId, {
  85. bottomTabs: {
  86. visible,
  87. drawBehind: true,
  88. animate: true
  89. }
  90. });
  91. }
  92. hideTabBarOnPush() {
  93. Navigation.push(this.props.componentId, {
  94. component: {
  95. name: 'navigation.playground.PushedScreen'
  96. }
  97. });
  98. }
  99. showSideMenu(side) {
  100. Navigation.mergeOptions(this.props.componentId, {
  101. sideMenu: {
  102. [side]: {
  103. visible: true
  104. }
  105. }
  106. });
  107. }
  108. onClickPop() {
  109. Navigation.pop(this.props.componentId);
  110. }
  111. }
  112. module.exports = TextScreen;
  113. const styles = {
  114. root: {
  115. flexGrow: 1,
  116. justifyContent: 'center',
  117. alignItems: 'center',
  118. backgroundColor: '#E3DCC3'
  119. },
  120. h1: {
  121. fontSize: 24,
  122. textAlign: 'center',
  123. margin: 10
  124. },
  125. h2: {
  126. fontSize: 12,
  127. textAlign: 'center',
  128. margin: 10
  129. },
  130. footer: {
  131. fontSize: 10,
  132. color: '#888',
  133. marginTop: 10
  134. }
  135. };