简体   繁体   English

Reactjs material-ui TextField 更改颜色标签和下划线活动字段输入

[英]Reactjs material-ui TextField change color label and underline activity field input

在此处输入图片说明

<TextField
    id="standard-full-width"
    label="Password"
    style={{ margin: 8 }}
    fullWidth
    margin="normal"
    placeholder="*******"
/>

I'm not able to figure out how to change the color of the label and the underline when the focus is activated on the input field.当焦点在输入字段上激活时,我无法弄清楚如何更改标签和下划线的颜色。

Some advice?一些忠告?

You can overrule style by supplying them via the classes property.您可以通过classes属性提供它们来否决样式。 I've added an example using the makeStyles hook but the property can also be used with classes supplied from the withStyles HOC .我添加了一个使用makeStyles钩子的示例,但该属性也可以与withStyles HOC提供的类一起使用。

import React from "react";
import { TextField } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
  root: {
    "& label.Mui-focused": {
      color: "orange"
    },
    "& .MuiInput-underline:after": {
      borderBottomColor: "orange"
    }
  }
}));

function App() {
  const classes = useStyles();
  return <TextField label="My label" classes={classes} />;
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

So, in case you're using a Component it will be like this:因此,如果您使用的是Component ,它将是这样的:

import React from "react";
import { TextField } from "@material-ui/core";
import { withStyles } from "@material-ui/core/styles";

const styles = theme => ({
    root: {
        "& label.Mui-focused": {
          color: "orange"
        },
        "& .MuiInput-underline:after": {
          borderBottomColor: "orange"
        }
    }
})

class App extends React.Component {
    render() {
        return (
            <TextField label="My label" classes={this.props.classes} />
        )
    }
}

export default withStyles(styles)(App)

To read up on customizing the TextField component check out these examples: https://material-ui.com/components/text-fields/#customized-inputs要了解自定义 TextField 组件,请查看以下示例: https : //material-ui.com/components/text-fields/#customized-inputs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM