MaterialUI 自定义悬停样式

新手上路,请多包涵

我是 React 的新手,我对如何在 Material UI 中覆盖类有点困惑。我查看了这些示例并尝试模仿它,但它似乎并没有按照我的意愿去做。

基本上,在表格行悬停时,我想让它设置与当前所做的不同的背景颜色。

这是我的方法:

 const styles = theme => ({
  root: {
    width: "100%",
    marginTop: theme.spacing.unit * 3
  },
  table: {
    minWidth: 1020
  },
  tableWrapper: {
    overflowX: "auto"
  },
  hover: {
    "&:hover": {
      backgroundColor: 'rgb(7, 177, 77, 0.42)'
    }
  }
});

return <TableRow hover classes={{hover: classes.hover}} role="checkbox" aria-checked={isSelected} tabIndex={-1} key={n.row_id} selected={isSelected}>
     {this.insertRow(n, isSelected, counter, checkbox)}

;

 export default withStyles(styles)(EnhancedTable);

谢谢你的帮助!

原文由 Tim 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 694
2 个回答

您应该将 TableRow 的键定义为类名,然后将悬停样式作为对象放在该类名上。

 const styles = theme => ({
  ...
  tr: {
    background: "#f1f1f1",
    '&:hover': {
       background: "#f00",
    },
  },
  ...
});

return <TableRow className={props.classes.tr} ...>

在另一个示例中,它将是这样的:

 const styles = {
  tr: {
    background: "#f1f1f1",
    '&:hover': {
      background: "#f00",
    }
  }
};

function Table(props) {
  return (
    <Table>
      <TableRow className={props.classes.tr}>
        {"table row"}
      </TableRow>
    </Table>
  );
}

export default withStyles(styles)(Table);

原文由 Spleen 发布,翻译遵循 CC BY-SA 4.0 许可协议

通过添加一个简单的语句,您可以自定义悬停属性。

 '&:hover': {
background: "rgb(7, 177, 77, 0.42)",

}

所以,

 tableWrapper: {
    overflowX: "auto",

  hover: {
    "&:hover": {
      backgroundColor: 'rgb(7, 177, 77, 0.42)'
    },
}

原文由 Rana Muhammad Usama 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进