Skip to content

Commit

Permalink
🐞 Fix: Context Menu Events
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Aug 25, 2024
1 parent 95d0a23 commit a988953
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/components/ContextMenuButton/ContextMenuButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ export class ContextMenuButton extends React.PureComponent<ContextMenuButtonProp

private _handleOnPressMenuItem: OnPressMenuItemEvent = (event) => {
this.props.onPressMenuItem?.(event);

// guard: event is a native event
if(event.isUsingActionSheetFallback) return;
event.stopPropagation();
};

Expand Down
8 changes: 3 additions & 5 deletions src/components/ContextMenuView/ContextMenuView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { StyleSheet, View } from 'react-native';

import { TSEventEmitter } from '@dominicstop/ts-event-emitter';
import { Helpers } from 'react-native-ios-utilities';

import { RNIContextMenuView, type RNIContextMenuViewRef } from '../../native_components/RNIContextMenuView';
import { ContextMenuViewContext } from '../../context/ContextMenuViewContext';
Expand All @@ -12,6 +11,7 @@ import type { ContextMenuViewProps, ContextMenuViewState, ContextMenuEventEmitte
import type { MenuElementConfig } from '../../types/MenuConfig';

import { LIB_ENV } from '../../constants/LibEnv';
import * as Helpers from '../../functions/Helpers';


// const NATIVE_ID_KEYS = {
Expand Down Expand Up @@ -246,20 +246,18 @@ export class ContextMenuView extends
private _handleOnPressMenuItem: OnPressMenuItemEvent = async (event) => {
const props = this.getProps();
const eventProps = props.eventProps;

// guard: event is a native event
if(event.isUsingActionSheetFallback) return;

event.stopPropagation();
event.persist();


const isKeepsMenuPresentedEnabled =
event.nativeEvent.menuAttributes?.includes('keepsMenuPresented');

const shouldWaitForMenuToHide =
!isKeepsMenuPresentedEnabled &&
props.shouldWaitForMenuToHideBeforeFiringOnPressMenuItem;

try {
if(shouldWaitForMenuToHide){
// wait for `onMenuDidHide`
Expand Down
40 changes: 40 additions & 0 deletions src/functions/Helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,43 @@ export function getRNIUtilitiesModule(): typeof RNIUtilitiesModule{
// @ts-ignore
return global.RNIUtilitiesModule;
};

// eslint-disable-next-line consistent-this
export function setStateAsync<T extends {}>(
that: React.Component,
newState: T | ((prevState: T) => T)
){
return new Promise<void>((resolve) => {
that.setState(newState, () => {
resolve();
});
});
};

/** wrapper for timeout that returns a promise */
export function timeout(ms: number) {
return new Promise<void>(resolve => {
const timeoutID = setTimeout(() => {
clearTimeout(timeoutID);
resolve();
}, ms);
});
};

/** Wraps a promise that will reject if not not resolved in <ms> milliseconds */
export function promiseWithTimeout<T>(ms: number, promise: Promise<T>){
// Create a promise that rejects in <ms> milliseconds
const timeoutPromise = new Promise<T>((_, reject) => {
const timeoutID = setTimeout(() => {
clearTimeout(timeoutID);
reject(`Promise timed out in ${ms} ms.`)
}, ms);
});

// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeoutPromise]);
};

export function pad(num: number | string, places = 2){
return String(num).padStart(places, '0');
};
8 changes: 1 addition & 7 deletions src/types/SharedMenuEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ export type OnRequestDeferredElementObject = NativeSyntheticEvent<{
deferredID: string;
}>;

export type OnPressMenuItemEventObject = NativeSyntheticEvent<MenuActionConfig> & {
isUsingActionSheetFallback?: false;

} | {
isUsingActionSheetFallback: true;
nativeEvent: MenuActionConfig;
};
export type OnPressMenuItemEventObject = NativeSyntheticEvent<MenuActionConfig>;

export type OnPressMenuPreviewEventObject = NativeSyntheticEvent<{
}>;
Expand Down

0 comments on commit a988953

Please sign in to comment.