【问题标题】:Changing the color of the Inactive Tab Color in Material UI在 Material UI 中更改非活动选项卡颜色的颜色
【发布时间】:2020-11-10 08:58:56
【问题描述】:

在我的应用程序中,我想将活动选项卡的颜色保持为红色,将非活动选项卡的颜色保持为蓝色。

样式如下:

newStyle: {
    backgroundColor: 'red',
    '&$selected': {
        backgroundColor: 'blue'
    },
}

这里是渲染函数:

<Tabs
    variant="fullWidth"
    value={this.state.value}
    onChange={(event, newValue) => {
        this.setState({ value: newValue })
    }}
    aria-label="simple tabs example">
    {
        this.props.devices.map((device, itr) => {
            let alertsCount = this.props.alerts ? this.props.alerts.count ? this.props.alerts.count[device.id] : 0 : 0
            return <Tab className = {classes.newStyle} label={<Badge variant="dot" badgeContent={alertsCount} color="secondary">{device.machineName}</Badge>} {...a11yProps(itr)} />
        })
    }
</Tabs>

如您所见,我使用classes.newStyle 调用样式,在调用它之后,活动和非活动选项卡的颜色都变为红色,尽管非活动选项卡的颜色变为较浅的红色阴影。但是,我希望非活动标签的颜色保持为蓝色。

【问题讨论】:

    标签: css reactjs material-ui


    【解决方案1】:

    可以使用&amp;.Mui-selected 选择器来完成。

    tab: {
      background: 'green',
      '&.Mui-selected': {
        background: 'red'
      }
    },
    

    source

    https://codesandbox.io/s/material-demo-forked-n2cxl

    【讨论】:

    • 非常感谢您的回答。
    • 类名现在因Tab API class 中的selected 属性而异。就我而言,我使用 &amp;.MuiTab-selected 让它工作
    • 谢谢@Nicole。我不确定你是什么意思。我找不到文档提到MuiTab-selected 的地方,我的代码适用于最新的材料-ui (@4.11.4)。
    猜你喜欢
    • 2018-03-26
    • 2016-04-12
    • 1970-01-01
    • 2018-06-02
    • 2016-07-26
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多