I would like to override default form input style, so I've found this code below. The problem is that I would like to add error behaviour
of the form input, but I don't know how. Could anyone help me to add red border to the input when it's in error state? Thank you very much!
import React from 'react';
import { withStyles } from 'material-ui/styles';
import TextField from 'material-ui/TextField';
const styles = theme => ({
textFieldRoot: {
padding: 0,
'label + &': {
marginTop: theme.spacing.unit * 3,
},
},
textFieldInput: {
borderRadius: 4,
backgroundColor: theme.palette.common.white,
border: '1px solid #ced4da',
fontSize: 16,
padding: '10px 12px',
width: 'calc(100% - 24px)',
transition: theme.transitions.create(['border-color', 'box-shadow']),
'&:focus': {
borderColor: '#80bdff',
boxShadow: '0 0 0 0.2rem rgba(0,123,255,.25)',
},
},
textFieldFormLabel: {
fontSize: 18,
},
});
function CustomizedInputs(props) {
const { classes } = props;
return (
<div className={classes.container}>
<TextField
defaultValue="react-bootstrap"
label="Bootstrap"
id="bootstrap-input"
InputProps={{
disableUnderline: true,
classes: {
root: classes.textFieldRoot,
input: classes.textFieldInput,
},
}}
InputLabelProps={{
shrink: true,
className: classes.textFieldFormLabel,
}}
/>
</div>
);
}
export default withStyles(styles)(CustomizedInputs);
You could approach this a few ways possibly using a regex
, also Redux has a validate
attribute but this example uses onChange
to run a check function that sets a boolean if there is an error. Then that boolean determines which class name the input will pick up.
import React from 'react';
import { withStyles } from 'material-ui/styles';
import TextField from 'material-ui/TextField';
const styles = theme => ({
textFieldRoot: {
padding: 0,
'label + &': {
marginTop: theme.spacing.unit * 3,
},
},
textFieldError: {
border: '1px solid red',
},
textFieldInput: {
borderRadius: 4,
backgroundColor: theme.palette.common.white,
border: '1px solid #ced4da',
fontSize: 16,
padding: '10px 12px',
width: 'calc(100% - 24px)',
transition: theme.transitions.create(['border-color', 'box-shadow']),
'&:focus': {
borderColor: '#80bdff',
boxShadow: '0 0 0 0.2rem rgba(0,123,255,.25)',
},
},
textFieldFormLabel: {
fontSize: 18,
},
});
function CustomizedInputs(props) {
const { classes } = props;
const checkField = () => {
// some condition to check for error
errorVariable = true
}
return (
<div className={classes.container}>
<TextField
defaultValue="react-bootstrap"
label="Bootstrap"
id="bootstrap-input"
onChange={checkField}
InputProps={{
disableUnderline: true,
classes: {
root: classes.textFieldRoot,
input: errorVariable ? classes.textFieldError : classes.textFieldInput,
},
}}
InputLabelProps={{
shrink: true,
className: classes.textFieldFormLabel,
}}
/>
</div>
);
}
export default withStyles(styles)(CustomizedInputs);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.