import { memo, useMemo } from 'react'; import type { MouseEventHandler, ReactElement } from 'react'; import { useToggle } from 'react-use'; import { SortableHandle as sortableHandle } from 'react-sortable-hoc'; import clsx from 'clsx'; import noop from 'lodash/noop'; import MuiGrid from '@mui/material/Grid'; import MuiList from '@mui/material/List'; import MuiMenuItem from '@mui/material/MenuItem'; import MuiTypography from '@mui/material/Typography'; import { useTheme } from '@mui/material/styles'; import { ASSETS_URL } from '../../consts/common'; import { Checkbox } from '../@forms/checkbox'; import { CustomIcon } from '../custom-icon'; import MenuItemActionIcon from './MenuItemActionIcon'; import type { MenuItemActionIconProps, MenuItemProps } from './types'; import createClasses from './styles'; const preventDefault: MouseEventHandler = e => e.preventDefault(); const SortableHandle = sortableHandle( (props: { index: number; styles: ReturnType }) => { const { index, styles } = props; return ( ); } ); const MenuItem = ({ modifyMenuItem, ...props }: MenuItemProps) => { const modifiedProps: MenuItemProps = modifyMenuItem ? { ...props, ...modifyMenuItem(props) } : props; const { id, name, value, multiple, selected, styles = createClasses({}), sortable, deletable, disabled, hidden, handleClick, handleDelete, index = 0, showSelectedIcon, checkboxProps, textNode, ariaLabelledby, nestedListItems, actionIcons = [], options, classes, style, ...otherProps } = modifiedProps; const theme = useTheme(); const [hover, toggleHover] = useToggle(false); const icon = useMemo(() => { if (!checkboxProps?.icon) { return theme.components?.MuiCheckbox?.defaultProps?.icon; } return hover ? theme.components?.MuiCheckbox?.defaultProps?.icon : checkboxProps?.icon; }, [checkboxProps?.icon, hover, theme.components?.MuiCheckbox?.defaultProps?.icon]); const optionsToRender = useMemo(() => options?.filter(item => !item.hidden), [options]); const nestedListLabel = useMemo( () => textNode || ( {name} {optionsToRender?.filter(item => !!item.selected).length || 0} of{' '} {optionsToRender?.length} ), [name, optionsToRender, textNode] ); const { disabled: disabledCheckbox } = checkboxProps || {}; const key = `key_${id || name}${index}`; const menuItemActionIcons = [...actionIcons]; if (deletable && !nestedListItems) { const deleteActionIcon: MenuItemActionIconProps = { 'data-testid': `deleteIcon_${index}`, iconSrc: `${ASSETS_URL}/icons/icon_trash.svg`, handleClick: handleDelete }; menuItemActionIcons.push(deleteActionIcon); } if (selected && !multiple && showSelectedIcon) { const singleCheckedIcon: MenuItemActionIconProps = { 'data-testid': `singleCheckedIcon`, iconSrc: `${ASSETS_URL}/icons/icon_checkbox.svg` }; menuItemActionIcons.push(singleCheckedIcon); } return nestedListItems ? ( ) : ( ); }; export default memo(MenuItem);