Material-UI样式替代? [英] Material-UI Style Override?

查看:66
本文介绍了Material-UI样式替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm updating my app from Material-UI v1 to v2. I'm trying to use a style override to set the color of a selected <BottomNavigationAction> element.

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项目的颜色没有任何作用.

But, this does not do anything to the color of selected items.

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

I've read the Material-UI docs on CSS in JS and JSS, but haven't quite gotten it yet. What is the correct syntax for this?

更新

基于对此线程的响应,我已经尝试过:

Based on a response to this thread I've tried this:

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>
    );
}

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

...but have not yet gotten the new color to appear on the web page.

推荐答案

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

Your updated solution looks good, there are just a few small changes...

  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'选择器起作用所必需的.
  1. You need to pass classes={{selected: classes.selected}} to BottomNavigationAction. This is required for the '&$selected' selector to work.

<BottomNavigation
  value={value}
  onChange={this.handleChange}
  className={classes.root}
>
  <BottomNavigationAction
    classes={{
      root: classes.actionItemStyles,
      selected: classes.selected
    }}
    label="Recents"
    value="recents"
    icon={<RestoreIcon />}
  />
</BottomNavigation>

实时示例:

这篇关于Material-UI样式替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