skip to Main Content

I have a created checkbox to each of MenuItem in a MUI Menu. And a user can select more than one menu item at a time.

Is there a way to get the selected menu item values ?

Because if I put event.target.value it is giving me on in my alert statement.

https://stackblitz.com/edit/react-avdghj?file=Demo.js

Kindly guide me on how to achieve this.
Any help is much appreciated

Thanks

2

Answers


  1. You can create a state that keeps track of the selected menu items and update it whenever a menu item is clicked.

    I also added a useEffect to log selected items whenever they change

    import * as React from 'react';
    import Box from '@mui/material/Box';
    import Avatar from '@mui/material/Avatar';
    import Menu from '@mui/material/Menu';
    import MenuItem from '@mui/material/MenuItem';
    import ListItemIcon from '@mui/material/ListItemIcon';
    import Divider from '@mui/material/Divider';
    import IconButton from '@mui/material/IconButton';
    import Typography from '@mui/material/Typography';
    import Tooltip from '@mui/material/Tooltip';
    import PersonAdd from '@mui/icons-material/PersonAdd';
    import Settings from '@mui/icons-material/Settings';
    import Logout from '@mui/icons-material/Logout';
    import Checkbox from '@mui/material/Checkbox';
    
    export default function AccountMenu() {
      const [anchorEl, setAnchorEl] = React.useState(null);
      const open = Boolean(anchorEl);
    
      const [selectedItems, setSelectedItems] = React.useState([]);
    
      const handleClick = (event) => {
        setAnchorEl(event.currentTarget);
      };
    
      const handleClose = () => {
        setAnchorEl(null);
      };
    
      const handleItemClick = (item) => {
        if (selectedItems.includes(item)) {
          setSelectedItems(
            selectedItems.filter((selectedItem) => selectedItem !== item)
          );
        } else {
          setSelectedItems([...selectedItems, item]);
        }
      };
    
      React.useEffect(() => {
        console.log(selectedItems);
      }, [selectedItems]);
    
      return (
        <React.Fragment>
          <Box sx={{ display: 'flex', alignItems: 'center', textAlign: 'center' }}>
            <Tooltip title="Account settings">
              <IconButton
                onClick={handleClick}
                size="small"
                sx={{ ml: 2 }}
                aria-controls={open ? 'account-menu' : undefined}
                aria-haspopup="true"
                aria-expanded={open ? 'true' : undefined}
              >
                <Avatar sx={{ width: 32, height: 32 }}>M</Avatar>
              </IconButton>
            </Tooltip>
          </Box>
          <Menu
            anchorEl={anchorEl}
            id="account-menu"
            open={open}
            onClose={handleClose}
            PaperProps={{
              elevation: 0,
              sx: {
                overflow: 'visible',
                filter: 'drop-shadow(0px 2px 8px rgba(0,0,0,0.32))',
                mt: 1.5,
                '& .MuiAvatar-root': {
                  width: 32,
                  height: 32,
                  ml: -0.5,
                  mr: 1,
                },
                '&::before': {
                  content: '""',
                  display: 'block',
                  position: 'absolute',
                  top: 0,
                  right: 14,
                  width: 10,
                  height: 10,
                  bgcolor: 'background.paper',
                  transform: 'translateY(-50%) rotate(45deg)',
                  zIndex: 0,
                },
              },
            }}
            transformOrigin={{ horizontal: 'right', vertical: 'top' }}
            anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }}
          >
            <MenuItem onClick={() => handleItemClick('profile')}>
              <Checkbox checked={selectedItems.includes('profile')} /> Profile
            </MenuItem>
            <MenuItem onClick={() => handleItemClick('account')}>
              <Checkbox checked={selectedItems.includes('account')} /> My account
            </MenuItem>
          </Menu>
        </React.Fragment>
      );
    }
    
    
    Login or Signup to reply.
  2. Try with Mui Autocomplete. You can add a checkbox in menu items and it will return the array of values in the value of handleChange.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search