简体   繁体   English

如何为禁用的 Material UI TextField 覆盖全局边框颜色 styles?

[英]How to override global border color styles for disabled Material UI TextField?

I'm trying to override the global style for the Mui TextField components that are disabled, but I can't get the border color to change.我正在尝试覆盖已禁用的 Mui TextField 组件的全局样式,但我无法更改边框颜色。

I've managed to change the label color if the field is disabled, but not the border color.如果该字段被禁用,我已经设法更改 label 颜色,但不是边框颜色。 Here is what I have so far:这是我到目前为止所拥有的:

export const theme = createMuiTheme({
  overrides: {
    // For label
    MuiInputLabel: {
      root: {
        '&$disabled': {
          color: '#000000',
        },
      },
    },
    // For border color of field (doesn't work)
    MuiTextField: {
      root: {
        '&$disabled': {
          borderColor: '#FFFFFF'
        },
      },
    },
  },
});

I've tried all kinds of variations, including,我尝试了各种变化,包括,

    MuiOutlinedInput: {
      root: {
        fieldset: {
          borderColor: '#FFFFFF',
        },
      }
    }

But this only changes the border color of non-disabled fields.但这只会改变非禁用字段的边框颜色。 What am I doing wrong here?我在这里做错了什么? Here is how it looks:这是它的外观: 图像

The border is getting from the fieldset element.边框来自fieldset元素。 You can style if based on your needs:如果根据您的需要,您可以设置样式:

MuiInputBase: {
  root: {
    "&$disabled": {
      '& fieldset.MuiOutlinedInput-notchedOutline': {
        borderColor: "blue",
        background: "grey"
      }
    }
  }
}

You can find a working example here: https://codesandbox.io/s/material-styling-disabled-textfield-ckp14?file=/demo.js您可以在这里找到一个工作示例: https://codesandbox.io/s/material-styling-disabled-textfield-ckp14?file=/demo.js

Here is a "walkthrough" for how to do it yourself:以下是如何自己做的“演练”:

  1. Check the html output:检查 html output: 在此处输入图像描述
  2. Open the image in new tab to check the marks.在新标签中打开图像以检查标记。
    As you can see - the label is actually not a parent of the fieldset tag, which is the one that draws the border.如您所见 - label 实际上不是 fieldset 标签的父标签,它是绘制边框的标签。
  3. The fieldset is inside the div.MuiInputBase , which is also disabled, so I had to add the &$disabled on the root of the MuiInputBase .字段集在div.MuiInputBase内部,它也被禁用,所以我必须在MuiInputBase的根目录上添加&$disabled
  4. As for the & fieldset - the fieldset element is a child-element fo the MuiInputBase - so the space between the & and the tag-name means this is a child-element.至于& fieldset - fieldset元素是MuiInputBase的子元素 - 所以&和标签名之间的空格意味着这是一个子元素。
  5. The fieldset element has the MuiOutlinedInput-notchedOutline so I used it as the relevant selector. fieldset 元素具有MuiOutlinedInput-notchedOutline ,因此我将其用作相关选择器。 You could probably just use .MuiOutlinedInput-root.Mui-disabled fieldset without the last-selector.您可能只使用没有最后选择器的.MuiOutlinedInput-root.Mui-disabled fieldset Try it:-).试试看:-)。

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

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