CustomTabs.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @flow
  3. */
  4. import React from 'react';
  5. import {
  6. Platform,
  7. ScrollView,
  8. StyleSheet,
  9. StatusBar,
  10. Text,
  11. TouchableOpacity,
  12. View,
  13. } from 'react-native';
  14. import {
  15. createNavigator,
  16. createNavigationContainer,
  17. SafeAreaView,
  18. TabRouter,
  19. } from 'react-navigation';
  20. import SampleText from './SampleText';
  21. import { Button } from './commonComponents/ButtonWithMargin';
  22. const MyNavScreen = ({ navigation, banner }) => (
  23. <ScrollView>
  24. <SafeAreaView forceInset={{ horizontal: 'always' }}>
  25. <SampleText>{banner}</SampleText>
  26. <Button
  27. onPress={() => {
  28. navigation.goBack(null);
  29. }}
  30. title="Go back"
  31. />
  32. </SafeAreaView>
  33. <StatusBar barStyle="default" />
  34. </ScrollView>
  35. );
  36. const MyHomeScreen = ({ navigation }) => (
  37. <MyNavScreen banner="Home Screen" navigation={navigation} />
  38. );
  39. const MyNotificationsScreen = ({ navigation }) => (
  40. <MyNavScreen banner="Notifications Screen" navigation={navigation} />
  41. );
  42. const MySettingsScreen = ({ navigation }) => (
  43. <MyNavScreen banner="Settings Screen" navigation={navigation} />
  44. );
  45. const CustomTabBar = ({ navigation }) => {
  46. const { routes } = navigation.state;
  47. return (
  48. <SafeAreaView style={styles.tabContainer}>
  49. {routes.map(route => (
  50. <TouchableOpacity
  51. onPress={() => navigation.navigate(route.routeName)}
  52. style={styles.tab}
  53. key={route.routeName}
  54. >
  55. <Text>{route.routeName}</Text>
  56. </TouchableOpacity>
  57. ))}
  58. </SafeAreaView>
  59. );
  60. };
  61. const CustomTabView = ({ descriptors, navigation }) => {
  62. const { routes, index } = navigation.state;
  63. const descriptor = descriptors[routes[index].key];
  64. const ActiveScreen = descriptor.getComponent();
  65. return (
  66. <SafeAreaView forceInset={{ top: 'always' }}>
  67. <CustomTabBar navigation={navigation} />
  68. <ActiveScreen navigation={descriptor.navigation} />
  69. </SafeAreaView>
  70. );
  71. };
  72. const CustomTabRouter = TabRouter(
  73. {
  74. Home: {
  75. screen: MyHomeScreen,
  76. path: '',
  77. },
  78. Notifications: {
  79. screen: MyNotificationsScreen,
  80. path: 'notifications',
  81. },
  82. Settings: {
  83. screen: MySettingsScreen,
  84. path: 'settings',
  85. },
  86. },
  87. {
  88. // Change this to start on a different tab
  89. initialRouteName: 'Home',
  90. }
  91. );
  92. const CustomTabs = createNavigationContainer(
  93. createNavigator(CustomTabView, CustomTabRouter, {})
  94. );
  95. const styles = StyleSheet.create({
  96. tabContainer: {
  97. flexDirection: 'row',
  98. height: 48,
  99. },
  100. tab: {
  101. flex: 1,
  102. alignItems: 'center',
  103. justifyContent: 'center',
  104. margin: 4,
  105. borderWidth: 1,
  106. borderColor: '#ddd',
  107. borderRadius: 4,
  108. },
  109. });
  110. export default CustomTabs;