react-native-navigation的迁移库

FirstTabScreen.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import React, {Component, PropTypes} from 'react';
  2. import {
  3. Text,
  4. View,
  5. ScrollView,
  6. TouchableOpacity,
  7. StyleSheet,
  8. Alert
  9. } from 'react-native';
  10. import {connect} from 'react-redux';
  11. import * as counterActions from '../reducers/counter/actions';
  12. import _ from 'lodash';
  13. let topBarVisible = true;
  14. // this is a traditional React component connected to the redux store
  15. class FirstTabScreen extends Component {
  16. static navigatorStyle = {
  17. statusBarColor: '#303F9F',
  18. toolBarColor: '#3F51B5',
  19. navigationBarColor: '#303F9F',
  20. tabSelectedTextColor: '#FFA000',
  21. tabNormalTextColor: '#FFC107',
  22. tabIndicatorColor: '#FFA000'
  23. };
  24. static navigatorButtons = {
  25. rightButtons: [
  26. {
  27. title: 'Edit',
  28. id: 'edit'
  29. },
  30. {
  31. icon: require('../../img/navicon_add.png'),
  32. title: 'Add',
  33. id: 'add'
  34. }
  35. ]
  36. };
  37. constructor(props) {
  38. super(props);
  39. // if you want to listen on navigator events, set this up
  40. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  41. }
  42. onNavigatorEvent(event) {
  43. switch (event.id) {
  44. case 'edit':
  45. Alert.alert('NavBar', 'Edit button pressed');
  46. break;
  47. case 'add':
  48. Alert.alert('NavBar', 'Add button pressed');
  49. break;
  50. default:
  51. console.log('Unhandled event ' + event.id);
  52. break;
  53. }
  54. }
  55. render() {
  56. return (
  57. <View style={{flex: 1, padding: 20}}>
  58. <Text style={styles.text}>
  59. <Text style={{fontWeight: '500'}}>Same Counter: </Text> {this.props.counter.count}
  60. </Text>
  61. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  62. <Text style={styles.button}>Increment Counter</Text>
  63. </TouchableOpacity>
  64. <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
  65. <Text style={styles.button}>Push Screen</Text>
  66. </TouchableOpacity>
  67. <TouchableOpacity onPress={ this.onShowModalPress.bind(this) }>
  68. <Text style={styles.button}>Modal Screen</Text>
  69. </TouchableOpacity>
  70. <TouchableOpacity onPress={ this.onToggleNavBarPress.bind(this) }>
  71. <Text style={styles.button}>Toggle NavBar</Text>
  72. </TouchableOpacity>
  73. <TouchableOpacity onPress={ this.onSetTitlePress.bind(this) }>
  74. <Text style={styles.button}>Set Title</Text>
  75. </TouchableOpacity>
  76. <TouchableOpacity onPress={ this.onSetOneButtonsPress.bind(this) }>
  77. <Text style={styles.button}>Set One Buttons</Text>
  78. </TouchableOpacity>
  79. <TouchableOpacity onPress={ this.onSetTwoButtonsPress.bind(this) }>
  80. <Text style={styles.button}>Set Two Buttons</Text>
  81. </TouchableOpacity>
  82. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  83. <Text style={{fontWeight: '500'}}>Number prop: {this.props.num}</Text>
  84. <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text>
  85. <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text>
  86. </View>
  87. );
  88. }
  89. onIncrementPress() {
  90. this.props.dispatch(counterActions.increment());
  91. }
  92. onPushPress() {
  93. this.props.navigator.push({
  94. title: "More",
  95. screen: "example.PushedScreen",
  96. passProps: {
  97. str: 'This is a prop passed in \'navigator.push()\'!',
  98. obj: {
  99. str: 'This is a prop passed in an object!',
  100. arr: [
  101. {
  102. str: 'This is a prop in an object in an array in an object!'
  103. }
  104. ]
  105. },
  106. num: 1234
  107. }
  108. });
  109. }
  110. onShowModalPress() {
  111. this.props.navigator.showModal({
  112. title: "Modal Screen",
  113. screen: "example.PushedScreen",
  114. passProps: {
  115. str: 'This is a prop passed in \'navigator.showModal()\'!',
  116. obj: {
  117. str: 'This is a prop passed in an object!',
  118. arr: [
  119. {
  120. str: 'This is a prop in an object in an array in an object!'
  121. }
  122. ]
  123. },
  124. num: 1234
  125. }
  126. });
  127. }
  128. onToggleNavBarPress() {
  129. topBarVisible = !topBarVisible;
  130. this.props.navigator.toggleNavBar({
  131. to: topBarVisible ? 'shown' : 'hidden',
  132. animated: true
  133. });
  134. }
  135. onSetTitlePress() {
  136. this.props.navigator.setTitle(_.random(0, 100).toString());
  137. }
  138. onSetOneButtonsPress() {
  139. this.props.navigator.setButtons({
  140. rightButtons: [
  141. {
  142. title: 'Account Box',
  143. icon: require('../../img/ic_account_box_.png'),
  144. id: 'accountBox'
  145. }
  146. ]
  147. });
  148. }
  149. onSetTwoButtonsPress() {
  150. this.props.navigator.setButtons({
  151. rightButtons: [
  152. {
  153. title: 'Add Alert',
  154. icon: require('../../img/ic_add_alert.png'),
  155. id: 'addAlert',
  156. color: '#F44336',
  157. enabled: false
  158. },
  159. {
  160. title: 'Home',
  161. icon: require('../../img/ic_home.png'),
  162. id: 'home',
  163. color: '#9CCC65'
  164. }
  165. ]
  166. })
  167. }
  168. }
  169. const styles = StyleSheet.create({
  170. text: {
  171. textAlign: 'center',
  172. fontSize: 18,
  173. marginBottom: 10,
  174. marginTop: 10
  175. },
  176. button: {
  177. textAlign: 'center',
  178. fontSize: 18,
  179. marginBottom: 10,
  180. marginTop: 10,
  181. color: 'blue'
  182. }
  183. });
  184. // which props do we want to inject, given the global state?
  185. function mapStateToProps(state) {
  186. return {
  187. counter: state.counter
  188. };
  189. }
  190. export default connect(mapStateToProps)(FirstTabScreen);