Skip to content

Commit bb5f9f9

Browse files
authored
Add leadingAccessory and trailingAccessory props (#1385)
1 parent 82c0c27 commit bb5f9f9

File tree

5 files changed

+61
-20
lines changed

5 files changed

+61
-20
lines changed

demo/src/screens/componentScreens/TabControllerScreen/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class TabControllerScreen extends Component<{}, State> {
4141
label: tab,
4242
key: tab,
4343
icon: index === 2 ? Assets.icons.demo.dashboard : undefined,
44-
badge: index === 5 ? {label: '2'} : undefined
44+
badge: index === 5 ? {label: '2'} : undefined,
45+
leadingAccessory: index === 3 ? <Text marginR-4>{Assets.emojis.movie_camera}</Text> : undefined,
46+
trailingAccessory: index === 4 ? <Text marginL-4>{Assets.emojis.camera}</Text> : undefined
4547
}))
4648
.value();
4749

generatedTypes/components/tabController/TabBarItem.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PureComponent } from 'react';
1+
import { PureComponent, ReactElement } from 'react';
22
import { /* processColor, */ TextStyle, LayoutChangeEvent, StyleProp, ViewStyle } from 'react-native';
33
import _ from 'lodash';
44
import Reanimated from 'react-native-reanimated';
@@ -41,6 +41,14 @@ export interface TabControllerItemProps {
4141
* Badge component props to display next the item label
4242
*/
4343
badge?: BadgeProps;
44+
/**
45+
* Pass to render a leading element
46+
*/
47+
leadingAccessory?: ReactElement;
48+
/**
49+
* Pass to render a trailing element
50+
*/
51+
trailingAccessory?: ReactElement;
4452
/**
4553
* maximun number of lines the label can break
4654
*/

generatedTypes/components/tabController2/TabBarItem.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference types="react" />
1+
import { ReactElement } from 'react';
22
import { TextStyle, LayoutChangeEvent, StyleProp, ViewStyle } from 'react-native';
33
import Reanimated from 'react-native-reanimated';
44
import { BadgeProps } from '../badge';
@@ -39,6 +39,14 @@ export interface TabControllerItemProps {
3939
* Badge component props to display next the item label
4040
*/
4141
badge?: BadgeProps;
42+
/**
43+
* Pass to render a leading element
44+
*/
45+
leadingAccessory?: ReactElement;
46+
/**
47+
* Pass to render a trailing element
48+
*/
49+
trailingAccessory?: ReactElement;
4250
/**
4351
* A fixed width for the item
4452
*/
@@ -84,5 +92,5 @@ interface Props extends TabControllerItemProps {
8492
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/TabControllerScreen/index.tsx
8593
* @notes: Must be rendered as a direct child of TabController.TabBar.
8694
*/
87-
export default function TabBarItem({ index, label, labelColor, selectedLabelColor, labelStyle, selectedLabelStyle, icon, badge, uppercase, activeOpacity, activeBackgroundColor, testID, ignore, style, ...props }: Props): JSX.Element;
95+
export default function TabBarItem({ index, label, labelColor, selectedLabelColor, labelStyle, selectedLabelStyle, icon, badge, leadingAccessory, trailingAccessory, uppercase, activeOpacity, activeBackgroundColor, testID, ignore, style, ...props }: Props): JSX.Element;
8896
export {};

src/components/tabController/TabBarItem.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// TODO: support commented props
2-
import React, {PureComponent} from 'react';
2+
import React, {PureComponent, ReactElement} from 'react';
33
import {StyleSheet, /* processColor, */ TextStyle, LayoutChangeEvent, StyleProp, ViewStyle} from 'react-native';
44
import _ from 'lodash';
55
import Reanimated, {processColor} from 'react-native-reanimated';
@@ -51,6 +51,14 @@ export interface TabControllerItemProps {
5151
* Badge component props to display next the item label
5252
*/
5353
badge?: BadgeProps;
54+
/**
55+
* Pass to render a leading element
56+
*/
57+
leadingAccessory?: ReactElement;
58+
/**
59+
* Pass to render a trailing element
60+
*/
61+
trailingAccessory?: ReactElement;
5462
/**
5563
* maximun number of lines the label can break
5664
*/
@@ -170,16 +178,8 @@ export default class TabBarItem extends PureComponent<Props> {
170178
}
171179

172180
getLabelStyle() {
173-
const {
174-
index,
175-
currentPage,
176-
targetPage,
177-
labelColor,
178-
selectedLabelColor,
179-
ignore,
180-
labelStyle,
181-
selectedLabelStyle
182-
} = this.props;
181+
const {index, currentPage, targetPage, labelColor, selectedLabelColor, ignore, labelStyle, selectedLabelStyle} =
182+
this.props;
183183

184184
let fontWeight, letterSpacing, fontFamily;
185185

@@ -256,17 +256,26 @@ export default class TabBarItem extends PureComponent<Props> {
256256
inactiveColor = processColor(inactiveColor);
257257
}
258258

259-
const tintColor = cond(eq(currentPage, index),
260-
activeColor,
261-
ignore ? activeColor : inactiveColor);
259+
const tintColor = cond(eq(currentPage, index), activeColor, ignore ? activeColor : inactiveColor);
262260

263261
return {
264262
tintColor
265263
};
266264
}
267265

268266
render() {
269-
const {label, icon, badge, state, uppercase, activeOpacity, activeBackgroundColor, testID} = this.props;
267+
const {
268+
label,
269+
icon,
270+
badge,
271+
leadingAccessory,
272+
trailingAccessory,
273+
state,
274+
uppercase,
275+
activeOpacity,
276+
activeBackgroundColor,
277+
testID
278+
} = this.props;
270279

271280
return (
272281
<TouchableOpacity
@@ -279,6 +288,7 @@ export default class TabBarItem extends PureComponent<Props> {
279288
onPress={this.onPress}
280289
testID={testID}
281290
>
291+
{leadingAccessory}
282292
{icon && (
283293
<Reanimated.Image
284294
source={icon}
@@ -295,6 +305,7 @@ export default class TabBarItem extends PureComponent<Props> {
295305
// @ts-ignore
296306
<Badge backgroundColor={Colors.red30} size={BADGE_SIZES.default} {...badge} containerStyle={styles.badge}/>
297307
)}
308+
{trailingAccessory}
298309
</TouchableOpacity>
299310
);
300311
}

src/components/tabController2/TabBarItem.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// TODO: support commented props
2-
import React, {useCallback, useContext, useEffect, useRef} from 'react';
2+
import React, {useCallback, useContext, useEffect, useRef, ReactElement} from 'react';
33
import {StyleSheet, TextStyle, LayoutChangeEvent, StyleProp, ViewStyle} from 'react-native';
44
import _ from 'lodash';
55
import Reanimated, {useAnimatedStyle, useSharedValue} from 'react-native-reanimated';
@@ -50,6 +50,14 @@ export interface TabControllerItemProps {
5050
* Badge component props to display next the item label
5151
*/
5252
badge?: 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;
5361
/**
5462
* A fixed width for the item
5563
*/
@@ -106,6 +114,8 @@ export default function TabBarItem({
106114
selectedLabelStyle,
107115
icon,
108116
badge,
117+
leadingAccessory,
118+
trailingAccessory,
109119
uppercase,
110120
activeOpacity = 0.9,
111121
activeBackgroundColor,
@@ -181,6 +191,7 @@ export default function TabBarItem({
181191
onPress={onPress}
182192
testID={testID}
183193
>
194+
{leadingAccessory}
184195
{icon && (
185196
<Reanimated.Image
186197
source={icon}
@@ -195,6 +206,7 @@ export default function TabBarItem({
195206
{badge && (
196207
<Badge backgroundColor={Colors.red30} size={BADGE_SIZES.default} {...badge} containerStyle={styles.badge}/>
197208
)}
209+
{trailingAccessory}
198210
</TouchableOpacity>
199211
);
200212
}

0 commit comments

Comments
 (0)