【问题标题】:Can't override Fab disabled color material - ui无法覆盖 Fab 禁用的颜色材料 - ui
【发布时间】:2020-02-17 07:47:12
【问题描述】:

无法更改禁用按钮的样式。我尝试了这里讨论的方法https://github.com/mui-org/material-ui/issues/13779

梅版-

"@material-ui/core": "3.8.1",

"@material-ui/icons": "3.0.1",

           const styles = theme => ({
              fabButton: {
                 boxShadow: 'none',
                 backgroundColor: '#fff', 
              },
              disabled: {
                 backgroundColor: '#fff', 
              },
             icon: {
                width: '20px', 
                 height: '20px', 
                 color: grey[600],
               },
            });


          <Hint title="Previous">
              <Fab 
                size="small" 
                classes={{
                  root: classes.fabButton,
                  disabled: classes.disabled
                }}
                disabled={true}
                component="div"
                onClick={onClickHandle}
              >
                <IconChevronLeft className={classes.icon} />
              </Fab>
            </Hint>


           const styles = theme => ({
              fabButton: {
                 boxShadow: 'none',
                 backgroundColor: '#fff', 
                 '&:disabled': {
                      backgroundColor: '#fff',   
                 }
              },
              icon: {
                 width: '20px', 
                 height: '20px', 
                 color: grey[600],
               },
            });


          <Hint title="Previous">
              <Fab 
                size="small" 
                className={classes.fabButton}
                disabled={true}
                component="div"
                onClick={onClickHandle}
              >
                <IconChevronLeft className={classes.icon} />
              </Fab>
            </Hint>

disabled 两种方式都不会应用自定义样式,而是采用默认样式。任何帮助将不胜感激。

请在此处查看演示

https://codesandbox.io/s/material-demo-rh06m

【问题讨论】:

  • 能否请您发布该组件的完整代码?如果这是整个代码,那么您需要在导出时将组件包装在 withStyles HOC 中。
  • 已经做到了。将通过沙箱共享代码。
  • @lek 请查看演示,编辑问题。

标签: reactjs material-ui


【解决方案1】:

以下方法有效:

const styles = theme => ({
  fab: {
    margin: theme.spacing.unit,
    "&$disabled": {
      backgroundColor: "red"
    }
  },
  disabled:{},
  icon: {
    color: "#000"
  },
  extendedIcon: {
    marginRight: theme.spacing.unit
  }
});

function FloatingActionButtons(props) {
  const { classes } = props;
  return (
    <div>
      <Tooltip title="F">
        <Fab
          disabled
          aria-label="Delete"
          classes={{root: classes.fab, disabled: classes.disabled}}
          component="div"
        >
          <DeleteIcon className={classes.icon} />
        </Fab>
      </Tooltip>
    </div>
  );
}

【讨论】:

  • 你能解释一下区别吗?为什么会这样?
  • @David 类对象的结构不同,disabled 为空,而 OP 已指定样式。使用 classes 覆盖样式比指定 className 属性更精细。有关使用 &amp;$disabled 的更多信息 in the docs
  • 我使用 material-ui 已经好几年了。我仍然停留在 v3 上,css 类系统很好,但是代码中的 css 实现被搞砸了,完全无法理解。对我不起作用。希望 v4 会更好。
猜你喜欢
  • 2020-05-16
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 2018-11-24
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多