How to override styles for material-ui TextField component without using the MUIThemeProvider?

跟風遠走 提交于 2019-12-05 22:47:25

This is how you do it:

<TextField
    id="name"
    label="Name"
    value={this.state.name}
    margin="normal"
    InputProps={{disableUnderline: true}}
/>

How did I figure it out?

You have linked to the Input documentation, which does indeed have a disableUnderline prop.

However, you are using a TextField component:

It's important to understand that the text field is a simple abstraction on top of the following components:

  • FormControl
  • InputLabel
  • Input
  • FormHelperText

If you look at the list of available props for TextField:

InputProps - object - Properties applied to the Input element.

I found a way to fix this issue..

<TextField InputProps={{classes: {underline: classes.underline}}} />

const styles = theme => ({
  underline: {
    '&:hover': {
      '&:before': {
        borderBottom: ['rgba(0, 188, 212, 0.7)', '!important'],
      }
    },
    '&:before': {
      borderBottom: 'rgba(0, 188, 212, 0.7)',
    }
  }
})

With the most recent version of Material-UI this was the only way I could make this work:

<TextField InputProps={{classes: {underline: classes.underline}}} />

const styles = theme => ({
  underline: {
    '&:before': {
      borderBottomColor: colors.white,
    },
    '&:after': {
      borderBottomColor: colors.white,
    },
    '&:hover:before': {
      borderBottomColor: [colors.white, '!important'],
    },
  },
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!