Skip to content

Commit

Permalink
feat: add the 'drawerPress' event (#9529)
Browse files Browse the repository at this point in the history
  • Loading branch information
ger88555 authored and satya164 committed Nov 27, 2022
1 parent a98c5fe commit 46d52ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
7 changes: 6 additions & 1 deletion packages/drawer/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,12 @@ export type DrawerHeaderProps = {
navigation: DrawerNavigationProp<ParamListBase>;
};

export type DrawerNavigationEventMap = {};
export type DrawerNavigationEventMap = {
/**
* Event which fires on tapping on the item in the drawer menu.
*/
drawerPress: { data: undefined; canPreventDefault: true };
};

export type DrawerNavigationHelpers = NavigationHelpers<
ParamListBase,
Expand Down
27 changes: 19 additions & 8 deletions packages/drawer/src/views/DrawerItemList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ export default function DrawerItemList({

return state.routes.map((route, i) => {
const focused = i === state.index;

const onPress = () => {
const event = navigation.emit({
type: 'drawerPress',
target: route.key,
canPreventDefault: true,
});

if (!event.defaultPrevented) {
navigation.dispatch({
...(focused
? DrawerActions.closeDrawer()
: CommonActions.navigate({ name: route.name, merge: true })),
target: state.key,
});
}
};

const {
title,
drawerLabel,
Expand Down Expand Up @@ -68,14 +86,7 @@ export default function DrawerItemList({
labelStyle={drawerLabelStyle}
style={drawerItemStyle}
to={buildLink(route.name, route.params)}
onPress={() => {
navigation.dispatch({
...(focused
? DrawerActions.closeDrawer()
: CommonActions.navigate({ name: route.name, merge: true })),
target: state.key,
});
}}
onPress={onPress}
/>
);
}) as React.ReactNode as React.ReactElement;
Expand Down

0 comments on commit 46d52ca

Please sign in to comment.