BlueWallet/components/TooltipMenu.ios.js

133 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;
2024-03-31 18:33:25 +02:00
const onPress = props.onPress ?? undefined;
2021-09-16 02:19:06 +02:00
const buttonStyle = props.buttonStyle;
return isButton ? (
2024-04-02 01:12:06 +02:00
<TouchableOpacity onPress={onPress} disabled={disabled} accessibilityRole="button" style={buttonStyle}>
<ContextMenuButton
ref={ref}
2024-04-02 01:12:06 +02:00
useActionSheetFallback={false}
onPressMenuItem={({ nativeEvent }) => {
props.onPressMenuItem(nativeEvent.actionKey);
}}
2024-04-02 01:12:06 +02:00
isMenuPrimaryAction={isMenuPrimaryAction}
menuConfig={{
menuTitle,
menuItems,
}}
>
{props.children}
2024-04-02 01:12:06 +02:00
</ContextMenuButton>
</TouchableOpacity>
2024-04-02 01:12:06 +02: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 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
};