125 lines
3.9 KiB
TypeScript
125 lines
3.9 KiB
TypeScript
|
|
'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;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function SubdivisionCombobox({
|
||
|
|
value,
|
||
|
|
onChange,
|
||
|
|
country,
|
||
|
|
placeholder = 'Select region…',
|
||
|
|
disabled,
|
||
|
|
className,
|
||
|
|
clearable = true,
|
||
|
|
id,
|
||
|
|
'data-testid': testId,
|
||
|
|
}: SubdivisionComboboxProps) {
|
||
|
|
const [open, setOpen] = useState(false);
|
||
|
|
|
||
|
|
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 (
|
||
|
|
<Popover open={open} onOpenChange={setOpen}>
|
||
|
|
<PopoverTrigger asChild>
|
||
|
|
<Button
|
||
|
|
id={id}
|
||
|
|
variant="outline"
|
||
|
|
role="combobox"
|
||
|
|
aria-expanded={open}
|
||
|
|
disabled={isDisabled}
|
||
|
|
className={cn('w-full justify-between', !selected && 'text-muted-foreground', className)}
|
||
|
|
data-testid={testId}
|
||
|
|
>
|
||
|
|
<span className="truncate text-sm">{triggerLabel}</span>
|
||
|
|
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
||
|
|
</Button>
|
||
|
|
</PopoverTrigger>
|
||
|
|
<PopoverContent className="w-[320px] p-0" align="start">
|
||
|
|
<Command>
|
||
|
|
<CommandInput placeholder={`Search ${country ?? 'regions'}…`} />
|
||
|
|
<CommandList>
|
||
|
|
<CommandEmpty>No region found.</CommandEmpty>
|
||
|
|
{clearable && value ? (
|
||
|
|
<CommandGroup>
|
||
|
|
<CommandItem
|
||
|
|
value="__clear__"
|
||
|
|
onSelect={() => {
|
||
|
|
onChange(null);
|
||
|
|
setOpen(false);
|
||
|
|
}}
|
||
|
|
className="text-muted-foreground"
|
||
|
|
>
|
||
|
|
Clear selection
|
||
|
|
</CommandItem>
|
||
|
|
</CommandGroup>
|
||
|
|
) : null}
|
||
|
|
<CommandGroup>
|
||
|
|
{options.map((opt) => (
|
||
|
|
<CommandItem
|
||
|
|
key={opt.code}
|
||
|
|
value={`${opt.name} ${opt.code}`}
|
||
|
|
onSelect={() => {
|
||
|
|
onChange(opt.code);
|
||
|
|
setOpen(false);
|
||
|
|
}}
|
||
|
|
>
|
||
|
|
<Check
|
||
|
|
className={cn('mr-2 h-4 w-4', value === opt.code ? 'opacity-100' : 'opacity-0')}
|
||
|
|
/>
|
||
|
|
<span className="flex-1 truncate text-sm">{opt.name}</span>
|
||
|
|
<span className="text-xs text-muted-foreground">{opt.code}</span>
|
||
|
|
</CommandItem>
|
||
|
|
))}
|
||
|
|
</CommandGroup>
|
||
|
|
</CommandList>
|
||
|
|
</Command>
|
||
|
|
</PopoverContent>
|
||
|
|
</Popover>
|
||
|
|
);
|
||
|
|
}
|