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

5

9

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.

Rumanian answered 20/5, 2018 at 15:40 Comment(0)
B
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.

Bionics answered 20/5, 2018 at 17:22 Comment(2)
Hey man. Yeah I did figure that out already. Thanks anyways for the help!Rumanian
when i use inputProps, the suggestions disappear for autocomplete free solo.Ungainly
C
8

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'],
    },
  },
})
Carbonate answered 8/11, 2018 at 17:17 Comment(0)
I
3

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)',
    }
  }
})
Inconvertible answered 13/9, 2018 at 8:41 Comment(0)
M
2

solving this slightly different but in the same vein as the accepted answer as I hit a typescript error attempting to use the disableUnderline on the component itself.

import { createTheme } from '@mui/material/styles'

export const componentTheme = createTheme({
  components: {
    MuiTextField: {
      defaultProps: {
        InputProps: {
          disableUnderline: true,
        },
      },
    },
  },
}
Minimus answered 22/8, 2022 at 3:9 Comment(0)
W
0

Alternative way for solving using mui styled.

  const CssAutocomplete = styled(Autocomplete)({
     "& .MuiInput-underline:after": {
         borderBottomColor: "#CDCDCD",
      },
     "& .MuiInput-underline:before": {
         borderBottomColor: "#fff",
      },
     "& .MuiInput-underline:hover:before": {
         borderBottomColor: ["#fff", "!important"],
      },
    });
Weinman answered 24/11, 2023 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.