MaterialUI自定义悬停样式

Tim*_*Tim 13 styles reactjs

我是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)}
Run Code Online (Sandbox Code Playgroud)

;

export default withStyles(styles)(EnhancedTable);
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

小智 30

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

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

return <TableRow className={props.classes.tr} ...>
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)


小智 9

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

'&:hover': {
background: "rgb(7, 177, 77, 0.42)",    
             
}
Run Code Online (Sandbox Code Playgroud)

所以,

tableWrapper: {
    overflowX: "auto",
  
  hover: {
    "&:hover": {
      backgroundColor: 'rgb(7, 177, 77, 0.42)'
    },
}
Run Code Online (Sandbox Code Playgroud)


the*_*rwl 7

如果您想要制作一些自定义悬停动画,那么您可以尝试这种风格。
此代码块将更改悬停时卡片的颜色。

欲了解更多信息,请查看此处MUI 中的转换

card : {
    transition: theme.transitions.create(["background", "background-color"], {
      duration: theme.transitions.duration.complex,
    }),
    "&:hover": {
      backgroundColor: "#333",
    },
}

Run Code Online (Sandbox Code Playgroud)