【问题标题】:How I can change the input color of Material UI TextField when is input is disabled [MUI v: 5.0.8] [duplicate]禁用输入时如何更改 Material UI TextField 的输入颜色 [MUI v: 5.0.8] [重复]
【发布时间】:2021-11-08 09:22:31
【问题描述】:

我想在禁用输入时更改边框颜色和文本颜色。我尝试了所有变体,如下所示:

const textFieldStyle = {
    '& label': {
        color: darkMode?'#1976d2':'',
    },

    '& .MuiOutlinedInput-root': {
        color:darkMode?'#1976d2':'',
        '& fieldset': {
          borderColor:darkMode?'#1976d2':'',
        },
        '&:hover fieldset': {
            borderColor: darkMode?'#1976d2':'',
        }, 
 
    },
    "& input.Mui-disabled": {
        color: "green"
      }

};

<TextField value={formState.vinInput} type="text" label="Stack" sx={textFieldStyle}/>

样式的其余部分就像一般颜色和焦点颜色一样正常工作!

【问题讨论】:

    标签: javascript css reactjs material-ui


    【解决方案1】:

    您可以在 v4 中使用此代码:

     MuiInputBase: {
        root: {
          "&$disabled": {
            color: "red",
            border: "1px solid red"
          }
        }
      }
    

    基于版本 5 中的 Material-ui 文档 用于 JSS 的 $ 语法不适用于 Emotion。您需要将这些选择器替换为有效的类选择器。

    这就是风格

        const useStyles = makeStyles({
        customDisable: {
          "& .MuiInputBase-input.Mui-disabled": {
            color: "red !important",
            "-webkit-text-fill-color": "red !important",
            borderColor: "red !important"
          },
          "& .Mui-disabled .MuiOutlinedInput-notchedOutline": {
            borderColor: "red !important"
          }
        }
      });
    

    然后像这样在 TextField 中

    <TextField
            disabled
            className={classes.customDisable}
            id="outlined-disabled"
            defaultValue="Hello World"
          />
    

    这里是Codesandbox

    【讨论】:

    • 它不工作,我知道为什么!所有样式都适用于正常输入工作,但是当我想应用于禁用的东西时,它不起作用。顺便说一句,Mui 版本是 5.0.6
    猜你喜欢
    • 2022-11-10
    • 2018-11-10
    • 2019-12-14
    • 2011-11-11
    • 1970-01-01
    • 2019-05-14
    • 2019-11-07
    • 2020-11-04
    • 2021-12-07
    相关资源
    最近更新 更多