简体   繁体   English

在 Slider Material-UI (React) 上自定义样式

[英]Customizing styles on Slider Material-UI (React)

I'm trying to make a slider with restricted values with Material UI and React.我正在尝试使用 Material UI 和 React 制作一个具有受限值滑块 I have copy the basic implementation from the documentation page and it seem's not to need CSS to function.我已经从文档页面复制了基本实现,似乎不需要 CSS 来运行。 But when I use it in my project, the labels appear overlapped as the color of the line representing the chosen value (with 0 value, the line is light blue, with 5 value, all the line is heavy blue).但是当我在我的项目中使用它时,标签显示为代表所选值的线的颜色重叠(0 值,线是浅蓝色,5 值,所有线都是深蓝色)。

This is the code I have implemented:这是我实现的代码:

const useStyles = makeStyles({


root: {
    width: 300,
  },
  
});

const marks = [
  {
    value: 1,
    label: 'Nada',
  },
  {
    value: 2,
    label: 'Un poco',
  },
  {
    value: 3,
    label: 'Moderado',
  },
  {
    value: 4,
    label: 'Bastante',
  },
  {
      value: 5,
      label: 'Totalmente'
  }
];

function valuetext(value) {
  return `${value}`;
}

function valueLabelFormat(value) {
  return marks.findIndex((mark) => mark.value === value) + 1;
}

export default function DiscreteSlider() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Typography id="discrete-slider-restrict" gutterBottom>
        Restricted values
      </Typography>
      <Slider
        defaultValue={0}
        valueLabelFormat={valueLabelFormat}
        getAriaValueText={valuetext}
        aria-labelledby="discrete-slider-restrict"
        step={null}
        valueLabelDisplay="auto"
        marks={marks}
      />
    </div>
  );
}

And the Slider looks like that:滑块看起来像这样: 滑块

How can I change the overlapping and make it functional?如何更改重叠并使其起作用? Is there any Slider prop that I can use?有我可以使用的 Slider 道具吗?

Thank you very much!非常感谢!

Set max for the Slider to 5 - currently it is defaulting to 100 (see docs for default value).Slider max设置为5 - 当前默认为100 (有关默认值,请参阅文档)。 Currently, your mark values are 1 to 5 so they are being squeezed in that tiny area目前,您的标记值为 1 到 5,因此它们被挤压在那个小区域

<Slider
  max={5}  // <-- set max
  defaultValue={0}
  valueLabelFormat={valueLabelFormat}
  getAriaValueText={valuetext}
  aria-labelledby="discrete-slider-restrict"
  step={null}
  valueLabelDisplay="auto"
  marks={marks}
/>

and allocate more width to accomodate the labels并分配更多的width来容纳标签

const useStyles = makeStyles({
  root: {
    width: 600
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM