【问题标题】:Material-UI : Rating Component, change color based on valueMaterial-UI : Rating Component,根据值改变颜色
【发布时间】:2020-12-06 15:17:44
【问题描述】:

目标

根据悬停期间的值更改评级(材质 UI 组件)的颜色。例如,如果我将鼠标悬停在第 1 颗星上,颜色变为红色,如果悬停在第 5 颗星上,则变为绿色。

我尝试过制作一个自定义组件,它会在悬停时改变颜色,就像这样 -

const StyledRating = withStyles({
  root: {
    color: "#ff6d75",
  },
  iconFilled: {
    color: '#ff6d75',
  },
  iconHover: {
    color: '#fff',
    backgroundColor: "#000",
  },
})(Rating);  

但它会改变所有图标的颜色。

谁能指导我,如何根据我选择或悬停在上面的值更改评级颜色中的特定图标。

Current sandbox link.

【问题讨论】:

    标签: reactjs typescript material-ui material-design gatsby


    【解决方案1】:

    您可以使用动态样式 (Overriding styles with classes)

    const useStyles = makeStyles({
      root: {
        // ...
      },
      'icon-1': { color: 'red' },
      'icon-2': { color: 'coral' },
      'icon-3': { color: 'orange' },
      'icon-4': { color: 'skyblue' },
      'icon-5': { color: 'green' }
    });
    
    export default function HoverRating() {
      // ... 
      return (
        <div className={classes.root}>
          <Rating
            classes={{
              iconHover: classes[`icon-${Math.ceil(hover)}`]
            }}
            //...
          />
          ...
        </div>
      );
    }
    

    【讨论】:

    • 哇!工作真棒!非常感谢!
    【解决方案2】:

    我已经为我们需要声明状态的打字员形成了一个答案。

    https://codesandbox.io/s/ratings-vksqc?file=/demo.tsx

    <div className={classes.root}>
          <Rating
            name="hover-feedback"
            value={value}
            precision={1}
            onChange={(event, newValue) => {
              console.log(event, "This is th e event");
              setValue(newValue);
              switch (true) {
                case newValue <= 1: {
                  setIconFilled(classes.iconFilled1);
                  break;
                }
                case newValue <= 2 && newValue > 1: {
                  setIconFilled(classes.iconFilled2);
                  break;
                }
                case newValue <= 3 && newValue > 2: {
                  setIconFilled(classes.iconFilled3);
                  break;
                }
                case newValue <= 4 && newValue > 3: {
                  setIconFilled(classes.iconFilled4);
                  break;
                }
                case newValue > 4: {
                  setIconFilled(classes.iconFilled5);
                  break;
                }
              }
            }}
            onChangeActive={(event, newHover) => {
              switch (true) {
                case newHover <= 1: {
                  setIconHover(classes.iconHover1);
                  break;
                }
                case newHover <= 2 && newHover > 1: {
                  setIconHover(classes.iconHover2);
                  break;
                }
                case newHover <= 3 && newHover > 2: {
                  setIconHover(classes.iconHover3);
                  break;
                }
                case newHover <= 4 && newHover > 3: {
                  setIconHover(classes.iconHover4);
                  break;
                }
                case newHover > 4: {
                  setIconHover(classes.iconHover5);
                  break;
                }
              }
            }}
            defaultValue={2}
            icon={<FiberManualRecordIcon fontSize="inherit" />}
            classes={{
              iconFilled: iconFilledVar,
              iconHover: iconHoverVar
            }}
          />
        </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-23
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 2020-03-27
      • 2018-03-26
      相关资源
      最近更新 更多