'use client' import { XIcon } from 'lucide-react' import { useState } from 'react' import { Badge } from '@/components/ui/Badge' import { Field, FieldError, FieldLabel } from '@/components/ui/field' import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select' export type RelationOption = { id: number name: string } type RelationMultiSelectProps = { /** FormData field name — repeated hidden inputs, one per selected id. */ name: string label: string options: RelationOption[] initialSelectedIDs?: number[] error?: string placeholder?: string } /** Select-to-add + removable chips; submits repeated hidden inputs. */ export const RelationMultiSelect = ({ name, label, options, initialSelectedIDs = [], error, placeholder = 'Adicionar…', }: RelationMultiSelectProps) => { const [selectedIDs, setSelectedIDs] = useState(initialSelectedIDs) const optionById = new Map(options.map((option) => [option.id, option])) const available = options.filter((option) => !selectedIDs.includes(option.id)) const selectId = `${name}-multi-select` return ( {label} {selectedIDs.length ? ( ) : null} { const id = Number(event.target.value) if (Number.isInteger(id) && id > 0 && !selectedIDs.includes(id)) { setSelectedIDs([...selectedIDs, id]) } }} className="min-h-11 w-full" aria-invalid={error ? true : undefined} > {placeholder} {available.map((option) => ( {option.name} ))} {error ? {error} : null} ) }