Skip to content

adding badgeStyle props #71

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ npm install react-native-paper-tabs react-native-pager-view
## Usage

```tsx
import { useRef } from "react";
import { StyleSheet } from "react-native"
import {
Button,
Title,
Expand All @@ -71,6 +73,14 @@ import {
} from 'react-native-paper-tabs';

function Example() {
const badgeRef = useRef(null);
const myBadgeProps = {
style:styles.badge,
visible:false,
ref:badgeRef,
//theme:mytheme
}

return (
<TabsProvider
defaultIndex={0}
Expand Down Expand Up @@ -100,6 +110,7 @@ function Example() {
// badge={true} // only show indicator
// badge="text"
// badge={1}
// badgeProps={myBadgeProps}
// onPressIn={() => {
// console.log('onPressIn explore');
// }}
Expand All @@ -114,6 +125,15 @@ function Example() {
)
}


const styles = StyleSheet.create({
badge: {
position: "absolute",
top: 4,
right: 0,
backgroundColor: "rgb(120, 69, 172)",
}
});
function ExploreWitHookExamples() {
const goTo = useTabNavigation();
const index = useTabIndex();
Expand Down
12 changes: 11 additions & 1 deletion src/TabScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import * as React from 'react';
import type { GestureResponderEvent } from 'react-native';
import type { Animated, GestureResponderEvent } from 'react-native';
import type { IconSource } from 'react-native-paper/lib/typescript/src/components/Icon';
import type { ThemeProps } from 'react-native-paper/lib/typescript/src/types';
import { StyleProp, TextStyle } from 'react-native';
export interface BadgeProps {
visible?: boolean;
size?: number;
style?: StyleProp<TextStyle>;
ref?: React.RefObject<typeof Animated.Text>;
theme?:ThemeProps;
}

export interface TabScreenProps {
label: string;
icon?: IconSource;
badge?: string | number | boolean;
badgeProps?: BadgeProps;
children: any;
onPress?: (event: GestureResponderEvent) => void;
onPressIn?: (event: GestureResponderEvent) => void;
Expand Down
24 changes: 15 additions & 9 deletions src/TabsHeaderItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,18 @@ export default function TabsHeaderItem({

const badgeIsFilled =
tab.props.badge !== undefined && tab.props.badge !== null;

const badgeWithoutContent = typeof tab.props.badge === 'boolean';
const badgePropsIsFiiled = badgeIsFilled &&
tab.props.badgeProps !== undefined && tab.props.badgeProps !== null; // False if badge is not filled - badgeIsFilled==false
// check that badgeProps.visible is filled
const badgeVisibleIsFilled = badgePropsIsFiiled && tab.props.badgeProps?.visible !== undefined && tab.props.badgeProps?.visible !== null ;
const badgeVisible = badgeVisibleIsFilled ? tab.props.badgeProps?.visible : badgeIsFilled
const badgeRef = tab.props.badgeProps?.ref !== undefined ? tab.props.badgeProps?.ref : undefined
const badgeSizeIsFiiled = badgePropsIsFiiled && (tab.props.badgeProps?.size !== undefined) && (tab.props.badgeProps?.size !== null) ;
const badgeSize = badgeSizeIsFiiled ? tab.props.badgeProps?.size : badgeWithoutContent ? 8 : 16
const badgeTheme = tab.props.badgeProps?.theme
const badgeStyle = tab.props.badgeProps?.style


return (
<View
Expand Down Expand Up @@ -136,14 +146,10 @@ export default function TabsHeaderItem({
: 0) - 2,
},
]}
>
{badgeWithoutContent ? (
<Badge visible={true} size={8} />
) : (
<Badge visible={true} size={16}>
{tab.props.badge as any}
</Badge>
)}
>
<Badge visible={badgeVisible} theme={badgeTheme} ref={badgeRef as any} size={badgeSize} style={badgeStyle} >
{tab.props.badge as any}
</Badge>
</View>
) : null}
{showTextLabel ? (
Expand Down