材质UI删除TextField自动填充上的黄色背景

时间:2019-02-15 10:06:40

标签: reactjs material-ui

我真的很难从Material UI TextField组件中删除自动填充上的黄色背景。

在旧版本中,我是这样进行的:

const inputStyle = { WebkitBoxShadow: '0 0 0 1000px white inset' };
<TextField
    ...
    inputStyle={inputStyle}
/>

但是在最新版本中,inputStyle道具被删除并改为添加了InputProps

我试图用这种方法将其删除,但是黄色背景颜色仍然出现: enter image description here

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: {
    ':-webkit-autofill': {
        WebkitBoxShadow: '0 0 0 1000px white inset',
        backgroundColor: 'red !important'
    }
  },
  input: {
    ':-webkit-autofill': {
        WebkitBoxShadow: '0 0 0 1000px white inset',
        backgroundColor: 'red !important'
    }
  }
};

const renderTextField = (props) => {
    const {
        classes,
        label,
        input,
        meta: { touched, error },
        ...custom
    } = props;

  return (
    <TextField
        label={label}
        placeholder={label}
        error={touched && error}
        helperText={touched && error}
        className={classes.root}
        InputProps={{
            className: classes.input
        }}
        {...input}
        {...custom}
    />
  );
}

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

export default withStyles(styles)(renderTextField);

2 个答案:

答案 0 :(得分:3)

inputStyle替换为inputProps

const inputStyle = { WebkitBoxShadow: "0 0 0 1000px white inset" };
<TextField name="last_name" inputProps={{ style: inputStyle }} />

InputPropsinputProps的比较是一个常见的混淆点。大写字母“ I” InputPropsTextField中的Input元素提供了道具(Input将本地input包装在div中)。小写的“ i” inputProps为在input组件中呈现的本机Input元素提供了支持。如果您想为原生input元素提供内联样式,则上面的代码示例将解决问题。

还有其他几种通过withStyles使用类来执行此操作的方法。

如果要使用className属性,则必须再次在input上(而不是将其包裹的div上),才能获得所需的效果。因此,以下内容也将起作用:

const styles = {
  input: {
    WebkitBoxShadow: "0 0 0 1000px white inset"
  }
};
const MyTextField = ({classes}) => {
   return <TextField name="email" inputProps={{ className: classes.input }} />;
}
export default withStyles(styles)(MyTextField);

如果您想利用“:-webkit-autofill”伪类,只需调整JSS语法并添加"&" to reference the selector of the parent rule

const styles = {
  input: {
    "&:-webkit-autofill": {
      WebkitBoxShadow: "0 0 0 1000px white inset"
    }
  }
};
const MyTextField = ({classes}) => {
   return <TextField name="email" inputProps={{ className: classes.input }} />;
}
export default withStyles(styles)(MyTextField);

您还可以利用以下两种方法之一,但可以通过classes属性使用大写的“ I” InputProps

const styles = {
  input: {
    WebkitBoxShadow: "0 0 0 1000px white inset"
  }
};
const MyTextField = ({classes}) => {
   return <TextField name="email" InputProps={{ classes: { input: classes.input } }} />;
}
export default withStyles(styles)(MyTextField);

这是所有这些方法的有效示例:

Edit rr9omj7q0p

答案 1 :(得分:0)

您可以将其添加到覆盖的主题中。

overrides: {
  MuiOutlinedInput: {
    input: {
      '&:-webkit-autofill': {
        '-webkit-box-shadow': '0 0 0 100px #000 inset',
        '-webkit-text-fill-color': '#fff'
      }
    }
  }
}
相关问题