Skip to content

Feat/ TabController's TabBarItem custom elements #1385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class TabControllerScreen extends Component<{}, State> {
label: tab,
key: tab,
icon: index === 2 ? Assets.icons.demo.dashboard : undefined,
badge: index === 5 ? {label: '2'} : undefined
badge: index === 5 ? {label: '2'} : undefined,
leadingAccessory: index === 3 ? <Text marginR-4>{Assets.emojis.movie_camera}</Text> : undefined,
trailingAccessory: index === 4 ? <Text marginL-4>{Assets.emojis.camera}</Text> : undefined
}))
.value();

Expand Down
10 changes: 9 additions & 1 deletion generatedTypes/components/tabController/TabBarItem.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PureComponent } from 'react';
import { PureComponent, ReactElement } from 'react';
import { /* processColor, */ TextStyle, LayoutChangeEvent, StyleProp, ViewStyle } from 'react-native';
import _ from 'lodash';
import Reanimated from 'react-native-reanimated';
Expand Down Expand Up @@ -41,6 +41,14 @@ export interface TabControllerItemProps {
* Badge component props to display next the item label
*/
badge?: BadgeProps;
/**
* Pass to render a leading element
*/
leadingAccessory?: ReactElement;
/**
* Pass to render a trailing element
*/
trailingAccessory?: ReactElement;
/**
* maximun number of lines the label can break
*/
Expand Down
12 changes: 10 additions & 2 deletions generatedTypes/components/tabController2/TabBarItem.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference types="react" />
import { ReactElement } from 'react';
import { TextStyle, LayoutChangeEvent, StyleProp, ViewStyle } from 'react-native';
import Reanimated from 'react-native-reanimated';
import { BadgeProps } from '../badge';
Expand Down Expand Up @@ -39,6 +39,14 @@ export interface TabControllerItemProps {
* Badge component props to display next the item label
*/
badge?: BadgeProps;
/**
* Pass to render a leading element
*/
leadingAccessory?: ReactElement;
/**
* Pass to render a trailing element
*/
trailingAccessory?: ReactElement;
/**
* A fixed width for the item
*/
Expand Down Expand Up @@ -84,5 +92,5 @@ interface Props extends TabControllerItemProps {
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/TabControllerScreen/index.tsx
* @notes: Must be rendered as a direct child of TabController.TabBar.
*/
export default function TabBarItem({ index, label, labelColor, selectedLabelColor, labelStyle, selectedLabelStyle, icon, badge, uppercase, activeOpacity, activeBackgroundColor, testID, ignore, style, ...props }: Props): JSX.Element;
export default function TabBarItem({ index, label, labelColor, selectedLabelColor, labelStyle, selectedLabelStyle, icon, badge, leadingAccessory, trailingAccessory, uppercase, activeOpacity, activeBackgroundColor, testID, ignore, style, ...props }: Props): JSX.Element;
export {};
41 changes: 26 additions & 15 deletions src/components/tabController/TabBarItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: support commented props
import React, {PureComponent} from 'react';
import React, {PureComponent, ReactElement} from 'react';
import {StyleSheet, /* processColor, */ TextStyle, LayoutChangeEvent, StyleProp, ViewStyle} from 'react-native';
import _ from 'lodash';
import Reanimated, {processColor} from 'react-native-reanimated';
Expand Down Expand Up @@ -51,6 +51,14 @@ export interface TabControllerItemProps {
* Badge component props to display next the item label
*/
badge?: BadgeProps;
/**
* Pass to render a leading element
*/
leadingAccessory?: ReactElement;
/**
* Pass to render a trailing element
*/
trailingAccessory?: ReactElement;
/**
* maximun number of lines the label can break
*/
Expand Down Expand Up @@ -170,16 +178,8 @@ export default class TabBarItem extends PureComponent<Props> {
}

getLabelStyle() {
const {
index,
currentPage,
targetPage,
labelColor,
selectedLabelColor,
ignore,
labelStyle,
selectedLabelStyle
} = this.props;
const {index, currentPage, targetPage, labelColor, selectedLabelColor, ignore, labelStyle, selectedLabelStyle} =
this.props;

let fontWeight, letterSpacing, fontFamily;

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

const tintColor = cond(eq(currentPage, index),
activeColor,
ignore ? activeColor : inactiveColor);
const tintColor = cond(eq(currentPage, index), activeColor, ignore ? activeColor : inactiveColor);

return {
tintColor
};
}

render() {
const {label, icon, badge, state, uppercase, activeOpacity, activeBackgroundColor, testID} = this.props;
const {
label,
icon,
badge,
leadingAccessory,
trailingAccessory,
state,
uppercase,
activeOpacity,
activeBackgroundColor,
testID
} = this.props;

return (
<TouchableOpacity
Expand All @@ -279,6 +288,7 @@ export default class TabBarItem extends PureComponent<Props> {
onPress={this.onPress}
testID={testID}
>
{leadingAccessory}
{icon && (
<Reanimated.Image
source={icon}
Expand All @@ -295,6 +305,7 @@ export default class TabBarItem extends PureComponent<Props> {
// @ts-ignore
<Badge backgroundColor={Colors.red30} size={BADGE_SIZES.default} {...badge} containerStyle={styles.badge}/>
)}
{trailingAccessory}
</TouchableOpacity>
);
}
Expand Down
14 changes: 13 additions & 1 deletion src/components/tabController2/TabBarItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: support commented props
import React, {useCallback, useContext, useEffect, useRef} from 'react';
import React, {useCallback, useContext, useEffect, useRef, ReactElement} from 'react';
import {StyleSheet, TextStyle, LayoutChangeEvent, StyleProp, ViewStyle} from 'react-native';
import _ from 'lodash';
import Reanimated, {useAnimatedStyle, useSharedValue} from 'react-native-reanimated';
Expand Down Expand Up @@ -50,6 +50,14 @@ export interface TabControllerItemProps {
* Badge component props to display next the item label
*/
badge?: BadgeProps;
/**
* Pass to render a leading element
*/
leadingAccessory?: ReactElement;
/**
* Pass to render a trailing element
*/
trailingAccessory?: ReactElement;
/**
* A fixed width for the item
*/
Expand Down Expand Up @@ -106,6 +114,8 @@ export default function TabBarItem({
selectedLabelStyle,
icon,
badge,
leadingAccessory,
trailingAccessory,
uppercase,
activeOpacity = 0.9,
activeBackgroundColor,
Expand Down Expand Up @@ -181,6 +191,7 @@ export default function TabBarItem({
onPress={onPress}
testID={testID}
>
{leadingAccessory}
{icon && (
<Reanimated.Image
source={icon}
Expand All @@ -195,6 +206,7 @@ export default function TabBarItem({
{badge && (
<Badge backgroundColor={Colors.red30} size={BADGE_SIZES.default} {...badge} containerStyle={styles.badge}/>
)}
{trailingAccessory}
</TouchableOpacity>
);
}
Expand Down