admin管理员组文章数量:1332896
So I want to display selected values as <Chip />
outside of the <TextField />
in <Autoplete />
. I was able to display values as state
. However, I still have trouble deleting those chips, i.e. not just the display, but also change the selected
prop in <Autoplete />
. Does anyone have an idea?
const [val, setVal] = useState([]);
const valHtml = val.map((option, index) => (
<Chip
label={option.title}
deleteIcon={<RemoveIcon />}
onDelete={() => {}}
/>
));
return (
<div>
<Autoplete
multiple
filterSelectedOptions
options={top100Films}
onChange={(e, newValue) => setVal(newValue)}
getOptionLabel={option => option.title}
renderTags={() => {}}
renderInput={params => (
<TextField
{...params}
variant="standard"
placeholder="Favorites"
margin="normal"
fullWidth
/>
)}
/>
<div className="selectedTags">{valHtml}</div>
</div>
);
}
Complete code here
So I want to display selected values as <Chip />
outside of the <TextField />
in <Autoplete />
. I was able to display values as state
. However, I still have trouble deleting those chips, i.e. not just the display, but also change the selected
prop in <Autoplete />
. Does anyone have an idea?
const [val, setVal] = useState([]);
const valHtml = val.map((option, index) => (
<Chip
label={option.title}
deleteIcon={<RemoveIcon />}
onDelete={() => {}}
/>
));
return (
<div>
<Autoplete
multiple
filterSelectedOptions
options={top100Films}
onChange={(e, newValue) => setVal(newValue)}
getOptionLabel={option => option.title}
renderTags={() => {}}
renderInput={params => (
<TextField
{...params}
variant="standard"
placeholder="Favorites"
margin="normal"
fullWidth
/>
)}
/>
<div className="selectedTags">{valHtml}</div>
</div>
);
}
Complete code here
Share Improve this question asked Nov 18, 2019 at 23:46 user2683470user2683470 1891 gold badge3 silver badges15 bronze badges1 Answer
Reset to default 7You need two things:
- Appropriate logic in the
onDelete
of theChip
such as:
onDelete={() => {
setVal(val.filter(entry => entry !== option));
}}
- Specify the value (which you are already managing in state) on the
Autoplete
:
<Autoplete
value={val}
// ... other properties
/>
Here is a working version of your sandbox: https://codesandbox.io/s/autoplete-with-chips-85rqq
本文标签: javascriptHow to remove a selected Chip outside of Autocomplete in material UIStack Overflow
版权声明:本文标题:javascript - How to remove a selected Chip outside of Autocomplete in material UI - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313172a2451287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论