简体   繁体   English

MaterialUI 文本字段/输入在 iPhone 上不起作用

[英]MaterialUI Textfield/Input not working on IPhone

I just discovered that all apps I've been working on using MaterialUI, iPhone users are unable to type in either Textfield or Input, with value and setValue properly set.我刚刚发现我一直在使用 MaterialUI 开发的所有应用程序,iPhone 用户无法在正确设置 value 和 setValue 的情况下输入 Textfield 或 Input。

I was able to fix this individually by adding我可以通过添加来单独解决这个问题

      <TextField
        name="source-text"
        multiline
        id="source"
        minRows={3}
        fullWidth
        value={sourceText}
        variant="standard" // <== to enable us disable border
        onChange={(e) => handleSourceTextChange(e.target.value)}
        sx={{
          fontSize: 122,
          fontWeight: 500,
          color: "#474747",
        }}
        inputProps={{
          style: {
            fontSize: 22,
            "-webkit-user-select": "text" /* Chrome, Opera, Safari */,
            "-moz-user-select": "text" /* Firefox 2+ */,
            "-ms-user-select": "text" /* IE 10+ */,
            "user-select": "text" /* Standard syntax */,
          },
        }} // font size of input text
        InputProps={{
          style: {
            fontSize: 22,
            "-webkit-user-select": "text" /* Chrome, Opera, Safari */,
            "-moz-user-select": "text" /* Firefox 2+ */,
            "-ms-user-select": "text" /* IE 10+ */,
            "user-select": "text" /* Standard syntax */,
          }, // font size of input label
          disableUnderline: true, // <== to hide underline in standard TextField variant
        }}
      />

and the handler和处理程序

  const handleSourceTextChange = (value) =>  setSourceText(value);

I'd like to know if there's a way to set this style from the MUI createTheme, so I won't have to keep repeating my code in each Textfield我想知道是否有办法从 MUI createTheme 设置这种样式,所以我不必在每个文本字段中重复我的代码

I've tried adding this to the root theme我已经尝试将它添加到根主题

    MuiTextField: {
      styleOverrides: {
        root: {
          "-webkit-user-select": "text !important" /* Chrome, Opera, Safari */,
          "-moz-user-select": "text !important" /* Firefox 2+ */,
          "-ms-user-select": "text !important" /* IE 10+ */,
          "user-select": "text !important" /* Standard syntax */,
          // border: "3px solid red !important",

          "& input:valid:focus + fieldset": {
            "-webkit-user-select": "text !important" /* Chrome, Opera, Safari */,
            "-moz-user-select": "text !important" /* Firefox 2+ */,
            "-ms-user-select": "text !important" /* IE 10+ */,
            "user-select": "text !important" /* Standard syntax */,
            // borderLeftWidth: 6,
            // padding: "4px !important", // override inline-style
          },
        },
      },
    },

Create a style override for all input components in MaterialUI that you'll be using, like this:为您将使用的 MaterialUI 中的所有输入组件创建样式覆盖,如下所示:

import { createTheme } from "@mui/material/styles";

const iPhoneInput = {
  styleOverrides: {
    root: {
      "*": {
        "-webkit-user-select": "text !important" /* Chrome, Opera, Safari */,
        "-moz-user-select": "text !important" /* Firefox 2+ */,
        "-ms-user-select": "text !important" /* IE 10+ */,
        "user-select": "text !important" /* Standard syntax */,
      },
    },
  },
};

const muiTheme = createTheme({
  components: {
    MuiTextField: iPhoneInput,
    MuiInput: iPhoneInput,
    MuiFilledInput: iPhoneInput,
    MuiOutlinedInput: iPhoneInput,
  },
});

export default muiTheme;

In your app entry, wrap your components with <ThemeProvider theme={theme}> and pass the theme object to it.在您的应用程序条目中,使用<ThemeProvider theme={theme}>包装您的组件并将主题 object 传递给它。

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

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