admin管理员组文章数量:1425786
How do I change the color of an active input? I'd like to change the default blue one but cant find how to.
From what I've tried it's not a matter of color
attribute, neither in the FormControl, the InputLabel or the Input. Also there is no underlineStyle
prop available (docs)
I'd like to change the color only when the input is active, that is to say the user is writing in it, to my primary
color as defined in my theme
.
I'm using Input
and not TextField
because I want to use InputAdornments
as per .
EDIT
It seems like I should change .MuiInput-inkbar-169:after
's background-color
. How do you suggest I do that? Is there another way?
/* eslint-disable flowtype/require-valid-file-annotation */
import React from 'react';
import { withStyles } from 'material-ui/styles';
import Input, { InputLabel } from 'material-ui/Input';
import { FormControl } from 'material-ui/Form';
const styles = theme => ({
formControl: {
margin: theme.spacing.unit,
width: '100%',
},
textField: {
marginLeft: theme.spacing.unit,
marginRight: theme.spacing.unit,
}
});
class CustomInput extends React.Component {
...
render() {
const { classes, label, id } = this.props;
const error = (this.props.valid === undefined ? false : !this.props.valid) && this.state.visited
return (
<FormControl className={classes.formControl} >
<InputLabel error={error}>{label}</InputLabel>
<Input
id={id}
className={classes.textField}
value={this.state.value || ''}
endAdornment={this.props.endAdornment ?
<InputAdornment position="end">
{this.props.endAdornment}
</InputAdornment> : ''
}
/>
</FormControl>
);
}
}
CustomInput.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(CustomInput);
Adding this to my style.css
does not change anything, even trying with an !important
.MuiInput-inkbar-169:after {
background-color: #3f51b5 !important
}
How do I change the color of an active input? I'd like to change the default blue one but cant find how to.
From what I've tried it's not a matter of color
attribute, neither in the FormControl, the InputLabel or the Input. Also there is no underlineStyle
prop available (docs)
I'd like to change the color only when the input is active, that is to say the user is writing in it, to my primary
color as defined in my theme
.
I'm using Input
and not TextField
because I want to use InputAdornments
as per https://material-ui-next./demos/text-fields/#input-adornments.
EDIT
It seems like I should change .MuiInput-inkbar-169:after
's background-color
. How do you suggest I do that? Is there another way?
/* eslint-disable flowtype/require-valid-file-annotation */
import React from 'react';
import { withStyles } from 'material-ui/styles';
import Input, { InputLabel } from 'material-ui/Input';
import { FormControl } from 'material-ui/Form';
const styles = theme => ({
formControl: {
margin: theme.spacing.unit,
width: '100%',
},
textField: {
marginLeft: theme.spacing.unit,
marginRight: theme.spacing.unit,
}
});
class CustomInput extends React.Component {
...
render() {
const { classes, label, id } = this.props;
const error = (this.props.valid === undefined ? false : !this.props.valid) && this.state.visited
return (
<FormControl className={classes.formControl} >
<InputLabel error={error}>{label}</InputLabel>
<Input
id={id}
className={classes.textField}
value={this.state.value || ''}
endAdornment={this.props.endAdornment ?
<InputAdornment position="end">
{this.props.endAdornment}
</InputAdornment> : ''
}
/>
</FormControl>
);
}
}
CustomInput.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(CustomInput);
Adding this to my style.css
does not change anything, even trying with an !important
.MuiInput-inkbar-169:after {
background-color: #3f51b5 !important
}
Share
Improve this question
edited Dec 20, 2017 at 11:47
ted
asked Dec 20, 2017 at 11:21
tedted
14.8k10 gold badges68 silver badges113 bronze badges
1 Answer
Reset to default 2Define some classes (pay attention to the green classes):
const styles = theme => ({
container: {
display: 'flex',
flexWrap: 'wrap',
},
formControl: {
margin: theme.spacing.unit,
},
greenLabel: {
color: '#0f0',
},
greenUnderline: {
'&:hover:not($disabled):before': {
backgroundColor: '#040',
},
},
greenInkbar: {
'&:after': {
backgroundColor: '#0f0',
},
},
});
Add them as a classes
prop using the withStyles HoC:
export default withStyles(styles)(ComposedTextField);
Override the classes used in the ponents with the classnames in the classes
prop provided by withStyles:
render() {
const { classes } = this.props;
return (
<div className={classes.container}>
<FormControl className={classes.formControl}>
<InputLabel FormControlClasses={{ focused: classes.greenLabel }} htmlFor="name-simple">
Name
</InputLabel>
<Input
classes={{ inkbar: classes.greenInkbar, underline: classes.greenUnderline }}
id="name-simple"
value={this.state.name}
onChange={this.handleChange}
/>
</FormControl>
</div>
);
Input uses the theme's primary color in its inkbar and underline classes, so we override them with the greenInkbar and greenUnderline classes we've defined.
For InputLabel, which is a wrapper for FormLabel, we have to pass classes through via the FormControlClasses prop.
Take a look at this codesandbox for a working reproduction.
本文标签: javascriptMaterial UI change Input39s active colorStack Overflow
版权声明:本文标题:javascript - Material UI change Input's active color - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745425934a2658112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论