2024-05-20 10:54:13 +01:00
|
|
|
import React, { forwardRef, Ref, useCallback, useMemo } from 'react';
|
2021-10-22 02:45:48 -04:00
|
|
|
import { TouchableOpacity } from 'react-native';
|
2024-05-20 10:54:13 +01:00
|
|
|
import { ContextMenuButton, ContextMenuView, RenderItem } from 'react-native-ios-context-menu';
|
|
|
|
|
|
|
|
import { Action, ToolTipMenuProps } from './types';
|
2021-02-10 00:18:40 -05:00
|
|
|
|
2024-05-18 12:48:03 -04:00
|
|
|
const BaseToolTipMenu = (props: ToolTipMenuProps, ref: Ref<any>) => {
|
2024-05-18 12:56:57 -04:00
|
|
|
const {
|
|
|
|
title = '',
|
|
|
|
isButton = false,
|
|
|
|
isMenuPrimaryAction = false,
|
|
|
|
renderPreview,
|
|
|
|
disabled = false,
|
|
|
|
onPress,
|
|
|
|
onMenuWillShow,
|
|
|
|
onMenuWillHide,
|
|
|
|
buttonStyle,
|
|
|
|
onPressMenuItem,
|
2024-05-25 17:50:46 -04:00
|
|
|
...restProps
|
2024-05-18 12:56:57 -04:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const menuItemMapped = useCallback(({ action, menuOptions }: { action: Action; menuOptions?: string[] }) => {
|
2024-05-18 12:48:03 -04:00
|
|
|
const item: any = {
|
2024-05-17 18:48:00 -04:00
|
|
|
actionKey: action.id.toString(),
|
2021-08-23 15:39:52 -04:00
|
|
|
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;
|
2024-05-18 12:56:57 -04:00
|
|
|
}, []);
|
2021-10-03 23:47:01 -04:00
|
|
|
|
2024-05-18 12:56:57 -04:00
|
|
|
const menuItems = useMemo(
|
|
|
|
() =>
|
|
|
|
props.actions.map(action => {
|
|
|
|
if (Array.isArray(action)) {
|
|
|
|
const mapped = action.map(actionToMap => menuItemMapped({ action: actionToMap }));
|
|
|
|
return {
|
|
|
|
menuOptions: ['displayInline'],
|
|
|
|
menuItems: mapped,
|
|
|
|
menuTitle: '',
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return menuItemMapped({ action });
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[props.actions, menuItemMapped],
|
|
|
|
);
|
2023-07-25 14:50:04 +01:00
|
|
|
|
2024-05-18 12:56:57 -04:00
|
|
|
const handlePressMenuItem = useCallback(
|
|
|
|
({ nativeEvent }: { nativeEvent: { actionKey: string } }) => {
|
|
|
|
onPressMenuItem(nativeEvent.actionKey);
|
|
|
|
},
|
|
|
|
[onPressMenuItem],
|
|
|
|
);
|
2024-05-18 12:48:03 -04:00
|
|
|
|
2024-05-18 12:56:57 -04:00
|
|
|
const renderContextMenuButton = () => (
|
|
|
|
<ContextMenuButton
|
2024-04-01 19:12:06 -04:00
|
|
|
ref={ref}
|
2024-05-18 12:56:57 -04:00
|
|
|
onMenuWillShow={onMenuWillShow}
|
|
|
|
onMenuWillHide={onMenuWillHide}
|
2024-04-01 19:12:06 -04:00
|
|
|
useActionSheetFallback={false}
|
2024-05-18 12:56:57 -04:00
|
|
|
onPressMenuItem={handlePressMenuItem}
|
|
|
|
isMenuPrimaryAction={isMenuPrimaryAction}
|
2024-04-01 19:12:06 -04:00
|
|
|
menuConfig={{
|
2024-05-18 12:48:03 -04:00
|
|
|
menuTitle: title,
|
2024-04-01 19:12:06 -04:00
|
|
|
menuItems,
|
|
|
|
}}
|
2024-05-25 17:50:46 -04:00
|
|
|
{...restProps}
|
|
|
|
style={buttonStyle}
|
2024-04-01 19:12:06 -04:00
|
|
|
>
|
2024-05-25 17:50:46 -04:00
|
|
|
<TouchableOpacity onPress={onPress} disabled={disabled} accessibilityRole="button" {...restProps}>
|
|
|
|
{props.children}
|
|
|
|
</TouchableOpacity>
|
2024-05-18 12:56:57 -04:00
|
|
|
</ContextMenuButton>
|
|
|
|
);
|
|
|
|
|
|
|
|
const renderContextMenuView = () => (
|
2021-08-16 00:43:04 -04:00
|
|
|
<ContextMenuView
|
2021-10-06 23:15:32 -04:00
|
|
|
ref={ref}
|
2024-05-12 16:37:30 -04:00
|
|
|
lazyPreview
|
|
|
|
shouldEnableAggressiveCleanup
|
2024-05-18 12:56:57 -04:00
|
|
|
internalCleanupMode="automatic"
|
|
|
|
onPressMenuItem={handlePressMenuItem}
|
2024-01-01 14:52:59 -04:00
|
|
|
useActionSheetFallback={false}
|
2021-08-16 00:43:04 -04:00
|
|
|
menuConfig={{
|
2024-05-18 12:48:03 -04:00
|
|
|
menuTitle: title,
|
2021-10-03 23:47:01 -04:00
|
|
|
menuItems,
|
2021-08-16 00:43:04 -04:00
|
|
|
}}
|
2024-03-03 09:56:22 -04:00
|
|
|
{...(renderPreview
|
2021-08-26 21:31:36 -04:00
|
|
|
? {
|
|
|
|
previewConfig: {
|
|
|
|
previewType: 'CUSTOM',
|
|
|
|
backgroundColor: 'white',
|
|
|
|
},
|
2024-05-18 12:48:03 -04:00
|
|
|
renderPreview: renderPreview as RenderItem,
|
2021-08-26 21:31:36 -04:00
|
|
|
}
|
|
|
|
: {})}
|
2021-08-16 00:43:04 -04:00
|
|
|
>
|
2024-05-18 21:25:31 -04:00
|
|
|
{onPress ? (
|
2024-05-25 17:50:46 -04:00
|
|
|
<TouchableOpacity accessibilityRole="button" onPress={onPress} {...restProps}>
|
2024-05-18 21:25:31 -04:00
|
|
|
{props.children}
|
|
|
|
</TouchableOpacity>
|
|
|
|
) : (
|
|
|
|
props.children
|
|
|
|
)}
|
2021-08-16 00:43:04 -04:00
|
|
|
</ContextMenuView>
|
2021-02-12 23:48:19 -05:00
|
|
|
);
|
2024-05-18 12:56:57 -04:00
|
|
|
|
2024-05-18 22:04:52 -04:00
|
|
|
return isMenuPrimaryAction && onPress ? (
|
2024-05-25 17:50:46 -04:00
|
|
|
<TouchableOpacity onPress={onPress} disabled={disabled} accessibilityRole="button" {...restProps}>
|
2024-05-18 12:56:57 -04:00
|
|
|
{renderContextMenuButton()}
|
|
|
|
</TouchableOpacity>
|
2024-05-18 22:04:52 -04:00
|
|
|
) : isButton ? (
|
|
|
|
renderContextMenuButton()
|
2024-05-18 12:56:57 -04:00
|
|
|
) : (
|
2024-05-18 21:25:31 -04:00
|
|
|
renderContextMenuView()
|
2024-05-18 12:56:57 -04:00
|
|
|
);
|
2021-02-10 00:18:40 -05:00
|
|
|
};
|
|
|
|
|
2024-04-06 19:54:10 -04:00
|
|
|
const ToolTipMenu = forwardRef(BaseToolTipMenu);
|
|
|
|
|
2024-03-29 21:22:05 -04:00
|
|
|
export default ToolTipMenu;
|