BlueWallet/components/TooltipMenu.ios.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

import React, { forwardRef } from 'react';
2021-08-23 19:34:28 +02:00
import { ContextMenuView, ContextMenuButton } from 'react-native-ios-context-menu';
2021-02-10 06:18:40 +01:00
import PropTypes from 'prop-types';
2021-10-22 08:45:48 +02:00
import { TouchableOpacity } from 'react-native';
2021-02-10 06:18:40 +01:00
const ToolTipMenu = (props, ref) => {
2021-10-04 05:47:01 +02:00
const menuItemMapped = ({ action, menuOptions }) => {
const item = {
actionKey: action.id,
actionTitle: action.text,
icon: action.icon,
2021-10-04 05:47:01 +02:00
menuOptions,
menuTitle: action.menuTitle,
};
2021-08-23 21:54:22 +02:00
item.menuState = action.menuStateOn ? 'on' : 'off';
if (action.disabled) {
item.menuAttributes = ['disabled'];
}
return item;
2021-10-04 05:47:01 +02: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 06:43:04 +02:00
const menuTitle = props.title ?? '';
const isButton = !!props.isButton;
2021-08-23 19:34:28 +02:00
const isMenuPrimaryAction = props.isMenuPrimaryAction ? props.isMenuPrimaryAction : false;
2024-03-03 14:56:22 +01:00
const renderPreview = props.renderPreview ?? undefined;
const disabled = props.disabled ?? false;
2021-09-16 02:19:06 +02:00
const buttonStyle = props.buttonStyle;
return isButton ? (
<ContextMenuButton
ref={ref}
disabled={disabled}
onPressMenuItem={({ nativeEvent }) => {
props.onPressMenuItem(nativeEvent.actionKey);
}}
isMenuPrimaryAction={isMenuPrimaryAction}
menuConfig={{
menuTitle,
2021-10-04 05:47:01 +02:00
menuItems,
}}
2021-09-16 02:19:06 +02:00
style={buttonStyle}
>
2023-04-02 02:16:00 +02:00
{props.onPress ? (
<TouchableOpacity accessibilityRole="button" onPress={props.onPress}>
{props.children}
</TouchableOpacity>
) : (
props.children
)}
</ContextMenuButton>
) : props.onPress ? (
<TouchableOpacity accessibilityRole="button" onPress={props.onPress}>
<ContextMenuView
ref={ref}
internalCleanupMode="viewController"
onPressMenuItem={({ nativeEvent }) => {
props.onPressMenuItem(nativeEvent.actionKey);
}}
useActionSheetFallback={false}
menuConfig={{
menuTitle,
menuItems,
}}
2024-03-03 14:56:22 +01:00
{...(renderPreview
? {
previewConfig: {
previewType: 'CUSTOM',
backgroundColor: 'white',
},
2024-03-03 14:56:22 +01:00
renderPreview,
}
: {})}
>
{props.children}
</ContextMenuView>
</TouchableOpacity>
) : (
2021-08-16 06:43:04 +02:00
<ContextMenuView
ref={ref}
internalCleanupMode="viewController"
2021-08-16 06:43:04 +02:00
onPressMenuItem={({ nativeEvent }) => {
props.onPressMenuItem(nativeEvent.actionKey);
2021-08-16 06:43:04 +02:00
}}
useActionSheetFallback={false}
2021-08-16 06:43:04 +02:00
menuConfig={{
menuTitle,
2021-10-04 05:47:01 +02:00
menuItems,
2021-08-16 06:43:04 +02:00
}}
2024-03-03 14:56:22 +01:00
{...(renderPreview
2021-08-27 03:31:36 +02:00
? {
previewConfig: {
previewType: 'CUSTOM',
backgroundColor: 'white',
},
2024-03-03 14:56:22 +01:00
renderPreview,
2021-08-27 03:31:36 +02:00
}
: {})}
2021-08-16 06:43:04 +02:00
>
{props.children}
2021-08-16 06:43:04 +02:00
</ContextMenuView>
);
2021-02-10 06:18:40 +01:00
};
export default forwardRef(ToolTipMenu);
2021-02-10 06:18:40 +01:00
ToolTipMenu.propTypes = {
2021-10-27 04:51:47 +02:00
actions: PropTypes.object.isRequired,
2021-08-16 06:43:04 +02:00
title: PropTypes.string,
children: PropTypes.node.isRequired,
onPressMenuItem: PropTypes.func.isRequired,
isMenuPrimaryAction: PropTypes.bool,
isButton: PropTypes.bool,
2024-03-03 14:56:22 +01:00
renderPreview: PropTypes.element,
onPress: PropTypes.func,
2021-08-27 03:31:36 +02:00
previewValue: PropTypes.string,
disabled: PropTypes.bool,
2021-02-10 06:18:40 +01:00
};