'use client'; import { useMemo, useState } from 'react'; import { Check, ChevronsUpDown } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from '@/components/ui/command'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { cn } from '@/lib/utils'; import { subdivisionsForCountry } from '@/lib/i18n/subdivisions'; import type { CountryCode } from '@/lib/i18n/countries'; interface SubdivisionComboboxProps { value: string | null | undefined; onChange: (code: string | null) => void; /** * Country whose subdivisions populate the dropdown. When the country * has no recognized subdivisions, the trigger renders disabled with * an empty-state hint so the form still lays out. */ country: CountryCode | null | undefined; placeholder?: string; disabled?: boolean; className?: string; clearable?: boolean; id?: string; 'data-testid'?: string; /** Open the dropdown on first render. Used by inline-edit wrappers. */ defaultOpen?: boolean; /** Notified whenever the dropdown opens/closes. Inline-edit wrappers use * this to auto-exit edit mode when the user dismisses without picking. */ onOpenChange?: (open: boolean) => void; } export function SubdivisionCombobox({ value, onChange, country, placeholder = 'Select region…', disabled, className, clearable = true, id, 'data-testid': testId, defaultOpen = false, onOpenChange, }: SubdivisionComboboxProps) { const [open, setOpen] = useState(defaultOpen); const handleOpenChange = (next: boolean) => { setOpen(next); onOpenChange?.(next); }; const options = useMemo(() => { if (!country) return []; return subdivisionsForCountry(country); }, [country]); const selected = value ? options.find((o) => o.code === value) : undefined; const noCountry = !country; const noSubdivisions = !noCountry && options.length === 0; const isDisabled = disabled || noCountry || noSubdivisions; let triggerLabel: string; if (selected) triggerLabel = selected.name; else if (noCountry) triggerLabel = 'Pick a country first'; else if (noSubdivisions) triggerLabel = 'No regions available'; else triggerLabel = placeholder; return ( No region found. {clearable && value ? ( { onChange(null); setOpen(false); }} className="text-muted-foreground" > Clear selection ) : null} {options.map((opt) => ( { onChange(opt.code); setOpen(false); }} > {opt.name} {opt.code} ))} ); }