MaterialUI - Changing the color Textfield on focus

穿精又带淫゛_ 提交于 2019-12-25 00:18:57

问题


I'm trying to change the color of the label text in Textfield but I can't seem to figure it out.

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:

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!


回答1:


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>


来源:https://stackoverflow.com/questions/52615530/materialui-changing-the-color-textfield-on-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!