I have been trying to work out how to style a material-ui TextField component.
<TextField id="email" label="Email" className={classes.textField} value={this.state.form_email} onChange={this.handle_change('form_email')} margin="normal" />
My classes are created as follows:
const styles = theme => ({ textField: { width: '90%', marginLeft: 'auto', marginRight: 'auto', color: 'white', paddingBottom: 0, marginTop: 0, fontWeight: 500 }, });
My problem is that I can not seem to get the colour of the text field to change to white. I seem to be able to apply styling to the overall text field (because the width styling works etc)... but I think the problem is that I am not applying the styles further down the chain and into the actual input.
I have tried to look at the other answers dealing with passing inputProps but have had no success.
Have tried everything to the best of my ability but think I need to ask if anyone knows what I am doing wrong.
What it currently looks like
To style a React Material UI text field, we can call the makeStyles function with a function that returns an object that specifies the styles. Then we can set the InputProps prop of the TextField to specify the styles of the input element in the text field.
const styles = theme => ({ textField: { width: '90%', marginLeft: 'auto', marginRight: 'auto', color: 'white', paddingBottom: 0, marginTop: 0, fontWeight: 500 }, });
To make a controlled text field, we can add the value and onChange props to the TextField . We have the name state which is passed into the value prop. And the handleChange function which is passed into the onChange prop.
To change the text field font color in React Material UI, we call the makeStyles function with an object with the styles we want to apply. to call makeStyles with an object that has the input property that's set to an object with the color property set to 'blue' . Next, we call useStyles to return the classes object.
You need to add the InputProps
property to the TextField.
const styles = theme => ({ textField: { width: '90%', marginLeft: 'auto', marginRight: 'auto', paddingBottom: 0, marginTop: 0, fontWeight: 500 }, input: { color: 'white' } });
JSX:
<TextField id="email" label="Email" className={classes.textField} value={this.state.form_email} onChange={this.handle_change('form_email')} margin="normal" InputProps={{ className: classes.input, }} />
As an aside, you can also style the label or use an override as described here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With