react-native-navigation的迁移库

FirstTabScreen.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. drawUnderTabBar: true
  24. };
  25. static navigatorButtons = {
  26. rightButtons: [
  27. {
  28. title: 'Edit',
  29. id: 'edit'
  30. },
  31. {
  32. icon: require('../../img/navicon_add.png'),
  33. title: 'Add',
  34. id: 'add'
  35. }
  36. ]
  37. };
  38. constructor(props) {
  39. super(props);
  40. // if you want to listen on navigator events, set this up
  41. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  42. }
  43. onNavigatorEvent(event) {
  44. if (event.type == 'DeepLink') {
  45. this.handleDeepLink(event);
  46. } else {
  47. switch (event.id) {
  48. case 'edit':
  49. Alert.alert('NavBar', 'Edit button pressed');
  50. break;
  51. case 'add':
  52. Alert.alert('NavBar', 'Add button pressed');
  53. break;
  54. case 'selectedTabChanged':
  55. this.onTabSelected(event.position);
  56. break;
  57. default:
  58. console.log('Unhandled event ' + event.id);
  59. break;
  60. }
  61. }
  62. }
  63. handleDeepLink(event) {
  64. const parts = event.link.split('/');
  65. if (parts[0] == 'tab1' && parts[1] == 'pushScreen') {
  66. this.props.navigator.toggleDrawer({
  67. side: 'left',
  68. animated: true,
  69. to: 'closed'
  70. });
  71. this.props.navigator.push({
  72. title: "Pushed from SideMenu",
  73. screen: parts[2],
  74. passProps: {
  75. str: 'This is a prop passed in \'navigator.push()\'!',
  76. obj: {
  77. str: 'This is a prop passed in an object!',
  78. arr: [
  79. {
  80. str: 'This is a prop in an object in an array in an object!'
  81. }
  82. ]
  83. },
  84. num: 1234
  85. }
  86. });
  87. }
  88. return;
  89. }
  90. onTabSelected(position) {
  91. console.log('selectedTabChanged ' + position);
  92. let rightButtons;
  93. switch (position) {
  94. case 0:
  95. rightButtons = [
  96. {
  97. id: 'account',
  98. icon: require('../../img/ic_account_box_.png')
  99. }
  100. ];
  101. break;
  102. case 1:
  103. rightButtons = [
  104. {
  105. id: 'account',
  106. icon: require('../../img/ic_add_alert.png')
  107. }
  108. ];
  109. break;
  110. default:
  111. rightButtons = [];
  112. break;
  113. }
  114. this.props.navigator.setButtons({rightButtons});
  115. }
  116. render() {
  117. return (
  118. <View style={{flex: 1, padding: 20}}>
  119. <Text style={styles.text}>
  120. <Text style={{fontWeight: '500'}}>Same Counter: </Text> {this.props.counter.count}
  121. </Text>
  122. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  123. <Text style={styles.button}>Increment Counter</Text>
  124. </TouchableOpacity>
  125. <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
  126. <Text style={styles.button}>Push Screen</Text>
  127. </TouchableOpacity>
  128. <TouchableOpacity onPress={ this.onShowModalPress.bind(this) }>
  129. <Text style={styles.button}>Modal Screen</Text>
  130. </TouchableOpacity>
  131. <TouchableOpacity onPress={ this.onToggleNavBarPress.bind(this) }>
  132. <Text style={styles.button}>Toggle NavBar</Text>
  133. </TouchableOpacity>
  134. <TouchableOpacity onPress={ this.onSetTitlePress.bind(this) }>
  135. <Text style={styles.button}>Set Title</Text>
  136. </TouchableOpacity>
  137. <TouchableOpacity onPress={ this.onSetOneButtonsPress.bind(this) }>
  138. <Text style={styles.button}>Set One Buttons</Text>
  139. </TouchableOpacity>
  140. <TouchableOpacity onPress={ this.onSetTwoButtonsPress.bind(this) }>
  141. <Text style={styles.button}>Set Two Buttons</Text>
  142. </TouchableOpacity>
  143. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  144. <Text style={{fontWeight: '500'}}>Function prop: {this.props.fn ? this.props.fn() : ''}</Text>
  145. {this.props.obj ? <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text> : false}
  146. {this.props.obj && this.props.obj.arr ? <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text> : false}
  147. </View>
  148. );
  149. }
  150. onIncrementPress() {
  151. this.props.dispatch(counterActions.increment());
  152. }
  153. onPushPress() {
  154. this.props.navigator.push({
  155. title: "More",
  156. screen: "example.PushedScreen",
  157. overrideBackPress: true,
  158. passProps: {
  159. str: 'This is a prop passed in \'navigator.push()\'!',
  160. obj: {
  161. str: 'This is a prop passed in an object!',
  162. arr: [
  163. {
  164. str: 'This is a prop in an object in an array in an object!'
  165. }
  166. ]
  167. },
  168. num: 1234
  169. }
  170. });
  171. }
  172. onShowModalPress() {
  173. this.props.navigator.showModal({
  174. title: "Modal Screen",
  175. screen: "example.PushedScreen",
  176. passProps: {
  177. str: 'This is a prop passed in \'navigator.showModal()\'!',
  178. obj: {
  179. str: 'This is a prop passed in an object!',
  180. arr: [
  181. {
  182. str: 'This is a prop in an object in an array in an object!'
  183. }
  184. ]
  185. },
  186. num: 1234
  187. }
  188. });
  189. }
  190. onToggleNavBarPress() {
  191. topBarVisible = !topBarVisible;
  192. this.props.navigator.toggleNavBar({
  193. to: topBarVisible ? 'shown' : 'hidden',
  194. animated: true
  195. });
  196. }
  197. onSetTitlePress() {
  198. this.props.navigator.setTitle(_.random(0, 100).toString());
  199. }
  200. onSetOneButtonsPress() {
  201. this.props.navigator.setButtons({
  202. rightButtons: [
  203. {
  204. title: 'Account Box',
  205. icon: require('../../img/ic_account_box_.png'),
  206. id: 'accountBox'
  207. }
  208. ]
  209. });
  210. }
  211. onSetTwoButtonsPress() {
  212. this.props.navigator.setButtons({
  213. rightButtons: [
  214. {
  215. title: 'Add Alert',
  216. icon: require('../../img/ic_add_alert.png'),
  217. id: 'addAlert',
  218. color: '#F44336',
  219. enabled: false
  220. },
  221. {
  222. title: 'Home',
  223. icon: require('../../img/ic_home.png'),
  224. id: 'home',
  225. color: '#9CCC65'
  226. }
  227. ]
  228. })
  229. }
  230. }
  231. const styles = StyleSheet.create({
  232. text: {
  233. textAlign: 'center',
  234. fontSize: 18,
  235. marginBottom: 10,
  236. marginTop: 10
  237. },
  238. button: {
  239. textAlign: 'center',
  240. fontSize: 18,
  241. marginBottom: 10,
  242. marginTop: 10,
  243. color: 'blue'
  244. }
  245. });
  246. // which props do we want to inject, given the global state?
  247. function mapStateToProps(state) {
  248. return {
  249. counter: state.counter
  250. };
  251. }
  252. export default connect(mapStateToProps)(FirstTabScreen);