react-native-navigation的迁移库

FirstTabScreen.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import React, {Component, PropTypes} from 'react';
  2. import {
  3. Text,
  4. View,
  5. ScrollView,
  6. TouchableOpacity,
  7. StyleSheet,
  8. Alert,
  9. Platform
  10. } from 'react-native';
  11. import {connect} from 'react-redux';
  12. import * as counterActions from '../reducers/counter/actions';
  13. import _ from 'lodash';
  14. let topBarVisible = true;
  15. // this is a traditional React component connected to the redux store
  16. class FirstTabScreen extends Component {
  17. static navigatorStyle = {
  18. statusBarColor: '#303F9F',
  19. toolBarColor: '#3F51B5',
  20. navigationBarColor: '#303F9F',
  21. tabSelectedTextColor: '#FFA000',
  22. tabNormalTextColor: '#FFC107',
  23. tabIndicatorColor: '#FFA000',
  24. drawUnderTabBar: true,
  25. navBarTextSubtitleColor: '#0060A0'
  26. };
  27. static navigatorButtons = {
  28. rightButtons: [
  29. {
  30. title: 'Edit',
  31. id: 'edit'
  32. },
  33. {
  34. icon: require('../../img/navicon_add.png'),
  35. title: 'Add',
  36. id: 'add'
  37. }
  38. ],
  39. fab: {
  40. id: 'share',
  41. collapsedIcon: require('../../img/ic_share.png'),
  42. expendedIcon: require('../../img/ic_clear.png'),
  43. backgroundColor: '#3F51B5',
  44. actions: [
  45. {
  46. id: 'mail',
  47. icon: require('../../img/ic_mail.png'),
  48. backgroundColor: '#03A9F4'
  49. },
  50. {
  51. id: 'action2',
  52. icon: require('../../img/ic_home.png'),
  53. backgroundColor: '#4CAF50'
  54. },
  55. {
  56. id: 'action3',
  57. icon: require('../../img/ic_home.png'),
  58. backgroundColor: '#FFEB3B'
  59. },
  60. {
  61. id: 'action3',
  62. icon: require('../../img/ic_share.png'),
  63. backgroundColor: '#FF5722'
  64. }
  65. ]
  66. }
  67. };
  68. constructor(props) {
  69. super(props);
  70. // if you want to listen on navigator events, set this up
  71. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  72. }
  73. onNavigatorEvent(event) {
  74. if (event.type == 'DeepLink') {
  75. this.handleDeepLink(event);
  76. } else {
  77. switch (event.id) {
  78. case 'edit':
  79. Alert.alert('NavBar', 'Edit button pressed');
  80. break;
  81. case 'add':
  82. Alert.alert('NavBar', 'Add button pressed');
  83. break;
  84. case 'tabSelected':
  85. this.onTabSelected();
  86. break;
  87. default:
  88. console.log('Unhandled event ' + event.id);
  89. break;
  90. }
  91. }
  92. }
  93. handleDeepLink(event) {
  94. const parts = event.link.split('/');
  95. if (parts[0] == 'tab1' && parts[1] == 'pushScreen') {
  96. this.props.navigator.toggleDrawer({
  97. side: 'left',
  98. animated: true,
  99. to: 'closed'
  100. });
  101. this.props.navigator.push({
  102. title: "Pushed from SideMenu",
  103. screen: parts[2],
  104. passProps: {
  105. str: 'This is a prop passed in \'navigator.push()\'!',
  106. obj: {
  107. str: 'This is a prop passed in an object!',
  108. arr: [
  109. {
  110. str: 'This is a prop in an object in an array in an object!'
  111. }
  112. ]
  113. },
  114. num: 1234
  115. }
  116. });
  117. }
  118. }
  119. onTabSelected() {
  120. console.log('selectedTabChanged');
  121. this.props.navigator.setButtons({rightButtons: [
  122. {
  123. id: 'account',
  124. icon: require('../../img/ic_account_box_.png')
  125. }
  126. ]});
  127. }
  128. render() {
  129. return (
  130. <ScrollView>
  131. <View style={{flex: 1, padding: 20}}>
  132. <Text style={styles.text}>
  133. <Text style={{fontWeight: '500'}}>Same Counter: </Text> {this.props.counter.count}
  134. </Text>
  135. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  136. <Text style={styles.button}>Increment Counter</Text>
  137. </TouchableOpacity>
  138. <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
  139. <Text style={styles.button}>Push Screen</Text>
  140. </TouchableOpacity>
  141. <TouchableOpacity onPress={ this.onShowModalPress.bind(this) }>
  142. <Text style={styles.button}>Modal Screen</Text>
  143. </TouchableOpacity>
  144. <TouchableOpacity onPress={ this.onToggleNavBarPress.bind(this) }>
  145. <Text style={styles.button}>Toggle NavBar</Text>
  146. </TouchableOpacity>
  147. {
  148. Platform.OS === 'android' ?
  149. <TouchableOpacity onPress={ this.onShowSnackbarPress.bind(this) }>
  150. <Text style={styles.button}>Show Snackbar</Text>
  151. </TouchableOpacity> : false
  152. }
  153. <TouchableOpacity onPress={ this.onSetTitlePress.bind(this) }>
  154. <Text style={styles.button}>Set Title</Text>
  155. </TouchableOpacity>
  156. <TouchableOpacity onPress={ this.onSetSubtitlePress.bind(this) }>
  157. <Text style={styles.button}>Set Subtitle</Text>
  158. </TouchableOpacity>
  159. <TouchableOpacity onPress={ this.onSetOneButtonsPress.bind(this) }>
  160. <Text style={styles.button}>Set One Buttons</Text>
  161. </TouchableOpacity>
  162. <TouchableOpacity onPress={ this.onSetTwoButtonsPress.bind(this) }>
  163. <Text style={styles.button}>Set Two Buttons</Text>
  164. </TouchableOpacity>
  165. <TouchableOpacity onPress={ this.onToggleDrawerPress.bind(this) }>
  166. <Text style={styles.button}>Toggle Drawer</Text>
  167. </TouchableOpacity>
  168. <TouchableOpacity onPress={ this.onSelectSecondTabPress.bind(this) }>
  169. <Text style={styles.button}>Select Second Tab</Text>
  170. </TouchableOpacity>
  171. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  172. <Text style={{fontWeight: '500'}}>Function prop: {this.props.fn ? this.props.fn() : ''}</Text>
  173. {this.props.obj ? <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text> : false}
  174. {this.props.obj && this.props.obj.arr ? <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text> : false}
  175. </View>
  176. </ScrollView>
  177. );
  178. }
  179. onIncrementPress() {
  180. this.props.dispatch(counterActions.increment());
  181. }
  182. onPushPress() {
  183. this.props.navigator.push({
  184. title: "More",
  185. screen: "example.PushedScreen",
  186. overrideBackPress: true,
  187. passProps: {
  188. str: 'This is a prop passed in \'navigator.push()\'!',
  189. obj: {
  190. str: 'This is a prop passed in an object!',
  191. arr: [
  192. {
  193. str: 'This is a prop in an object in an array in an object!'
  194. }
  195. ]
  196. },
  197. num: 1234
  198. }
  199. });
  200. }
  201. onShowModalPress() {
  202. this.props.navigator.showModal({
  203. title: "Modal Screen",
  204. screen: "example.PushedScreen",
  205. passProps: {
  206. str: 'This is a prop passed in \'navigator.showModal()\'!',
  207. obj: {
  208. str: 'This is a prop passed in an object!',
  209. arr: [
  210. {
  211. str: 'This is a prop in an object in an array in an object!'
  212. }
  213. ]
  214. },
  215. num: 1234
  216. }
  217. });
  218. }
  219. onToggleNavBarPress() {
  220. topBarVisible = !topBarVisible;
  221. this.props.navigator.toggleNavBar({
  222. to: topBarVisible ? 'shown' : 'hidden',
  223. animated: true
  224. });
  225. }
  226. onShowSnackbarPress() {
  227. this.props.navigator.showSnackbar({
  228. text: 'Counter: ' + this.props.counter.count,
  229. actionText: 'Undo',
  230. actionColor: '#ff0000',
  231. actionId: 'undo',
  232. duration: 'indefinite'
  233. });
  234. }
  235. onSetTitlePress() {
  236. this.props.navigator.setTitle({
  237. title: 'Title ' + _.random(0, 100).toString()
  238. });
  239. }
  240. onSetSubtitlePress() {
  241. this.props.navigator.setSubTitle({
  242. subtitle: 'Subtitle ' + _.random(0, 100).toString()
  243. });
  244. }
  245. onSetOneButtonsPress() {
  246. this.props.navigator.setButtons({
  247. rightButtons: [
  248. {
  249. title: 'Account Box',
  250. icon: require('../../img/ic_account_box_.png'),
  251. id: 'accountBox'
  252. }
  253. ]
  254. });
  255. }
  256. onSetTwoButtonsPress() {
  257. this.props.navigator.setButtons({
  258. rightButtons: [
  259. {
  260. title: 'Add Alert',
  261. icon: require('../../img/ic_add_alert.png'),
  262. id: 'addAlert',
  263. color: '#F44336',
  264. enabled: false
  265. },
  266. {
  267. title: 'Home',
  268. icon: require('../../img/ic_home.png'),
  269. id: 'home',
  270. color: '#9CCC65'
  271. }
  272. ]
  273. })
  274. }
  275. onToggleDrawerPress() {
  276. this.props.navigator.toggleDrawer({
  277. side: 'left',
  278. animated: true
  279. })
  280. }
  281. onSelectSecondTabPress() {
  282. this.props.navigator.handleDeepLink({
  283. link: 'tab2/select'
  284. })
  285. }
  286. }
  287. const styles = StyleSheet.create({
  288. text: {
  289. textAlign: 'center',
  290. fontSize: 18,
  291. marginBottom: 10,
  292. marginTop: 10
  293. },
  294. button: {
  295. textAlign: 'center',
  296. fontSize: 18,
  297. marginBottom: 10,
  298. marginTop: 10,
  299. color: 'blue'
  300. }
  301. });
  302. // which props do we want to inject, given the global state?
  303. function mapStateToProps(state) {
  304. return {
  305. counter: state.counter
  306. };
  307. }
  308. export default connect(mapStateToProps)(FirstTabScreen);