简体   繁体   English

MaterialUI - 更改焦点上的颜色文本字段

[英]MaterialUI - Changing the color Textfield on focus

I'm trying to change the color of the label text in Textfield but I can't seem to figure it out.我正在尝试更改 Textfield 中标签文本的颜色,但我似乎无法弄清楚。

Here is what I'm trying:这是我正在尝试的:

<TextField
    value={value}
    key={name}
    label={label}
    id={id}
    name={name}
    InputLabelProps={{
      shrink: true,
      FormLabelClasses: {
        'root': {
          '&:focused': {
            color: 'white'
          }
        },
        focused: 'true'
      }
    }}
  />

Can someone give me a pointer on what I'm doing wrong here?有人可以告诉我我在这里做错了什么吗?

I've also tried using the MuiThemeProvider but can't seem to figure that one out either:我也尝试过使用MuiThemeProvider但似乎也无法弄清楚:

const theme = createMuiTheme({
  overrides: {
    MuiFormLabel: {
      focused: true,
      root: {
        '&.focused': {
          color: 'white'
        }
      }
    }
  }
});

How can I change the color of the Label?如何更改标签的颜色? In this photo, I want the "Notes" to match the color of the underline在这张照片中,我希望“注释”与下划线的颜色相匹配

Thanks for your help!谢谢你的帮助! 在此处输入图片说明

Tim!蒂姆! Here is the snippet that should help you.这是应该对您有所帮助的片段。

 const { TextField, createMuiTheme, MuiThemeProvider, CssBaseline, } = window['material-ui']; const theme = createMuiTheme({ overrides: { MuiFormLabel: { root: { "&$focused": { color: "tomato", fontWeight: "bold" } }, focused: {} } } }); class Index extends React.Component { render() { return ( <MuiThemeProvider theme={theme}> <div> <CssBaseline /> <TextField label="Text field" InputLabelProps={{shrink:true}} /> </div> </MuiThemeProvider> ); } } ReactDOM.render(<Index />, document.getElementById('root'));
 <script src="https://unpkg.com/react@latest/umd/react.development.js" crossorigin="anonymous"></script> <script src="https://unpkg.com/react-dom@latest/umd/react-dom.development.js" crossorigin="anonymous"></script> <script src="https://unpkg.com/@material-ui/core/umd/material-ui.development.js" crossorigin="anonymous"></script> <div id="root"></div>

Another way of doing it without the override is to have:没有覆盖的另一种方法是:

const textStyles = makeStyles({
  root: {
    "& .Mui-focused": {
      color: "tomato", 
      fontWeight: "bold"
    },
});

class Index extends React.Component {  
  const TextClass = textStyles()
  render() {
    return (
      <MuiThemeProvider theme={theme}>
        <div>
          <CssBaseline />
          <TextField className={textStyles.root} label="Text field" InputLabelProps={{shrink:true}} />
        </div>
      </MuiThemeProvider>
    );
  }
}

for v5 of @mui this code works对于@mui 的 v5,此代码有效

const theme = createTheme({
  components: {
    MuiInputLabel: {
      styleOverrides: {
        root: {
          fontWeight: 'bold',
          "&.Mui-focused": {
            color: 'rgba(0, 0, 0, 0.87)',
          },
        },
      }
    },
    MuiTextField: {
      defaultProps: {
        variant: 'standard',
      },
    },
  },

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

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