import { MoreVert as MoreVertIcon } from '@mui/icons-material'; import { Box } from '@mui/material'; import { FC, MouseEventHandler, useCallback, useMemo, useState } from 'react'; import ContainedButton from './ContainedButton'; import IconButton from './IconButton/IconButton'; import Menu from './Menu'; const ButtonWithMenu: FC = (props) => { const { children, containedButtonProps, iconButtonProps, muiMenuProps, onButtonClick, onItemClick, variant = 'icon', ...menuProps } = props; const [anchorEl, setAnchorEl] = useState(null); const open = useMemo(() => Boolean(anchorEl), [anchorEl]); const buttonContent = useMemo(() => { if (children) { return children; } if (variant === 'icon') { return ; } return 'Options'; }, [children, iconButtonProps?.size, variant]); const buttonClickHandler = useCallback>( (...args) => { const { 0: { currentTarget }, } = args; setAnchorEl(currentTarget); return onButtonClick?.call(null, ...args); }, [onButtonClick], ); const buttonElement = useMemo(() => { if (variant === 'contained') { return ( {buttonContent} ); } return ( {buttonContent} ); }, [ buttonClickHandler, buttonContent, containedButtonProps, iconButtonProps, variant, ]); const itemClickHandler = useCallback< Exclude >( (key, value, ...rest) => { setAnchorEl(null); return onItemClick?.call(null, key, value, ...rest); }, [onItemClick], ); return ( {buttonElement} setAnchorEl(null), ...muiMenuProps, }} onItemClick={itemClickHandler} open={open} {...menuProps} /> ); }; export default ButtonWithMenu as ( ...args: Parameters>> ) => ReturnType>>;