Mui用芯片选择,然后带删除按钮筹码

问题描述 投票:0回答:1
so,我正在尝试按照芯片选择

mui select上的示例代码:

这是我更改为他们的代码的方法:

import * as React from 'react'; import { useTheme } from '@mui/material/styles'; import Box from '@mui/material/Box'; import OutlinedInput from '@mui/material/OutlinedInput'; import InputLabel from '@mui/material/InputLabel'; import MenuItem from '@mui/material/MenuItem'; import FormControl from '@mui/material/FormControl'; import Select from '@mui/material/Select'; import Chip from '@mui/material/Chip'; const ITEM_HEIGHT = 48; const ITEM_PADDING_TOP = 8; const MenuProps = { PaperProps: { style: { maxHeight: ITEM_HEIGHT * 4.5 + ITEM_PADDING_TOP, width: 250, }, }, }; const names = [ 'Oliver Hansen', 'Van Henry', 'April Tucker', 'Ralph Hubbard', 'Omar Alexander', 'Carlos Abbott', 'Miriam Wagner', 'Bradley Wilkerson', 'Virginia Andrews', 'Kelly Snyder', ]; function getStyles(name, personName, theme) { return { fontWeight: personName.includes(name) ? theme.typography.fontWeightMedium : theme.typography.fontWeightRegular, }; } export default function MultipleSelectChip() { const theme = useTheme(); const [personName, setPersonName] = React.useState([]); const handleChange = (event) => { const { target: { value }, } = event; setPersonName( // On autofill we get a stringified value. typeof value === 'string' ? value.split(',') : value, ); }; const handleDelete = (nameToDelete) => { setPersonName((names) => names.filter((name) => name !== nameToDelete) ); }; return ( <div> <FormControl sx={{ m: 1, width: 300 }}> <InputLabel id="demo-multiple-chip-label">Chip</InputLabel> <Select labelId="demo-multiple-chip-label" id="demo-multiple-chip" multiple value={personName} onChange={handleChange} input={<OutlinedInput id="select-multiple-chip" label="Chip" />} renderValue={(selected) => ( <Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}> {selected.map((value) => ( <Chip key={value} label={value} onDelete={(event) => { event.stopPropagation(); // Prevent the select dropdown from opening handleDelete(value); }} /> ))} </Box> )} MenuProps={MenuProps} > {names.map((name) => ( <MenuItem key={name} value={name} style={getStyles(name, personName, theme)} > {name} </MenuItem> ))} </Select> </FormControl> </div> ); }
是在我向Codium核对的情况下,它建议添加此部分:

event.stopPropagation();


但是它仍然无法使用,每当我单击芯片上的(x)按钮时,它都会打开列表,而不是删除芯片。

有人有任何想法吗?

这是SandBox

中的代码((x)给出了鼠标手动图标,但打开列表)。

提供更高的
reactjs material-ui
1个回答
0
投票
chip

元素将起作用。

<Chip
  sx={{ zIndex: 9999 }}
  key={value}
  label={value}
  onDelete={(event) => {
    console.log("Deleting...");
    event.stopPropagation();
    handleDelete(value);
  }}
/>
检查此工作demo

.
说,对于MultiSelect UI,我认为您可以使用

材料AutoCocteTe组件,因为它具有内置功能来处理删除所选项目。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.