在材质用户界面中更改非活动选项卡颜色的颜色

时间:2020-11-10 08:58:56

标签: css reactjs material-ui

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

样式如下:

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调用样式,并且在调用它之后,活动和非活动标签的颜色都变为红色,尽管非活动标签的颜色变为了较浅的红色。但是,我希望非活动标签的颜色保持蓝色。

1 个答案:

答案 0 :(得分:1)

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

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

source

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