如何覆盖Material-UI中Chip组件的deleteIcon/cancelIcon配置?
解决方案
你可以通过自定义主题配置实现全局覆盖,以下是两种适配不同需求的实现方案:
方案1:仅修改Autocomplete内部生成的Chip删除图标(不影响全局其他Chip)
该方案只会作用于多选Autocomplete内部自动生成的Chip,不会改动项目中其他单独引用的Chip组件:
import { createTheme } from '@mui/material/styles'; import Chip from '@mui/material/Chip'; // 替换为你想要使用的目标图标组件 import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline'; const customTheme = createTheme({ components: { MuiAutocomplete: { defaultProps: { // 重写多选模式下默认的标签渲染逻辑 renderTags: (value, getTagProps) => value.map((option, index) => ( <Chip {...getTagProps({ index })} label={option.label} // 替换默认删除图标 deleteIcon={<DeleteOutlineIcon />} /> )), }, }, }, });
如果你的选项值展示字段不是label,对应修改label属性的取值逻辑即可。
方案2:全局修改所有Chip的删除图标(包含Autocomplete内部生成的)
如果你希望项目中所有Chip组件的删除图标统一替换,使用该方案更简便:
import { createTheme } from '@mui/material/styles'; // 替换为你想要使用的目标图标组件 import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline'; const customTheme = createTheme({ components: { MuiChip: { defaultProps: { deleteIcon: <DeleteOutlineIcon />, }, }, }, });

内容的提问来源于stack exchange,提问作者franciscojs
相关产品推荐
相关产品推荐

