Material-UI 样式覆盖?

Material-UI Style Override?

我正在将我的应用程序从 Material-UI v1 更新到 v2。我正在尝试使用样式覆盖来设置所选 <BottomNavigationAction> 元素的颜色。

const styles = {
    bottomNavStyle: {
        position: 'fixed',
        left: '0px',
        bottom: '0px',
        height: '50px',
        width: '100%',
        zIndex: '100'
    },
    '&$selected': {
        color: "#00bcd4"  //<==trying to add this color to selected items
    },
};


class bottom_nav extends Component {
    state = {
        selectedIndex: -1,
    };

    handleChange = (event, value) => {
        this.setState({value});
    };


    render() {
        const { classes } = this.props;

        return (
            <Paper className={classes.bottomNavStyle}>
                <BottomNavigation
                    value={this.props.selectedBottomNavIndex}
                    onChange={this.handleChange}
                    showLabels
                 >
                    <BottomNavigationAction
                        label="Appointments"
                        icon={theApptsIcon}
                    />
                    <BottomNavigationAction
                        label="Contacts"
                        icon={theEmailIcon}
                    />
                    <BottomNavigationAction
                        label="Video Call"
                        icon={theVideoCall}
                    />
                </BottomNavigation>
            </Paper>
        );
    }
}

export default withStyles(styles)(bottom_nav);

但是,这对 selected 项的颜色没有任何影响。

我已经阅读了关于 CSS in JS 和 JSS 的 Material-UI 文档,但还没有完全理解。正确的语法是什么?

更新

根据对此线程的回复,我试过这个:

const styles = {
    bottomNavStyle: {
        position: 'fixed',
        left: '0px',
        bottom: '0px',
        height: '50px',
        width: '100%',
        zIndex: '100'
    },
    actionItemStyle: {
        '&$selected': {
            color: "#00bcd4 !important"
        },
    },
}

[.......]

    return (
        <Paper className={classes.bottomNavStyle}>
            <BottomNavigation
                value={this.props.selectedBottomNavIndex}
                onChange={this.handleChange}
                showLabels
            >
                <BottomNavigationAction
                    label="Appointments"
                    icon={theApptsIcon}
                    className={classes.actionItemStyle}
                />
                <BottomNavigationAction
                    label="Contacts"
                    icon={theEmailIcon}
                    className={classes.actionItemStyle}
                />
                <BottomNavigationAction
                    label="Video Call"
                    icon={theVideoCall}
                    className={classes.actionItemStyle}
                />
            </BottomNavigation>
        </Paper>
    );
}

...但尚未在网页上显示新颜色。

有几件事我想提出建议。

1) 将元器件的名称写成首字母大写,因为小写首字母和大写的命名方式不同。

2) 如果没有其他方法可以应用您的 cs 规则,如果它总是因为某些 css 特殊性而被覆盖,请在规则末尾使用 !iportant。

3) 在 jss 中尝试这种 css 的嵌套:

const styles = {
    bottomNavStyle: {
        position: 'fixed',
        left: '0px',
        bottom: '0px',
        height: '50px',
        width: '100%',
        zIndex: '100',
        '&:selected': {
           color: "#00bcd4"
        },
    },
};

您更新的解决方案看起来不错,只是有一些小的变化...

  1. 您需要在样式规则中包含一个空 .selected class。
const styles = {
  // Root styles for `BottomNavigationAction` component
  actionItemStyles: {
    "&$selected": {
      color: "red"
    }
  },
  // This is required for the '&$selected' selector to work
  selected: {}
};
  1. 您需要将 classes={{selected: classes.selected}} 传递给 BottomNavigationAction。这是 '&$selected' 选择器工作所必需的。
<BottomNavigation
  value={value}
  onChange={this.handleChange}
  className={classes.root}
>
  <BottomNavigationAction
    classes={{
      root: classes.actionItemStyles,
      selected: classes.selected
    }}
    label="Recents"
    value="recents"
    icon={<RestoreIcon />}
  />
</BottomNavigation>

实例: