我想做什么:
我正在尝试为我的EnhancedTable
组件提供自定义样式的选项,用户可以通过传递一个包含属性的样式对象,如headCellColor
,headCellBackgroundColor
,bodyCellColor
,bodyCellBackgroundColor
等,这些属性可以用于在TableHead
和TableBody
中着色单元格。
在TableHead
组件中,我使用了一个TableSortLabel
,类似于这个material-ui文档示例中所做的https://material-ui.com/components/tables/#sorting-amp-selecting
我希望基于用户提供的props,在鼠标悬停和激活状态时自定义颜色文本和箭头图标。
让我们看看不同情况下TableSortLabel
的颜色:
文本的颜色最初是灰色,没有箭头。当鼠标悬停在上面时,会出现一个灰色的箭头,并且文本变为黑色。单击后,设置为活动状态,灰色箭头变为黑色,并且文本永久变为黑色,直到删除活动状态。
我已经尝试过:
const useStyles = makeStyles({
tableSortLabel: props => ({
backgroundColor: "blue",
color: props.headCellColor,
fill: props.headCellColor,
"&:hover": {
backgroundColor: "blue"
}
})
});
function EnhancedTableHeadCell(props) {
const { isActive, onHoverSortState, clickHandler, ...otherProps } = props;
const classes = useStyles(props.styles);
return (
<FancyTableCell styles={props.styles} {...otherProps}>
<TableSortLabel
active={isActive}
classes={{
icon: classes.tableSortLabel,
active: classes.tableSortLabel
}}
direction={onHoverSortState}
onClick={clickHandler}
>
{props.children}
</TableSortLabel>
</FancyTableCell>
);
}
这是在浏览器中的样子:https://i.postimg.cc/fW7W2MRB/c1.jpg。
第一个是正常的标题,第二个是悬停时显示的,第三个是点击后的状态。
从我们观察到的情况来看,在所有三种情况下(正常、悬停、激活),文本颜色完全不受
color
CSS 属性的影响。在悬停时,backgroundColor
只影响图标而不影响文本。然而,我们可以看到当它处于激活状态时,backgroundColor
影响文本。图标一切如常,唯一的问题就在于文本。我可能做错了什么?我该如何解决这个问题?