2021-10-06 23:15:32 -04:00
|
|
|
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
|
|
|
|
2021-10-06 23:15:32 -04:00
|
|
|
const ToolTipMenu = (props, ref) => {
|
2021-10-03 23:47:01 -04:00
|
|
|
const menuItemMapped = ({ action, menuOptions }) => {
|
2021-08-23 15:39:52 -04:00
|
|
|
const item = {
|
|
|
|
actionKey: action.id,
|
|
|
|
actionTitle: action.text,
|
|
|
|
icon: action.icon,
|
2021-10-03 23:47:01 -04:00
|
|
|
menuOptions,
|
2021-08-23 15:39:52 -04:00
|
|
|
menuTitle: action.menuTitle,
|
|
|
|
};
|
2021-08-23 15:54:22 -04:00
|
|
|
item.menuState = action.menuStateOn ? 'on' : 'off';
|
|
|
|
|
2021-08-23 15:39:52 -04:00
|
|
|
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-23 15:39:52 -04:00
|
|
|
});
|
2021-08-16 00:43:04 -04:00
|
|
|
const menuTitle = props.title ?? '';
|
2021-08-23 15:39:52 -04:00
|
|
|
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;
|
2021-10-26 16:47:09 -04:00
|
|
|
const disabled = props.disabled ?? false;
|
2023-07-25 14:50:04 +01:00
|
|
|
|
2021-09-15 20:19:06 -04:00
|
|
|
const buttonStyle = props.buttonStyle;
|
2021-08-23 15:39:52 -04:00
|
|
|
return isButton ? (
|
|
|
|
<ContextMenuButton
|
2021-10-06 23:15:32 -04:00
|
|
|
ref={ref}
|
2021-10-26 16:47:09 -04:00
|
|
|
disabled={disabled}
|
2021-08-23 15:39:52 -04:00
|
|
|
onPressMenuItem={({ nativeEvent }) => {
|
2021-10-06 23:15:32 -04:00
|
|
|
props.onPressMenuItem(nativeEvent.actionKey);
|
2021-08-23 15:39:52 -04:00
|
|
|
}}
|
|
|
|
isMenuPrimaryAction={isMenuPrimaryAction}
|
|
|
|
menuConfig={{
|
|
|
|
menuTitle,
|
2021-10-03 23:47:01 -04:00
|
|
|
menuItems,
|
2021-08-23 15:39:52 -04:00
|
|
|
}}
|
2021-09-15 20:19:06 -04:00
|
|
|
style={buttonStyle}
|
2021-08-23 15:39:52 -04:00
|
|
|
>
|
2023-04-01 20:16:00 -04:00
|
|
|
{props.onPress ? (
|
|
|
|
<TouchableOpacity accessibilityRole="button" onPress={props.onPress}>
|
|
|
|
{props.children}
|
|
|
|
</TouchableOpacity>
|
|
|
|
) : (
|
|
|
|
props.children
|
|
|
|
)}
|
2021-08-23 15:39:52 -04:00
|
|
|
</ContextMenuButton>
|
2024-01-01 14:52:59 -04:00
|
|
|
) : 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-23 15:39:52 -04:00
|
|
|
) : (
|
2021-08-16 00:43:04 -04:00
|
|
|
<ContextMenuView
|
2021-10-06 23:15:32 -04:00
|
|
|
ref={ref}
|
2023-03-24 22:09:53 -04:00
|
|
|
internalCleanupMode="viewController"
|
2021-08-16 00:43:04 -04:00
|
|
|
onPressMenuItem={({ nativeEvent }) => {
|
2021-10-06 23:15:32 -04:00
|
|
|
props.onPressMenuItem(nativeEvent.actionKey);
|
2021-08-16 00:43:04 -04:00
|
|
|
}}
|
2024-01-01 14:52:59 -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
|
|
|
>
|
2024-01-01 14:52:59 -04:00
|
|
|
{props.children}
|
2021-08-16 00:43:04 -04:00
|
|
|
</ContextMenuView>
|
2021-02-12 23:48:19 -05:00
|
|
|
);
|
2021-02-10 00:18:40 -05:00
|
|
|
};
|
|
|
|
|
2021-10-06 23:15:32 -04: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,
|
2021-10-06 23:15:32 -04:00
|
|
|
onPressMenuItem: PropTypes.func.isRequired,
|
2021-08-23 15:39:52 -04:00
|
|
|
isMenuPrimaryAction: PropTypes.bool,
|
|
|
|
isButton: PropTypes.bool,
|
2021-08-26 21:31:36 -04:00
|
|
|
previewQRCode: PropTypes.bool,
|
2021-10-21 14:54:32 -04:00
|
|
|
onPress: PropTypes.func,
|
2021-08-26 21:31:36 -04:00
|
|
|
previewValue: PropTypes.string,
|
2021-10-26 16:47:09 -04:00
|
|
|
disabled: PropTypes.bool,
|
2021-02-10 00:18:40 -05:00
|
|
|
};
|