'use client';
import * as React from 'react';
import { cn } from '@/registry/default/lib/utils';
import { Button, ButtonArrow } from '@/registry/default/ui/button';
import {
Command,
CommandCheck,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from '@/registry/default/ui/command';
import { Popover, PopoverContent, PopoverTrigger } from '@/registry/default/ui/popover';
import { ScrollArea, ScrollBar } from '@/registry/default/ui/scroll-area';
const groupedCountries = [
{
group: 'Europe',
countries: [
{ value: 'netherlands', label: 'Netherlands', flag: '๐ณ๐ฑ' },
{ value: 'united_kingdom', label: 'United Kingdom', flag: '๐ฌ๐ง' },
{ value: 'france', label: 'France', flag: '๐ซ๐ท' },
{ value: 'germany', label: 'Germany', flag: '๐ฉ๐ช' },
{ value: 'italy', label: 'Italy', flag: '๐ฎ๐น' },
],
},
{
group: 'Asia',
countries: [
{ value: 'japan', label: 'Japan', flag: '๐ฏ๐ต' },
{ value: 'china', label: 'China', flag: '๐จ๐ณ' },
{ value: 'india', label: 'India', flag: '๐ฎ๐ณ' },
{ value: 'uae', label: 'United Arab Emirates', flag: '๐ฆ๐ช' },
{ value: 'south_korea', label: 'South Korea', flag: '๐ฐ๐ท' },
],
},
{
group: 'Africa',
countries: [
{ value: 'south_africa', label: 'South Africa', flag: '๐ฟ๐ฆ' },
{ value: 'nigeria', label: 'Nigeria', flag: '๐ณ๐ฌ' },
{ value: 'egypt', label: 'Egypt', flag: '๐ช๐ฌ' },
{ value: 'kenya', label: 'Kenya', flag: '๐ฐ๐ช' },
{ value: 'morocco', label: 'Morocco', flag: '๐ฒ๐ฆ' },
],
},
{
group: 'North America',
countries: [
{ value: 'united_states', label: 'United States', flag: '๐บ๐ธ' },
{ value: 'canada', label: 'Canada', flag: '๐จ๐ฆ' },
{ value: 'mexico', label: 'Mexico', flag: '๐ฒ๐ฝ' },
{ value: 'cuba', label: 'Cuba', flag: '๐จ๐บ' },
{ value: 'jamaica', label: 'Jamaica', flag: '๐ฏ๐ฒ' },
],
},
{
group: 'South America',
countries: [
{ value: 'brazil', label: 'Brazil', flag: '๐ง๐ท' },
{ value: 'argentina', label: 'Argentina', flag: '๐ฆ๐ท' },
{ value: 'colombia', label: 'Colombia', flag: '๐จ๐ด' },
{ value: 'chile', label: 'Chile', flag: '๐จ๐ฑ' },
{ value: 'peru', label: 'Peru', flag: '๐ต๐ช' },
],
},
{
group: 'Oceania',
countries: [
{ value: 'australia', label: 'Australia', flag: '๐ฆ๐บ' },
{ value: 'new_zealand', label: 'New Zealand', flag: '๐ณ๐ฟ' },
{ value: 'fiji', label: 'Fiji', flag: '๐ซ๐ฏ' },
{ value: 'papua_new_guinea', label: 'Papua New Guinea', flag: '๐ต๐ฌ' },
{ value: 'samoa', label: 'Samoa', flag: '๐ผ๐ธ' },
],
},
];
export default function ComboboxDemo() {
const [open, setOpen] = React.useState(false);
const [value, setValue] = React.useState('');
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
mode="input"
placeholder={!value}
aria-expanded={open}
className="w-[250px]"
>
<span className={cn('truncate')}>
{value
? groupedCountries.flatMap((group) => group.countries).find((country) => country.value === value)?.label
: 'Select country...'}
</span>
<ButtonArrow />
</Button>
</PopoverTrigger>
<PopoverContent className="w-(--radix-popper-anchor-width) p-0">
<Command>
<CommandInput placeholder="Search country..." />
<CommandList>
<ScrollArea viewportClassName="max-h-[300px]">
<CommandEmpty>No country found.</CommandEmpty>
{groupedCountries.map((group) => (
<CommandGroup key={group.group} heading={group.group}>
{group.countries.map((country) => (
<CommandItem
key={country.value}
value={country.value}
onSelect={(currentValue) => {
setValue(currentValue === value ? '' : currentValue);
setOpen(false);
}}
>
<span className="flex items-center gap-2">
<span className="text-sm">{country.flag}</span>
{country.label}
</span>
{value === country.value && <CommandCheck />}
</CommandItem>
))}
</CommandGroup>
))}
<ScrollBar />
</ScrollArea>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}