Skip to content

Commit

Permalink
fix: add event emit for long press (callstack#3769)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomasz Janiczek <[email protected]>
  • Loading branch information
josemak25 and Tomasz Janiczek committed Mar 20, 2023
1 parent 842e0aa commit baa0372
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/react-navigation/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
54 changes: 39 additions & 15 deletions src/react-navigation/views/MaterialBottomTabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
MaterialBottomTabDescriptorMap,
MaterialBottomTabNavigationConfig,
MaterialBottomTabNavigationHelpers,
MaterialBottomTabNavigationEventMap,
} from '../types';

type Props = MaterialBottomTabNavigationConfig & {
Expand All @@ -24,6 +25,19 @@ type Props = MaterialBottomTabNavigationConfig & {
descriptors: MaterialBottomTabDescriptorMap;
};

type EventHandler = React.ComponentProps<typeof BottomNavigation>[
| 'onTabPress'
| 'onTabLongPress'];

type EventHandlerArgument = Parameters<NonNullable<EventHandler>>[0];

type EventHandlerCallback = EventHandlerArgument & {
route: EventHandlerArgument['route'] & {
name: string;
params?: object;
};
};

export default function MaterialBottomTabView({
state,
navigation,
Expand All @@ -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 (
<BottomNavigation
{...rest}
Expand Down Expand Up @@ -108,22 +141,13 @@ export default function MaterialBottomTabView({
getTestID={({ route }) =>
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,
});
}
}}
})}
/>
);
}
Expand Down

0 comments on commit baa0372

Please sign in to comment.