如何自定义Material UI组件(例如,在Select组件上去除蓝色轮廓)?

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

How do I customize Material UI components (e.g. removing blue outline on Select component)?

问题

I understand that you want translations for the code-related parts only. Here's the translation of those sections:

我刚刚开始开发一个React应用程序并决定尝试使用Material UI的预构建组件

然而我一直在努力弄清楚如何正确自定义组件及其样式

我的代码

import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select from '@mui/material/Select';

const Dropdown = ({ value, label, options, handleChange }) => {

return (
    <FormControl
        className="dropdown-container"
        variant='outlined'
        sx={{
            minWidth: 120,
            backgroundColor: "#eb6060",
            borderRadius: "5px",
            border: "1px solid #eb6060",
        }}
    >
        <InputLabel shrink={false}>{value === "" ? label : ""}</InputLabel>
        <Select
            onChange={handleChange}
            label={value === "" ? label : ""}
            value={value}
            notched={false}
        >
            {options.map((option) => (
                <MenuItem
                    key={option.code}
                    value={option.code}
                >
                    {option.alias}
                </MenuItem>
            ))}
        </Select>
    </FormControl>
);

};

export default Dropdown;


这是我尝试自定义MUI的Select组件的方式。我尝试移除了标签的收缩效果以及每个Select组件边框上出现的缺口。

1. 我是否以正确的方式进行自定义?

2. 如何进一步自定义以移除我点击下拉组件时出现的蓝色焦点轮廓,以及第一个MenuItem的键盘焦点高亮显示?

编辑:根据Prem Chaudhary提供的代码,已经移除了蓝色轮廓。现在看起来是这样的:

如何解决这种问题?例如,我仍然想对此组件进行以下更改:

1. 移除选择组件标签上的蓝色高亮显示
2. 移除打开菜单时第一个菜单项上的灰色高亮显示
3. 在打开菜单时轻微加深选择按钮的背景

理想情况下,我希望能够自己找到这些问题的解决方案,而无需每次都向Stack Overflow提问。我如何找到要覆盖样式的类?我尝试使用CSS概述来解决问题1,但未能实现我想要的更改:

I hope this helps with your code translation. If you have any more questions or need further assistance, please feel free to ask.

英文:

I just started developing a React app and decided to give Material UI a try for its pre-built components.

However, I've been having a difficult time figuring out how to properly customize the components and their styles.

My code:

import InputLabel from &#39;@mui/material/InputLabel&#39;;
import MenuItem from &#39;@mui/material/MenuItem&#39;;
import FormControl from &#39;@mui/material/FormControl&#39;;
import Select from &#39;@mui/material/Select&#39;;

const Dropdown = ({ value, label, options, handleChange }) =&gt; {

    return (
        &lt;FormControl
            className=&quot;dropdown-container&quot;
            variant=&#39;outlined&#39;
            sx={{
                minWidth: 120,
                backgroundColor: &quot;#eb6060&quot;,
                borderRadius: &quot;5px&quot;,
                border: &quot;1px solid #eb6060&quot;,
            }}
        &gt;
            &lt;InputLabel shrink={false}&gt;{value === &quot;&quot; ? label : &quot;&quot;}&lt;/InputLabel&gt;
            &lt;Select

                onChange={handleChange}
                label={value === &quot;&quot; ? label : &quot;&quot;}
                value={value}
                notched={false}
            &gt;
                {options.map((option) =&gt; (
                    &lt;MenuItem
                        key={option.code}
                        value={option.code}
                    &gt;
                        {option.alias}
                    &lt;/MenuItem&gt;
                ))}
            &lt;/Select&gt;
        &lt;/FormControl&gt;
    );
};

export default Dropdown;

This has been my attempt so far at customizing MUI's Select component. I have tried removing the shrinking label and the notch that appears on each Select component's border.

  1. Have I been customizing this the right way?

  2. How can I further customize it to remove the blue focus outline that appears when I click on my dropdown component, as well as the keyboard focus highlight on the first MenuItem?

如何自定义Material UI组件(例如,在Select组件上去除蓝色轮廓)?

EDIT: Adding the code given by Prem Chaudhary has removed the blue outline. This is what it looks like now:

如何自定义Material UI组件(例如,在Select组件上去除蓝色轮廓)?

How should I go about figuring out a solution like this myself? For example, I still want to make the following changes to this component:

  1. Remove the blue highlight on the select component label
  2. Remove the grey highlight on the first menu item when the menu opens
  3. Slightly darken the select button's background when I open the menu

Ideally I'd be able to find solutions to all of these by myself without having to ask stack overflow each time. How can I find which classes to override styles for? I have tried using CSS overview for #1 but was not able to make the change I wanted:

如何自定义Material UI组件(例如,在Select组件上去除蓝色轮廓)?

答案1

得分: 1

要实现所需的自定义,可以进行以下修改:

调整 FormControlMuiOutlinedInput-notchedOutline 样式:

sx={{
  minWidth: 120,
  backgroundColor: "#eb6060",
  borderRadius: "5px",
  border: "1px solid #eb6060",
  '& .MuiOutlinedInput-notchedOutline': {
    border: 'none',
  },
  '&:hover .MuiOutlinedInput-notchedOutline': {
    border: 'none',
  },
  '&.Mui-focused .MuiOutlinedInput-notchedOutline': {
    border: 'none',
  },
}}

