Material-ui: How to style TextFields input color?

Created on 20 Dec 2017  路  9Comments  路  Source: mui-org/material-ui

There is no guidance on how to change the text colour of these inputs https://material-ui-next.com/demos/text-fields/

Do we just use external css to do this?

I also tried this but it doesn't pass TypeScript checks:

 <TextField
          inputProps={{ style: { fontFamily: 'Arial', color: 'white'}}}
          style={{ flex: 1, margin: '0 20px 0 0', color: 'white'}}
          onChange={(e: ChangeEvent<HTMLInputElement>) => changeSearch(e.target.value)}
          type="text"
          value={reducerState.search}
        />

(50,11): error TS2339: Property 'inputProps' does not exist on type 'IntrinsicAttributes & TextFieldProps & { children?: ReactNode; }'.

image

This might be purely a TypeScript types issue

TextField question

Most helpful comment

@QuantumInformation I'm glad you found a solution. I would encourage people to do it this way:

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

const styles = {
  root: {
    background: "black"
  },
  input: {
    color: "white"
  }
};

function CustomizedInputs(props) {
  const { classes } = props;

  return (
    <TextField
      defaultValue="color"
      className={classes.root}
      InputProps={{
        className: classes.input
      }}
    />
  );
}

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

export default withStyles(styles)(CustomizedInputs);

https://codesandbox.io/s/1370v7y4zj

capture d ecran 2018-12-06 a 19 31 05

Notice that you can also target the global class names (.MuiInputBase-root and .MuiInputBase-input).

All 9 comments

I just done it with external css

.something input {
  color: white;
}

@QuantumInformation I'm glad you found a solution. I would encourage people to do it this way:

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

const styles = {
  root: {
    background: "black"
  },
  input: {
    color: "white"
  }
};

function CustomizedInputs(props) {
  const { classes } = props;

  return (
    <TextField
      defaultValue="color"
      className={classes.root}
      InputProps={{
        className: classes.input
      }}
    />
  );
}

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

export default withStyles(styles)(CustomizedInputs);

https://codesandbox.io/s/1370v7y4zj

capture d ecran 2018-12-06 a 19 31 05

Notice that you can also target the global class names (.MuiInputBase-root and .MuiInputBase-input).

Does not work in "@material-ui/core"

Any updates, how to make it working with @material-ui/core? I'm badly stuck at this

@pankajparkar I have updated my previous answer to work with the latest version: https://github.com/mui-org/material-ui/issues/9574#issuecomment-353179532. I hope that help, let me know! Alternatively, we have a customization demo in https://material-ui.com/demos/text-fields/#customized-inputs.

@oliviertassinari thanks alot. I had to remove label to make it working.
Do you have any solution / workaround to make it working with label. I understand why label gets overlay (I kind of partially sorted it by applying z-index, but that led to different problems).

Problem codepen here

thank you very much

Dang, never realised how unpopular my initial answer was

@oliviertassinari thanks alot. I had to remove label to make it working.
Do you have any solution / workaround to make it working with label. I understand why label gets overlay (I kind of partially sorted it by applying z-index, but that led to different problems).

Problem codepen here

I also need exactly the same!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

ryanflorence picture ryanflorence  路  3Comments

ericraffin picture ericraffin  路  3Comments

pola88 picture pola88  路  3Comments

finaiized picture finaiized  路  3Comments