Romanas
Romanas

Reputation: 645

How to change TextField input's focus border using Material-UI theme

I'm trying to create my own theme with Material-Ui v.5.4.0. And I faced with problem. I can't change TextField focused border color and width. I spend hours for research and didn't find working solution. And I started to think is it even possible to do that in theme? But it's not logical.

My current theme code:

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

// Colors
const blue = '#5A5BD4'
const blueDark = '#4f4fd8'

// Parameters
const buttonsBorderRadius = 20
const buttonPadding = '5px 15px'
const inputBorderRadius = 10

const theme = createTheme({
  components: {
    // Buttons
    MuiButton: {
      variants: [
        //   Blue button
        {
          props: { variant: 'blueButton' },
          style: {
            backgroundColor: blue,
            color: '#ffffff',
            borderRadius: buttonsBorderRadius,
            textTransform: 'none',
            padding: buttonPadding,
            '&:hover': {
              backgroundColor: blueDark
            }
          }
        },
        //   Transparent button
        {
          props: { variant: 'transparentButton' },
          style: {
            color: blue,
            borderRadius: buttonsBorderRadius,
            textTransform: 'none',
            padding: buttonPadding

          }
        }
      ]
    },
    // Inputs
    MuiOutlinedInput: {
      styleOverrides: {
        root: {
          borderRadius: inputBorderRadius,
          '& fieldset': {
            border: `1px solid ${blue}`
          }
        },
        focus: {
          border: `1px solid ${blueDark}`
        }
      }
    }
  }
})

export default theme

My input code:

<TextField
                size='small'
                variant='outlined'
                label={t('paslelbimo_data_nuo')}
                type='date'
                InputLabelProps={{
                  shrink: true
                }}
                fullWidth
                value={publicationDateFrom}
                onChange={(e) => setPublicationDateFrom(e.target.value)}
              />

Upvotes: 8

Views: 9345

Answers (1)

Steve G
Steve G

Reputation: 13397

Since I wasn't able to tell exactly what your desired effect was on focus vs not focused, I decided to just create a generic example, with overly dramatic styling, that may be useful to modify for your needs:

Essentially, I'm just overriding .MuiOutlinedInput-notchedOutline for both the focused an unfocused states:

const theme = createTheme({
  components: {
    // Inputs
    MuiOutlinedInput: {
      styleOverrides: {
        root: {
          ...
          "& .MuiOutlinedInput-notchedOutline": {
            border: `5px solid green`,
          },
          "&.Mui-focused": {
            "& .MuiOutlinedInput-notchedOutline": {
              border: `5px dotted red`,
            },
          }
        },
      }
    }
  }
});

Working example CodeSandbox: https://codesandbox.io/s/customstyles-material-demo-forked-uri26?file=/theme.js:84-531

Upvotes: 16

Related Questions