6

Currently, I have a styled TextField. When I start to type in the email field, autofill choices appear. If I select one of the autofill choices, the background of the TextField turns white with black text. I want to change these styles.

I've tried this:

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

const styles = {
  underline: {
    "&::before": {
      borderBottom: "1px solid #90caf9"
    },
    "&:hover:not(.Mui-disabled):before": {
      borderBottom: "2px solid #90caf9"
    },
    "&::after": {
      borderBottom: "2px solid #90caf9"
    }
  },
  input: {
    "&:-webkit-autofill": {
      WebkitBoxShadow: "0 0 0 1000px black inset"
    }
  }
};

const DarkTextField = withStyles(styles)(props => {
  const { classes, ...other } = props;

  return <TextField InputProps={{ className: classes.underline }} {...other} />;
});

export default DarkTextField;

Changed DarkTextField component to the following in light of comments:

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

const styles = {
  underline: {
    "&::before": {
      borderBottom: "1px solid #90caf9"
    },
    "&:hover:not(.Mui-disabled):before": {
      borderBottom: "2px solid #90caf9"
    },
    "&::after": {
      borderBottom: "2px solid #90caf9"
    }
  },
  input: {
    "&:-webkit-autofill": {
      WebkitBoxShadow: "0 0 0 1000px black inset"
    }
  }

};

const DarkTextField = withStyles(styles)(props => {
  const { classes, ...other } = props;

  return <TextField InputProps={ classNames("classes.underline", "classes.input") } {...other} />;
});

export default DarkTextField;

The above made no change.

  1. Are either of the above approaches correct (other than the missing className in InputProps)?
  2. How do I use more than one className in the InputProps?
yodude
  • 159
  • 3
  • 5
  • 13
  • Related answer: https://stackoverflow.com/questions/54706919/material-ui-remove-the-yellow-background-on-textfield-autofill/54714640#54714640 – Ryan Cogswell Jul 26 '19 at 19:51
  • If https://stackoverflow.com/questions/46066675/how-to-add-multiple-classes-in-material-ui-using-the-classes-props helps for multiple classNames – Garry Jul 26 '19 at 19:53
  • Thank you for your replies! I placed the code for my DarkTextField above. What did I do wrong? – yodude Jul 26 '19 at 20:17
  • Ok, I tried a different syntax for the const DarkTextField, which seems to be doing something. Have TS errors I have to figure out. – yodude Jul 26 '19 at 20:45

1 Answers1

2

For the DarkTextField syntax there are a couple options:

This first syntax will pass all the classes along via the separate keys (underline and input) of the classes prop of Input.

const DarkTextField = withStyles(styles)(props => {
  const { classes } = props;
  return <TextField InputProps={ {classes: classes}} />;
});

This second syntax will combine the class names (classNames provides an easy way to get the comprehensive space-delimited string of class names) to use at the root level of the Input via the className prop.

const DarkTextField = withStyles(styles)(props => {
  const { classes } = props;
  return <TextField InputProps={ {className: classNames(classes.underline, classes.input)}} />;
});
Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • Ok Ryan, that's awesome. I was trying to use a different approach I found in the other example that you linked. This also got rid of the TS error I had, thank god. What is the "...other" spread referring to? – yodude Jul 26 '19 at 21:28
  • 1
    I just copied that from your code. It just means any additional props you pass to DarkTextField will pass through to `InputProps`. – Ryan Cogswell Jul 26 '19 at 21:30
  • I also need to change the autofill text color. I saw this somewhere yesterday....searching... – yodude Jul 26 '19 at 21:33
  • Ok, I fixed it! caretColor: "#fff" – yodude Jul 26 '19 at 21:37
  • Everything is working except for a font size difference. I tried: fontSize & WebkitTextFontSize – yodude Jul 26 '19 at 21:48
  • 1
    The `underline` styles don’t make sense if you’re using an outlined input. Just remove it. – Ryan Cogswell Jul 27 '19 at 21:18
  • Thanks, yeah I put everything under input. Any idea on the how to change the font size that shows when mousing over the autofill options? – yodude Jul 28 '19 at 23:04
  • 1
    Please provide a [CodeSandbox](https://codesandbox.io/s/new) of what you have so far. It may be best to do this in a new question asking specifically about the font size with the code sandbox, the code you have, and the steps to reproduce how to see the text you want to change the size of. – Ryan Cogswell Jul 29 '19 at 15:09
  • Ok, thank you. I will circle back on this. Not sure if a sandbox will work...I'll have to try it. – yodude Jul 29 '19 at 22:14
  • 1
    You can use the [sandbox from my related answer](https://codesandbox.io/s/rr9omj7q0p) as a starting point. – Ryan Cogswell Jul 29 '19 at 22:30
  • Thank you Ryan! I will get to this, I want to fix the font size. I have another issue on this app that I need to fix first though. Cheers. – yodude Jul 31 '19 at 19:04
  • Hi Ryan, I looked at your sandbox and setup a new question: https://stackoverflow.com/questions/57437732/webkit-autofill-text-size – yodude Aug 09 '19 at 22:20