如何样式化 Mui AutoComplete 的 endAdornment 图标

huangapple go评论68阅读模式
英文:

how to style Mui AutoComplete endAdornment icon

问题

我尝试更改 Mui Autocomplete 的向下图标的颜色和大小,这是我尝试过的内容,但它不起作用:

<Autocomplete
  disablePortal
  id="search-bar"
  options={data}
  sx={{
    width: "100%",
    flex: 1,
    "& .MuiAutocomplete-root": {
      padding: "0px",
    },
  }}
  renderInput={(params) => (
    <TextField
      {...params}
      InputLabelProps={{
        sx: { color: "#12efc8" },
      }}
      name="subCategory"
      type="text"
      sx={{
        width: "100%",
        input: {
          color: "#12efc8",
        },
        maxWidth: "inherit",
        "& .MuiInputBase-root": {
          height: "49px",

          "&.focus": {
            backgroundColor: "transparent",
          },
        },
        "& .MuiFormControl-root": {
          "& .MuiAutocomplete-endAdornment": {
            color: "white",
            scale: 2,
          },
        },
      }}
      variant="outlined"
      label="Add Sub-Category"
    />
  )}
/>

我还尝试将以下代码移到父元素 (AutoComplete) 的样式中,但也没有起作用:

"& .MuiAutocomplete-endAdornment": {
  color: "white",
  scale: 2,
},

显然,图标的类是 .MuiInputBase-root 的子类,但不确定为什么,因为它被称为 .MuiAutocomplete-endAdornment。

英文:

I'm trying to change the color and size of downwards icon for the Mui Autocomplete, this is what I've tried but it's not working:

&lt;Autocomplete
      disablePortal
      id=&quot;search-bar&quot;
      options={data}
      sx={{
        width: &quot;100%&quot;,
        flex: 1,
        &quot;&amp; .MuiAutocomplete-root&quot;: {
          padding: &quot;0px&quot;,
        },
      }}
      renderInput={(params) =&gt; (
        &lt;TextField
          {...params}
          InputLabelProps={{
            sx: { color: &quot;#12efc8&quot; },
          }}
          name=&quot;subCategory&quot;
          type=&quot;text&quot;
          sx={{
            width: &quot;100%&quot;,
            input: {
              color: &quot;#12efc8&quot;,
            },
            maxWidth: &quot;inherit&quot;,
            &quot;&amp; .MuiInputBase-root&quot;: {
              height: &quot;49px&quot;,

              &quot;&amp;.focus&quot;: {
                backgroundColor: &quot;transparent&quot;,
              },
            },
            &quot;&amp; .MuiFormControl-root&quot;: {
              &quot;&amp; .MuiAutocomplete-endAdornment&quot;: {
                color: &quot;white&quot;,
                scale: 2,
              },
            },
          }}
          variant=&quot;outlined&quot;
          label=&quot;Add Sub-Category&quot;
        /&gt;
      )}
    /&gt;

I've also tried moving the following code up to the styling of the parent element (AutoComplete), but it didn't work either:

      &quot;&amp; .MuiAutocomplete-endAdornment&quot;: {
        color: &quot;white&quot;,
        scale: 2,
      },

Apparently, the class for the icon is a subclass of .MuiInputBase-root, not sure how since it's called .MuiAutocomplete-endAdornment

答案1

得分: 1

Of course, you're right - 显然,图标的类是.MuiInputBase-root的子类,不确定为什么,因为它被称为.MuiAutocomplete-endAdornment

它将样式设置为endAdornment。然而,如果你想改变Mui Autocomplete下拉图标的颜色和大小,你需要添加MuiIconButton-rootMuiSvgIcon-root

例如:

&amp; .MuiAutocomplete-endAdornment .MuiIconButton-root: {
  color: "red",
  width: 32,
  height: 32,
},

或者

&amp; .MuiAutocomplete-endAdornment .MuiSvgIcon-root: {
  color: "red",
  width: 32,
  height: 32,
},

你可以在这里看到完整的示例链接

英文:

Of course, you're right - Apparently, the class for the icon is a subclass of .MuiInputBase-root, not sure how since it's called .MuiAutocomplete-endAdornment.

It will set style to endAdornment. However, if you want to change the color and size of the downward icon for the Mui Autocomplete, you need to add MuiIconButton-root or MuiSvgIcon-root.

For example:

&quot;&amp; .MuiAutocomplete-endAdornment .MuiIconButton-root&quot;: {
  color: &quot;red&quot;,
  width: 32,
  height: 32,
},

or

&quot;&amp; .MuiAutocomplete-endAdornment .MuiSvgIcon-root&quot;: {
  color: &quot;red&quot;,
  width: 32,
  height: 32,
},

You can see the whole example here.

huangapple
  • 本文由 发表于 2023年8月5日 00:35:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定