5

我正在将我的应用程序从 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>
    );
}

...但尚未使新颜色出现在网页上。

4

2 回答 2

10

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

  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>

现场示例:

编辑 Material UI ButtonNavigationAction 样式

于 2018-10-02T23:30:03.370 回答
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"
        },
    },
};
于 2018-10-02T10:27:09.080 回答