react-native-navigation的迁移库

FirstTabScreen.js 9.1KB

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