简体   繁体   English

使用 redux 表单的 react-tag-autocomplete

[英]react-tag-autocomplete with redux form

Im trying to use react-tag-autocomplete with redux form.我正在尝试将 react-tag-autocomplete 与 redux 表单一起使用。

This is the documentation for react-tag-autocomplete React-tag-autocomplete这是 react-tag-autocomplete 的文档React-tag-autocomplete

And below is react-tag-autocomplete with redux-form下面是带有 redux-form 的 react-tag-autocomplete

编辑 Redux 表单 - 简单示例

I just wanted to know whether it's possible to display the id number along with the value (fruit name) in the list.我只是想知道是否可以在列表中显示 ID 号和值(水果名称)。 See image below (red number refers to the id).见下图(红色数字指的是 id)。 I've gone through the documentation but i can't seem to find anything that deals with this.我已经浏览了文档,但似乎找不到任何与此相关的内容。 What i ultimately want to achieve is something like stackoverflow's tag system with id number and description.我最终想要实现的是带有 ID 号和描述的 stackoverflow 的标签系统。

我想要的是

You can use suggestionComponent prop to customise suggestion based on your needs.您可以使用SuggestionComponent 属性根据您的需要自定义建议

Imp note - suggestionComponent is still in beta version.小鬼注意- 建议组件仍处于测试版。 You have to update your library to version 6.您必须将库更新到版本 6。

See Working copy of your code here (with version 6) 在此处查看您的代码的工作副本(版本 6)

Code snippet代码片段

function SuggestionComponent({ item, query }) {
  return (
    <span id={item.id}>
      {item.name} - {item.id}
    </span>
  );
}
const TagAutocomplete = ({ input: { value, onChange } }) => {
  const suggestions = [
    { id: 1, name: "Apples" },
    { id: 2, name: "Pears" },
    { id: 3, name: "Bananas" },
    { id: 4, name: "Mangos" },
    { id: 5, name: "Lemons" },
    { id: 6, name: "Apricots" }
  ];
  const newValue = !value ? [] : value;

  const handleDelete = i => {
    const tags = [...newValue];
    tags.splice(i, 1);
    onChange(tags);
  };

  const handleAdd = e => {
    console.log("e", e);
    onChange([...newValue, e]);
  };

  return (
    <ReactTags
      suggestionComponent={SuggestionComponent}
      tags={newValue}
      suggestions={suggestions}
      handleDelete={handleDelete}
      handleAddition={handleAdd}
    />
  );
};

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

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