【问题标题】:How do you customize the material UI select component?如何自定义材质 UI 选择组件?
【发布时间】:2022-01-18 19:57:56
【问题描述】:

我有一个需要自定义的 MUI 选择组件。我在深色背景上渲染它,我需要轮廓及其所有内容为浅色(即灰色和白色)。

我已经设法将大纲和文本内容设置为浅色,但我无法弄清楚如何更改您单击以打开和关闭选择选项的向下和向上箭头的颜色。如果你看我分享的截图,你甚至几乎看不到向上的箭头,因为它是黑色的。我将如何将其颜色更改为灰色或白色?

这就是我现在拥有的:

<Select
    variant="outlined"
    sx={{
    width: 100,
    height: 40,
    marginRight: 15,
    border: "1px solid darkgrey",
    color: "#fff",
    }}
    value={currency}
    onChange={(e) => setCurrency(e.target.value)}
>
    <MenuItem value={"USD"}>USD</MenuItem>
    <MenuItem value={"INR"}>INR</MenuItem>
</Select>

【问题讨论】:

    标签: css reactjs material-ui


    【解决方案1】:

    这就是我最终更改箭头图标颜色的方式。

    <Select
        variant="outlined"
        sx={{
        width: 100,
        height: 40,
        marginRight: 15,
        border: "1px solid darkgrey",
        color: "#fff",
        "& .MuiSvgIcon-root": {
            color: "white",
        },
        }}
        value={currency}
        onChange={(e) => setCurrency(e.target.value)}
    >
        <MenuItem value={"USD"}>USD</MenuItem>
        <MenuItem value={"INR"}>INR</MenuItem>
    </Select>
    

    【讨论】:

      【解决方案2】:

      您可以使用选择中的iconcss class(或iconOutlined,在您的情况下)。

      例如makeStyle
      material-ui 5 已过时,请参阅评论和其他答案。

      // outside the component
      const useStyles = makeStyles(() =>
        createStyles({
          iconClassName: {
            fill: "red",
          },
        })
      );
      
      ...
      
      // later in the component
      const classes = useStyles();
      
      <Select 
        classes={{ iconOutlined: classes.iconClassName }}
      />
      

      另一种选择是使用IconComponent props 一个完全替换的图标:

      <Select 
        IconComponent={<YourComponentHere />}
      />
      

      【讨论】:

      • 嘿,我没有以这种方式使用 makeStyles,因为根据 MUI 5 文档,这是 MUI 组件样式的过时方式。现在,他们建议开发人员结合使用样式组件和 sx 属性。我结束了使用 sx 道具,只添加了以下代码: "& .MuiSvgIcon-root": { color: "white", },
      猜你喜欢
      • 2022-01-18
      • 2021-05-27
      • 2020-03-30
      • 2020-01-22
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2021-07-01
      相关资源
      最近更新 更多