如何将 MaterialUI TextField 的值设置为大写?

How can I set the value of my MaterialUI TextField to uppercase?

我有一个 Material UI TextField 作为输入,我需要强制输入的文本为大写。我曾尝试使用 textTransform: "uppercase" 作为样式属性的一部分,但这似乎不起作用。我组件中的所有其他样式都已正确应用,但 textTransform 不是。

我也尝试过使用标准样式方法将我的样式作为道具传递给组件,但我得到了相同的结果。

我的组件:

  const MenuInput = (props) => {
  const useStyles = makeStyles((theme) => ({
    input: {
      textTransform: "uppercase",
      marginTop: "10px",
      width: "100%",
      borderRadius: 4,
      backgroundColor: "#FFFFFF",
    },
  }));

  const classes = useStyles();
  return (
    <TextField
      className={classes.input}
      id={props.id}
      color="primary"
      label={props.label}
      variant="filled"
      onChange={(e) => props.onChange(e)}
      error={props.isError}
      helperText={props.error}
    />
  );
};

输出:

尝试添加重要的 textTransform:“大写!重要”

或者添加内联样式

您可以尝试通过 inputProps 应用样式,如下所示:

 <TextField
      className={classes.input}
      id={props.id}
      color="primary"
      label={props.label}
      variant="filled"
      onChange={(e) => props.onChange(e)}
      error={props.isError}
      helperText={props.error}
      inputProps={{ style: { textTransform: "uppercase" } }}
/>

我会留下一个 link 和一个我测试该解决方案的沙箱。