mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-01-12 22:51:18 +08:00
feat: add tabBarBadge and tabBarIndicator options for material top tabs
This commit is contained in:
@@ -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<React.ComponentProps<typeof TabBar>['renderIndicator']>[0],
|
||||
'navigationState'
|
||||
> & { state: TabNavigationState<ParamListBase> }
|
||||
) => React.ReactNode;
|
||||
|
||||
/**
|
||||
* Style object for the tab bar indicator.
|
||||
*/
|
||||
tabBarIndicatorStyle?: StyleProp<ViewStyle>;
|
||||
|
||||
/**
|
||||
* Style object for the view containing the tab bar indicator.
|
||||
*/
|
||||
tabBarIndicatorContainerStyle?: StyleProp<ViewStyle>;
|
||||
|
||||
/**
|
||||
* 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<ViewStyle>;
|
||||
|
||||
/**
|
||||
* Style object for the tab bar indicator.
|
||||
*/
|
||||
tabBarIndicatorStyle?: StyleProp<ViewStyle>;
|
||||
|
||||
/**
|
||||
* Style object for the view containing the tab bar indicator.
|
||||
*/
|
||||
tabBarIndicatorContainerStyle?: StyleProp<ViewStyle>;
|
||||
|
||||
/**
|
||||
* Style object for the view containing the tab items.
|
||||
*/
|
||||
|
||||
@@ -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<ParamListBase>,
|
||||
...rest,
|
||||
})
|
||||
) : (
|
||||
<TabBarIndicator navigationState={state} {...rest} />
|
||||
);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user