I'm having a really hard time to remove the yellow background on autofill from the Material UI TextField component.
In older versions I did it this way:
const inputStyle = { WebkitBoxShadow: '0 0 0 1000px white inset' };
<TextField
...
inputStyle={inputStyle}
/>
But in the recent version the inputStyle prop was removed and added InputProps instead.
I've tried to remove it this way, but the yellow background color still appears:
import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "#material-ui/core/styles";
import TextField from "#material-ui/core/TextField";
const styles = {
root: {
':-webkit-autofill': {
WebkitBoxShadow: '0 0 0 1000px white inset',
backgroundColor: 'red !important'
}
},
input: {
':-webkit-autofill': {
WebkitBoxShadow: '0 0 0 1000px white inset',
backgroundColor: 'red !important'
}
}
};
const renderTextField = (props) => {
const {
classes,
label,
input,
meta: { touched, error },
...custom
} = props;
return (
<TextField
label={label}
placeholder={label}
error={touched && error}
helperText={touched && error}
className={classes.root}
InputProps={{
className: classes.input
}}
{...input}
{...custom}
/>
);
}
renderTextField.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(renderTextField);
The replacement for inputStyle would be inputProps:
const inputStyle = { WebkitBoxShadow: "0 0 0 1000px white inset" };
<TextField name="last_name" inputProps={{ style: inputStyle }} />
InputProps vs. inputProps is a common point of confusion. Uppercase "I" InputProps provides props for the Input element within TextField (Input wraps the native input in a div). Lowercase "i" inputProps provides props for the native input element rendered within the Input component. If you want to provide inline styles to the native input element, the code example above will do the trick.
There are also several other ways to do this using classes via withStyles.
If you want to use the className property, again this needs to be on the input (rather than the div wrapping it) in order to have the desired effect. So the following will also work:
const styles = {
input: {
WebkitBoxShadow: "0 0 0 1000px white inset"
}
};
const MyTextField = ({classes}) => {
return <TextField name="email" inputProps={{ className: classes.input }} />;
}
export default withStyles(styles)(MyTextField);
If you want to leverage the ":-webkit-autofill" pseudo-class, you just need to adjust your JSS syntax and add the "&" to reference the selector of the parent rule:
const styles = {
input: {
"&:-webkit-autofill": {
WebkitBoxShadow: "0 0 0 1000px white inset"
}
}
};
const MyTextField = ({classes}) => {
return <TextField name="email" inputProps={{ className: classes.input }} />;
}
export default withStyles(styles)(MyTextField);
You can also leverage either of these class approaches, but using uppercase "I" InputProps via the classes property:
const styles = {
input: {
WebkitBoxShadow: "0 0 0 1000px white inset"
}
};
const MyTextField = ({classes}) => {
return <TextField name="email" InputProps={{ classes: { input: classes.input } }} />;
}
export default withStyles(styles)(MyTextField);
Here is a working example with all of these approaches:
You can add it to a theme on the overrides.
overrides: {
MuiOutlinedInput: {
input: {
'&:-webkit-autofill': {
'-webkit-box-shadow': '0 0 0 100px #000 inset',
'-webkit-text-fill-color': '#fff'
}
}
}
}
Related
I am trying to change the Material UI DatePicker date text and calendar icon color.
I tried to change it passing style to InputProps, but it worked only for removing border.
Rather than that, nothing changes, I tried to apply style to theme.tsx, but it also didn't help.
Any help will be appreciated.
import * as React from "react";
import Stack from "#mui/material/Stack";
import TextField from "#mui/material/TextField";
import AdapterDateFns from "#mui/lab/AdapterDateFns";
import LocalizationProvider from "#mui/lab/LocalizationProvider";
import DesktopDatePicker from "#mui/lab/DesktopDatePicker";
import { makeStyles, createStyles } from "#material-ui/core";
const useStyles = makeStyles(() =>
createStyles({
noBorder: {
outline: "none",
border: "none",
color: "#fff",
},
})
);
export default function DatePicker() {
const [value, setValue] = React.useState<Date | null>();
const classes = useStyles();
const handleChange = (newvalue: Date | null) => {
setValue(newvalue);
};
return (
<LocalizationProvider dateAdapter={AdapterDateFns}>
<Stack spacing={2}>
<DesktopDatePicker
inputFormat="dd/MM/yyy"
value={value}
onChange={handleChange}
renderInput={(params) => <TextField {...params} />}
InputProps={{
classes: { notchedOutline: classes.noBorder },
}}
/>
</Stack>
</LocalizationProvider>
);
}
1st solution - using sx property
You can set sx property to <TextField/> component in order to overwrite default style properties:
const color = "#c44242";
...
return (
<DatePicker
renderInput={(params) => {
return (
<TextField
{...params}
sx={{
svg: { color },
input: { color },
label: { color }
}}
/>
);
}}
...other props
/>
)
Setting colors with sx prop
2nd solution - providing a custom theme
You can also create a custom theme and overwrite colors inside it:
const theme = createTheme({
components: {
MuiIconButton: {
styleOverrides: {
sizeMedium: {
color
}
}
},
MuiOutlinedInput: {
styleOverrides: {
root: {
color
}
}
},
MuiInputLabel: {
styleOverrides: {
root: {
color
}
}
}
}
});
Then you wrap your component with ThemeProvdier.
Overriding Theme Demo
The solution that worked for me is as follows -
I used the sx property in <TextField/> which is also mentioned by dmitriif.
The implementation looked something like this -
<DateTimePicker
value={value}
onChange={handleChange}
renderInput={(params) => (
<TextField
{...params}
sx={{
svg: { color: '#fff' },
input: { color: '#fff' },
}}
/>
)}
/>
Try to inspect element then you can see the styling of the inspected element. Inspect element is so useful.
How can I delete the arrows of a textField number?
this doesn't work:
const useStyles = makeStyles(theme => ({
const theme = createMuiTheme({
MuiInput: {
root: {
"&::-webkit-outer-spin-button, &::-webkit-inner-spin-button": {
"-webkit-appearance": "none",
display: "none"
}
}
}
})
by mean this question this should work, but in my case no
Disable webkit's spin buttons on input type="number"?
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
my textfield is
<TextField
InputProps={{ classes: { input: classes.inputStyle } }}
size="small"
type="number"
but when i've applied my textField grow too much, i want to size be small
CSS approach is right. this style should be applied to the input element and to apply the styles to the input element, target the input class instead of root. And another issue is here you're trying to use makeStyles and createMuiTheme at once which is not right.
To style using createMuiTheme this is the way:
const theme = createMuiTheme({
overrides: {
MuiInput: {
input: {
"&::-webkit-outer-spin-button, &::-webkit-inner-spin-button": {
"-webkit-appearance": "none",
display: "none"
}
}
}
}
});
And then wrap your app within <MuiThemeProvider>.
To do this using makeStyles:
const useStyles = makeStyles((theme) => ({
inputStyle: {
"&::-webkit-outer-spin-button, &::-webkit-inner-spin-button": {
"-webkit-appearance": "none",
display: "none"
}
}
}));
And then target the input class by using InputProps prop.
export default function TextStyle() {
const classes = useStyles();
return (
<div>
<TextField
InputProps={{ classes: { input: classes.inputStyle } }}
type="number"
/>
</div>
);
}
Here is the working demo:
The colour of a disabled input text field created using material UI is light grey by default and it is not very visible against a white background. Is there any way to change the font colour of a disabled input text field?
Below is an example of how to do this showing the customized version next to the default styling.
import React from "react";
import { withStyles } from "#material-ui/core/styles";
import TextField from "#material-ui/core/TextField";
import Button from "#material-ui/core/Button";
const DarkerDisabledTextField = withStyles({
root: {
marginRight: 8,
"& .MuiInputBase-root.Mui-disabled": {
color: "rgba(0, 0, 0, 0.6)" // (default alpha is 0.38)
}
}
})(TextField);
export default function Demo() {
const [disabled, setDisabled] = React.useState(true);
return (
<>
<Button onClick={() => setDisabled(!disabled)}>Toggle Disabled</Button>
<br />
<br />
<DarkerDisabledTextField
disabled={disabled}
id="outlined-basic"
label="Custom"
value={`Disabled = ${disabled}`}
variant="outlined"
/>
<TextField
disabled={disabled}
id="outlined-basic"
label="Default"
value={`Disabled = ${disabled}`}
variant="outlined"
/>
</>
);
}
I think the simplest way is to create an object to define the font color and pass it to the TextField's inputProps.
const fontColor = {
style: { color: 'rgb(50, 50, 50)' }
}
This way you can toggle the font with the components state as you wish or simply keep it constant.
<TextField
label="Title"
onChange={updateTitle}
value={title}
disabled={true}
inputProps={fontColor}/>
import { TextField, styled } from '#mui/material'
const CustomTextField = styled(TextField)({
'& .MuiInputBase-root.Mui-disabled': {
backgroundColor: '#f0f0f0',
},
});
Then use them as standard component
<CustomTextField
name="manual"
label="Manual"
size="small"
disabled={watch({}).platform === 'manual' ? false : true}
/>
if u use RHF with controller, mean u create custom TextField using RHF, u just change the component inside the styled()
For example:
import RHFTextField from "../RHFTextField"
const CustomTextField = styled(RHFTextField)({
'& .MuiInputBase-root.Mui-disabled': {
backgroundColor: '#f0f0f0',
},
});
with background color changes, it will more visible..
In the Mui-v5 above solution is not working.
Below are the solutions for Mui-v5.
Solution 1:
const styles = theme => ({
input: {
"& input.Mui-disabled": {
color: "green"
}
}
});
Solution: 2 (use sx property for the component)
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}
eg.
<TextField
fullWidth
disabled=true
variant="outlined"
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}
/>
None of all the many answers online worked for me, using MUI v4.
Finally I found a very easy but hacky solution for setting the font color of a disabled TextField component.
The problem is, (at least for Firefox) it will use this weird style cheet:
.css-[...]-MuiInputBase-input-MuiOutlinedInput-input.Mui-disabled {
opacity: 1;
-webkit-text-fill-color: rgba(0, 0, 0, 0.38);
}
I'm too new to this stuff and I don't know how to change it, so my way was to overwrite it:
.css-[...]-MuiFormControl-root-MuiTextField-root input {
-webkit-text-fill-color: rgba(255,255,255,0.6) !important;
color: rgba(255,255,255,0.6);
}
The !important part is important!
In React with MUI v4, it's as simple as that:
const textFieldColor = /*...*/;
const textFieldSX = {
input: {
"-webkit-text-fill-color": `${textFieldColor} !important`,
color: `${textFieldColor} !important`,
},
};
/*...*/
<TextField sx={textFieldSX} />
Just in case, this -webkit-text-fill-color property would turn into color one time, one can use an !important there too.
works (MUIv5):
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}
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>
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:
As per the latest version of material UI. ie. "#mui/material": "^5.0.1"
We can do it like this:
<FormLabel required>Name:</FormLabel>
And in the theme:
import { createTheme } from "#mui/material";
export const theme = createTheme({
components: {
MuiFormLabel: {
styleOverrides: {
asterisk: {
color: "#db3131",
"&$error": {
color: "#db3131",
},
},
},
},
},
});
In Mui v5 :
const theme = createTheme({
components: {
MuiFormLabel: {
styleOverrides: {
asterisk: {color:"red"},
},
},
},
})
Alvin's answer shows how to do this globally in your theme. You can also do this on a case-by-case basis using the FormLabel asterisk class via the InputLabel props.
Below are the relevant portions from your code that I changed. Also note that the default behavior for the asterisk is for it to be red if the input is in an "error" state. For instance if you add the error property to the TextField the asterisk will be red, but that also has additional effects on styling beyond the asterisk.
const styles = {
labelAsterisk: {
color: "red"
}
};
<InputLabel
FormLabelClasses={{
asterisk: this.props.classes.labelAsterisk
}}
required
shrink
htmlFor="age-native-simple"
>
Age
</InputLabel>
<TextField
required
InputLabelProps={{
shrink: true,
FormLabelClasses: {
asterisk: this.props.classes.labelAsterisk
}
}}
id="standard-name"
label="Name"
margin="normal"
helperText="Some important text"
/>
const StyledApp = withStyles(styles)(App);
//import createTheme and ThemeProvider at the top
import { createTheme, ThemeProvider } from '#mui/material/styles';
const abc = () => {
//add the theme at the top of your arrow function
const theme = createTheme({
components: {
MuiFormLabel: {
styleOverrides: {
asterisk: { color: "red" },
},
},
},
})
return ( // wrap your jsx with <ThemeProvider>
<ThemeProvider theme={theme}>
<TextField required
id="outlined-required"
label="Full Name"
type="text"
size='small'
/>
</ThemeProvider>
)
}
For those who are looking answer for MUI v5 with TextField outlined variant
const theme = createTheme({
components:{
MuiInputLabel:{
styleOverrides:{
asterisk:{
color:"#d32f2f"
}
}
}
}
});
Try this simple and easy
render(){
const name = <p>Name<span style={{ color: "red" } >*</span></p>
const email = <p>Email<span style={{ color: "red" } >*</span></p>
.
.
.
return (
<div>
<TextField type="text" label={name} />//or Input tag
<TextField type="email" label={email} />//or Input tag
.
.
.
</div>
)
}
I'm currently using MUI.
And I'm having issues trying to change the font color of the multiline TextField.
<TextField className = "textfield"
fullWidth
multiline
label = "Debugger"
rows = "10"
margin = "normal"/>
And the CSS:
.textfield {
background-color: #000;
color: green;
}
However, somehow I only get the black background and the font is still black. Does anyone know how to properly change the font color of a TextField using MUI?
In MUI v5, you can just do this using sx prop:
<TextField sx={{ input: { color: 'red' } }} />
A bit longer approach if you want something more reusable:
const options = {
shouldForwardProp: (prop) => prop !== 'fontColor',
};
const StyledTextField = styled(
TextField,
options,
)(({ fontColor }) => ({
input: {
color: fontColor,
},
}));
<StyledTextField label="Outlined" fontColor="green" />
<StyledTextField label="Outlined" fontColor="purple" />
Live Demo
I referred this page TextField API
And I override the TextField using Classes
const styles = theme => ({
multilineColor:{
color:'red'
}
});
Apply the class to TextField using InputProps.
<TextField
className = "textfield"
fullWidth
multiline
InputProps={{
className: classes.multilineColor
}}
label = "Debugger"
rows = "10"
margin = "normal" />
EDIT In older version you have to specify the key input
<TextField
className = "textfield"
fullWidth
multiline
InputProps={{
classes: {
input: classes.multilineColor
}
}}
label = "Debugger"
rows = "10"
margin = "normal"
/>
Hope this will work.
Using inputProps is correct, as others have posted. Here's a slightly simpler example:
<TextField
multiline
inputProps={{ style: { color: "red" } }}
/* ... */
/>
How to set color property and background property of Text Field
import PropTypes from "prop-types";
import { withStyles } from "#material-ui/core/styles";
import TextField from "#material-ui/core/TextField";
const styles = {
root: {
background: "black"
},
input: {
color: "white"
}
};
function CustomizedInputs(props) {
const { classes } = props;
return (
<TextField
defaultValue="color"
className={classes.root}
InputProps={{
className: classes.input
}}
/>
);
}
CustomizedInputs.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(CustomizedInputs);
//textfield customization
const CssTextField = withStyles({
root: {
'& .MuiInputBase-root': {
color: 'white',
},
},
})(TextField);
This should work !
import { TextField, makeStyles } from "#material-ui/core";
const useStyles = makeStyles((theme) => ({
input: {
color: "#FFF",
},
}));
const MyInput = () => {
const classes = useStyles();
return (
<TextField
inputProps={{ className: classes.input }}
id="outlined-basic"
label="Write something..."
variant="outlined"
/>
);
};
export default MyInput;
If you are looking for a more generic fix, you can change your theme to contain that color, in my case I needed to change the input background and the disabled as well, so I end up using the ThemeProvider and a custom Theme.
Custom theme
const theme = createTheme({
components: {
MuiInputBase: {
styleOverrides: {
root: {
backgroundColor: '#fff',
'&.Mui-disabled': {
backgroundColor: '#e4e4e4',
},
},
},
},
},
});
const withDefaultTheme =
<P extends object>(Component: React.ComponentType<P>) =>
(props: any) =>
(
<ThemeProvider theme={theme}>
<Component {...props} />
</ThemeProvider>
);
This is working in my case:
import React from 'react';
import { TextField, } from '#mui/material';
import { makeStyles } from "#mui/styles";
const useStyles = makeStyles((theme) => ({
textfield_input: {
color: `#c5cae9 !important`,
}
}));
function Videoedit() {
const classes = useStyles();
return (<div>
<TextField
value={title}
required
label="Title"
variant="filled"
inputProps={{className: classes.textfield_input}}
color="primary"
focused
/>)
</div>;
}
export default Videoedit;
if you are using styled component with TextField then just write this code inside your styled component:
input: {
color: '#ffffff',
},
if you want to change placeholder color:
label: {
color: '#ffffff',
},
Use your Component like below
const MyTextField = withStyles({
root: {
"& .MuiInputBase-root.Mui-disabled": {
color: "rgba(0, 0, 0,0.0)"
},
"& .MuiFormLabel-root.Mui-disabled": {
color: "rgba(0, 0, 0,0.0)"
},
}
})(TextField);
Try below css
.textfield{
color: #000;
}