"use client" import * as React from "react" import { X, Plus } from "lucide-react" import { cn } from "@/lib/utils" import { Input } from "@/components/ui/input" interface TagInputProps { value: string[] onChange: (value: string[]) => void placeholder?: string className?: string disabled?: boolean id?: string } function TagInput({ value, onChange, placeholder = "Add tag...", className, disabled = false, }: TagInputProps) { const [inputValue, setInputValue] = React.useState("") const inputRef = React.useRef(null) const handleAddTag = () => { const trimmed = inputValue.trim() if (trimmed && !value.includes(trimmed)) { onChange([...value, trimmed]) setInputValue("") } } const handleRemoveTag = (tagToRemove: string) => { onChange(value.filter((tag) => tag !== tagToRemove)) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault() handleAddTag() } else if (e.key === "Backspace" && !inputValue && value.length > 0) { handleRemoveTag(value[value.length - 1]) } } return (
{value.map((tag) => ( {tag} {!disabled && ( )} ))}
setInputValue(e.target.value)} onKeyDown={handleKeyDown} onBlur={handleAddTag} placeholder={value.length === 0 ? placeholder : ""} disabled={disabled} className="flex-1 border-0 bg-transparent px-0 py-1 text-white placeholder:text-neutral-500 focus-visible:ring-0 focus-visible:ring-offset-0" /> {inputValue.trim() && ( )}
) } export { TagInput }