简体   繁体   English

如何在 React Material UI 自动完成表单中禁用 ENTER 键

[英]How to Disable the ENTER Key in React Material UI Autocomplete form

I have a Material UI Autocomplete form in a React component.我在 React 组件中有一个 Material UI 自动完成表单。 It works perfect, except the ENTER key is currently clearing the input field.它工作完美,除了 ENTER 键当前正在清除输入字段。 I simply want the input field to not be cleared when the user hits ENTER key .我只是希望在用户按下 ENTER 键时清除输入字段 I searched through all the similar questions on Stackoverflow, and none of them deal with ignoring this key inside of an Autocomplete form (they mostly deal with regular input forms).我在 Stackoverflow 上搜索了所有类似的问题,但没有一个涉及在自动完成表单中忽略此键(它们主要处理常规输入表单)。 Below I list all the things I have tried that don't work.下面我列出了所有我尝试过但不起作用的方法。

How can I disable the ENTER key in this situation??在这种情况下如何禁用 ENTER 键?

I have tried ignoring the enter key such as:我试过忽略回车键,例如:

onKeyPress={(event) => {return event.key !== 'Enter';}}

I have also tried stopping the autocomplete enter key from being taken as a form submit (hoping it wouldn't clear the form) by doing this:我还尝试通过以下方式阻止自动完成输入键被视为表单提交(希望它不会清除表单):

onKeyPress={(event) => {
    if (event.key === 'Enter') {
      event.preventDefault();
      return false;
    }
}

I even tried:我什至试过:

onKeyPress={(event) => {
    if (event.key === 'Enter') {
      event.stopPropagation();
      return false;
    }
}

And, yes, I also tried using onKeyDown instead onKeyPress for both the above examples.而且,是的,我还尝试在上述两个示例中使用onKeyDown而不是onKeyPress

Finally, I tried using the disableClearable option in the Autocomplete component as below:最后,我尝试在自动完成组件中使用disableClearable选项,如下所示:

  const onInputChange = useCallback(
    (_event: ChangeEvent<{}>, newInputValue: string) => {
      debounceFetchData(newInputValue);
    },
    [debounceFetchData]
  );

  return (
    <section className={classes.container}>
      <SearchIcon className={classes.icon} />
      <Autocomplete
        id="search"
        options={options}
        disableClearable
        getOptionLabel={() => ''}
        filterOptions={(x) => x}
        fullWidth
        freeSolo
        onInputChange={onInputChange}
        renderInput={(params) => (
          <TextField
            {...params}
            placeholder="Search"
            variant="outlined"
            size="small"
            inputProps={{
              ...params.inputProps,
              'aria-label': 'Search',
            }}
          />
        )}
        ...
        ...
        ...
      />

This works, but it removes the ability to use the keyboard to select a value.这有效,但它删除了使用键盘输入 select 值的能力。

Note that the ellipses after {...params} should be omitted.请注意,应省略{...params}之后的省略号。

I had stuck on this for some time as well and found the answer on here .我也坚持了一段时间,并在这里找到了答案。

Simply it can be handled by passing onKeyDown handler to inputProps of <TextField> :只需将onKeyDown处理程序传递给<TextField>inputProps即可处理:

 <Autocomplete
        ...
        renderInput={(params) => (
          <TextField
            {...params}
            ...
            inputProps={{
              ...params.inputProps,
              onKeyDown: (e) => {
                    if (e.key === 'Enter') {
                      e.stopPropagation();
                    }
              },
            }}
          />
        )}
        ...
        ...
        ...
      />

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

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