Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to style TextFields input color? #9574

Closed
quantuminformation opened this issue Dec 20, 2017 · 14 comments
Closed

How to style TextFields input color? #9574

quantuminformation opened this issue Dec 20, 2017 · 14 comments
Labels
component: text field This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement

Comments

@quantuminformation
Copy link
Contributor

quantuminformation commented Dec 20, 2017

There is no guidance on how to change the text colour of these inputs https://material-ui.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

@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Dec 20, 2017
@quantuminformation

This comment has been minimized.

@oliviertassinari
Copy link
Member

oliviertassinari commented Dec 20, 2017

@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).

@oliviertassinari oliviertassinari added the component: text field This is the name of the generic UI component, not the React module! label Dec 20, 2017
@pankajparkar
Copy link

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

@oliviertassinari
Copy link
Member

oliviertassinari commented Dec 6, 2018

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

@oliviertassinari oliviertassinari changed the title [documentation] How to style TextFields input colour How to style TextFields input color? Dec 6, 2018
@pankajparkar
Copy link

pankajparkar commented Dec 7, 2018

@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

@vaydi

This comment has been minimized.

@quantuminformation

This comment has been minimized.

@AjithkumarJM
Copy link

@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!

@YohanObadia
Copy link

YohanObadia commented Feb 9, 2021

@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).

Thanks for the solution but how would you do it if color is actually a parameter you want to pass to the CustomizedInput and not a static value as is the case in your example ?

In my case I get an error : Invalid prop 'className' of type 'object' supplied to 'ForwardRef(InputBase)', expected 'string'

The section of my code that is of interest. It is a React.Component btw.

render () {
        let {error, editValue} = this.state;
        const isError = error!==""
        const classes = {inputStyle:{color:this.props.color}}
        return (
            <TextField
            error={isError}
            id={'edit-'+this.props.fieldId}
            value={editValue}
            placeholder=""
            helperText={isError?error:""}
            size="small"
            onChange={this.onInputchange}
            onBlur={this.handleBlur}
            InputProps={{
                className: classes.inputStyle
            }}
            multiline
            />
          );
    };

@quantuminformation

This comment has been minimized.

@Ridoineel
Copy link

no inputProps, this's InputProps,

capitale "I"

zakrzewskib added a commit to zakrzewskib/seller-dashboard that referenced this issue Dec 7, 2021
-MUI brings a lot of problems...

mui/material-ui#9574
@Duoquote
Copy link

Would love to see something like that to dynamically change the color.

image

@squirrl
Copy link

squirrl commented Jun 2, 2022

Sorry to resurrect a dead thread, but in case someone comes across this with the same question:

Would love to see something like that to dynamically change the color.

image

<TextField> generates a three layer tree, with a grandparent <div> (MuiFormControl), parent <div> (MuiInputBase), and child <input> (MuiInput).

Other ways to do it are probably better, but if you have some specific need for inline styling you can pass props to the grandchild via the InputBase/inputBase props (note the capitalizations of 'I' and 'i'):

<TextField InputProps={{ inputProps: { style: { color: '#fff' }}}} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

10 participants