【问题标题】:React Material UI Slider to change Border RadiusReact Material UI Slider 更改边框半径
【发布时间】:2021-08-09 02:26:50
【问题描述】:

使用 React Material UI 的 [slider]:(https://material-ui.com/components/slider/#slider) 和 [button]:(https://material-ui.com/api/button/)。

我们的目标是用滑块改变按钮的边框半径,但努力抓住 value 变量并想办法将它应用到 Button 组件。

我因反复试验而脑死亡。有人可以帮助我或给我好的建议吗?

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Grid from "@material-ui/core/Grid";
import Slider from "@material-ui/core/Slider";
import Input from "@material-ui/core/Input";
import Button from "@material-ui/core/Button";
const useStyles = makeStyles((theme) => ({
  root: {
    "& > *": {
      margin: theme.spacing(1)
    }
  },
  Button: {
    width: 150,
    height: 50,
    borderRadius: "var(--borderRadius)"
  }
}));
export default function InputSlider() {
  const classes = useStyles();
  const [value, setValue] = React.useState(30);
  const handleSliderChange = (event, newValue) => {
    setValue(newValue);
  };
  const handleInputChange = (event) => {
    setValue(event.target.value === "" ? "" : Number(event.target.value));
  };
  const handleBlur = () => {
    if (value < 0) {
      setValue(0);
    } else if (value > 100) {
      setValue(100);
    }
  };
  return (
    <div className={classes.root}>
      <style>
        {`:root {
          --borderRadius = ${value}px;
        }`}
      </style>
      <Button
        variant="contained"
        color="primary"
        value="value"
        onChange={handleSliderChange}
        className={classes.Button}
      >
        Fire laser
      </Button>
      <Grid container spacing={2}>
        <Grid item xs>
          <Slider
            value={typeof value === "number" ? value : 0}
            onChange={handleSliderChange}
            aria-labelledby="input-slider"
          />
        </Grid>
        <Grid item>
          <Input
            value={value}
            margin="dense"
            onChange={handleInputChange}
            onBlur={handleBlur}
            inputProps={{
              step: 10,
              min: 0,
              max: 24,
              type: "number"
            }}
          />
        </Grid>
      </Grid>
    </div>
  );
}

这里是 [沙盒] 中的工作:(https://codesandbox.io/s/material-demo-forked-nm2qw?file=/demo.js:0-1878)

【问题讨论】:

    标签: css reactjs button material-ui slider


    【解决方案1】:

    您可以使用 style 属性更改 ButtonborderRadius

    只需将此添加到您的Button

    style={{ borderRadius: value }}
    

    查看此sandbox 以获取工作示例。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2020-12-26
    • 2014-09-06
    • 2016-10-15
    相关资源
    最近更新 更多