@greatapps/common 1.1.174 → 1.1.175

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,148 +1,205 @@
1
1
  'use client';
2
2
 
3
- import { ComponentPropsWithoutRef, forwardRef } from 'react';
4
- import Image from 'next/image';
5
- import { ChevronDown } from 'lucide-react';
6
- import { Input } from './Input';
7
- import { Separator } from '../data-display/Separator';
8
- import { cn } from '../../../infra/utils/clsx';
9
-
10
- export interface PhoneCountry {
11
- code: string;
12
- label: string;
13
- emoji: string;
3
+ import { useState, useEffect, useRef } from 'react';
4
+ import { AsYouType, CountryCode } from 'libphonenumber-js';
5
+ import { Input, Separator, Popover, PopoverTrigger, PopoverContent, cn } from '@greatapps/common';
6
+ import { IconChevronDown, IconSearch } from '@tabler/icons-react';
7
+ import { COUNTRIES, Country, flagUrl, parsePhoneValue } from '../../../utils/countries';
8
+
9
+ function formatNational(digits: string, iso: string): string {
10
+ const raw = digits.replace(/\D/g, '');
11
+ if (!raw) return '';
12
+ return new AsYouType(iso as CountryCode).input(raw);
14
13
  }
15
14
 
16
- export const PHONE_COUNTRIES: PhoneCountry[] = [
17
- { code: '+55', label: 'Brasil', emoji: '🇧🇷' },
18
- { code: '+1', label: 'EUA', emoji: '🇺🇸' },
19
- { code: '+351', label: 'Portugal', emoji: '🇵🇹' },
20
- { code: '+54', label: 'Argentina', emoji: '🇦🇷' },
21
- { code: '+56', label: 'Chile', emoji: '🇨🇱' },
22
- { code: '+52', label: 'México', emoji: '🇲🇽' },
23
- { code: '+44', label: 'Reino Unido', emoji: '🇬🇧' },
24
- { code: '+34', label: 'Espanha', emoji: '🇪🇸' },
25
- { code: '+49', label: 'Alemanha', emoji: '🇩🇪' },
26
- { code: '+33', label: 'França', emoji: '🇫🇷' },
27
- { code: '+57', label: 'Colômbia', emoji: '🇨🇴' },
28
- { code: '+598', label: 'Uruguai', emoji: '🇺🇾' },
29
- ];
30
-
31
- interface PhoneInputProps extends Omit<ComponentPropsWithoutRef<'input'>, 'type'> {
15
+ interface PhoneInputProps {
16
+ value?: string;
17
+ onChange?: (value: string) => void;
32
18
  label?: string;
33
19
  optional?: boolean;
34
20
  required?: boolean;
35
21
  hintMessage?: string;
36
22
  error?: boolean;
37
23
  errorMessage?: string;
38
- countryCode?: string;
39
- countryFlagSrc?: string;
40
- countryFlagAlt?: string;
24
+ placeholder?: string;
25
+ disabled?: boolean;
41
26
  classnameContainer?: string;
42
- selectedCountry?: PhoneCountry;
43
- onCountryChange?: (country: PhoneCountry) => void;
27
+ className?: string;
44
28
  }
45
29
 
46
- const PhoneInput = forwardRef<HTMLInputElement, PhoneInputProps>(
47
- (
48
- {
49
- label,
50
- optional = false,
51
- required = false,
52
- hintMessage,
53
- error = false,
54
- errorMessage,
55
- countryCode = '+55',
56
- countryFlagSrc = '/icons/brazil-flag.svg',
57
- countryFlagAlt = 'Brazil flag',
58
- className,
59
- classnameContainer,
60
- selectedCountry,
61
- onCountryChange,
62
- ...props
63
- },
64
- ref
65
- ) => {
66
- const finalMessage = error && errorMessage ? errorMessage : hintMessage;
67
- const showMessage = finalMessage && finalMessage.trim() !== '';
68
-
69
- const displayCode = selectedCountry ? selectedCountry.code : countryCode;
70
-
71
- return (
72
- <div className={cn('flex flex-col gap-1.5')}>
73
- {label && (
74
- <div className="flex items-center gap-1 paragraph-xsmall-semibold">
75
- <span className="text-gray-600">{label}</span>
76
- {optional && <span className="text-gray-500">(Opcional)</span>}
77
- {required && <span className="text-red-600">*</span>}
78
- </div>
79
- )}
30
+ export default function PhoneInput({
31
+ value,
32
+ onChange,
33
+ label,
34
+ optional = false,
35
+ required = false,
36
+ hintMessage,
37
+ error = false,
38
+ errorMessage,
39
+ placeholder,
40
+ disabled = false,
41
+ classnameContainer,
42
+ className,
43
+ }: PhoneInputProps) {
44
+ const parsed = parsePhoneValue(value ?? '');
45
+ const [selectedCountry, setSelectedCountry] = useState<Country>(parsed.country);
46
+ const [phoneNumber, setPhoneNumber] = useState(parsed.number);
47
+ const [search, setSearch] = useState('');
48
+ const [open, setOpen] = useState(false);
49
+ const prevValueRef = useRef(value);
50
+ const inputRef = useRef<HTMLInputElement>(null);
80
51
 
81
- <div
82
- className={cn(
83
- 'flex gap-2 items-center border rounded-lg h-10 transition-colors',
84
- error
85
- ? 'border-red-600 hover:border-red-700'
86
- : 'border-gray-200 hover:border-gray-400 focus-within:border-gray-950',
87
- classnameContainer
88
- )}
89
- >
90
- {onCountryChange ? (
91
- <div className="relative flex items-center gap-2 px-4 cursor-pointer shrink-0">
92
- <span className="text-base leading-none">{selectedCountry?.emoji ?? '🇧🇷'}</span>
93
- <span className="paragraph-small-regular text-gray-950">{displayCode}</span>
94
- <ChevronDown size={14} className="text-gray-500" />
95
- <select
96
- className="absolute inset-0 opacity-0 cursor-pointer w-full"
97
- value={displayCode}
98
- onChange={(e) => {
99
- const country = PHONE_COUNTRIES.find((c) => c.code === e.target.value);
100
- if (country) onCountryChange(country);
101
- }}
102
- >
103
- {PHONE_COUNTRIES.map((c) => (
104
- <option key={c.code} value={c.code}>
105
- {c.emoji} {c.code} {c.label}
106
- </option>
107
- ))}
108
- </select>
109
- </div>
110
- ) : (
111
- <div className="flex items-center gap-2 px-4">
112
- <Image
113
- src={countryFlagSrc}
114
- alt={countryFlagAlt}
115
- width={20}
116
- height={20}
117
- className="h-5! w-5! rounded-full"
118
- />
119
- <span className="paragraph-small-regular text-gray-950">{countryCode}</span>
120
- </div>
121
- )}
52
+ useEffect(() => {
53
+ if (value !== prevValueRef.current) {
54
+ prevValueRef.current = value;
55
+ const p = parsePhoneValue(value ?? '');
56
+ setSelectedCountry(p.country);
57
+ setPhoneNumber(formatNational(p.number, p.country.iso));
58
+ }
59
+ }, [value]);
60
+
61
+ const emit = (country: Country, number: string) => {
62
+ onChange?.(`${country.dial} ${number}`);
63
+ };
64
+
65
+ const handleCountrySelect = (country: Country) => {
66
+ const reformatted = formatNational(phoneNumber, country.iso);
67
+ setSelectedCountry(country);
68
+ setPhoneNumber(reformatted);
69
+ setOpen(false);
70
+ setSearch('');
71
+ emit(country, reformatted);
72
+ };
73
+
74
+ const handleNumberChange = (e: React.ChangeEvent<HTMLInputElement>) => {
75
+ const rawInput = e.target.value;
76
+ const cursorPos = e.target.selectionStart ?? rawInput.length;
77
+ const digitsBeforeCursor = rawInput.slice(0, cursorPos).replace(/\D/g, '').length;
78
+
79
+ const formatted = formatNational(rawInput, selectedCountry.iso);
80
+ setPhoneNumber(formatted);
81
+ emit(selectedCountry, formatted);
122
82
 
123
- <Separator orientation="vertical" />
124
-
125
- <Input
126
- ref={ref}
127
- type="tel"
128
- className={cn(
129
- 'paragraph-small-medium w-full h-full text-gray-950 border-0 focus-visible:ring-0 focus-visible:ring-offset-0',
130
- className
131
- )}
132
- {...props}
133
- />
83
+ requestAnimationFrame(() => {
84
+ if (!inputRef.current) return;
85
+ let digitCount = 0;
86
+ let newPos = formatted.length;
87
+ for (let i = 0; i < formatted.length; i++) {
88
+ if (/\d/.test(formatted[i])) {
89
+ digitCount++;
90
+ if (digitCount === digitsBeforeCursor) {
91
+ newPos = i + 1;
92
+ break;
93
+ }
94
+ }
95
+ }
96
+ inputRef.current.setSelectionRange(newPos, newPos);
97
+ });
98
+ };
99
+
100
+ const filtered = COUNTRIES.filter(
101
+ (c) =>
102
+ c.name.toLowerCase().includes(search.toLowerCase()) ||
103
+ c.dial.includes(search) ||
104
+ c.iso.toLowerCase().includes(search.toLowerCase())
105
+ );
106
+
107
+ const finalMessage = error && errorMessage ? errorMessage : hintMessage;
108
+ const showMessage = finalMessage && finalMessage.trim() !== '';
109
+
110
+ return (
111
+ <div className="flex flex-col gap-1.5">
112
+ {label && (
113
+ <div className="flex items-center gap-1 paragraph-xsmall-semibold">
114
+ <span className="text-zinc-600">{label}</span>
115
+ {optional && <span className="text-zinc-500">(Opcional)</span>}
116
+ {required && <span className="text-red-600">*</span>}
134
117
  </div>
118
+ )}
135
119
 
136
- {showMessage && (
137
- <span className={cn('paragraph-xsmall-medium', error ? 'text-red-600' : 'text-gray-600')}>
138
- {finalMessage}
139
- </span>
120
+ <div
121
+ className={cn(
122
+ 'flex gap-2 items-center border rounded-lg h-10 transition-colors',
123
+ error
124
+ ? 'border-red-600 hover:border-red-700'
125
+ : 'border-zinc-200 hover:border-zinc-400 focus-within:border-zinc-950',
126
+ disabled && 'opacity-50 cursor-not-allowed',
127
+ classnameContainer
140
128
  )}
141
- </div>
142
- );
143
- }
144
- );
129
+ >
130
+ <Popover open={open} onOpenChange={setOpen}>
131
+ <PopoverTrigger asChild>
132
+ <button
133
+ type="button"
134
+ className="flex items-center gap-1.5 px-3 h-full shrink-0 focus:outline-none"
135
+ disabled={disabled}
136
+ >
137
+ <img src={flagUrl(selectedCountry.iso)} alt={selectedCountry.iso} className="w-5 h-5 rounded-full" />
138
+ <span className="paragraph-small-regular text-zinc-950">{selectedCountry.dial}</span>
139
+ <IconChevronDown size={14} className={cn("text-zinc-500 transition-transform duration-200", open && "rotate-180")} />
140
+ </button>
141
+ </PopoverTrigger>
142
+ <PopoverContent className="w-72 p-0" align="start">
143
+ <div className="flex items-center gap-2 p-2 border-b border-zinc-100">
144
+ <IconSearch size={14} className="text-zinc-400 shrink-0" />
145
+ <input
146
+ type="text"
147
+ className="flex-1 text-sm outline-none placeholder:text-zinc-400"
148
+ placeholder="Buscar país..."
149
+ value={search}
150
+ onChange={(e) => setSearch(e.target.value)}
151
+ autoFocus
152
+ />
153
+ </div>
154
+ <div className="max-h-60 overflow-y-auto overscroll-contain" onTouchMove={(e) => e.stopPropagation()}>
155
+ {filtered.map((country) => (
156
+ <button
157
+ key={country.iso}
158
+ type="button"
159
+ className={cn(
160
+ 'flex items-center gap-2.5 w-full px-3 py-2 text-left hover:bg-zinc-50 transition-colors',
161
+ selectedCountry.iso === country.iso && 'bg-zinc-100'
162
+ )}
163
+ onClick={() => handleCountrySelect(country)}
164
+ >
165
+ <img src={flagUrl(country.iso)} alt={country.iso} className="w-5 h-5 rounded-full shrink-0" />
166
+ <span className="paragraph-small-regular text-zinc-950 flex-1 truncate">
167
+ {country.name}
168
+ </span>
169
+ <span className="paragraph-small-regular text-zinc-500 shrink-0">
170
+ {country.dial}
171
+ </span>
172
+ </button>
173
+ ))}
174
+ {filtered.length === 0 && (
175
+ <p className="text-sm text-zinc-500 p-3 text-center">Nenhum país encontrado</p>
176
+ )}
177
+ </div>
178
+ </PopoverContent>
179
+ </Popover>
180
+
181
+ <Separator orientation="vertical" />
145
182
 
146
- PhoneInput.displayName = 'PhoneInput';
183
+ <Input
184
+ ref={inputRef}
185
+ type="tel"
186
+ value={phoneNumber}
187
+ onChange={handleNumberChange}
188
+ placeholder={placeholder}
189
+ disabled={disabled}
190
+ className={cn(
191
+ 'paragraph-small-medium w-full h-full text-zinc-950 border-0 focus-visible:ring-0 focus-visible:ring-offset-0',
192
+ disabled && 'cursor-not-allowed',
193
+ className
194
+ )}
195
+ />
196
+ </div>
147
197
 
148
- export default PhoneInput;
198
+ {showMessage && (
199
+ <span className={cn('paragraph-xsmall-medium', error ? 'text-red-600' : 'text-zinc-600')}>
200
+ {finalMessage}
201
+ </span>
202
+ )}
203
+ </div>
204
+ );
205
+ }
@@ -0,0 +1,224 @@
1
+ export interface Country {
2
+ iso: string;
3
+ name: string;
4
+ dial: string;
5
+ }
6
+
7
+ export function flagUrl(iso: string): string {
8
+ return `https://hatscripts.github.io/circle-flags/flags/${iso.toLowerCase()}.svg`;
9
+ }
10
+
11
+ export const COUNTRIES: Country[] = [
12
+ { iso: 'BR', name: 'Brasil', dial: '+55' },
13
+ { iso: 'US', name: 'United States', dial: '+1' },
14
+ { iso: 'PT', name: 'Portugal', dial: '+351' },
15
+ { iso: 'AF', name: 'Afghanistan', dial: '+93' },
16
+ { iso: 'AL', name: 'Albania', dial: '+355' },
17
+ { iso: 'DZ', name: 'Algeria', dial: '+213' },
18
+ { iso: 'AD', name: 'Andorra', dial: '+376' },
19
+ { iso: 'AO', name: 'Angola', dial: '+244' },
20
+ { iso: 'AG', name: 'Antigua and Barbuda', dial: '+1268' },
21
+ { iso: 'AR', name: 'Argentina', dial: '+54' },
22
+ { iso: 'AM', name: 'Armenia', dial: '+374' },
23
+ { iso: 'AU', name: 'Australia', dial: '+61' },
24
+ { iso: 'AT', name: 'Austria', dial: '+43' },
25
+ { iso: 'AZ', name: 'Azerbaijan', dial: '+994' },
26
+ { iso: 'BS', name: 'Bahamas', dial: '+1242' },
27
+ { iso: 'BH', name: 'Bahrain', dial: '+973' },
28
+ { iso: 'BD', name: 'Bangladesh', dial: '+880' },
29
+ { iso: 'BB', name: 'Barbados', dial: '+1246' },
30
+ { iso: 'BY', name: 'Belarus', dial: '+375' },
31
+ { iso: 'BE', name: 'Belgium', dial: '+32' },
32
+ { iso: 'BZ', name: 'Belize', dial: '+501' },
33
+ { iso: 'BJ', name: 'Benin', dial: '+229' },
34
+ { iso: 'BT', name: 'Bhutan', dial: '+975' },
35
+ { iso: 'BO', name: 'Bolivia', dial: '+591' },
36
+ { iso: 'BA', name: 'Bosnia and Herzegovina', dial: '+387' },
37
+ { iso: 'BW', name: 'Botswana', dial: '+267' },
38
+ { iso: 'BN', name: 'Brunei', dial: '+673' },
39
+ { iso: 'BG', name: 'Bulgaria', dial: '+359' },
40
+ { iso: 'BF', name: 'Burkina Faso', dial: '+226' },
41
+ { iso: 'BI', name: 'Burundi', dial: '+257' },
42
+ { iso: 'KH', name: 'Cambodia', dial: '+855' },
43
+ { iso: 'CM', name: 'Cameroon', dial: '+237' },
44
+ { iso: 'CA', name: 'Canada', dial: '+1' },
45
+ { iso: 'CV', name: 'Cape Verde', dial: '+238' },
46
+ { iso: 'CF', name: 'Central African Republic', dial: '+236' },
47
+ { iso: 'TD', name: 'Chad', dial: '+235' },
48
+ { iso: 'CL', name: 'Chile', dial: '+56' },
49
+ { iso: 'CN', name: 'China', dial: '+86' },
50
+ { iso: 'CO', name: 'Colombia', dial: '+57' },
51
+ { iso: 'KM', name: 'Comoros', dial: '+269' },
52
+ { iso: 'CG', name: 'Congo', dial: '+242' },
53
+ { iso: 'CD', name: 'Congo (DR)', dial: '+243' },
54
+ { iso: 'CR', name: 'Costa Rica', dial: '+506' },
55
+ { iso: 'HR', name: 'Croatia', dial: '+385' },
56
+ { iso: 'CU', name: 'Cuba', dial: '+53' },
57
+ { iso: 'CY', name: 'Cyprus', dial: '+357' },
58
+ { iso: 'CZ', name: 'Czech Republic', dial: '+420' },
59
+ { iso: 'DK', name: 'Denmark', dial: '+45' },
60
+ { iso: 'DJ', name: 'Djibouti', dial: '+253' },
61
+ { iso: 'DM', name: 'Dominica', dial: '+1767' },
62
+ { iso: 'DO', name: 'Dominican Republic', dial: '+1809' },
63
+ { iso: 'EC', name: 'Ecuador', dial: '+593' },
64
+ { iso: 'EG', name: 'Egypt', dial: '+20' },
65
+ { iso: 'SV', name: 'El Salvador', dial: '+503' },
66
+ { iso: 'GQ', name: 'Equatorial Guinea', dial: '+240' },
67
+ { iso: 'ER', name: 'Eritrea', dial: '+291' },
68
+ { iso: 'EE', name: 'Estonia', dial: '+372' },
69
+ { iso: 'SZ', name: 'Eswatini', dial: '+268' },
70
+ { iso: 'ET', name: 'Ethiopia', dial: '+251' },
71
+ { iso: 'FJ', name: 'Fiji', dial: '+679' },
72
+ { iso: 'FI', name: 'Finland', dial: '+358' },
73
+ { iso: 'FR', name: 'France', dial: '+33' },
74
+ { iso: 'GA', name: 'Gabon', dial: '+241' },
75
+ { iso: 'GM', name: 'Gambia', dial: '+220' },
76
+ { iso: 'GE', name: 'Georgia', dial: '+995' },
77
+ { iso: 'DE', name: 'Germany', dial: '+49' },
78
+ { iso: 'GH', name: 'Ghana', dial: '+233' },
79
+ { iso: 'GR', name: 'Greece', dial: '+30' },
80
+ { iso: 'GD', name: 'Grenada', dial: '+1473' },
81
+ { iso: 'GT', name: 'Guatemala', dial: '+502' },
82
+ { iso: 'GN', name: 'Guinea', dial: '+224' },
83
+ { iso: 'GW', name: 'Guinea-Bissau', dial: '+245' },
84
+ { iso: 'GY', name: 'Guyana', dial: '+592' },
85
+ { iso: 'HT', name: 'Haiti', dial: '+509' },
86
+ { iso: 'HN', name: 'Honduras', dial: '+504' },
87
+ { iso: 'HU', name: 'Hungary', dial: '+36' },
88
+ { iso: 'IS', name: 'Iceland', dial: '+354' },
89
+ { iso: 'IN', name: 'India', dial: '+91' },
90
+ { iso: 'ID', name: 'Indonesia', dial: '+62' },
91
+ { iso: 'IR', name: 'Iran', dial: '+98' },
92
+ { iso: 'IQ', name: 'Iraq', dial: '+964' },
93
+ { iso: 'IE', name: 'Ireland', dial: '+353' },
94
+ { iso: 'IL', name: 'Israel', dial: '+972' },
95
+ { iso: 'IT', name: 'Italy', dial: '+39' },
96
+ { iso: 'JM', name: 'Jamaica', dial: '+1876' },
97
+ { iso: 'JP', name: 'Japan', dial: '+81' },
98
+ { iso: 'JO', name: 'Jordan', dial: '+962' },
99
+ { iso: 'KZ', name: 'Kazakhstan', dial: '+7' },
100
+ { iso: 'KE', name: 'Kenya', dial: '+254' },
101
+ { iso: 'KI', name: 'Kiribati', dial: '+686' },
102
+ { iso: 'KW', name: 'Kuwait', dial: '+965' },
103
+ { iso: 'KG', name: 'Kyrgyzstan', dial: '+996' },
104
+ { iso: 'LA', name: 'Laos', dial: '+856' },
105
+ { iso: 'LV', name: 'Latvia', dial: '+371' },
106
+ { iso: 'LB', name: 'Lebanon', dial: '+961' },
107
+ { iso: 'LS', name: 'Lesotho', dial: '+266' },
108
+ { iso: 'LR', name: 'Liberia', dial: '+231' },
109
+ { iso: 'LY', name: 'Libya', dial: '+218' },
110
+ { iso: 'LI', name: 'Liechtenstein', dial: '+423' },
111
+ { iso: 'LT', name: 'Lithuania', dial: '+370' },
112
+ { iso: 'LU', name: 'Luxembourg', dial: '+352' },
113
+ { iso: 'MG', name: 'Madagascar', dial: '+261' },
114
+ { iso: 'MW', name: 'Malawi', dial: '+265' },
115
+ { iso: 'MY', name: 'Malaysia', dial: '+60' },
116
+ { iso: 'MV', name: 'Maldives', dial: '+960' },
117
+ { iso: 'ML', name: 'Mali', dial: '+223' },
118
+ { iso: 'MT', name: 'Malta', dial: '+356' },
119
+ { iso: 'MH', name: 'Marshall Islands', dial: '+692' },
120
+ { iso: 'MR', name: 'Mauritania', dial: '+222' },
121
+ { iso: 'MU', name: 'Mauritius', dial: '+230' },
122
+ { iso: 'MX', name: 'Mexico', dial: '+52' },
123
+ { iso: 'FM', name: 'Micronesia', dial: '+691' },
124
+ { iso: 'MD', name: 'Moldova', dial: '+373' },
125
+ { iso: 'MC', name: 'Monaco', dial: '+377' },
126
+ { iso: 'MN', name: 'Mongolia', dial: '+976' },
127
+ { iso: 'ME', name: 'Montenegro', dial: '+382' },
128
+ { iso: 'MA', name: 'Morocco', dial: '+212' },
129
+ { iso: 'MZ', name: 'Mozambique', dial: '+258' },
130
+ { iso: 'MM', name: 'Myanmar', dial: '+95' },
131
+ { iso: 'NA', name: 'Namibia', dial: '+264' },
132
+ { iso: 'NR', name: 'Nauru', dial: '+674' },
133
+ { iso: 'NP', name: 'Nepal', dial: '+977' },
134
+ { iso: 'NL', name: 'Netherlands', dial: '+31' },
135
+ { iso: 'NZ', name: 'New Zealand', dial: '+64' },
136
+ { iso: 'NI', name: 'Nicaragua', dial: '+505' },
137
+ { iso: 'NE', name: 'Niger', dial: '+227' },
138
+ { iso: 'NG', name: 'Nigeria', dial: '+234' },
139
+ { iso: 'KP', name: 'North Korea', dial: '+850' },
140
+ { iso: 'MK', name: 'North Macedonia', dial: '+389' },
141
+ { iso: 'NO', name: 'Norway', dial: '+47' },
142
+ { iso: 'OM', name: 'Oman', dial: '+968' },
143
+ { iso: 'PK', name: 'Pakistan', dial: '+92' },
144
+ { iso: 'PW', name: 'Palau', dial: '+680' },
145
+ { iso: 'PS', name: 'Palestine', dial: '+970' },
146
+ { iso: 'PA', name: 'Panama', dial: '+507' },
147
+ { iso: 'PG', name: 'Papua New Guinea', dial: '+675' },
148
+ { iso: 'PY', name: 'Paraguay', dial: '+595' },
149
+ { iso: 'PE', name: 'Peru', dial: '+51' },
150
+ { iso: 'PH', name: 'Philippines', dial: '+63' },
151
+ { iso: 'PL', name: 'Poland', dial: '+48' },
152
+ { iso: 'QA', name: 'Qatar', dial: '+974' },
153
+ { iso: 'RO', name: 'Romania', dial: '+40' },
154
+ { iso: 'RU', name: 'Russia', dial: '+7' },
155
+ { iso: 'RW', name: 'Rwanda', dial: '+250' },
156
+ { iso: 'KN', name: 'Saint Kitts and Nevis', dial: '+1869' },
157
+ { iso: 'LC', name: 'Saint Lucia', dial: '+1758' },
158
+ { iso: 'VC', name: 'Saint Vincent and the Grenadines', dial: '+1784' },
159
+ { iso: 'WS', name: 'Samoa', dial: '+685' },
160
+ { iso: 'SM', name: 'San Marino', dial: '+378' },
161
+ { iso: 'ST', name: 'São Tomé and Príncipe', dial: '+239' },
162
+ { iso: 'SA', name: 'Saudi Arabia', dial: '+966' },
163
+ { iso: 'SN', name: 'Senegal', dial: '+221' },
164
+ { iso: 'RS', name: 'Serbia', dial: '+381' },
165
+ { iso: 'SC', name: 'Seychelles', dial: '+248' },
166
+ { iso: 'SL', name: 'Sierra Leone', dial: '+232' },
167
+ { iso: 'SG', name: 'Singapore', dial: '+65' },
168
+ { iso: 'SK', name: 'Slovakia', dial: '+421' },
169
+ { iso: 'SI', name: 'Slovenia', dial: '+386' },
170
+ { iso: 'SB', name: 'Solomon Islands', dial: '+677' },
171
+ { iso: 'SO', name: 'Somalia', dial: '+252' },
172
+ { iso: 'ZA', name: 'South Africa', dial: '+27' },
173
+ { iso: 'KR', name: 'South Korea', dial: '+82' },
174
+ { iso: 'SS', name: 'South Sudan', dial: '+211' },
175
+ { iso: 'ES', name: 'Spain', dial: '+34' },
176
+ { iso: 'LK', name: 'Sri Lanka', dial: '+94' },
177
+ { iso: 'SD', name: 'Sudan', dial: '+249' },
178
+ { iso: 'SR', name: 'Suriname', dial: '+597' },
179
+ { iso: 'SE', name: 'Sweden', dial: '+46' },
180
+ { iso: 'CH', name: 'Switzerland', dial: '+41' },
181
+ { iso: 'SY', name: 'Syria', dial: '+963' },
182
+ { iso: 'TW', name: 'Taiwan', dial: '+886' },
183
+ { iso: 'TJ', name: 'Tajikistan', dial: '+992' },
184
+ { iso: 'TZ', name: 'Tanzania', dial: '+255' },
185
+ { iso: 'TH', name: 'Thailand', dial: '+66' },
186
+ { iso: 'TL', name: 'Timor-Leste', dial: '+670' },
187
+ { iso: 'TG', name: 'Togo', dial: '+228' },
188
+ { iso: 'TO', name: 'Tonga', dial: '+676' },
189
+ { iso: 'TT', name: 'Trinidad and Tobago', dial: '+1868' },
190
+ { iso: 'TN', name: 'Tunisia', dial: '+216' },
191
+ { iso: 'TR', name: 'Turkey', dial: '+90' },
192
+ { iso: 'TM', name: 'Turkmenistan', dial: '+993' },
193
+ { iso: 'TV', name: 'Tuvalu', dial: '+688' },
194
+ { iso: 'UG', name: 'Uganda', dial: '+256' },
195
+ { iso: 'UA', name: 'Ukraine', dial: '+380' },
196
+ { iso: 'AE', name: 'United Arab Emirates', dial: '+971' },
197
+ { iso: 'GB', name: 'United Kingdom', dial: '+44' },
198
+ { iso: 'UY', name: 'Uruguay', dial: '+598' },
199
+ { iso: 'UZ', name: 'Uzbekistan', dial: '+998' },
200
+ { iso: 'VU', name: 'Vanuatu', dial: '+678' },
201
+ { iso: 'VE', name: 'Venezuela', dial: '+58' },
202
+ { iso: 'VN', name: 'Vietnam', dial: '+84' },
203
+ { iso: 'YE', name: 'Yemen', dial: '+967' },
204
+ { iso: 'ZM', name: 'Zambia', dial: '+260' },
205
+ { iso: 'ZW', name: 'Zimbabwe', dial: '+263' },
206
+ ];
207
+
208
+ // Sorted by dial code length desc for correct prefix matching
209
+ const SORTED_BY_DIAL_LENGTH = [...COUNTRIES].sort(
210
+ (a, b) => b.dial.length - a.dial.length
211
+ );
212
+
213
+ const DEFAULT_COUNTRY = COUNTRIES[0]; // Brasil
214
+
215
+ export function parsePhoneValue(value: string): { country: Country; number: string } {
216
+ if (!value) return { country: DEFAULT_COUNTRY, number: '' };
217
+ for (const country of SORTED_BY_DIAL_LENGTH) {
218
+ const prefix = country.dial + ' ';
219
+ if (value.startsWith(prefix)) {
220
+ return { country, number: value.slice(prefix.length) };
221
+ }
222
+ }
223
+ return { country: DEFAULT_COUNTRY, number: value };
224
+ }