BlueWallet/components/TooltipMenu.ios.js

136 lines
3.8 KiB
JavaScript
Raw Normal View History

import React, { forwardRef } 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-08-26 21:31:36 -04:00
import QRCodeComponent from './QRCodeComponent';
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;
2021-08-26 21:31:36 -04:00
const previewQRCode = props.previewQRCode ?? false;
const previewValue = props.previewValue;
const disabled = props.disabled ?? false;
2021-09-15 20:19:06 -04:00
const buttonStyle = props.buttonStyle;
return isButton ? (
<ContextMenuButton
ref={ref}
disabled={disabled}
onPressMenuItem={({ nativeEvent }) => {
props.onPressMenuItem(nativeEvent.actionKey);
}}
isMenuPrimaryAction={isMenuPrimaryAction}
menuConfig={{
menuTitle,
2021-10-03 23:47:01 -04:00
menuItems,
}}
2021-09-15 20:19:06 -04:00
style={buttonStyle}
>
2023-04-01 20:16:00 -04: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,
}}
{...(previewQRCode
? {
previewConfig: {
previewType: 'CUSTOM',
backgroundColor: 'white',
},
renderPreview: () => <QRCodeComponent value={previewValue} isMenuAvailable={false} />,
}
: {})}
>
{props.children}
</ContextMenuView>
</TouchableOpacity>
) : (
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
}}
2021-08-26 21:31:36 -04:00
{...(previewQRCode
? {
previewConfig: {
previewType: 'CUSTOM',
backgroundColor: 'white',
},
renderPreview: () => <QRCodeComponent value={previewValue} isMenuAvailable={false} />,
}
: {})}
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
};
export default forwardRef(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,
children: PropTypes.node.isRequired,
onPressMenuItem: PropTypes.func.isRequired,
isMenuPrimaryAction: PropTypes.bool,
isButton: PropTypes.bool,
2021-08-26 21:31:36 -04:00
previewQRCode: PropTypes.bool,
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
};