Material ui Autocomplete 按回车键创建新芯片

新手上路,请多包涵

我希望我可以使用材料 ui 的自动完成来做这样的事情: wertarbyte

也就是说,在没有必须从中选择的元素列表的情况下插入文本(字符串)。

因此,不应出现 noOptions 消息,每次在键盘上按下回车键时都会插入文本。

在此处输入图像描述

链接: codesandbox

代码:

 import React from "react";
import Chip from "@material-ui/core/Chip";
import Autocomplete from "@material-ui/lab/Autocomplete";
import { makeStyles } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";

const useStyles = makeStyles(theme => ({
  root: {
    width: 500,
    "& > * + *": {
      marginTop: theme.spacing(3)
    }
  }
}));

export default function Tags() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Autocomplete
        multiple
        id="tags-outlined"
        options={[]}
        defaultValue={["foo", "bar"]}
        //getOptionLabel={(option) => option}
        //defaultValue={[top100Films[13]]}
        //filterSelectedOptions
        renderInput={params => (
          <TextField
            {...params}
            variant="outlined"
            label="filterSelectedOptions"
            placeholder="Favorites"
          />
        )}
      />
    </div>
  );
}

原文由 Paul 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 694
2 个回答

如果你有简单的元素(不是对象,只是字符串),并且你真的不需要处理状态(你的自动完成不受控制)你可以使用 freeSoloAutocomplete

 <Autocomplete
    multiple
    freeSolo
    id="tags-outlined"
    options={["foo", "bar"]}
    defaultValue={["foo", "bar"]}
    renderInput={params => (
        <TextField
            {...params}
            variant="outlined"
            label="filterSelectedOptions"
            placeholder="Favorites"
        />
    )}
/>

如果您的元素更复杂并且您确实需要控制该元素:

  1. 确保 Autocomplete 标签是受控的(你设法估价)。

  2. 监听 TextField 上的按键事件。

  3. 如果代码是 Enter ( e.code === 'Enter' ) - 获取输入值并将其推送到您拥有的当前值列表。

  4. 确保您还处理了 onChange 来处理元素的删除:

这是代码:

 const [autoCompleteValue, setAutoCompleteValue] = useState(["foo", "bar"]);

return (

  <Autocomplete
    multiple
    id="tags-outlined"
    options={[]}
    value={autoCompleteValue}
    onChange={(e, newval, reason) => {
      setAutoCompleteValue(newval);
    }}
    renderInput={params => (
      <TextField
        {...params}
        variant="outlined"
        label="filterSelectedOptions"
        placeholder="Favorites"
        onKeyDown={e => {
          if (e.code === 'enter' && e.target.value) {
            setAutoCompleteValue(autoCompleteValue.concat(e.target.value));
          }
        }}
      />
    )}
  />
);

检查两个选项的实时工作示例: https ://codesandbox.io/s/mui-autocomplete-create-options-on-enter-gw1jc

原文由 Dekel 发布,翻译遵循 CC BY-SA 4.0 许可协议

对于任何想在回车键上输入 当前最佳匹配 的人(而不是任何自定义文本),您可以使用 autoHighlight 道具。

 <Autocomplete
    multiple
    autoHighlight
    id="tags-outlined"
    options={["foo", "bar"]}
    defaultValue={["foo", "bar"]}
    renderInput={params => (
        <TextField
            {...params}
            variant="outlined"
            label="filterSelectedOptions"
            placeholder="Favorites"
        />
    )}
/>

原文由 wihlke 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题