|
| 1 | +import React, { Component } from 'react' |
| 2 | +import { connect } from 'react-redux' |
| 3 | +import { AppRegistry, StyleSheet, Text, View, Button } from 'react-native' |
| 4 | +import { NavigationComponent } from 'react-native-material-bottom-navigation' |
| 5 | +import { TabNavigator } from 'react-navigation' |
| 6 | +import Icon from 'react-native-vector-icons/MaterialIcons' |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Screen for first tab. |
| 11 | + * You usually will have this in a separate file. |
| 12 | + */ |
| 13 | +class MoviesAndTV extends Component { |
| 14 | + static navigationOptions = { |
| 15 | + tabBarLabel: "Movies & TV", |
| 16 | + tabBarIcon: () => <Icon size={24} name="ondemand-video" color="white" /> |
| 17 | + } |
| 18 | + |
| 19 | + render() { |
| 20 | + return <View><Text>Movies & TV</Text></View> |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Screen for second tab. |
| 26 | + * You usually will have this in a separate file. |
| 27 | + */ |
| 28 | +class Music extends Component { |
| 29 | + static navigationOptions = { |
| 30 | + tabBarLabel: "Music", |
| 31 | + tabBarIcon: () => <Icon size={24} name="music-note" color="white" /> |
| 32 | + } |
| 33 | + |
| 34 | + render() { |
| 35 | + return <View><Text>Music</Text></View> |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Screen for third tab. |
| 41 | + * You usually will have this in a separate file. |
| 42 | + */ |
| 43 | +class Books extends Component { |
| 44 | + static navigationOptions = { |
| 45 | + tabBarLabel: "Books", |
| 46 | + tabBarIcon: () => <Icon size={24} name="book" color="white" /> |
| 47 | + } |
| 48 | + |
| 49 | + render() { |
| 50 | + return <View><Text>Books</Text></View> |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Component which wraps NavigationComponent and is passed additional properties which can |
| 56 | + * dynamically alter the bottomNavigationOptions. For example, you could use Redux to get |
| 57 | + * the unread book count, pass it into this components props, and then change the badge count. |
| 58 | + */ |
| 59 | +function UnreadBooksNavigationComponent(props) { |
| 60 | + const { unreadBookCount, bottomNavigationOptions, ...rest } = props; |
| 61 | + return ( |
| 62 | + <NavigationComponent |
| 63 | + bottomNavigationOptions={{ |
| 64 | + ...bottomNavigationOptions, |
| 65 | + tabs: { |
| 66 | + ...bottomNavigationOptions.tabs, |
| 67 | + Books: { |
| 68 | + ...bottomNavigationOptions.tabs.Books, |
| 69 | + badgeText: unreadBookCount > 0 ? "" + unreadBookCount : undefined, |
| 70 | + } |
| 71 | + } |
| 72 | + }} |
| 73 | + {...rest} |
| 74 | + /> |
| 75 | + ); |
| 76 | +} |
| 77 | +const ReduxNavigationComponent = connect( |
| 78 | + (state) => ({ |
| 79 | + unreadBookCount: state.unreadBooks.length |
| 80 | + }), |
| 81 | + (dispatch) => {} |
| 82 | +)(UnreadBooksNavigationComponent); |
| 83 | + |
| 84 | +/** |
| 85 | + * react-navigation's TabNavigator. |
| 86 | + */ |
| 87 | +const MyApp = TabNavigator({ |
| 88 | + MoviesAndTV: { screen: MoviesAndTV }, |
| 89 | + Music: { screen: Music }, |
| 90 | + Books: { screen: Books } |
| 91 | +}, { |
| 92 | + tabBarComponent: ReduxNavigationComponent, |
| 93 | + tabBarPosition: 'bottom', |
| 94 | + tabBarOptions: { |
| 95 | + bottomNavigationOptions: { |
| 96 | + labelColor: 'white', |
| 97 | + rippleColor: 'white', |
| 98 | + tabs: { |
| 99 | + MoviesAndTV: { |
| 100 | + barBackgroundColor: '#37474F' |
| 101 | + }, |
| 102 | + Music: { |
| 103 | + barBackgroundColor: '#00796B' |
| 104 | + }, |
| 105 | + Books: { |
| 106 | + barBackgroundColor: '#5D4037' |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +}) |
| 112 | + |
| 113 | +AppRegistry.registerComponent('MyApp', () => MyApp) |
0 commit comments