我使用的是React 17和MUI 5.6.我有一个组件,需要根据用户输入向父容器添加新边框.我找到了一些非MUI CSS tutorials来实现我想要的功能,但它们的边框是用CSS静态编码的,不是根据用户需求添加的.

一个边界Here's a working sandbox美元.如果您能在如何支持多个边界方面提供帮助,我们将不胜感激.

推荐答案

这应该有助于解决你的问题.这是工作代码,用户可以用按钮决定 colored颜色 ,但也可以用下拉选项实现相同的解决方案.https://codesandbox.io/embed/wonderful-worker-g8fzyi?fontsize=14&hidenavigation=1&theme=dark

import { Box } from "@mui/material";
import Button from "@mui/material/Button";
import { useState } from "react";
import "./styles.css";

    export default function App() {
      const [colors, setColors] = useState([]);
      const [pixel, setPixel] = useState(6);
      const addColor = async (color) => {
        setPixel(pixel + 2);
        let randomColor = `0 0 0 ${pixel}px ${color}`;
        setColors([...colors, randomColor]);
      };
    
      return (
        <div
          style={{
            width: "100vw",
            height: "100vh",
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
            flexDirection: "column"
          }}
        >
          <Box
            sx={{
              width: "50%",
              height: "20%",
              border: 3,
              boxShadow: colors.join(",")
            }}
          ></Box>
          <div style={{ marginTop: 100 }}>
            <Button onClick={() => addColor("#FFFF00")}>Yellow</Button>
            <Button onClick={() => addColor("#FF002B")}>Red</Button>
            <Button onClick={() => addColor("#0000FF")}>Blue</Button>
            <Button onClick={() => addColor("#2BFF00")}>Green</Button>
          </div>
        </div>
      );
    }

Css相关问答推荐

搜索栏突出显示角形HTML中的Mat-Form-field输入框的焦点

显示Reaction组件上的问题.使用FlexBox时的意外大小

VueJS3+Vutify中缺少一些CSS类

当弯曲方向设置为列时如何制作等高卡片?

为什么删除 AnimatePresence framer.div 时会出现轻微卡顿?

使用 place-content: center 的全宽 CSS 网格项目

如何在 Tailwind 中使用 `margin: y x;` 速记?

通过css修改样式shadowroot元素

Angular (13) material - 覆盖 CSS 文件不起作用

基于类的 CSS Select 器

仅对父容器应用模糊效果

固定元素在 Chrome 中消失

CSS:背景图像和填充

css3 nth 类型限制为类

使用边距:0 自动;在 Internet Explorer 8 中

从样式设置为 % 的元素获取宽度(以像素为单位)?

通过 Bootstrap4 对列进行排序

你如何在 SASS 中定义属性 Select 器?

对象拟合不影响图像

flexbox容器中的省略号