Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How would I hide / remove the underline in a TextField component without using the following code:

const theme = createMuiTheme({
  overrides: {
    MuiInput: {
      underline: {
        '&:hover:not($disabled):before': {
          backgroundColor: 'rgba(0, 188, 212, 0.7)',
        },
      },
    },
  },
});

I would like to do it with props and according to the docs: https://material-ui.com/api/input/

I should be able to change the underline prop, but it does not work.

like image 658
Kevin Vugts Avatar asked May 20 '18 15:05

Kevin Vugts


3 Answers

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.

like image 138
thirtydot Avatar answered Oct 08 '22 11:10

thirtydot


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'],
    },
  },
})
like image 42
juank11memphis Avatar answered Oct 08 '22 11:10

juank11memphis


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)',
    }
  }
})
like image 36
Jonghee Park Avatar answered Oct 08 '22 11:10

Jonghee Park