简体   繁体   English

如何将 material-ui TextField Input 的 inputProps 传递给 styled-components 元素?

[英]How to pass inputProps for material-ui TextField Input into styled-components element?

I have material-ui TextField element now and styles for it:我现在有 material-ui TextField 元素和 styles :

import TextField from "@mui/material/TextField";

const inputStyle = {
  fontSize: 24,
  pl: 2,
  pt: 0.5,
  pb: 0.5,
};

<TextField
  variant="outlined"
  inputProps={{
   sx: inputStyle,
  }}
/>

Now I want to make a component with styled-components:现在我想用样式组件制作一个组件:

import { styled } from "@mui/material/styles";
import TextField from "@mui/material/TextField";

export const CustomizedTextField = styled(TextField)`
  font-size: 24px;
  padding-left: 16px;
  padding-top: 8px;
  padding-bottom: 8px;
`;

<CustomizedTextField
  variant="outlined"
/>

But it doesn't work.但它不起作用。 I'm sure I'm not passing inputProps for the TextField correctly, but I can't figure out how to do it right.我确定我没有正确传递 TextField 的 inputProps,但我不知道如何正确地做到这一点。

If you or anyone else still run into this: I'm doing something similar but am just using the 'style' input prop.如果您或其他任何人仍然遇到这种情况:我正在做类似的事情,但只是使用“样式”输入道具。 I've also used the 'className' prop with success.我也成功地使用了“className”道具。

In your case, I believe you could just do something like:就您而言,我相信您可以执行以下操作:

<TextField
  variant="outlined"
  InputProps={{
    style: inputStyle,
  }}
/>

Also not sure if it matters but there seems to be some difference between the capitalized/uncapitalized versions of inputProps/InputProps.也不确定它是否重要,但 inputProps/InputProps 的大写/非大写版本之间似乎存在一些差异。 I've always used the capitalized version if that makes a difference.如果有区别,我总是使用大写的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM