BlueWallet/components/TooltipMenu.ios.tsx

119 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-05-18 12:56:57 -04:00
import React, { forwardRef, Ref, useMemo, useCallback } from 'react';
2024-05-18 12:48:03 -04:00
import { ContextMenuView, ContextMenuButton, RenderItem } from 'react-native-ios-context-menu';
2021-10-22 02:45:48 -04:00
import { TouchableOpacity } from 'react-native';
2024-05-18 12:48:03 -04:00
import { ToolTipMenuProps, Action } from './types';
2021-02-10 00:18:40 -05:00
2024-05-18 12:48:03 -04:00
const BaseToolTipMenu = (props: ToolTipMenuProps, ref: Ref<any>) => {
2024-05-18 12:56:57 -04:00
const {
title = '',
isButton = false,
isMenuPrimaryAction = false,
renderPreview,
disabled = false,
onPress,
onMenuWillShow,
onMenuWillHide,
buttonStyle,
onPressMenuItem,
} = props;
const menuItemMapped = useCallback(({ action, menuOptions }: { action: Action; menuOptions?: string[] }) => {
2024-05-18 12:48:03 -04:00
const item: any = {
2024-05-17 18:48:00 -04:00
actionKey: action.id.toString(),
actionTitle: action.text,
icon: action.icon,
2021-10-03 23:47:01 -04:00
menuOptions,
menuTitle: action.menuTitle,
};
2021-08-23 15:54:22 -04:00
item.menuState = action.menuStateOn ? 'on' : 'off';
if (action.disabled) {
item.menuAttributes = ['disabled'];
}
return item;
2024-05-18 12:56:57 -04:00
}, []);
2021-10-03 23:47:01 -04:00
2024-05-18 12:56:57 -04:00
const menuItems = useMemo(
() =>
props.actions.map(action => {
if (Array.isArray(action)) {
const mapped = action.map(actionToMap => menuItemMapped({ action: actionToMap }));
return {
menuOptions: ['displayInline'],
menuItems: mapped,
menuTitle: '',
};
} else {
return menuItemMapped({ action });
}
}),
[props.actions, menuItemMapped],
);
2024-05-18 12:56:57 -04:00
const handlePressMenuItem = useCallback(
({ nativeEvent }: { nativeEvent: { actionKey: string } }) => {
onPressMenuItem(nativeEvent.actionKey);
},
[onPressMenuItem],
);
2024-05-18 12:48:03 -04:00
2024-05-18 12:56:57 -04:00
const renderContextMenuButton = () => (
<ContextMenuButton
2024-04-01 19:12:06 -04:00
ref={ref}
2024-05-18 12:56:57 -04:00
onMenuWillShow={onMenuWillShow}
onMenuWillHide={onMenuWillHide}
2024-04-01 19:12:06 -04:00
useActionSheetFallback={false}
2024-05-18 12:56:57 -04:00
onPressMenuItem={handlePressMenuItem}
isMenuPrimaryAction={isMenuPrimaryAction}
2024-04-01 19:12:06 -04:00
menuConfig={{
2024-05-18 12:48:03 -04:00
menuTitle: title,
2024-04-01 19:12:06 -04:00
menuItems,
}}
>
2024-05-18 12:56:57 -04:00
{props.children}
</ContextMenuButton>
);
const renderContextMenuView = () => (
2021-08-16 00:43:04 -04:00
<ContextMenuView
ref={ref}
2024-05-12 16:37:30 -04:00
lazyPreview
shouldEnableAggressiveCleanup
2024-05-18 12:56:57 -04:00
internalCleanupMode="automatic"
onPressMenuItem={handlePressMenuItem}
useActionSheetFallback={false}
2021-08-16 00:43:04 -04:00
menuConfig={{
2024-05-18 12:48:03 -04:00
menuTitle: title,
2021-10-03 23:47:01 -04:00
menuItems,
2021-08-16 00:43:04 -04:00
}}
2024-03-03 09:56:22 -04:00
{...(renderPreview
2021-08-26 21:31:36 -04:00
? {
previewConfig: {
previewType: 'CUSTOM',
backgroundColor: 'white',
},
2024-05-18 12:48:03 -04:00
renderPreview: renderPreview as RenderItem,
2021-08-26 21:31:36 -04:00
}
: {})}
2021-08-16 00:43:04 -04:00
>
2024-05-18 12:56:57 -04:00
<TouchableOpacity accessibilityRole="button" onPress={onPress}>
{props.children}
</TouchableOpacity>
2021-08-16 00:43:04 -04:00
</ContextMenuView>
);
2024-05-18 12:56:57 -04:00
return isButton ? (
<TouchableOpacity onPress={onPress} disabled={disabled} accessibilityRole="button" style={buttonStyle}>
{renderContextMenuButton()}
</TouchableOpacity>
) : props.onPress ? (
renderContextMenuView()
) : (
2024-05-18 13:50:50 -04:00
renderContextMenuButton()
2024-05-18 12:56:57 -04:00
);
2021-02-10 00:18:40 -05:00
};
const ToolTipMenu = forwardRef(BaseToolTipMenu);
2024-03-29 21:22:05 -04:00
export default ToolTipMenu;