I'm currently utilizing Material-UI
to construct a form using makeStyles
and CSS-In-JS
for styling purposes. I have a form component from the Material-UI library that I want to style according to my needs. My main focus is on how to address classes originating from the library's form element and override their styles.
Below is your reference, showcasing the form and the specific Material-UI
class that you aim to modify (the before tag of the form's initial text input field):
https://i.sstatic.net/FQ1iY.png
The objective is to alter the border-bottom
property of the text field. Here's what I've tried so far. Pay attention to the underline
class in the CSS and the first text input of the form:
const useStyles = makeStyles((theme) => ({
root: {
display: 'flex',
width: '100%',
display: 'flex',
alignItems: 'center'
},
container: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100vh'
},
paper: {
padding: theme.spacing(2),
display: 'flex',
justifyContent: 'center',
color: 'snow',
background: 'salmon'
},
form: {
background: 'salmon',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'flex-start',
height: '50vh',
width: '50%'
},
customInput: {
background: 'black'
},
underline: {
'&:before': {
borderBottom: '10px solid green'
},
},
}));
export default function FormOne() {
const classes = useStyles();
return (
<Grid container className={classes.container}>
<Grid item xs={12} md={6}>
<Paper elevation="5" className={classes.paper}>
<Formik
initialValues={{
email: '',
password: '',
}}
validate={values => {
const errors = {};
if (!values.email) {
errors.email = 'Required';
} else if (
!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)
) {
errors.email = 'Invalid email address';
}
return errors;
}}
onSubmit={(values, { setSubmitting }) => {
setTimeout(() => {
setSubmitting(false);
alert(JSON.stringify(values, null, 2));
}, 500);
}}
>
{({ submitForm, isSubmitting }) => (
<Form className={classes.form}>
<Field
component={TextField}
name="email"
type="email"
label="Email"
fullWidth="true"
variant="filled"
size="small"
color="primary"
className={classes.underline}
/>
<br />
<Field
component={TextField}
type="password"
label="Password"
name="password"
fullWidth="true"
variant="filled"
size="small"
color="secondary"
/>
<br />
<Field
component={TextField}
type="password"
label="Password"
name="password"
fullWidth="true"
variant="filled"
size="small"
color="primary"
/>
{isSubmitting && <LinearProgress />}
<br />
<Button
variant="contained"
color="primary"
disabled={isSubmitting}
onClick={submitForm}
className={classes.button}
>
Submit
</Button>
</Form>
)}
</Formik>
</Paper>
</Grid>
</Grid>
);
}
In general, this method works well when styling custom elements. By incorporating const classes = useStyles();
within my function, I can use className={classes.nameOfClass}
on the element and target it through makeStyles
for styling. However, this approach does not seem to work smoothly when trying to override classes from the Material-UI
library.
If you have any insights on how to pinpoint .MuiFilledInput-underline:before
in makeStyles
and customize its styles effectively, I would greatly appreciate your guidance!