@plexui/react-native 0.2.10 → 0.2.11

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.
@@ -0,0 +1,249 @@
1
+ // Generated from Intl.DisplayNames("en", { type: "region" }) for every region libphonenumber-js knows: the names where the
2
+ // engine has no Intl.DisplayNames (Hermes). Keep in sync with packages/ui/src/components/PhoneInput/countryNames.ts.
3
+ export const COUNTRY_NAMES: Record<string, string> = {
4
+ AC: "Ascension Island",
5
+ AD: "Andorra",
6
+ AE: "United Arab Emirates",
7
+ AF: "Afghanistan",
8
+ AG: "Antigua & Barbuda",
9
+ AI: "Anguilla",
10
+ AL: "Albania",
11
+ AM: "Armenia",
12
+ AO: "Angola",
13
+ AR: "Argentina",
14
+ AS: "American Samoa",
15
+ AT: "Austria",
16
+ AU: "Australia",
17
+ AW: "Aruba",
18
+ AX: "Åland Islands",
19
+ AZ: "Azerbaijan",
20
+ BA: "Bosnia & Herzegovina",
21
+ BB: "Barbados",
22
+ BD: "Bangladesh",
23
+ BE: "Belgium",
24
+ BF: "Burkina Faso",
25
+ BG: "Bulgaria",
26
+ BH: "Bahrain",
27
+ BI: "Burundi",
28
+ BJ: "Benin",
29
+ BL: "St. Barthélemy",
30
+ BM: "Bermuda",
31
+ BN: "Brunei",
32
+ BO: "Bolivia",
33
+ BQ: "Caribbean Netherlands",
34
+ BR: "Brazil",
35
+ BS: "Bahamas",
36
+ BT: "Bhutan",
37
+ BW: "Botswana",
38
+ BY: "Belarus",
39
+ BZ: "Belize",
40
+ CA: "Canada",
41
+ CC: "Cocos (Keeling) Islands",
42
+ CD: "Congo - Kinshasa",
43
+ CF: "Central African Republic",
44
+ CG: "Congo - Brazzaville",
45
+ CH: "Switzerland",
46
+ CI: "Côte d’Ivoire",
47
+ CK: "Cook Islands",
48
+ CL: "Chile",
49
+ CM: "Cameroon",
50
+ CN: "China",
51
+ CO: "Colombia",
52
+ CR: "Costa Rica",
53
+ CU: "Cuba",
54
+ CV: "Cape Verde",
55
+ CW: "Curaçao",
56
+ CX: "Christmas Island",
57
+ CY: "Cyprus",
58
+ CZ: "Czechia",
59
+ DE: "Germany",
60
+ DJ: "Djibouti",
61
+ DK: "Denmark",
62
+ DM: "Dominica",
63
+ DO: "Dominican Republic",
64
+ DZ: "Algeria",
65
+ EC: "Ecuador",
66
+ EE: "Estonia",
67
+ EG: "Egypt",
68
+ EH: "Western Sahara",
69
+ ER: "Eritrea",
70
+ ES: "Spain",
71
+ ET: "Ethiopia",
72
+ FI: "Finland",
73
+ FJ: "Fiji",
74
+ FK: "Falkland Islands",
75
+ FM: "Micronesia",
76
+ FO: "Faroe Islands",
77
+ FR: "France",
78
+ GA: "Gabon",
79
+ GB: "United Kingdom",
80
+ GD: "Grenada",
81
+ GE: "Georgia",
82
+ GF: "French Guiana",
83
+ GG: "Guernsey",
84
+ GH: "Ghana",
85
+ GI: "Gibraltar",
86
+ GL: "Greenland",
87
+ GM: "Gambia",
88
+ GN: "Guinea",
89
+ GP: "Guadeloupe",
90
+ GQ: "Equatorial Guinea",
91
+ GR: "Greece",
92
+ GT: "Guatemala",
93
+ GU: "Guam",
94
+ GW: "Guinea-Bissau",
95
+ GY: "Guyana",
96
+ HK: "Hong Kong SAR China",
97
+ HN: "Honduras",
98
+ HR: "Croatia",
99
+ HT: "Haiti",
100
+ HU: "Hungary",
101
+ ID: "Indonesia",
102
+ IE: "Ireland",
103
+ IL: "Israel",
104
+ IM: "Isle of Man",
105
+ IN: "India",
106
+ IO: "British Indian Ocean Territory",
107
+ IQ: "Iraq",
108
+ IR: "Iran",
109
+ IS: "Iceland",
110
+ IT: "Italy",
111
+ JE: "Jersey",
112
+ JM: "Jamaica",
113
+ JO: "Jordan",
114
+ JP: "Japan",
115
+ KE: "Kenya",
116
+ KG: "Kyrgyzstan",
117
+ KH: "Cambodia",
118
+ KI: "Kiribati",
119
+ KM: "Comoros",
120
+ KN: "St. Kitts & Nevis",
121
+ KP: "North Korea",
122
+ KR: "South Korea",
123
+ KW: "Kuwait",
124
+ KY: "Cayman Islands",
125
+ KZ: "Kazakhstan",
126
+ LA: "Laos",
127
+ LB: "Lebanon",
128
+ LC: "St. Lucia",
129
+ LI: "Liechtenstein",
130
+ LK: "Sri Lanka",
131
+ LR: "Liberia",
132
+ LS: "Lesotho",
133
+ LT: "Lithuania",
134
+ LU: "Luxembourg",
135
+ LV: "Latvia",
136
+ LY: "Libya",
137
+ MA: "Morocco",
138
+ MC: "Monaco",
139
+ MD: "Moldova",
140
+ ME: "Montenegro",
141
+ MF: "St. Martin",
142
+ MG: "Madagascar",
143
+ MH: "Marshall Islands",
144
+ MK: "North Macedonia",
145
+ ML: "Mali",
146
+ MM: "Myanmar (Burma)",
147
+ MN: "Mongolia",
148
+ MO: "Macao SAR China",
149
+ MP: "Northern Mariana Islands",
150
+ MQ: "Martinique",
151
+ MR: "Mauritania",
152
+ MS: "Montserrat",
153
+ MT: "Malta",
154
+ MU: "Mauritius",
155
+ MV: "Maldives",
156
+ MW: "Malawi",
157
+ MX: "Mexico",
158
+ MY: "Malaysia",
159
+ MZ: "Mozambique",
160
+ NA: "Namibia",
161
+ NC: "New Caledonia",
162
+ NE: "Niger",
163
+ NF: "Norfolk Island",
164
+ NG: "Nigeria",
165
+ NI: "Nicaragua",
166
+ NL: "Netherlands",
167
+ NO: "Norway",
168
+ NP: "Nepal",
169
+ NR: "Nauru",
170
+ NU: "Niue",
171
+ NZ: "New Zealand",
172
+ OM: "Oman",
173
+ PA: "Panama",
174
+ PE: "Peru",
175
+ PF: "French Polynesia",
176
+ PG: "Papua New Guinea",
177
+ PH: "Philippines",
178
+ PK: "Pakistan",
179
+ PL: "Poland",
180
+ PM: "St. Pierre & Miquelon",
181
+ PR: "Puerto Rico",
182
+ PS: "Palestinian Territories",
183
+ PT: "Portugal",
184
+ PW: "Palau",
185
+ PY: "Paraguay",
186
+ QA: "Qatar",
187
+ RE: "Réunion",
188
+ RO: "Romania",
189
+ RS: "Serbia",
190
+ RU: "Russia",
191
+ RW: "Rwanda",
192
+ SA: "Saudi Arabia",
193
+ SB: "Solomon Islands",
194
+ SC: "Seychelles",
195
+ SD: "Sudan",
196
+ SE: "Sweden",
197
+ SG: "Singapore",
198
+ SH: "St. Helena",
199
+ SI: "Slovenia",
200
+ SJ: "Svalbard & Jan Mayen",
201
+ SK: "Slovakia",
202
+ SL: "Sierra Leone",
203
+ SM: "San Marino",
204
+ SN: "Senegal",
205
+ SO: "Somalia",
206
+ SR: "Suriname",
207
+ SS: "South Sudan",
208
+ ST: "São Tomé & Príncipe",
209
+ SV: "El Salvador",
210
+ SX: "Sint Maarten",
211
+ SY: "Syria",
212
+ SZ: "Eswatini",
213
+ TA: "Tristan da Cunha",
214
+ TC: "Turks & Caicos Islands",
215
+ TD: "Chad",
216
+ TG: "Togo",
217
+ TH: "Thailand",
218
+ TJ: "Tajikistan",
219
+ TK: "Tokelau",
220
+ TL: "Timor-Leste",
221
+ TM: "Turkmenistan",
222
+ TN: "Tunisia",
223
+ TO: "Tonga",
224
+ TR: "Türkiye",
225
+ TT: "Trinidad & Tobago",
226
+ TV: "Tuvalu",
227
+ TW: "Taiwan",
228
+ TZ: "Tanzania",
229
+ UA: "Ukraine",
230
+ UG: "Uganda",
231
+ US: "United States",
232
+ UY: "Uruguay",
233
+ UZ: "Uzbekistan",
234
+ VA: "Vatican City",
235
+ VC: "St. Vincent & Grenadines",
236
+ VE: "Venezuela",
237
+ VG: "British Virgin Islands",
238
+ VI: "U.S. Virgin Islands",
239
+ VN: "Vietnam",
240
+ VU: "Vanuatu",
241
+ WF: "Wallis & Futuna",
242
+ WS: "Samoa",
243
+ XK: "Kosovo",
244
+ YE: "Yemen",
245
+ YT: "Mayotte",
246
+ ZA: "South Africa",
247
+ ZM: "Zambia",
248
+ ZW: "Zimbabwe",
249
+ }
@@ -0,0 +1,215 @@
1
+ // The logic of a phone number field, the same in @plexui/ui and @plexui/react-native (keep the two copies identical:
2
+ // packages/ui/src/components/PhoneInput/phone.ts). libphonenumber-js's `min` metadata (Google's libphonenumber, the
3
+ // lengths and leading digits of every region; ~37 kB gzipped) formats as you type, reads a pasted or autofilled
4
+ // international number, drops the national trunk prefix (the UK's 0) and checks the length of each region's numbers.
5
+ import {
6
+ AsYouType,
7
+ getCountries,
8
+ getCountryCallingCode,
9
+ parsePhoneNumberFromString,
10
+ validatePhoneNumberLength,
11
+ type CountryCode,
12
+ } from "libphonenumber-js/min"
13
+ import metadata from "libphonenumber-js/min/metadata"
14
+ import { COUNTRY_NAMES } from "./countryNames"
15
+
16
+ export type { CountryCode }
17
+
18
+ /** What the field knows about its number, beside its value. */
19
+ export type PhoneDetails = {
20
+ /** The number in E.164 (`+447700900123`); while it is typed, the digits so far after the code (`+44770`); "" when empty. */
21
+ value: string
22
+ /** The region the number is read in: the picked one, or the one a pasted international number names. */
23
+ country: CountryCode
24
+ /** The number as the field shows it, without the code. */
25
+ text: string
26
+ /** The number's length fits its region (it could be dialled). */
27
+ possible: boolean
28
+ /** The number fits its region's lengths and leading digits. */
29
+ valid: boolean
30
+ }
31
+
32
+ /** Every region with a calling code, as ISO 3166-1 alpha-2. */
33
+ export const ALL_COUNTRIES: readonly CountryCode[] = getCountries()
34
+
35
+ /** The calling code of a region, with its plus: `+44`. */
36
+ export function dialCode(country: CountryCode): string {
37
+ return `+${getCountryCallingCode(country)}`
38
+ }
39
+
40
+ /** The regions that share a calling code, its main region first (+1: the US, then Canada and the Caribbean). */
41
+ export function countriesForCode(code: string): CountryCode[] {
42
+ const list = (metadata as { country_calling_codes: Record<string, string[]> }).country_calling_codes[code]
43
+ return (list ?? []) as CountryCode[]
44
+ }
45
+
46
+ /** Whether a string is a region the metadata knows. */
47
+ export function isCountry(value: unknown): value is CountryCode {
48
+ return typeof value === "string" && (ALL_COUNTRIES as readonly string[]).includes(value)
49
+ }
50
+
51
+ let displayNames: { locale: string; names: { of: (code: string) => string | undefined } | null } | null = null
52
+
53
+ /** A region's name in the locale (Intl.DisplayNames), in English where the engine has no Intl.DisplayNames. */
54
+ export function countryName(country: CountryCode, locale?: string): string {
55
+ const key = locale ?? ""
56
+ if (!displayNames || displayNames.locale !== key) {
57
+ let names: { of: (code: string) => string | undefined } | null = null
58
+ try {
59
+ const DisplayNames = (Intl as unknown as { DisplayNames?: new (locales: string | undefined, options: { type: "region" }) => { of: (code: string) => string | undefined } }).DisplayNames
60
+ names = DisplayNames ? new DisplayNames(locale, { type: "region" }) : null
61
+ } catch {
62
+ names = null
63
+ }
64
+ displayNames = { locale: key, names }
65
+ }
66
+ const name = displayNames.names?.of(country)
67
+ return name && name !== country ? name : (COUNTRY_NAMES[country] ?? country)
68
+ }
69
+
70
+ /** The flag of a region, as its two regional indicator symbols. */
71
+ export function countryFlag(country: CountryCode): string {
72
+ return String.fromCodePoint(...[...country.toUpperCase()].map((letter) => 0x1f1e6 + letter.charCodeAt(0) - 65))
73
+ }
74
+
75
+ /** The device's region, from the locale the engine runs in (`en-GB` → GB); undefined when the locale names none. */
76
+ export function deviceCountry(): CountryCode | undefined {
77
+ try {
78
+ const locale = new Intl.DateTimeFormat().resolvedOptions().locale
79
+ const region = (Intl as unknown as { Locale?: new (tag: string) => { region?: string } }).Locale
80
+ ? new (Intl as unknown as { Locale: new (tag: string) => { region?: string } }).Locale(locale).region
81
+ : locale.split(/[-_]/).find((part, index) => index > 0 && /^[A-Z]{2}$/.test(part))
82
+ return isCountry(region) ? region : undefined
83
+ } catch {
84
+ return undefined
85
+ }
86
+ }
87
+
88
+ /** Folds a name for search: lower case, no accents (`Côte d’Ivoire` → `cote d’ivoire`). */
89
+ export function fold(text: string): string {
90
+ return text.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase()
91
+ }
92
+
93
+ /** Whether a region answers a search: by its name (any word's start), its ISO code or its calling code (`44`, `+44`). */
94
+ export function matchesCountry(country: CountryCode, name: string, query: string): boolean {
95
+ const q = fold(query.trim())
96
+ if (!q) return true
97
+ const digits = q.replace(/^\+/, "")
98
+ if (/^\d+$/.test(digits)) return String(getCountryCallingCode(country)).startsWith(digits)
99
+ if (q.length === 2 && country.toLowerCase() === q) return true
100
+ const folded = fold(name)
101
+ return folded.startsWith(q) || folded.includes(` ${q}`) || folded.includes(`(${q}`) || folded.includes(`-${q}`)
102
+ }
103
+
104
+ /** The characters a phone number may be typed with. */
105
+ const ALLOWED = /[^\d+\s().-]/g
106
+
107
+ /** Whether the text is an international number: a plus, or 00 (the international prefix most regions dial). */
108
+ function international(text: string): boolean {
109
+ const trimmed = text.replace(/[\s().-]/g, "")
110
+ return trimmed.startsWith("+") || trimmed.startsWith("00")
111
+ }
112
+
113
+ /** The digits of a text, in order. */
114
+ export function digitsOf(text: string): string {
115
+ return text.replace(/\D/g, "")
116
+ }
117
+
118
+ /** The number's details for its national digits in a region; `format` groups the digits as the region writes them. */
119
+ export function readNational(digits: string, country: CountryCode, format: boolean): PhoneDetails {
120
+ if (!digits) return { value: "", country, text: "", possible: false, valid: false }
121
+ const typer = new AsYouType(country)
122
+ const formatted = typer.input(digits)
123
+ const number = typer.getNumber()
124
+ const value = typer.getNumberValue() ?? `${dialCode(country)}${digits}`
125
+ return {
126
+ value,
127
+ country,
128
+ text: format ? formatted : digits,
129
+ possible: number?.isPossible() ?? false,
130
+ valid: number?.isValid() ?? false,
131
+ }
132
+ }
133
+
134
+ /** Whether national digits are longer than any number of the region (the field takes no more). */
135
+ export function tooLong(digits: string, country: CountryCode): boolean {
136
+ if (!digits) return false
137
+ const value = new AsYouType(country)
138
+ value.input(digits)
139
+ return tooLongValue(value.getNumberValue() ?? "")
140
+ }
141
+
142
+ /** Whether a value in E.164 is longer than any number of its region. */
143
+ export function tooLongValue(value: string): boolean {
144
+ return value.length > 2 && validatePhoneNumberLength(value) === "TOO_LONG"
145
+ }
146
+
147
+ /** The calling code a `+digits` text starts with (calling codes are prefix-free), and the digits after it. */
148
+ function splitCode(digits: string): { code: string; rest: string } | null {
149
+ for (let length = 1; length <= 3 && length <= digits.length; length++) {
150
+ const code = digits.slice(0, length)
151
+ if (countriesForCode(code).length) return { code, rest: digits.slice(length) }
152
+ }
153
+ return null
154
+ }
155
+
156
+ /** The region of a calling code: the picked one when it shares the code, else the code's main region. */
157
+ function regionFor(code: string, country: CountryCode, named?: CountryCode): CountryCode {
158
+ const shared = countriesForCode(code)
159
+ return shared.includes(country) ? country : (named ?? shared[0] ?? country)
160
+ }
161
+
162
+ /**
163
+ * What the number field holds after a change: a typed national number in the picked region, or a pasted or autofilled
164
+ * international one (`+44 7700 900123`, `0044…`), whose code picks the region (the picked region stays when it shares
165
+ * the code and the number fits it: +1 in Canada) and is dropped from the field.
166
+ */
167
+ export function readInput(text: string, country: CountryCode, format: boolean): PhoneDetails {
168
+ const clean = text.replace(ALLOWED, "")
169
+ if (international(clean)) {
170
+ const plus = clean.replace(/[\s().-]/g, "").replace(/^00/, "+")
171
+ const parsed = parsePhoneNumberFromString(plus)
172
+ if (parsed) {
173
+ const code = String(parsed.countryCallingCode)
174
+ const national = String(parsed.nationalNumber)
175
+ const fits = countriesForCode(code).includes(country) && parsePhoneNumberFromString(national, country)?.isPossible()
176
+ // The picked region stays where it shares the code and the number fits it (+1 in Canada); else the number's own
177
+ const region = fits ? country : (parsed.country ?? countriesForCode(code)[0] ?? country)
178
+ // The field holds the number as the region dials it, with its trunk prefix (07400 123456, 8 812 123-45-67): read
179
+ // again it gives the same number (Russia's 812 alone would lose its 8 as a trunk prefix)
180
+ const dialled = parsed.formatNational()
181
+ return { value: parsed.number, country: region, text: format ? dialled : digitsOf(dialled), possible: parsed.isPossible(), valid: parsed.isValid() }
182
+ }
183
+ // Too short to parse: a known code with digits after it (+447) is that code's region and those digits; a code
184
+ // still being typed (+4) stays as it is
185
+ const split = splitCode(digitsOf(plus))
186
+ if (split && split.rest) {
187
+ const region = regionFor(split.code, country)
188
+ return { value: `+${split.code}${split.rest}`, country: region, text: split.rest, possible: false, valid: false }
189
+ }
190
+ return { value: "", country, text: clean, possible: false, valid: false }
191
+ }
192
+ return readNational(digitsOf(clean), country, format)
193
+ }
194
+
195
+ /** The field's details for a value in E.164 (or the digits so far after a code), read in a region. */
196
+ export function readValue(value: string | undefined, country: CountryCode, format: boolean): PhoneDetails {
197
+ if (!value) return { value: "", country, text: "", possible: false, valid: false }
198
+ return readInput(value.startsWith("+") ? value : `+${value}`, country, format)
199
+ }
200
+
201
+ /** The caret's place after `count` digits in a formatted text (just after the count-th digit). */
202
+ export function caretAfterDigits(text: string, count: number): number {
203
+ if (count <= 0) {
204
+ const first = text.search(/\d/)
205
+ return first < 0 ? text.length : first
206
+ }
207
+ let seen = 0
208
+ for (let index = 0; index < text.length; index++) {
209
+ if (/\d/.test(text[index]!)) {
210
+ seen++
211
+ if (seen === count) return index + 1
212
+ }
213
+ }
214
+ return text.length
215
+ }
package/src/index.ts CHANGED
@@ -79,6 +79,7 @@ export {
79
79
  } from "./components/InputGroup/InputGroup"
80
80
  export { TagInput, type Tag, type TagInputProps } from "./components/TagInput/TagInput"
81
81
  export { OTPInput, type OTPInputProps } from "./components/OTPInput/OTPInput"
82
+ export { PhoneInput, type CountryCode, type PhoneDetails, type PhoneInputProps } from "./components/PhoneInput/PhoneInput"
82
83
  export { Select, type SelectOption, type SelectProps } from "./components/Select/Select"
83
84
  export { Menu, type MenuContentProps, type MenuItemProps, type MenuProps } from "./components/Menu/Menu"
84
85
  export { ContextMenu, type ContextMenuContentProps, type ContextMenuProps } from "./components/ContextMenu/ContextMenu"