TabsWithNavigationEvents.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @flow
  3. */
  4. import React from 'react';
  5. import { FlatList, SafeAreaView, StatusBar, Text, View } from 'react-native';
  6. import { NavigationEvents } from 'react-navigation';
  7. import { createMaterialBottomTabNavigator } from 'react-navigation-material-bottom-tabs';
  8. import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
  9. const Event = ({ event }) => (
  10. <View
  11. style={{
  12. borderColor: 'grey',
  13. borderWidth: 1,
  14. borderRadius: 3,
  15. padding: 5,
  16. flexDirection: 'row',
  17. alignItems: 'center',
  18. justifyContent: 'space-between',
  19. }}
  20. >
  21. <Text>{event.type}</Text>
  22. <Text>
  23. {event.action.type.replace('Navigation/', '')}
  24. {event.action.routeName ? '=>' + event.action.routeName : ''}
  25. </Text>
  26. </View>
  27. );
  28. const createTabScreen = (name, icon, focusedIcon) => {
  29. class TabScreen extends React.Component<any, any> {
  30. static navigationOptions = {
  31. tabBarLabel: name,
  32. tabBarIcon: ({ tintColor, focused }) => (
  33. <MaterialCommunityIcons
  34. name={focused ? focusedIcon : icon}
  35. size={26}
  36. style={{ color: focused ? tintColor : '#ccc' }}
  37. />
  38. ),
  39. };
  40. state = { eventLog: [] };
  41. append = navigationEvent => {
  42. this.setState(({ eventLog }) => ({
  43. eventLog: eventLog.concat(navigationEvent),
  44. }));
  45. };
  46. render() {
  47. return (
  48. <SafeAreaView
  49. forceInset={{ horizontal: 'always', top: 'always' }}
  50. style={{
  51. flex: 1,
  52. }}
  53. >
  54. <Text
  55. style={{
  56. margin: 10,
  57. marginTop: 30,
  58. fontSize: 30,
  59. fontWeight: 'bold',
  60. }}
  61. >
  62. Events for tab {name}
  63. </Text>
  64. <View style={{ flex: 1, width: '100%', marginTop: 10 }}>
  65. <FlatList
  66. data={this.state.eventLog}
  67. keyExtractor={item => `${this.state.eventLog.indexOf(item)}`}
  68. renderItem={({ item }) => (
  69. <View
  70. style={{
  71. marginVertical: 5,
  72. marginHorizontal: 10,
  73. backgroundColor: '#e4e4e4',
  74. }}
  75. >
  76. <Event event={item} />
  77. </View>
  78. )}
  79. />
  80. </View>
  81. <NavigationEvents
  82. onWillFocus={this.append}
  83. onDidFocus={this.append}
  84. onWillBlur={this.append}
  85. onDidBlur={this.append}
  86. />
  87. <StatusBar barStyle="default" />
  88. </SafeAreaView>
  89. );
  90. }
  91. }
  92. return TabScreen;
  93. };
  94. const TabsWithNavigationEvents = createMaterialBottomTabNavigator(
  95. {
  96. One: {
  97. screen: createTabScreen('One', 'numeric-1-box-outline', 'numeric-1-box'),
  98. },
  99. Two: {
  100. screen: createTabScreen('Two', 'numeric-2-box-outline', 'numeric-2-box'),
  101. },
  102. Three: {
  103. screen: createTabScreen(
  104. 'Three',
  105. 'numeric-3-box-outline',
  106. 'numeric-3-box'
  107. ),
  108. },
  109. },
  110. {
  111. shifting: false,
  112. activeTintColor: '#F44336',
  113. }
  114. );
  115. export default TabsWithNavigationEvents;