Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.

Commit f452188

Browse files
committed
feat: add a renderBadge prop to TabBar
1 parent 47cee5a commit f452188

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ It accepts the following props,
132132
- `renderIcon` - optional callback which receives the current scene and returns a React Element to be used as a icon
133133
- `renderLabel` - optional callback which receives the current scene and returns a React Element to be used as a label
134134
- `renderIndicator` - optional callback which receives the current scene and returns a React Element to be used as a tab indicator
135+
- `renderBadge` - optional callback which receives the current scene and returns a React Element to be used as a badge
135136
- `onTabItemPress` - optional callback invoked on tab press, useful for things like scroll to top
136137
- `tabStyle` - style object for the tab
137138

example/src/BottomBarIconTextExample.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { View, Image, StyleSheet } from 'react-native';
2+
import { View, Image, Text, StyleSheet } from 'react-native';
33
import { TabViewAnimated, TabViewPage, TabBar } from 'react-native-tab-view';
44

55
const styles = StyleSheet.create({
@@ -17,6 +17,23 @@ const styles = StyleSheet.create({
1717
tab: {
1818
padding: 0,
1919
},
20+
badge: {
21+
marginTop: 4,
22+
marginRight: 32,
23+
backgroundColor: '#f44336',
24+
height: 24,
25+
width: 24,
26+
borderRadius: 12,
27+
alignItems: 'center',
28+
justifyContent: 'center',
29+
elevation: 4,
30+
},
31+
count: {
32+
color: '#fff',
33+
fontSize: 12,
34+
fontWeight: 'bold',
35+
marginTop: -2,
36+
},
2037
});
2138

2239
export default class TopBarIconExample extends Component {
@@ -52,12 +69,24 @@ export default class TopBarIconExample extends Component {
5269
}
5370
};
5471

72+
_renderBadge = ({ route }) => {
73+
if (route.key === '2') {
74+
return (
75+
<View style={styles.badge}>
76+
<Text style={styles.count}>42</Text>
77+
</View>
78+
);
79+
}
80+
return null;
81+
};
82+
5583
_renderFooter = (props) => {
5684
return (
5785
<TabBar
5886
{...props}
5987
pressColor='rgba(0, 0, 0, .2)'
6088
renderIcon={this._renderIcon}
89+
renderBadge={this._renderBadge}
6190
tabStyle={styles.tab}
6291
style={styles.tabbar}
6392
/>

src/TabBar.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { SceneRendererPropType } from './TabViewPropTypes';
1212
import type { Scene, SceneRendererProps } from './TabViewTypeDefinitions';
1313

1414
const styles = StyleSheet.create({
15+
container: {
16+
flex: 1,
17+
},
1518
tabbar: {
1619
backgroundColor: 'black',
1720
flexDirection: 'row',
@@ -32,6 +35,11 @@ const styles = StyleSheet.create({
3235
alignItems: 'center',
3336
justifyContent: 'center',
3437
},
38+
badge: {
39+
position: 'absolute',
40+
top: 0,
41+
right: 0,
42+
},
3543
});
3644

3745
type DefaultProps = {
@@ -42,6 +50,7 @@ type Props = SceneRendererProps & {
4250
pressColor?: string;
4351
renderLabel?: (scene: Scene) => ?React.Element<any>;
4452
renderIcon?: (scene: Scene) => ?React.Element<any>;
53+
renderBadge?: (scene: Scene) => ?React.Element<any>;
4554
renderIndicator?: (props: SceneRendererProps) => ?React.Element<any>;
4655
onTabItemPress?: Function;
4756
tabStyle?: any;
@@ -86,6 +95,7 @@ export default class TabBar extends Component<DefaultProps, Props, void> {
8695
};
8796
const icon = this.props.renderIcon ? this.props.renderIcon(scene) : null;
8897
const label = this.props.renderLabel ? this.props.renderLabel(scene) : null;
98+
const badge = this.props.renderBadge ? this.props.renderBadge(scene) : null;
8999

90100
let tabStyle;
91101

@@ -110,10 +120,17 @@ export default class TabBar extends Component<DefaultProps, Props, void> {
110120
jumpToIndex(i);
111121
}}
112122
>
113-
<Animated.View style={[ styles.tabitem, { opacity }, tabStyle, this.props.tabStyle ]}>
114-
{this.props.renderIcon ? this.props.renderIcon(scene) : null}
115-
{this.props.renderLabel ? this.props.renderLabel(scene) : null}
116-
</Animated.View>
123+
<View style={styles.container}>
124+
<Animated.View style={[ styles.tabitem, { opacity }, tabStyle, this.props.tabStyle ]}>
125+
{icon}
126+
{label}
127+
</Animated.View>
128+
{badge ?
129+
<View style={styles.badge}>
130+
{badge}
131+
</View> : null
132+
}
133+
</View>
117134
</TouchableItem>
118135
);
119136
})}

0 commit comments

Comments
 (0)