【发布时间】:2019-11-03 01:21:12
【问题描述】:
我想做的事:
我正在尝试通过传入包含headCellColor、headCellBackgroundColor、bodyCellColor、bodyCellBackgroundColor 等属性的样式对象,为用户提供为我的EnhancedTable 组件提供自定义样式的选项,这些属性可以用于为TableHead 和TableBody 中的单元格着色。
在TableHead 组件中,我使用TableSortLabel 的方式类似于他们在这个material-ui 文档示例中所做的:https://material-ui.com/components/tables/#sorting-amp-selecting
我希望根据用户提供的道具在悬停时和活动时自定义文本和箭头图标的颜色。
让我们看看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 在文本处于活动状态时会影响它。使用图标,一切都按预期进行。唯一的问题是文本。
我可能做错什么?我该如何解决我的问题?
【问题讨论】:
标签: javascript html css reactjs material-ui