简体   繁体   English

如何自定义材质 UI select 组件?

[英]How do you customize the material UI select component?

I have a MUI select component that I need to customize.我有一个需要自定义的 MUI select 组件。 I am rendering it on a dark background and there I need the outline and all its contents to be light colors (ie grey and white).我在深色背景上渲染它,我需要轮廓及其所有内容为浅色 colors(即灰色和白色)。

在此处输入图像描述

I have managed to get the outline and the text contents to be light colors but I cannot figure out how to change the colors of the down and up arrows that you click on to open and close the select options.我设法使大纲和文本内容变亮 colors 但我无法弄清楚如何更改您单击以打开和关闭 Z99938282F04071859941E18F1 选项的向下和向上箭头的 colors。 If you look at the screenshot I shared, you can barely even see the up arrow because it is black.如果你看我分享的截图,你甚至几乎看不到向上的箭头,因为它是黑色的。 How would I go about changing the color of that to grey or white?我将如何将 go 的颜色更改为灰色或白色?

This is what I have now:这就是我现在所拥有的:

<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>

You can use the icon css class from the select (or iconOutlined in your case).您可以使用 select 中的icon css iconOutlined (或您的情况下的图标大纲)。

Like for example with makeStyle :例如makeStyle

// outside the component
const useStyles = makeStyles(() =>
  createStyles({
    iconClassName: {
      fill: "red",
    },
  })
);

...

// later in the component
const classes = useStyles();

<Select 
  classes={{ iconOutlined: classes.iconClassName }}
/>

Another option is to use the IconComponent props an completely replace the icon:另一种选择是使用IconComponent道具完全替换图标:

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

This is how I ended up changing the color of the arrow icon.这就是我最终改变箭头图标颜色的方式。

<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>

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

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