BlueWallet/components/TooltipMenu.ios.js

133 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-03-29 21:22:05 -04:00
import React from 'react';
2021-08-23 13:34:28 -04:00
import { ContextMenuView, ContextMenuButton } from 'react-native-ios-context-menu';
2021-02-10 00:18:40 -05:00
import PropTypes from 'prop-types';
2021-10-22 02:45:48 -04:00
import { TouchableOpacity } from 'react-native';
2021-02-10 00:18:40 -05:00
const ToolTipMenu = (props, ref) => {
2021-10-03 23:47:01 -04:00
const menuItemMapped = ({ action, menuOptions }) => {
const item = {
actionKey: action.id,
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;
2021-10-03 23:47:01 -04:00
};
const menuItems = props.actions.map(action => {
if (Array.isArray(action)) {
const mapped = [];
for (const actionToMap of action) {
mapped.push(menuItemMapped({ action: actionToMap }));
}
const submenu = {
menuOptions: ['displayInline'],
menuItems: mapped,
menuTitle: '',
};
return submenu;
} else {
return menuItemMapped({ action });
}
});
2021-08-16 00:43:04 -04:00
const menuTitle = props.title ?? '';
const isButton = !!props.isButton;
2021-08-23 13:34:28 -04:00
const isMenuPrimaryAction = props.isMenuPrimaryAction ? props.isMenuPrimaryAction : false;
2024-03-03 09:56:22 -04:00
const renderPreview = props.renderPreview ?? undefined;
const disabled = props.disabled ?? false;
2024-03-31 12:33:25 -04:00
const onPress = props.onPress ?? undefined;
2021-09-15 20:19:06 -04:00
const buttonStyle = props.buttonStyle;
return isButton ? (
2024-04-01 19:12:06 -04:00
<TouchableOpacity onPress={onPress} disabled={disabled} accessibilityRole="button" style={buttonStyle}>
<ContextMenuButton
ref={ref}
2024-04-01 19:12:06 -04:00
useActionSheetFallback={false}
onPressMenuItem={({ nativeEvent }) => {
props.onPressMenuItem(nativeEvent.actionKey);
}}
2024-04-01 19:12:06 -04:00
isMenuPrimaryAction={isMenuPrimaryAction}
menuConfig={{
menuTitle,
menuItems,
}}
>
{props.children}
2024-04-01 19:12:06 -04:00
</ContextMenuButton>
</TouchableOpacity>
2024-04-01 19:12:06 -04:00
) : props.onPress ? (
<ContextMenuView
ref={ref}
lazyPreview
shouldEnableAggressiveCleanup
shouldCleanupOnComponent
internalCleanupMode="viewController"
onPressMenuItem={({ nativeEvent }) => {
props.onPressMenuItem(nativeEvent.actionKey);
}}
useActionSheetFallback={false}
menuConfig={{
menuTitle,
menuItems,
}}
{...(renderPreview
? {
previewConfig: {
previewType: 'CUSTOM',
backgroundColor: 'white',
},
renderPreview,
}
: {})}
>
<TouchableOpacity accessibilityRole="button" onPress={props.onPress}>
{props.children}
</TouchableOpacity>
</ContextMenuView>
) : (
2021-08-16 00:43:04 -04:00
<ContextMenuView
ref={ref}
internalCleanupMode="viewController"
2021-08-16 00:43:04 -04:00
onPressMenuItem={({ nativeEvent }) => {
props.onPressMenuItem(nativeEvent.actionKey);
2021-08-16 00:43:04 -04:00
}}
useActionSheetFallback={false}
2021-08-16 00:43:04 -04:00
menuConfig={{
menuTitle,
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-03-03 09:56:22 -04:00
renderPreview,
2021-08-26 21:31:36 -04:00
}
: {})}
2021-08-16 00:43:04 -04:00
>
{props.children}
2021-08-16 00:43:04 -04:00
</ContextMenuView>
);
2021-02-10 00:18:40 -05:00
};
2024-03-29 21:22:05 -04:00
export default ToolTipMenu;
2021-02-10 00:18:40 -05:00
ToolTipMenu.propTypes = {
2021-10-26 22:51:47 -04:00
actions: PropTypes.object.isRequired,
2021-08-16 00:43:04 -04:00
title: PropTypes.string,
2024-03-29 21:22:05 -04:00
children: PropTypes.node,
onPressMenuItem: PropTypes.func.isRequired,
isMenuPrimaryAction: PropTypes.bool,
isButton: PropTypes.bool,
2024-03-03 09:56:22 -04:00
renderPreview: PropTypes.element,
onPress: PropTypes.func,
2021-08-26 21:31:36 -04:00
previewValue: PropTypes.string,
disabled: PropTypes.bool,
2021-02-10 00:18:40 -05:00
};