TabsWithNavigationFocus.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @flow
  3. */
  4. import React from 'react';
  5. import { SafeAreaView, StatusBar, Text, View } from 'react-native';
  6. import { withNavigationFocus } from 'react-navigation';
  7. import { createMaterialBottomTabNavigator } from 'react-navigation-material-bottom-tabs';
  8. import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
  9. import { Button } from './commonComponents/ButtonWithMargin';
  10. import SampleText from './SampleText';
  11. class Child extends React.Component<any, any> {
  12. render() {
  13. return (
  14. <Text style={{ color: this.props.isFocused ? 'green' : 'maroon' }}>
  15. {this.props.isFocused
  16. ? 'I know that my parent is focused!'
  17. : 'My parent is not focused! :O'}
  18. </Text>
  19. );
  20. }
  21. }
  22. const ChildWithNavigationFocus = withNavigationFocus(Child);
  23. const createTabScreen = (name, icon, focusedIcon, tintColor = '#673ab7') => {
  24. class TabScreen extends React.Component<any, any> {
  25. static navigationOptions = {
  26. tabBarLabel: name,
  27. tabBarIcon: ({ tintColor, focused }) => (
  28. <MaterialCommunityIcons
  29. name={focused ? focusedIcon : icon}
  30. size={26}
  31. style={{ color: focused ? tintColor : '#ccc' }}
  32. />
  33. ),
  34. };
  35. state = { showChild: false };
  36. render() {
  37. const { isFocused } = this.props;
  38. return (
  39. <SafeAreaView
  40. forceInset={{ horizontal: 'always', top: 'always' }}
  41. style={{
  42. flex: 1,
  43. alignItems: 'center',
  44. justifyContent: 'center',
  45. }}
  46. >
  47. <Text style={{ fontWeight: '700', fontSize: 16, marginBottom: 5 }}>
  48. {'Tab ' + name.toLowerCase()}
  49. </Text>
  50. <Text style={{ marginBottom: 20 }}>
  51. {'props.isFocused: ' + (isFocused ? ' true' : 'false')}
  52. </Text>
  53. {this.state.showChild ? (
  54. <ChildWithNavigationFocus />
  55. ) : (
  56. <Button
  57. title="Press me"
  58. onPress={() => this.setState({ showChild: true })}
  59. />
  60. )}
  61. <Button
  62. onPress={() => this.props.navigation.pop()}
  63. title="Back to other examples"
  64. />
  65. <StatusBar barStyle="default" />
  66. </SafeAreaView>
  67. );
  68. }
  69. }
  70. return withNavigationFocus(TabScreen);
  71. };
  72. const TabsWithNavigationFocus = createMaterialBottomTabNavigator(
  73. {
  74. One: {
  75. screen: createTabScreen('One', 'numeric-1-box-outline', 'numeric-1-box'),
  76. },
  77. Two: {
  78. screen: createTabScreen('Two', 'numeric-2-box-outline', 'numeric-2-box'),
  79. },
  80. Three: {
  81. screen: createTabScreen(
  82. 'Three',
  83. 'numeric-3-box-outline',
  84. 'numeric-3-box'
  85. ),
  86. },
  87. },
  88. {
  89. shifting: false,
  90. activeTintColor: '#F44336',
  91. }
  92. );
  93. export default TabsWithNavigationFocus;