2024-12-22 15:37:45 -05:00
|
|
|
import { Chip, Menu, MenuItem, Typography } from '@mui/joy'
|
2024-11-28 21:21:23 -05:00
|
|
|
import IconButton from '@mui/joy/IconButton'
|
|
|
|
import React, { useEffect, useRef, useState } from 'react'
|
2024-12-28 18:52:06 -05:00
|
|
|
import { getTextColorFromBackgroundColor } from '../../utils/Colors.jsx'
|
2024-11-28 21:21:23 -05:00
|
|
|
|
|
|
|
const IconButtonWithMenu = ({
|
|
|
|
key,
|
|
|
|
icon,
|
|
|
|
options,
|
|
|
|
onItemSelect,
|
|
|
|
selectedItem,
|
|
|
|
setSelectedItem,
|
|
|
|
isActive,
|
|
|
|
useChips,
|
2024-12-22 15:37:45 -05:00
|
|
|
title,
|
2024-11-28 21:21:23 -05:00
|
|
|
}) => {
|
|
|
|
const [anchorEl, setAnchorEl] = useState(null)
|
|
|
|
const menuRef = useRef(null)
|
|
|
|
|
|
|
|
const handleMenuOpen = event => {
|
|
|
|
setAnchorEl(event.currentTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleMenuClose = () => {
|
|
|
|
setAnchorEl(null)
|
|
|
|
}
|
|
|
|
useEffect(() => {
|
|
|
|
document.addEventListener('mousedown', handleMenuOutsideClick)
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('mousedown', handleMenuOutsideClick)
|
|
|
|
}
|
|
|
|
}, [anchorEl])
|
|
|
|
|
|
|
|
const handleMenuOutsideClick = event => {
|
|
|
|
if (menuRef.current && !menuRef.current.contains(event.target)) {
|
|
|
|
handleMenuClose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<IconButton
|
|
|
|
onClick={handleMenuOpen}
|
|
|
|
variant='outlined'
|
|
|
|
color={isActive ? 'primary' : 'neutral'}
|
|
|
|
size='sm'
|
|
|
|
sx={{
|
|
|
|
height: 24,
|
|
|
|
borderRadius: 24,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{icon}
|
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
<Menu
|
|
|
|
key={key}
|
|
|
|
ref={menuRef}
|
|
|
|
anchorEl={anchorEl}
|
|
|
|
open={Boolean(anchorEl)}
|
|
|
|
onClose={handleMenuClose}
|
|
|
|
>
|
2024-12-22 15:37:45 -05:00
|
|
|
{title && (
|
|
|
|
<MenuItem key={`${key}-title`} disabled>
|
|
|
|
<Typography level='body-sm' sx={{ fontWeight: 'bold' }}>
|
|
|
|
{title}
|
|
|
|
</Typography>
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
2024-11-28 21:21:23 -05:00
|
|
|
{options?.map(item => (
|
|
|
|
<MenuItem
|
|
|
|
key={`${key}-${item?.id}`}
|
|
|
|
onClick={() => {
|
|
|
|
onItemSelect(item)
|
|
|
|
setSelectedItem?.selectedItem(item.name)
|
|
|
|
handleMenuClose()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{useChips ? (
|
|
|
|
<Chip
|
|
|
|
sx={{
|
|
|
|
backgroundColor: item.color ? item.color : null,
|
|
|
|
color: getTextColorFromBackgroundColor(item.color),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{item.name}
|
|
|
|
</Chip>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{item?.icon}
|
|
|
|
{item.name}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</MenuItem>
|
|
|
|
))}
|
|
|
|
{/* {selectedItem && selectedItem !== 'All' && (
|
|
|
|
<MenuItem
|
|
|
|
id={`${id}cancel-all-filters`}
|
|
|
|
onClick={() => {
|
|
|
|
onItemSelect(null)
|
|
|
|
setSelectedItem?.setSelectedItem('All')
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Cancel All Filters
|
|
|
|
</MenuItem>
|
|
|
|
)} */}
|
|
|
|
</Menu>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
export default IconButtonWithMenu
|