Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 1522f1f

Browse files
matt-oakestimomeh
authored andcommitted
Support for badges from the NavigationComponent (#72)
* Support for badges from the NavigationComponent * Add an example of using Redux to get a badge count with React Navigation
1 parent b545cdc commit 1522f1f

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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)

lib/NavigationComponent.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ class NavigationComponent extends PureComponent<void, NCProps, void> {
9494
label: tabOptions.label || label,
9595
labelColor: tabOptions.labelColor,
9696
activeLabelColor: tabOptions.activeLabelColor,
97-
barBackgroundColor: tabOptions.barBackgroundColor
97+
barBackgroundColor: tabOptions.barBackgroundColor,
98+
badgeText: tabOptions.badgeText,
99+
badgeSize: tabOptions.badgeSize,
100+
badgeStyle: tabOptions.badgeStyle,
101+
isBadgeVisible: tabOptions.isBadgeVisible
98102
}
99103

100104
return <Tab

0 commit comments

Comments
 (0)