代码之家  ›  专栏  ›  技术社区  ›  VikR

材质UI样式替代?

  •  3
  • VikR  · 技术社区  · 6 年前

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

    更新

    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 回复  |  直到 6 年前
        1
  •  10
  •   Luke Peavey    6 年前

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

    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

        2
  •  0
  •   Lazar Nikolic    6 年前

    我想提出几点建议。

    1) 用首字母大写写下组件的名称,因为如果它是用小首字母和大写字母命名的,则处理方式不同。

    2) 如果没有其他方法可以应用你的cs规则,如果它总是因为css的特殊性而被覆盖,那么使用!我在规则的末尾很重要。

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