Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the asterisk color in required * field

I have two required fields in my form .I want the asterisk color should be red.Currently it is showing black .I am using material UI react library ? here is my code https://codesandbox.io/s/r7lq1jnjl4 documents https://material-ui.com/demos/text-fields/

<FormControl>
                  <TextField
                    required
                    InputLabelProps={{
                      shrink: true
                    }}
                    id="standard-name"
                    label="Name"
                    margin="normal"
                    helperText="Some important text"
                  />
                </FormControl>
like image 584
user944513 Avatar asked Mar 01 '19 01:03

user944513


2 Answers

Based on this documentation on how to customize components through theme overrides for a FormLabel (which will also include InputLabel), you should use createMuiTheme and add the following overrides:

const formLabelsTheme = createMuiTheme({
  overrides: {
    MuiFormLabel: {
      asterisk: {
        color: '#db3131',
        '&$error': {
          color: '#db3131'
        },
      }
    }
  }
})

Then, you wrap your <form> within a <MuiThemeProvider> like so:

<MuiThemeProvider theme={formLabelsTheme}>
  <form noValidate autoComplete="off">

...
...
...

  </form>
</MuiThemeProvider>

Here is a forked code sandbox which demonstrates this code in action.

Since you are already creating a theme, you could just put your overrides in that theme, but you'll need to move your <form> to be within the <MuiThemeProvider> that you already have in your code.

The resulting form labels look like this: resulting form, with theme overridden

like image 84
Alvin S. Lee Avatar answered Sep 22 '22 06:09

Alvin S. Lee


enter image description here

In Mui v5 :

const theme = createTheme({

  components: {
    MuiFormLabel: {
      styleOverrides: {
        asterisk: {color:"red"},
      },
    },
  },

})
like image 35
Samira Avatar answered Sep 18 '22 06:09

Samira