Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the label color of Material-UI <TextField/>

How can I change the color from "email" label and make it the same as the border line?

Example here

Here's my code:

import React, { Component } from "react";
import { Icon } from "semantic-ui-react";
import { Divider } from "semantic-ui-react";
import { TextField, Button, Grid } from "@material-ui/core";
import PropTypes from "prop-types";
import classNames from "classnames";
import { withStyles } from "@material-ui/core/styles";

let self;

const styles = theme => ({
  container: {
    display: "flex",
    flexWrap: "wrap"
  },
  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit,
    width: 280
  },
  cssLabel: {
    color: "#d3d3d3"
  },
  cssOutlinedInput: {
    "&:not(hover):not($disabled):not($cssFocused):not($error) $notchedOutline": {
      borderColor: "#d3d3d3" //default
    },
    "&:hover:not($disabled):not($cssFocused):not($error) $notchedOutline": {
      borderColor: "#d3d3d3" //hovered #DCDCDC
    },
    "&$cssFocused $notchedOutline": {
      borderColor: "#23A5EB" //focused
    }
  },
  cssInputLabel: {
    color: "#d3d3d3"
  },
  notchedOutline: {},
  cssFocused: {},
  error: {},
  disabled: {}
});
class NewLoginComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loggedIn: false,
      user: "",
      errorMsg: "",
      errorMsgLength: "",
      loginErrorMsg: "",
      flag: false,
      password: "",
      hidden: true,
      valuePassText: "SHOW"
    };
    self = this;
  }

  componentDidMount() {
    this._isMounted = true;
    if (this.props.password) {
      this.setState({ password: this.props.password });
    }
  }

  componentDidUpdate(prevProps) {}

  render() {
    const { classes } = this.props;
    let username = "";
    let password = "";
    return (
      <div className="container-fluid backround">
        <div className="container" id="loginform">
          <div className="form-group">
            <div>
              <div className="emailInput">
                <TextField
                  className={classes.textField}
                  id="email-txt-input"
                  label="Email"
                  variant="outlined"
                  inputRef={node => (username = node)}
                  InputLabelProps={{
                    classes: {
                      root: classes.cssLabel,
                      focused: classes.cssFocused
                    }
                  }}
                  InputProps={{
                    classes: {
                      root: classes.cssOutlinedInput,
                      focused: classes.cssFocused,
                      notchedOutline: classes.notchedOutline
                    },
                    inputMode: "numeric"
                  }}
                />
              </div>
              <div className="passwordInput">
                <TextField
                  className={classes.textField}
                  id="password-txt-input"
                  label="Password"
                  variant="outlined"
                  inputRef={node => (password = node)}
                  type={this.state.hidden ? "password" : "text"}
                  value={this.state.password}
                  onChange={this.handlePasswordChange}
                  InputLabelProps={{
                    classes: {
                      root: classes.cssLabel,
                      focused: classes.cssFocused
                    }
                  }}
                  InputProps={{
                    classes: {
                      root: classes.cssOutlinedInput,
                      focused: classes.cssFocused,
                      notchedOutline: classes.notchedOutline
                    },
                    inputMode: "numeric"
                  }}
                />
              </div>
            </div>
            <div className="form-group form">
              <Button
                variant="contained"
                id="normal-signin-Btn"
                type={"submit"}
                className={"btn btn-primary loginButton"}
              >
                LogIn
              </Button>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

NewLoginComponent.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(NewLoginComponent);

Edit thirsty-dust-u4vuz

like image 255
Petros Karkanis Avatar asked Dec 23 '22 18:12

Petros Karkanis


2 Answers

Below is one way to control the focused color of the input label to be the same as the focused color of the border:

  cssLabel: {
    color: "#d3d3d3",
    "&.Mui-focused": {
      color: "#23A5EB"
    }
  },

Edit romantic-kapitsa-z33pg

like image 170
Ryan Cogswell Avatar answered Jan 05 '23 19:01

Ryan Cogswell


You can simply use the InputLabelProps in your TextField component , for exemple :

<TextField InputLabelProps={{style : {color : 'green'} }}></TextField>
like image 34
user12179366 Avatar answered Jan 05 '23 17:01

user12179366