import React, { useState } from 'react'; import HStack from '../hstack/hstack'; import Tag from './tag'; interface ITagInput { tags: string[] onChange: (tags: string[]) => void placeholder?: string } /** Manage a list of tags. */ // https://blog.logrocket.com/building-a-tag-input-field-component-for-react/ const TagInput: React.FC = ({ tags, onChange, placeholder }) => { const [input, setInput] = useState(''); const handleTagDelete = (tag: string) => { onChange(tags.filter(item => item !== tag)); }; const handleKeyDown: React.KeyboardEventHandler = (e) => { const { key } = e; const trimmedInput = input.trim(); if (key === 'Tab') { e.preventDefault(); } if ([',', 'Tab', 'Enter'].includes(key) && trimmedInput.length && !tags.includes(trimmedInput)) { e.preventDefault(); onChange([...tags, trimmedInput]); setInput(''); } if (key === 'Backspace' && !input.length && tags.length) { e.preventDefault(); const tagsCopy = [...tags]; tagsCopy.pop(); onChange(tagsCopy); } }; return (
{tags.map((tag, i) => (
))} setInput(e.target.value)} onKeyDown={handleKeyDown} />
); }; export default TagInput;