Skip to content

Commit 82c0c27

Browse files
authored
add custom elements to TabBarItem (#1383)
1 parent d9f5b21 commit 82c0c27

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

demo/src/screens/componentScreens/TabBarScreen.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
33
import {StyleSheet, ScrollView} from 'react-native';
4-
import {Colors, View, Button, TabBar} from 'react-native-ui-lib';
5-
4+
import {Colors, View, Button, TabBar, Image} from 'react-native-ui-lib';
65

76
const labelsArray = [
87
['ONE TWO', 'THREE', 'THREEEEEEEE', 'FOUR', 'FIVE FIVE', 'SIX', 'SEVEN-ELEVEN'],
@@ -12,17 +11,18 @@ const labelsArray = [
1211
['FIVE FIVE', 'ONE TWO', 'THREE', 'THREEEEEEEE', 'FOUR', 'SIX', 'SEVEN-ELEVEN']
1312
];
1413
const ADD_ITEM_ICON = require('../../assets/icons/plus.png');
14+
const BELL_ICON = require('../../assets/icons/bell.png');
1515
const themeColors = [Colors.violet30, Colors.green30, Colors.red30, Colors.blue30, Colors.yellow30];
1616

17-
1817
export default class TabBarScreen extends Component {
1918
state = {
2019
selectedIndex: 1,
2120
selectedIndex1: 1,
2221
selectedIndex2: 1,
2322
labels: labelsArray[0],
2423
currentTabs: [],
25-
themeColor: themeColors[0]
24+
themeColor: themeColors[0],
25+
bellColor: Colors.red30
2626
};
2727

2828
counter = 0;
@@ -70,6 +70,14 @@ export default class TabBarScreen extends Component {
7070
this.setState({themeColor: themeColors[this.colorCounter]});
7171
};
7272

73+
updateBellColor = (index: number) => {
74+
if (index === 1) {
75+
this.setState({bellColor: Colors.green30});
76+
} else {
77+
this.setState({bellColor: Colors.red30});
78+
}
79+
};
80+
7381
/** Children Count change */
7482
addTab = () => {
7583
const random = Math.floor(Math.random() * 100000);
@@ -96,9 +104,7 @@ export default class TabBarScreen extends Component {
96104
if (showAddTab) {
97105
tabs.push(this.renderAddTabsTab());
98106
} else {
99-
tabs.push(
100-
this.renderTabs({id: this.state.currentTabs.length, displayLabel: `tab #${this.state.currentTabs.length}`})
101-
);
107+
tabs.push(this.renderTabs({id: this.state.currentTabs.length, displayLabel: `tab #${this.state.currentTabs.length}`}));
102108
}
103109
return tabs;
104110
}
@@ -143,6 +149,16 @@ export default class TabBarScreen extends Component {
143149
<TabBar.Item label="Bar" uppercase style={{backgroundColor: Colors.blue80}}/>
144150
</TabBar>
145151

152+
<TabBar style={styles.tabbar} enableShadow onChangeIndex={this.updateBellColor}>
153+
<TabBar.Item label="With"/>
154+
<TabBar.Item
155+
label="Custom"
156+
leadingAccessory={<Image marginR-4 source={BELL_ICON} tintColor={this.state.bellColor}/>}
157+
trailingAccessory={<Image marginL-4 source={BELL_ICON} tintColor={this.state.bellColor}/>}
158+
/>
159+
<TabBar.Item label="Elements"/>
160+
</TabBar>
161+
146162
<TabBar style={styles.tabbar} selectedIndex={0} enableShadow>
147163
<TabBar.Item label="Fixed"/>
148164
<TabBar.Item label="Width"/>

generatedTypes/components/tabBar/TabBarItem.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { ReactElement } from 'react';
22
import { StyleProp, ViewStyle, TextStyle } from 'react-native';
33
import { BadgeProps } from '../badge';
44
export interface TabBarItemProps {
@@ -27,6 +27,14 @@ export interface TabBarItemProps {
2727
*/
2828
badge?: BadgeProps;
2929
badgeProps?: BadgeProps;
30+
/**
31+
* Pass to render a leading element
32+
*/
33+
leadingAccessory?: ReactElement;
34+
/**
35+
* Pass to render a trailing element
36+
*/
37+
trailingAccessory?: ReactElement;
3038
/**
3139
* maximum number of lines the label can break
3240
*/

src/components/tabBar/TabBarItem.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import _ from 'lodash';
2-
import React, {PureComponent} from 'react';
3-
import {StyleSheet, Animated, Easing, LayoutChangeEvent, LayoutRectangle, StyleProp, ViewStyle, TextStyle} from 'react-native';
2+
import React, {PureComponent, ReactElement} from 'react';
3+
import {
4+
StyleSheet,
5+
Animated,
6+
Easing,
7+
LayoutChangeEvent,
8+
LayoutRectangle,
9+
StyleProp,
10+
ViewStyle,
11+
TextStyle
12+
} from 'react-native';
413
import {LogService} from '../../services';
514
import {Constants} from '../../helpers';
615
import {Colors, Typography, Spacings} from '../../style';
@@ -11,7 +20,6 @@ import Text from '../text';
1120
import Image from '../image';
1221
import Badge, {BadgeProps} from '../badge';
1322

14-
1523
const INDICATOR_HEIGHT = 2;
1624
const INDICATOR_BG_COLOR = Colors.primary;
1725
const HORIZONTAL_PADDING = Constants.isTablet ? Spacings.s7 : Spacings.s5;
@@ -42,6 +50,14 @@ export interface TabBarItemProps {
4250
*/
4351
badge?: BadgeProps; //TODO: remove after deprecation
4452
badgeProps?: BadgeProps;
53+
/**
54+
* Pass to render a leading element
55+
*/
56+
leadingAccessory?: ReactElement;
57+
/**
58+
* Pass to render a trailing element
59+
*/
60+
trailingAccessory?: ReactElement;
4561
/**
4662
* maximum number of lines the label can break
4763
*/
@@ -178,6 +194,8 @@ class TabBarItem extends PureComponent<TabBarItemProps, State> {
178194
labelStyle,
179195
badgeProps,
180196
badge,
197+
leadingAccessory,
198+
trailingAccessory,
181199
uppercase,
182200
maxLines,
183201
selectedLabelStyle,
@@ -193,9 +211,7 @@ class TabBarItem extends PureComponent<TabBarItemProps, State> {
193211

194212
const iconTint = iconColor || this.getColorFromStyle(labelStyle) || this.getColorFromStyle(styles.label);
195213
const iconSelectedTint =
196-
iconSelectedColor ||
197-
this.getColorFromStyle(selectedLabelStyle) ||
198-
this.getColorFromStyle(styles.selectedLabel);
214+
iconSelectedColor || this.getColorFromStyle(selectedLabelStyle) || this.getColorFromStyle(styles.selectedLabel);
199215
const badgeFinalProps = badgeProps || badge;
200216
const badgeSize = _.get(badgeFinalProps, 'size', 'small');
201217

@@ -213,6 +229,7 @@ class TabBarItem extends PureComponent<TabBarItemProps, State> {
213229
accessibilityLabel={accessibilityLabel}
214230
>
215231
<View row flex center style={[showDivider && styles.divider, styles.contentContainer]}>
232+
{leadingAccessory}
216233
{icon && (
217234
<Image
218235
style={!_.isEmpty(label) && styles.icon}
@@ -238,6 +255,7 @@ class TabBarItem extends PureComponent<TabBarItemProps, State> {
238255
containerStyle={[styles.badge, badgeFinalProps.containerStyle]}
239256
/>
240257
)}
258+
{trailingAccessory}
241259
</View>
242260
<Animated.View style={[{opacity: indicatorOpacity}, styles.indicator, indicatorStyle]}/>
243261
</TouchableOpacity>
@@ -247,7 +265,6 @@ class TabBarItem extends PureComponent<TabBarItemProps, State> {
247265

248266
export default asBaseComponent<TabBarItemProps>(TabBarItem);
249267

250-
251268
const styles = StyleSheet.create({
252269
contentContainer: {
253270
paddingHorizontal: HORIZONTAL_PADDING

0 commit comments

Comments
 (0)