BlueWallet/blue_modules/showPopupMenu.android.ts

33 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-10-20 09:00:38 +02:00
// @ts-ignore: Ignore
2021-10-20 08:52:58 +02:00
import type { Element } from 'react';
import { Text, TouchableNativeFeedback, TouchableWithoutFeedback, View, findNodeHandle, UIManager } from 'react-native';
type PopupMenuItem = { id?: any; label: string };
type OnPopupMenuItemSelect = (selectedPopupMenuItem: PopupMenuItem) => void;
type PopupAnchor = Element<typeof Text | typeof TouchableNativeFeedback | typeof TouchableWithoutFeedback | typeof View>;
type PopupMenuOptions = { onCancel?: () => void };
function showPopupMenu(
items: PopupMenuItem[],
onSelect: OnPopupMenuItemSelect,
anchor: PopupAnchor,
{ onCancel }: PopupMenuOptions = {},
): void {
UIManager.showPopupMenu(
2021-10-20 09:00:38 +02:00
// @ts-ignore: Ignore
2021-10-20 08:52:58 +02:00
findNodeHandle(anchor),
items.map(item => item.label),
2021-10-20 09:00:38 +02:00
function () {
2021-10-20 08:52:58 +02:00
if (onCancel) onCancel();
},
function (eventName: 'dismissed' | 'itemSelected', selectedIndex?: number) {
2021-10-20 09:00:38 +02:00
// @ts-ignore: Ignore
2021-10-20 08:52:58 +02:00
if (eventName === 'itemSelected') onSelect(items[selectedIndex]);
else onCancel && onCancel();
},
);
}
export type { PopupMenuItem, OnPopupMenuItemSelect, PopupMenuOptions };
export default showPopupMenu;