From baa0372a65d08c4a17aac182ce3aa8244e3ad1ac Mon Sep 17 00:00:00 2001 From: Amakiri Joseph Lucky Date: Mon, 20 Mar 2023 20:55:11 +0100 Subject: [PATCH] fix: add event emit for long press (#3769) Co-authored-by: Tomasz Janiczek --- src/react-navigation/types.tsx | 4 ++ .../views/MaterialBottomTabView.tsx | 54 +++++++++++++------ 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/react-navigation/types.tsx b/src/react-navigation/types.tsx index 14655e8135..d0b84ee684 100644 --- a/src/react-navigation/types.tsx +++ b/src/react-navigation/types.tsx @@ -14,6 +14,10 @@ export type MaterialBottomTabNavigationEventMap = { * Event which fires on tapping on the tab in the tab bar. */ tabPress: { data: undefined; canPreventDefault: true }; + /** + * Event which fires on long pressing on the tab in the tab bar. + */ + onTabLongPress: { data: undefined; canPreventDefault: true }; }; export type MaterialBottomTabNavigationHelpers = NavigationHelpers< diff --git a/src/react-navigation/views/MaterialBottomTabView.tsx b/src/react-navigation/views/MaterialBottomTabView.tsx index 48d7040927..1b16c87fc6 100644 --- a/src/react-navigation/views/MaterialBottomTabView.tsx +++ b/src/react-navigation/views/MaterialBottomTabView.tsx @@ -16,6 +16,7 @@ import type { MaterialBottomTabDescriptorMap, MaterialBottomTabNavigationConfig, MaterialBottomTabNavigationHelpers, + MaterialBottomTabNavigationEventMap, } from '../types'; type Props = MaterialBottomTabNavigationConfig & { @@ -24,6 +25,19 @@ type Props = MaterialBottomTabNavigationConfig & { descriptors: MaterialBottomTabDescriptorMap; }; +type EventHandler = React.ComponentProps[ + | 'onTabPress' + | 'onTabLongPress']; + +type EventHandlerArgument = Parameters>[0]; + +type EventHandlerCallback = EventHandlerArgument & { + route: EventHandlerArgument['route'] & { + name: string; + params?: object; + }; +}; + export default function MaterialBottomTabView({ state, navigation, @@ -32,6 +46,25 @@ export default function MaterialBottomTabView({ }: Props) { const buildLink = useNavigationLink(); + const eventHandlerCallback = ( + type: keyof MaterialBottomTabNavigationEventMap, + callback?: (route: EventHandlerCallback['route']) => void + ) => { + return ({ route, preventDefault }: EventHandlerCallback) => { + const event = navigation.emit({ + type, + target: route.key, + canPreventDefault: true, + }); + + if (event.defaultPrevented) { + return preventDefault(); + } + + callback?.(route); + }; + }; + return ( descriptors[route.key].options.tabBarButtonTestID } - onTabPress={({ route, preventDefault }) => { - const event = navigation.emit({ - type: 'tabPress', - target: route.key, - canPreventDefault: true, + onTabLongPress={eventHandlerCallback('onTabLongPress')} + onTabPress={eventHandlerCallback('tabPress', (route) => { + navigation.dispatch({ + ...CommonActions.navigate(route.name, route.params), + target: state.key, }); - - if (event.defaultPrevented) { - preventDefault(); - } else { - navigation.dispatch({ - ...CommonActions.navigate(route.name, route.params), - target: state.key, - }); - } - }} + })} /> ); }