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