'use client';
import * as React from 'react';
import { Button, ButtonArrow } from '@/registry/default/ui/button';
import {
Command,
CommandCheck,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from '@/registry/default/ui/command';
import { Input } from '@/registry/default/ui/input';
import { Popover, PopoverContent, PopoverTrigger } from '@/registry/default/ui/popover';
import { ScrollArea } from '@/registry/default/ui/scroll-area';
const countries = [
{ code: 'US', name: 'United States', dialCode: '+1', flag: '๐บ๐ธ' },
{ code: 'GB', name: 'United Kingdom', dialCode: '+44', flag: '๐ฌ๐ง' },
{ code: 'CA', name: 'Canada', dialCode: '+1', flag: '๐จ๐ฆ' },
{ code: 'AU', name: 'Australia', dialCode: '+61', flag: '๐ฆ๐บ' },
{ code: 'DE', name: 'Germany', dialCode: '+49', flag: '๐ฉ๐ช' },
{ code: 'FR', name: 'France', dialCode: '+33', flag: '๐ซ๐ท' },
{ code: 'IT', name: 'Italy', dialCode: '+39', flag: '๐ฎ๐น' },
{ code: 'ES', name: 'Spain', dialCode: '+34', flag: '๐ช๐ธ' },
{ code: 'NL', name: 'Netherlands', dialCode: '+31', flag: '๐ณ๐ฑ' },
{ code: 'BE', name: 'Belgium', dialCode: '+32', flag: '๐ง๐ช' },
{ code: 'CH', name: 'Switzerland', dialCode: '+41', flag: '๐จ๐ญ' },
{ code: 'AT', name: 'Austria', dialCode: '+43', flag: '๐ฆ๐น' },
{ code: 'SE', name: 'Sweden', dialCode: '+46', flag: '๐ธ๐ช' },
{ code: 'NO', name: 'Norway', dialCode: '+47', flag: '๐ณ๐ด' },
{ code: 'DK', name: 'Denmark', dialCode: '+45', flag: '๐ฉ๐ฐ' },
{ code: 'FI', name: 'Finland', dialCode: '+358', flag: '๐ซ๐ฎ' },
{ code: 'PL', name: 'Poland', dialCode: '+48', flag: '๐ต๐ฑ' },
{ code: 'CZ', name: 'Czech Republic', dialCode: '+420', flag: '๐จ๐ฟ' },
{ code: 'HU', name: 'Hungary', dialCode: '+36', flag: '๐ญ๐บ' },
{ code: 'PT', name: 'Portugal', dialCode: '+351', flag: '๐ต๐น' },
{ code: 'GR', name: 'Greece', dialCode: '+30', flag: '๐ฌ๐ท' },
{ code: 'TR', name: 'Turkey', dialCode: '+90', flag: '๐น๐ท' },
{ code: 'RU', name: 'Russia', dialCode: '+7', flag: '๐ท๐บ' },
{ code: 'JP', name: 'Japan', dialCode: '+81', flag: '๐ฏ๐ต' },
{ code: 'KR', name: 'South Korea', dialCode: '+82', flag: '๐ฐ๐ท' },
{ code: 'CN', name: 'China', dialCode: '+86', flag: '๐จ๐ณ' },
{ code: 'IN', name: 'India', dialCode: '+91', flag: '๐ฎ๐ณ' },
{ code: 'SG', name: 'Singapore', dialCode: '+65', flag: '๐ธ๐ฌ' },
{ code: 'HK', name: 'Hong Kong', dialCode: '+852', flag: '๐ญ๐ฐ' },
{ code: 'TW', name: 'Taiwan', dialCode: '+886', flag: '๐น๐ผ' },
{ code: 'MY', name: 'Malaysia', dialCode: '+60', flag: '๐ฒ๐พ' },
{ code: 'TH', name: 'Thailand', dialCode: '+66', flag: '๐น๐ญ' },
{ code: 'PH', name: 'Philippines', dialCode: '+63', flag: '๐ต๐ญ' },
{ code: 'ID', name: 'Indonesia', dialCode: '+62', flag: '๐ฎ๐ฉ' },
{ code: 'VN', name: 'Vietnam', dialCode: '+84', flag: '๐ป๐ณ' },
{ code: 'BR', name: 'Brazil', dialCode: '+55', flag: '๐ง๐ท' },
{ code: 'MX', name: 'Mexico', dialCode: '+52', flag: '๐ฒ๐ฝ' },
{ code: 'AR', name: 'Argentina', dialCode: '+54', flag: '๐ฆ๐ท' },
{ code: 'CL', name: 'Chile', dialCode: '+56', flag: '๐จ๐ฑ' },
{ code: 'CO', name: 'Colombia', dialCode: '+57', flag: '๐จ๐ด' },
{ code: 'PE', name: 'Peru', dialCode: '+51', flag: '๐ต๐ช' },
{ code: 'ZA', name: 'South Africa', dialCode: '+27', flag: '๐ฟ๐ฆ' },
{ code: 'EG', name: 'Egypt', dialCode: '+20', flag: '๐ช๐ฌ' },
{ code: 'NG', name: 'Nigeria', dialCode: '+234', flag: '๐ณ๐ฌ' },
{ code: 'KE', name: 'Kenya', dialCode: '+254', flag: '๐ฐ๐ช' },
{ code: 'IL', name: 'Israel', dialCode: '+972', flag: '๐ฎ๐ฑ' },
{ code: 'AE', name: 'United Arab Emirates', dialCode: '+971', flag: '๐ฆ๐ช' },
{ code: 'SA', name: 'Saudi Arabia', dialCode: '+966', flag: '๐ธ๐ฆ' },
];
export default function PhoneNumberInputDemo() {
const [open, setOpen] = React.useState(false);
const [selectedCountry, setSelectedCountry] = React.useState(countries[0]);
const [phoneNumber, setPhoneNumber] = React.useState('');
// Handle phone number input change
const handlePhoneChange = (inputValue: string) => {
// Remove any non-digit characters except + at the start
const cleanValue = inputValue.replace(/[^\d+]/g, '');
// Check if user is typing a country code
if (cleanValue.startsWith('+')) {
const matchedCountry = countries.find((country) => cleanValue.startsWith(country.dialCode));
if (matchedCountry && matchedCountry.code !== selectedCountry.code) {
setSelectedCountry(matchedCountry);
setPhoneNumber(cleanValue.slice(matchedCountry.dialCode.length));
return;
}
}
setPhoneNumber(cleanValue.startsWith('+') ? cleanValue : cleanValue);
};
// Handle country selection
const handleCountrySelect = (countryCode: string) => {
const country = countries.find((c) => c.code === countryCode);
if (country) {
setSelectedCountry(country);
setOpen(false);
}
};
return (
<div className="flex items-center w-[300px]">
{/* Country Selector */}
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="w-24 items-center justify-between rounded-e-none border-e-0 bg-transparent"
>
<span className="flex items-center gap-1.5">
<span className="text-sm leading-none">{selectedCountry.flag}</span>
<span className="text-xs leading-none">{selectedCountry.dialCode}</span>
</span>
<ButtonArrow />
</Button>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0" align="start">
<Command>
<CommandInput placeholder="Search country..." />
<CommandList>
<ScrollArea className="h-[300px]">
<CommandEmpty>No country found.</CommandEmpty>
<CommandGroup>
{countries.map((country) => (
<CommandItem
key={country.code}
value={`${country.name} ${country.dialCode}`}
onSelect={() => handleCountrySelect(country.code)}
>
<span className="flex items-center gap-1.5 leading-none">
<span className="text-sm">{country.flag}</span>
<span className="text-sm text-foreground truncate">{country.name}</span>
<span className="text-sm text-muted-foreground">{country.dialCode}</span>
</span>
{selectedCountry.code === country.code && <CommandCheck />}
</CommandItem>
))}
</CommandGroup>
</ScrollArea>
</CommandList>
</Command>
</PopoverContent>
</Popover>
{/* Phone Number Input */}
<Input
type="tel"
placeholder="Enter phone number"
value={phoneNumber.startsWith('+') ? phoneNumber : phoneNumber}
onChange={(e) => handlePhoneChange(e.target.value)}
className="rounded-l-none flex-1"
/>
</div>
);
}