I have a form
with a very awkward background color. Would like to change the color of the helper text of the Outlined TextField
when it is in an error state but can't seem to override it. It persists the red.
See CodeSandBox .
I have a form
with a very awkward background color. Would like to change the color of the helper text of the Outlined TextField
when it is in an error state but can't seem to override it. It persists the red.
See CodeSandBox .
The problem is caused by CSS specificity (the base style has more specific classname, i.e. MuiFormHelperText-root.Mui-error
than the overriding style class). It's recommended to use &$
syntax under this circumstance:
const helperTextStyles = makeStyles(theme => ({
root: {
margin: 4,
'&$error': {
color: 'white'
}
},
error: {} //<--this is required to make it work
}));
You can also refer to this section for an example and a bit more explanation.
For some reason, the error text color is generated under the following className: .MuiFormHelperText-root.Mui-error
So overriding error rule alone is not enough.
This will do the trick:
const helperTextStyles = makeStyles(theme => ({
root: {
margin: 4,
color:'black',
},
error: {
"&.MuiFormHelperText-root.Mui-error" :{
color: theme.palette.common.white,
},
},
}));
It would be better to customize your Mui Theme as follows:
const Theme = {
components: {
MuiFormHelperText: {
styleOverrides: {
root: {
color: "red"
}
}
}
}
};
See https://mui.com/material-ui/customization/theming/ for more information.
The problem is caused by CSS specificity (the base style has more specific classname, i.e. MuiFormHelperText-root.Mui-error
than the overriding style class). It's recommended to use &$
syntax under this circumstance:
const helperTextStyles = makeStyles(theme => ({
root: {
margin: 4,
'&$error': {
color: 'white'
}
},
error: {} //<--this is required to make it work
}));
You can also refer to this section for an example and a bit more explanation.
!important
... like color: 'white !important'
–
Kenyatta i found one solution to change the color of text field.
<TextField
error
id="filled-error-helper-text"
label="Error"
defaultValue="Hello World"
helperText="Incorrect entry."
variant="filled"
/>
here you can see error which is Boolean, so you might be having validation like YUP, if you do then you pass it like this.
<TextField
error={!valid}
id="filled-error-helper-text"
label="Error"
defaultValue="Hello World"
helperText="Incorrect entry."
variant="filled"
/>
here i am changing color base on valid keyword.
For those who search how to edit the color without theme overrides :
<TextField FormHelperTextProps={{ sx: { color: 'primary.main' } }} />
<TextField FormHelperTextProps={{ sx: { color: 'success.main' } }} />
<TextField FormHelperTextProps={{ sx: { color: 'red' } }} />
© 2022 - 2024 — McMap. All rights reserved.
!important
... likecolor: 'white !important'
– Kenyatta