'use client' import { useState, KeyboardEvent, ChangeEvent } from 'react' import { X } from 'lucide-react' import { FieldWrapper } from './shared' export type TagInputProps = { label?: string name: string value?: string[] onChange?: (value: string[]) => void placeholder?: string error?: string className?: string disabled?: boolean required?: boolean info?: string description?: string textSize?: 'sm' | 'md' } export default function TagInput({ label, name, value = [], onChange, placeholder = 'Add...', error, className, disabled, required, info, description, textSize = 'sm', }: TagInputProps) { const [inputValue, setInputValue] = useState('') function handleKeyDown(e: KeyboardEvent) { if ((e.key === 'Enter' || e.key === ',') && inputValue.trim()) { e.preventDefault() const val = inputValue.trim().replace(/,$/, '') if (val && !value.includes(val)) { const newValue = [...value, val] if (onChange) onChange(newValue) } setInputValue('') } else if (e.key === 'Backspace' && !inputValue && value.length > 0) { const newValue = value.slice(0, -1) if (onChange) onChange(newValue) } } function removeTag(index: number) { if (disabled) return const newValue = value.filter((_, i) => i !== index) if (onChange) onChange(newValue) } return (
{value.map((tag, index) => ( {tag} {!disabled && ( )} ))} ) => setInputValue(e.target.value)} onKeyDown={handleKeyDown} disabled={disabled} placeholder={value.length === 0 ? placeholder : ''} className='flex-1 bg-transparent outline-none min-w-30 text-login-text placeholder:text-login-200' />
) }