/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
docs/data/material/components/menus/SimpleListMenu.tsx
81 строка
2 KB
Silviu Alexandru Avram
[docs] Update menu examples to always have aria-expanded (#48211)
10 апр 2026, 14:57
Не верифицирован
10 апр 2026, 14:57
829e381
Код
Авторство
О чём код?
import * as React from 'react'; import List from '@mui/material/List'; import ListItemButton from '@mui/material/ListItemButton'; import ListItemText from '@mui/material/ListItemText'; import MenuItem from '@mui/material/MenuItem'; import Menu from '@mui/material/Menu'; const options = [ 'Show some love to MUI', 'Show all notification content', 'Hide sensitive notification content', 'Hide all notification content', ]; export default function SimpleListMenu() { const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null); const [selectedIndex, setSelectedIndex] = React.useState(1); const open = Boolean(anchorEl); const handleClickListItem = (event: React.MouseEvent<HTMLElement>) => { setAnchorEl(event.currentTarget); }; const handleMenuItemClick = ( event: React.MouseEvent<HTMLElement>, index: number, ) => { setSelectedIndex(index); setAnchorEl(null); }; const handleClose = () => { setAnchorEl(null); }; return ( <div> <List component="nav" aria-label="Device settings" sx={{ bgcolor: 'background.paper' }} > <ListItemButton id="lock-button" aria-haspopup="listbox" aria-controls="lock-menu" aria-label="when device is locked" aria-expanded={open} onClick={handleClickListItem} > <ListItemText primary="When device is locked" secondary={options[selectedIndex]} /> </ListItemButton> </List> <Menu id="lock-menu" anchorEl={anchorEl} open={open} onClose={handleClose} slotProps={{ list: { 'aria-labelledby': 'lock-button', role: 'listbox', }, }} > {options.map((option, index) => ( <MenuItem key={option} disabled={index === 0} selected={index === selectedIndex} onClick={(event) => handleMenuItemClick(event, index)} > {option} </MenuItem> ))} </Menu> </div> ); }