BlueWallet/components/TooltipMenu.android.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-08-16 06:43:04 +02:00
import React, { useRef, cloneElement } from 'react';
2021-02-10 06:18:40 +01:00
import PropTypes from 'prop-types';
import showPopupMenu from 'react-native-popup-menu-android';
2021-09-16 02:19:06 +02:00
import { Pressable, TouchableOpacity } from 'react-native';
2021-02-10 06:18:40 +01:00
2021-08-16 06:43:04 +02:00
const ToolTipMenu = props => {
const ref = useRef();
2021-09-16 02:19:06 +02:00
const isMenuPrimaryAction = props.isMenuPrimaryAction ?? false;
// eslint-disable-next-line react/prop-types
const buttonStyle = props.buttonStyle ?? {};
2021-02-10 06:18:40 +01:00
const handleToolTipSelection = selection => {
2021-08-16 06:43:04 +02:00
props.onPress(selection.id);
2021-02-10 06:18:40 +01:00
};
const showMenu = () => {
2021-10-04 05:47:01 +02:00
const menu = [];
for (const actions of props.actions) {
if (Array.isArray(actions)) {
for (const actionToMap of actions) {
menu.push({ id: actionToMap.id, label: actionToMap.text });
}
} else {
menu.push({ id: actions.id, label: actions.text });
}
2021-08-16 06:43:04 +02:00
}
2021-10-04 05:47:01 +02:00
showPopupMenu(menu, handleToolTipSelection, ref.current);
2021-02-10 06:18:40 +01:00
};
2021-08-16 06:43:04 +02:00
const child = (Array.isArray(props.children) ? props.children[0] : props.children) || null;
2021-09-16 02:19:06 +02:00
return isMenuPrimaryAction ? (
<TouchableOpacity style={buttonStyle} ref={ref} onPress={showMenu}>
{child}
</TouchableOpacity>
) : (
2021-08-16 21:12:52 +02:00
<Pressable ref={ref} onLongPress={showMenu}>
{child && cloneElement(child, { onLongPress: showMenu })}
</Pressable>
);
2021-02-10 06:18:40 +01:00
};
2021-08-16 06:43:04 +02:00
export default ToolTipMenu;
2021-02-10 06:18:40 +01:00
ToolTipMenu.propTypes = {
actions: PropTypes.arrayOf(PropTypes.shape).isRequired,
2021-08-17 16:03:55 +02:00
children: PropTypes.node.isRequired,
2021-08-16 19:14:47 +02:00
onPress: PropTypes.func.isRequired,
2021-09-16 02:19:06 +02:00
isMenuPrimaryAction: PropTypes.bool,
2021-02-10 06:18:40 +01:00
};