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

Jam*_*bla 7 styles input textfield reactjs material-ui

我有一个 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}
    />
  );
};
Run Code Online (Sandbox Code Playgroud)

输出:

文本字段的输出

luc*_*gas 27

您可以尝试通过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" } }}
/>
Run Code Online (Sandbox Code Playgroud)

我将留下一个沙箱链接,我在其中测试了该解决方案。


chr*_*awn 7

需要注意的是,仅更改样式不会更改发送到 onChange 的实际状态值。这将仅显示大写字母,但保存的数据仍将是原始小写字母等。