【问题标题】:Material UI Autocomplete clearOnBlur clears when false材质 UI 自动完成 clearOnBlur 为 false 时清除
【发布时间】:2021-11-10 07:15:48
【问题描述】:

在最近的一个项目中,我正在使用 Material UI 的自动完成功能。 那里我不想清除输入字段,所以我使用clearOnBlur 属性设置为false

即使这样,自动完成程序也会在焦点丢失后清除输入字段。

不胜感激!

这是一个例子:

https://codesandbox.io/s/blue-moon-2bk87?file=/src/ComboBox.js

【问题讨论】:

标签: javascript reactjs autocomplete material-ui


【解决方案1】:

尝试使用最新版本的材质 UI,使用最新版本总是好的。

使用最新版本的 MUI 尝试 this 沙盒。

这是改编自 here 的文档演示沙箱。

【讨论】:

    【解决方案2】:

    您使用的material-ui 5.0.0-beta 似乎有问题。在4.12.3 中它工作正常。请查看此codesandbox

    const ComboBox = function () {
      const [value, setValue] = useState("");
      const Combo = useRef();
    
      const onBlur = (e) => {
        Combo.current.inputValue = value;
      };
    
      const filterOptions = (options, state) => {
        return options;
      };
    
      return (
        <Autocomplete
          ref={Combo}
          onChange={(e) => {
            setValue(e.target.value);
          }}
          onSelect={(e) => {
            setValue(e.target.value);
          }}
          filterOptions={filterOptions}
          id="combo-box-demo"
          options={top100Films}
          getOptionLabel={(option) => option.title}
          style={{ width: 300 }}
          onBlur={onBlur}
          clearOnBlur={false}
          inputValue={value}
          renderInput={(params) => (
            <TextField {...params} label="Combo box" variant="outlined" />
          )}
        />
      );
    };
    

    我尝试手动设置输入值,但由于material-ui版本,它不起作用。

    【讨论】:

    • 谢谢!这是绝对正确的。在最新版本的 Material UI 中,该选项不起作用。不过幸运的是,已经有一个针对这个特定问题的 PR,希望很快就会出来。 github.com/mui-org/material-ui/pull/28190
    猜你喜欢
    • 2021-05-11
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2021-12-18
    • 2021-04-27
    • 2021-05-19
    • 2020-03-19
    • 2020-04-11
    相关资源
    最近更新 更多