多个自动完成材料用户界面filterSelectedOptions首次搜索无效

时间:2019-12-11 17:24:15

标签: javascript reactjs autocomplete material-ui

即使我在utocomplete中使用filterSelectedOptions道具,也只有在使用了预先定义的芯片时才起作用:image1

,但是当是一个全新的类型化选项时,它会重复多次: image2

我正在尝试使用filterOptions,但是它不起作用,我也不清楚如何做到这一点:( 这是我当前的代码:

 const handleAutocomplete = (e, valueTags) => {
    e.preventDefault();
    const tagIdsArray = [];
    valueTags.forEach((valueTag) => {
      tagIdsArray.push(Number(tags.filter(tag => valueTag.title === tag.title).shift().id));
    });
    const tagIds = [...new Set(tagIdsArray)];
    handleTag(tagIds);
  };
  return (
    <Autocomplete
      multiple
      options={tags}
      getOptionLabel={option => option.title}
      defaultValue={tagDafaultIds}
      filterSelectedOptions
      onChange={handleAutocomplete}
      // filterOptions={(options) => {
      //   options.filter(option => option.title);
      //   return options;
      // }}
      // filterOptions={(options) => {
      //   const tagsOptions = [...new Set(options)];
      //   return tagsOptions;
      // }}
      renderInput={params => (
        <TextField
          {...params}
          variant="outlined"
          label="Tags"
          placeholder="Select multiple tags"
          margin="normal"
          fullWidth
        />
      )}
    />
  );
};

谢谢

1 个答案:

答案 0 :(得分:0)

如果有人遇到相同的问题,我使用MUI的VALUE属性(而非默认值)进行了纠正。