react taginput component

Solutions on MaxInterview for react taginput component by the best coders in the world

showing results for - "react taginput component"
Sami
21 Jan 2018
1const TagInput = ({ tags }) => {
2  const [tagData, setTagData] = React.useState(tags);
3  const removeTagData = indexToRemove => {
4    setTagData([...tagData.filter((_, index) => index !== indexToRemove)]);
5  };
6  const addTagData = event => {
7    if (event.target.value !== '') {
8      setTagData([...tagData, event.target.value]);
9      event.target.value = '';
10    }
11  };
12  return (
13    <div className="tag-input">
14      <ul className="tags">
15        {tagData.map((tag, index) => (
16          <li key={index} className="tag">
17            <span className="tag-title">{tag}</span>
18            <span
19              className="tag-close-icon"
20              onClick={() => removeTagData(index)}
21            >
22              x
23            </span>
24          </li>
25        ))}
26      </ul>
27      <input
28        type="text"
29        onKeyUp={event => (event.key === 'Enter' ? addTagData(event) : null)}
30        placeholder="Press enter to add a tag"
31      />
32    </div>
33  );
34};