diff --git a/src/components/ContextMenuButton/ContextMenuButton.tsx b/src/components/ContextMenuButton/ContextMenuButton.tsx index 3cff7d38..04c168cd 100644 --- a/src/components/ContextMenuButton/ContextMenuButton.tsx +++ b/src/components/ContextMenuButton/ContextMenuButton.tsx @@ -132,9 +132,6 @@ export class ContextMenuButton extends React.PureComponent { this.props.onPressMenuItem?.(event); - - // guard: event is a native event - if(event.isUsingActionSheetFallback) return; event.stopPropagation(); }; diff --git a/src/components/ContextMenuView/ContextMenuView.tsx b/src/components/ContextMenuView/ContextMenuView.tsx index 466f58fc..5c6872e4 100644 --- a/src/components/ContextMenuView/ContextMenuView.tsx +++ b/src/components/ContextMenuView/ContextMenuView.tsx @@ -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'; @@ -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 = { @@ -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` diff --git a/src/functions/Helpers.tsx b/src/functions/Helpers.tsx index a5a0eed9..e1541ee2 100644 --- a/src/functions/Helpers.tsx +++ b/src/functions/Helpers.tsx @@ -5,3 +5,43 @@ export function getRNIUtilitiesModule(): typeof RNIUtilitiesModule{ // @ts-ignore return global.RNIUtilitiesModule; }; + +// eslint-disable-next-line consistent-this +export function setStateAsync( + that: React.Component, + newState: T | ((prevState: T) => T) +){ + return new Promise((resolve) => { + that.setState(newState, () => { + resolve(); + }); + }); +}; + +/** wrapper for timeout that returns a promise */ +export function timeout(ms: number) { + return new Promise(resolve => { + const timeoutID = setTimeout(() => { + clearTimeout(timeoutID); + resolve(); + }, ms); + }); +}; + +/** Wraps a promise that will reject if not not resolved in milliseconds */ +export function promiseWithTimeout(ms: number, promise: Promise){ + // Create a promise that rejects in milliseconds + const timeoutPromise = new Promise((_, 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'); +}; \ No newline at end of file diff --git a/src/types/SharedMenuEvents.ts b/src/types/SharedMenuEvents.ts index b93291c3..4ea690b3 100644 --- a/src/types/SharedMenuEvents.ts +++ b/src/types/SharedMenuEvents.ts @@ -32,13 +32,7 @@ export type OnRequestDeferredElementObject = NativeSyntheticEvent<{ deferredID: string; }>; -export type OnPressMenuItemEventObject = NativeSyntheticEvent & { - isUsingActionSheetFallback?: false; - -} | { - isUsingActionSheetFallback: true; - nativeEvent: MenuActionConfig; -}; +export type OnPressMenuItemEventObject = NativeSyntheticEvent; export type OnPressMenuPreviewEventObject = NativeSyntheticEvent<{ }>;