From fdb3ede3e0eb1e43c4be30c810663880e2f5467c Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Wed, 28 Jul 2021 11:58:34 +0200 Subject: [PATCH] feat: add tabBarBadge and tabBarIndicator options for material top tabs --- packages/material-top-tabs/src/types.tsx | 42 ++++++++++++++----- .../src/views/MaterialTopTabBar.tsx | 24 ++++++++++- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/packages/material-top-tabs/src/types.tsx b/packages/material-top-tabs/src/types.tsx index 38cd3fcf..873548d3 100644 --- a/packages/material-top-tabs/src/types.tsx +++ b/packages/material-top-tabs/src/types.tsx @@ -8,8 +8,13 @@ import type { TabActionHelpers, TabNavigationState, } from '@react-navigation/native'; +import type React from 'react'; import type { StyleProp, TextStyle, ViewStyle } from 'react-native'; -import type { SceneRendererProps, TabViewProps } from 'react-native-tab-view'; +import type { + SceneRendererProps, + TabBar, + TabViewProps, +} from 'react-native-tab-view'; export type MaterialTopTabNavigationEventMap = { /** @@ -92,6 +97,31 @@ export type MaterialTopTabNavigationOptions = { */ tabBarIcon?: (props: { focused: boolean; color: string }) => React.ReactNode; + /** + * Function that returns a React element to use as a badge for the tab. + */ + tabBarBadge?: () => React.ReactNode; + + /** + * Function that returns a React element as the tab bar indicator. + */ + tabBarIndicator?: ( + props: Omit< + Parameters['renderIndicator']>[0], + 'navigationState' + > & { state: TabNavigationState } + ) => React.ReactNode; + + /** + * Style object for the tab bar indicator. + */ + tabBarIndicatorStyle?: StyleProp; + + /** + * Style object for the view containing the tab bar indicator. + */ + tabBarIndicatorContainerStyle?: StyleProp; + /** * Accessibility label for the tab button. This is read by the screen reader when the user taps the tab. * It's recommended to set this if you don't have a label for the tab. @@ -165,16 +195,6 @@ export type MaterialTopTabNavigationOptions = { */ tabBarItemStyle?: StyleProp; - /** - * Style object for the tab bar indicator. - */ - tabBarIndicatorStyle?: StyleProp; - - /** - * Style object for the view containing the tab bar indicator. - */ - tabBarIndicatorContainerStyle?: StyleProp; - /** * Style object for the view containing the tab items. */ diff --git a/packages/material-top-tabs/src/views/MaterialTopTabBar.tsx b/packages/material-top-tabs/src/views/MaterialTopTabBar.tsx index b2a7c1ed..750de5de 100644 --- a/packages/material-top-tabs/src/views/MaterialTopTabBar.tsx +++ b/packages/material-top-tabs/src/views/MaterialTopTabBar.tsx @@ -1,8 +1,13 @@ -import { Route, useTheme } from '@react-navigation/native'; +import { + ParamListBase, + Route, + TabNavigationState, + useTheme, +} from '@react-navigation/native'; import Color from 'color'; import * as React from 'react'; import { StyleSheet, Text, View } from 'react-native'; -import { TabBar } from 'react-native-tab-view'; +import { TabBar, TabBarIndicator } from 'react-native-tab-view'; import type { MaterialTopTabBarProps } from '../types'; @@ -104,6 +109,21 @@ export default function TabBarTop({ return label({ focused, color }); }} + renderBadge={({ route }) => { + const { tabBarBadge } = descriptors[route.key].options; + + return tabBarBadge?.() ?? null; + }} + renderIndicator={({ navigationState: state, ...rest }) => { + return focusedOptions.tabBarIndicator ? ( + focusedOptions.tabBarIndicator({ + state: state as TabNavigationState, + ...rest, + }) + ) : ( + + ); + }} /> ); }