SimpleTabs.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @flow
  3. */
  4. import type {
  5. NavigationScreenProp,
  6. NavigationEventSubscription,
  7. } from 'react-navigation';
  8. import React from 'react';
  9. import { Platform, ScrollView, StatusBar, View } from 'react-native';
  10. import { SafeAreaView, createBottomTabNavigator } from 'react-navigation';
  11. import Ionicons from 'react-native-vector-icons/Ionicons';
  12. import SampleText from './SampleText';
  13. import { Button } from './commonComponents/ButtonWithMargin';
  14. const MyNavScreen = ({ navigation, banner }) => (
  15. <SafeAreaView forceInset={{ horizontal: 'always', top: 'always' }}>
  16. <SampleText>{banner}</SampleText>
  17. <Button
  18. onPress={() => navigation.navigate('Home')}
  19. title="Go to home tab"
  20. />
  21. <Button
  22. onPress={() => navigation.navigate('Settings')}
  23. title="Go to settings tab"
  24. />
  25. <Button onPress={() => navigation.goBack(null)} title="Go back" />
  26. <StatusBar barStyle="default" />
  27. </SafeAreaView>
  28. );
  29. const MyHomeScreen = ({ navigation }) => (
  30. <MyNavScreen banner="Home Tab" navigation={navigation} />
  31. );
  32. MyHomeScreen.navigationOptions = {
  33. tabBarTestIDProps: {
  34. testID: 'TEST_ID_HOME',
  35. accessibilityLabel: 'TEST_ID_HOME_ACLBL',
  36. },
  37. tabBarLabel: 'Home',
  38. tabBarIcon: ({ tintColor, focused }) => (
  39. <Ionicons
  40. name={focused ? 'ios-home' : 'ios-home-outline'}
  41. size={26}
  42. style={{ color: tintColor }}
  43. />
  44. ),
  45. };
  46. type MyPeopleScreenProps = {
  47. navigation: NavigationScreenProp<*>,
  48. };
  49. class MyPeopleScreen extends React.Component<MyPeopleScreenProps> {
  50. _s0: NavigationEventSubscription;
  51. _s1: NavigationEventSubscription;
  52. _s2: NavigationEventSubscription;
  53. _s3: NavigationEventSubscription;
  54. static navigationOptions = {
  55. tabBarLabel: 'People',
  56. tabBarIcon: ({ tintColor, focused }) => (
  57. <Ionicons
  58. name={focused ? 'ios-people' : 'ios-people-outline'}
  59. size={26}
  60. style={{ color: tintColor }}
  61. />
  62. ),
  63. };
  64. componentDidMount() {
  65. this._s0 = this.props.navigation.addListener('willFocus', this._onEvent);
  66. this._s1 = this.props.navigation.addListener('didFocus', this._onEvent);
  67. this._s2 = this.props.navigation.addListener('willBlur', this._onEvent);
  68. this._s3 = this.props.navigation.addListener('didBlur', this._onEvent);
  69. }
  70. componentWillUnmount() {
  71. this._s0.remove();
  72. this._s1.remove();
  73. this._s2.remove();
  74. this._s3.remove();
  75. }
  76. _onEvent = a => {
  77. console.log('EVENT ON PEOPLE TAB', a.type, a);
  78. };
  79. render() {
  80. const { navigation } = this.props;
  81. return <MyNavScreen banner="People Tab" navigation={navigation} />;
  82. }
  83. }
  84. type MyChatScreenProps = {
  85. navigation: NavigationScreenProp<*>,
  86. };
  87. class MyChatScreen extends React.Component<MyChatScreenProps> {
  88. _s0: NavigationEventSubscription;
  89. _s1: NavigationEventSubscription;
  90. _s2: NavigationEventSubscription;
  91. _s3: NavigationEventSubscription;
  92. static navigationOptions = {
  93. tabBarLabel: 'Chat',
  94. tabBarIcon: ({ tintColor, focused }) => (
  95. <Ionicons
  96. name={focused ? 'ios-chatboxes' : 'ios-chatboxes-outline'}
  97. size={26}
  98. style={{ color: tintColor }}
  99. />
  100. ),
  101. };
  102. componentDidMount() {
  103. this._s0 = this.props.navigation.addListener('willFocus', this._onEvent);
  104. this._s1 = this.props.navigation.addListener('didFocus', this._onEvent);
  105. this._s2 = this.props.navigation.addListener('willBlur', this._onEvent);
  106. this._s3 = this.props.navigation.addListener('didBlur', this._onEvent);
  107. }
  108. componentWillUnmount() {
  109. this._s0.remove();
  110. this._s1.remove();
  111. this._s2.remove();
  112. this._s3.remove();
  113. }
  114. _onEvent = a => {
  115. console.log('EVENT ON CHAT TAB', a.type, a);
  116. };
  117. render() {
  118. const { navigation } = this.props;
  119. return <MyNavScreen banner="Chat Tab" navigation={navigation} />;
  120. }
  121. }
  122. const MySettingsScreen = ({ navigation }) => (
  123. <MyNavScreen banner="Settings Tab" navigation={navigation} />
  124. );
  125. MySettingsScreen.navigationOptions = {
  126. tabBarLabel: 'Settings',
  127. tabBarIcon: ({ tintColor, focused }) => (
  128. <Ionicons
  129. name={focused ? 'ios-settings' : 'ios-settings-outline'}
  130. size={26}
  131. style={{ color: tintColor }}
  132. />
  133. ),
  134. };
  135. const SimpleTabs = createBottomTabNavigator(
  136. {
  137. Home: {
  138. screen: MyHomeScreen,
  139. path: '',
  140. },
  141. People: {
  142. screen: MyPeopleScreen,
  143. path: 'cart',
  144. },
  145. Chat: {
  146. screen: MyChatScreen,
  147. path: 'chat',
  148. },
  149. Settings: {
  150. screen: MySettingsScreen,
  151. path: 'settings',
  152. },
  153. },
  154. {
  155. tabBarOptions: {
  156. activeTintColor: '#e91e63',
  157. },
  158. }
  159. );
  160. type SimpleTabsContainerProps = {
  161. navigation: NavigationScreenProp<*>,
  162. };
  163. class SimpleTabsContainer extends React.Component<SimpleTabsContainerProps> {
  164. static router = SimpleTabs.router;
  165. _s0: NavigationEventSubscription;
  166. _s1: NavigationEventSubscription;
  167. _s2: NavigationEventSubscription;
  168. _s3: NavigationEventSubscription;
  169. componentDidMount() {
  170. this._s0 = this.props.navigation.addListener('willFocus', this._onAction);
  171. this._s1 = this.props.navigation.addListener('didFocus', this._onAction);
  172. this._s2 = this.props.navigation.addListener('willBlur', this._onAction);
  173. this._s3 = this.props.navigation.addListener('didBlur', this._onAction);
  174. }
  175. componentWillUnmount() {
  176. this._s0.remove();
  177. this._s1.remove();
  178. this._s2.remove();
  179. this._s3.remove();
  180. }
  181. _onAction = a => {
  182. console.log('TABS EVENT', a.type, a);
  183. };
  184. render() {
  185. return <SimpleTabs navigation={this.props.navigation} />;
  186. }
  187. }
  188. export default SimpleTabsContainer;