Material-UI样式替代?

时间:2018-10-02 05:16:28

标签: material-ui jss

我正在将我的应用程序从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项目的颜色没有任何作用。

我已经阅读了JS和JSS中关于CSS的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>
    );
}

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

2 个答案:

答案 0 :(得分:4)

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

  1. 您需要在样式规则中包括一个空的.selected类。
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>

实时示例:

Edit Material UI ButtonNavigationAction styles

答案 1 :(得分:0)

我想建议几件事。

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"
        },
    },
};
相关问题