在这里,FormControlMuiOutlinedInput-notchedOutline 样式已经被调整,以在未聚焦、悬停或聚焦并有内容时移除轮廓边框。

编辑: 当遇到问题或需要满足特定要求时,可以采用以下方法:

方法1:文档探索
解决问题的最可靠方法之一是彻底查看所使用的库或框架提供的文档。尽管这种方法可能需要一些时间和精力,但它被认为是最佳行动方案。此外,依赖官方文档可以确保我们的代码在未来出现任何问题时得到库的维护者的支持。

方法2:浏览器检查工具的利用
快速的方法是使用浏览器检查工具检查元素的结构和应用于其上的CSS。通过检查元素,您可以查看您的CSS是否应用于元素或被其他CSS覆盖。

例如,在这里,您可以在浏览器检查器中看到解决第一个问题的CSS。

解决您的后续问题的方法:

调整 FormControl 的样式:

sx={{ 
  minWidth: 120,
  backgroundColor: "#eb6060",
  borderRadius: "5px",
  border: "1px solid #eb6060",
  '& .MuiOutlinedInput-notchedOutline': {
    border: 'none',
  },
  '&:hover .MuiOutlinedInput-notchedOutline': {
    border: 'none',
  },
  '&.Mui-focused .MuiOutlinedInput-notchedOutline': {
    border: 'none',
  },
  '& .MuiSelect-select': {
    color: '#FFF' // 移除选择组件标签上的蓝色高亮
  },
  '& .Mui-focused .MuiSelect-select': {
    backgroundColor: '#e84545' // 在菜单打开时略微加深选择按钮的背景颜色
  }
}}

调整 MenuItem 的样式:

sx={{ 
  '&.Mui-selected.Mui-focusVisible': {
    backgroundColor: '#FFF' // 设置菜单打开时第一个菜单项的高亮颜色为白色
  }
}}
英文:

To achieve the desired customizations, the following modifications can be made:

Adjust the MuiOutlinedInput-notchedOutline styles of the FormControl:

sx={{
  minWidth: 120,
  backgroundColor: &quot;#eb6060&quot;,
  borderRadius: &quot;5px&quot;,
  border: &quot;1px solid #eb6060&quot;,
  &#39;&amp; .MuiOutlinedInput-notchedOutline&#39;: {
       border: &#39;none&#39;,
   },
   &#39;&amp;:hover .MuiOutlinedInput-notchedOutline&#39;: {
       border: &#39;none&#39;,
   },
   &#39;&amp;.Mui-focused .MuiOutlinedInput-notchedOutline&#39;: {
       border: &#39;none&#39;,
   },
}}

here, The MuiOutlinedInput-notchedOutline styles for the FormControl have been adjusted to remove the outlined border when not focused, hovered, or focused with content.

EDIT: When you encounter issues or aim to meet specific requirements, you can follow these approaches:

Approach 1: Documentation Exploration
One of the most reliable methods to address issues is by thoroughly examining the documentation provided by the library or framework being used. Although this approach may require some time and effort, it is considered the best course of action. Additionally, relying on official documentation ensures that our code is supported by the library's maintainers if any future complications arise.

Approach 2: Browser Inspector Tool Utilization
Fast approach is the use of a browser inspector tool to inspect the element's structure and the CSS applied to it. by inspecting the elements you can see whether your CSS is applied to the elements or overridden by the other CSS.

for example, here you can see the CSS in the browser inspector that resolves your first issue.

如何自定义Material UI组件(例如,在Select组件上去除蓝色轮廓)?

Solutions for your follow-up issues:

Adjust the styles of the FormControl:

sx={{ minWidth: 120,
    backgroundColor: &quot;#eb6060&quot;,
    borderRadius: &quot;5px&quot;,
    border: &quot;1px solid #eb6060&quot;,
    &#39;&amp; .MuiOutlinedInput-notchedOutline&#39;: {
        border: &#39;none&#39;,
    },
    &#39;&amp;:hover .MuiOutlinedInput-notchedOutline&#39;: {
        border: &#39;none&#39;,
    },
    &#39;&amp;.Mui-focused .MuiOutlinedInput-notchedOutline&#39;: {
        border: &#39;none&#39;,
    },
    &#39;&amp; .MuiSelect-select&#39;: {
        color: &#39;#FFF&#39; // remove the blue highlight on select component label
    },
    &#39;&amp; .Mui-focused .MuiSelect-select&#39;: {
        backgroundColor: &#39;#e84545&#39; // Slightly darken the select button&#39;s background when menu is opened
    }
}}

Adjust the styles of the MenuItem:

sx={{ 
    &#39;&amp;.Mui-selected.Mui-focusVisible&#39;: {
        backgroundColor: &#39;#FFF&#39; //Set the highlight color to white on the first menu item when the menu opens
    }
}}

huangapple
  • 本文由 发表于 2023年6月8日 10:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428227.html
匿名

发表评论

匿名网友

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

确定