BlueWallet/components/TooltipMenu.android.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

import React, { useRef, useEffect, forwardRef } from 'react';
2021-02-10 06:18:40 +01:00
import PropTypes from 'prop-types';
import { TouchableOpacity } from 'react-native';
2021-10-20 14:53:17 +02:00
import showPopupMenu from '../blue_modules/showPopupMenu';
2021-02-10 06:18:40 +01:00
const ToolTipMenu = (props, ref) => {
const menuRef = 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 => {
props.onPressMenuItem(selection.id);
};
useEffect(() => {
2021-10-07 07:00:20 +02:00
if (ref && ref.current) {
ref.current.dismissMenu = dismissMenu;
}
}, [ref]);
const dismissMenu = () => {
console.log('dismissMenu Not implemented');
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, menuRef.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;
return (
<TouchableOpacity
style={buttonStyle}
ref={menuRef}
{...(isMenuPrimaryAction ? { onPress: showMenu } : { onPress: props.onPress, onLongPress: showMenu })}
>
2021-09-16 02:19:06 +02:00
{child}
</TouchableOpacity>
2021-08-16 21:12:52 +02:00
);
2021-02-10 06:18:40 +01:00
};
export default forwardRef(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,
onPressMenuItem: PropTypes.func.isRequired,
2021-09-16 02:19:06 +02:00
isMenuPrimaryAction: PropTypes.bool,
onPress: PropTypes.func,
2021-02-10 06:18:40 +01:00
};