@10x-media/fields 0.1.0-beta.7 → 0.1.0-beta.8

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.
Files changed (93) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/LICENSE +21 -21
  3. package/dist/exports/client.d.ts +4 -1
  4. package/dist/exports/client.js +4 -1
  5. package/dist/exports/phone-utils.d.ts +4 -0
  6. package/dist/exports/phone-utils.js +4 -0
  7. package/dist/exports/phone.d.ts +6 -0
  8. package/dist/exports/phone.js +3 -0
  9. package/dist/exports/rsc.d.ts +3 -1
  10. package/dist/exports/rsc.js +3 -1
  11. package/dist/exports/types.d.ts +3 -2
  12. package/dist/fields/icon/rsc/IconFieldServer.js +1 -1
  13. package/dist/fields/icon/rsc/IconFieldServer.js.map +1 -1
  14. package/dist/fields/icon/rsc/IconLibrarySelectServer.js +1 -1
  15. package/dist/fields/icon/rsc/IconLibrarySelectServer.js.map +1 -1
  16. package/dist/fields/icon/shared/iconLabel.js +1 -1
  17. package/dist/fields/icon/shared/iconLabel.js.map +1 -1
  18. package/dist/fields/phoneNumber/client/CountryFlag.d.ts +16 -0
  19. package/dist/fields/phoneNumber/client/CountryFlag.js +40 -0
  20. package/dist/fields/phoneNumber/client/CountryFlag.js.map +1 -0
  21. package/dist/fields/phoneNumber/client/CountryPicker.d.ts +21 -0
  22. package/dist/fields/phoneNumber/client/CountryPicker.js +255 -0
  23. package/dist/fields/phoneNumber/client/CountryPicker.js.map +1 -0
  24. package/dist/fields/phoneNumber/client/PhoneNumberField.d.ts +26 -0
  25. package/dist/fields/phoneNumber/client/PhoneNumberField.js +318 -0
  26. package/dist/fields/phoneNumber/client/PhoneNumberField.js.map +1 -0
  27. package/dist/fields/phoneNumber/client/editModel.js +86 -0
  28. package/dist/fields/phoneNumber/client/editModel.js.map +1 -0
  29. package/dist/fields/phoneNumber/client/phoneNumberField.css +305 -0
  30. package/dist/fields/phoneNumber/engine/countries.d.ts +36 -0
  31. package/dist/fields/phoneNumber/engine/countries.js +68 -0
  32. package/dist/fields/phoneNumber/engine/countries.js.map +1 -0
  33. package/dist/fields/phoneNumber/engine/draft.d.ts +13 -0
  34. package/dist/fields/phoneNumber/engine/draft.js +72 -0
  35. package/dist/fields/phoneNumber/engine/draft.js.map +1 -0
  36. package/dist/fields/phoneNumber/engine/metadata.d.ts +11 -0
  37. package/dist/fields/phoneNumber/engine/metadata.js +32 -0
  38. package/dist/fields/phoneNumber/engine/metadata.js.map +1 -0
  39. package/dist/fields/phoneNumber/engine/phone.d.ts +37 -0
  40. package/dist/fields/phoneNumber/engine/phone.js +59 -0
  41. package/dist/fields/phoneNumber/engine/phone.js.map +1 -0
  42. package/dist/fields/phoneNumber/options.d.ts +72 -0
  43. package/dist/fields/phoneNumber/options.js +37 -0
  44. package/dist/fields/phoneNumber/options.js.map +1 -0
  45. package/dist/fields/phoneNumber/phoneNumberField.d.ts +14 -0
  46. package/dist/fields/phoneNumber/phoneNumberField.js +167 -0
  47. package/dist/fields/phoneNumber/phoneNumberField.js.map +1 -0
  48. package/dist/fields/phoneNumber/server/PhoneNumberCellServer.d.ts +19 -0
  49. package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js +73 -0
  50. package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js.map +1 -0
  51. package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.d.ts +16 -0
  52. package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js +57 -0
  53. package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js.map +1 -0
  54. package/dist/fields/phoneNumber/server/flagsEndpoint.js +51 -0
  55. package/dist/fields/phoneNumber/server/flagsEndpoint.js.map +1 -0
  56. package/dist/fields/phoneNumber/server/readPhoneOptions.js +18 -0
  57. package/dist/fields/phoneNumber/server/readPhoneOptions.js.map +1 -0
  58. package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js +33 -0
  59. package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js.map +1 -0
  60. package/dist/index.d.ts +4 -3
  61. package/dist/index.js +17 -0
  62. package/dist/index.js.map +1 -1
  63. package/dist/translations/ar.js +9 -1
  64. package/dist/translations/ar.js.map +1 -1
  65. package/dist/translations/de.js +9 -1
  66. package/dist/translations/de.js.map +1 -1
  67. package/dist/translations/en.js +9 -1
  68. package/dist/translations/en.js.map +1 -1
  69. package/dist/translations/es.js +9 -1
  70. package/dist/translations/es.js.map +1 -1
  71. package/dist/translations/fr.js +9 -1
  72. package/dist/translations/fr.js.map +1 -1
  73. package/dist/translations/id.js +9 -1
  74. package/dist/translations/id.js.map +1 -1
  75. package/dist/translations/keys.d.ts +8 -0
  76. package/dist/translations/keys.js +9 -1
  77. package/dist/translations/keys.js.map +1 -1
  78. package/dist/translations/ko.js +9 -1
  79. package/dist/translations/ko.js.map +1 -1
  80. package/dist/translations/pt.js +9 -1
  81. package/dist/translations/pt.js.map +1 -1
  82. package/dist/translations/ru.js +9 -1
  83. package/dist/translations/ru.js.map +1 -1
  84. package/dist/translations/uk.js +9 -1
  85. package/dist/translations/uk.js.map +1 -1
  86. package/dist/translations/zh.js +9 -1
  87. package/dist/translations/zh.js.map +1 -1
  88. package/dist/types.d.ts +22 -1
  89. package/dist/utils/resolveStaticLabel.js +13 -0
  90. package/dist/utils/resolveStaticLabel.js.map +1 -0
  91. package/package.json +15 -3
  92. package/dist/fields/icon/shared/resolveStaticLabel.js +0 -16
  93. package/dist/fields/icon/shared/resolveStaticLabel.js.map +0 -1
@@ -0,0 +1,26 @@
1
+ import { ResolvedPhoneOptions } from "../options.js";
2
+ import { PhoneSeed } from "../engine/draft.js";
3
+ import { GroupFieldClientProps, TextFieldClientProps } from "payload";
4
+ import React from "react";
5
+ //#region src/fields/phoneNumber/client/PhoneNumberField.d.ts
6
+ /**
7
+ * `cellFormat` and `validation` are read only on the server, so they are neither shipped
8
+ * nor readable here: reading `validation` client-side would state a verdict the server
9
+ * has not reached.
10
+ */
11
+ type PhoneFieldClientOptions = Omit<ResolvedPhoneOptions, 'cellFormat' | 'validation'>;
12
+ /** A group under object storage, a text field under e164; the row is identical either way. */
13
+ type PhoneNumberFieldProps = {
14
+ field: GroupFieldClientProps['field'] | TextFieldClientProps['field'];
15
+ phoneOptions: PhoneFieldClientOptions; /** The stored row as the server already split it, painted until metadata lands. */
16
+ seed?: null | PhoneSeed;
17
+ } & Omit<GroupFieldClientProps, 'field'>;
18
+ /**
19
+ * One 40px row holding the country trigger, the calling-code prefix, a borderless entry and
20
+ * a clear affordance. `phoneOptions` must already be resolved through `resolvePhoneOptionsSafe`:
21
+ * the field's own stamped layer carries no defaults and would render the wrong chrome.
22
+ */
23
+ declare const PhoneNumberField: React.FC<PhoneNumberFieldProps>;
24
+ //#endregion
25
+ export { PhoneNumberField, PhoneNumberFieldProps };
26
+ //# sourceMappingURL=PhoneNumberField.d.ts.map
@@ -0,0 +1,318 @@
1
+ "use client";
2
+ import { callingCodeFor, countryOptions } from "../engine/countries.js";
3
+ import { loadMetadata } from "../engine/metadata.js";
4
+ import { keys } from "../../../translations/keys.js";
5
+ import { useTranslation as useTranslation$1 } from "../../../translations/useTranslation.js";
6
+ import { resolveStaticLabel } from "../../../utils/resolveStaticLabel.js";
7
+ import { CountryPicker } from "./CountryPicker.js";
8
+ import { detectCountry, parsePhone } from "../engine/phone.js";
9
+ import { isInternational, provisionalCountry } from "../engine/draft.js";
10
+ import { displayFor, dropCallingCode, formatDraft, resolveCommit } from "./editModel.js";
11
+ import { FieldDescription, FieldError, FieldLabel, RenderCustomComponent, XIcon, fieldBaseClass, useField, useForm, useFormFields } from "@payloadcms/ui";
12
+ import { mergeFieldStyles } from "@payloadcms/ui/shared";
13
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
14
+ import { jsx, jsxs } from "react/jsx-runtime";
15
+ import "./phoneNumberField.css";
16
+ //#region src/fields/phoneNumber/client/PhoneNumberField.tsx
17
+ const baseClass = "fields-phone";
18
+ const COMMIT_DELAY = 250;
19
+ const NO_COUNTRIES = {
20
+ priority: [],
21
+ rest: []
22
+ };
23
+ /**
24
+ * One 40px row holding the country trigger, the calling-code prefix, a borderless entry and
25
+ * a clear affordance. `phoneOptions` must already be resolved through `resolvePhoneOptionsSafe`:
26
+ * the field's own stamped layer carries no defaults and would render the wrong chrome.
27
+ */
28
+ const PhoneNumberField = (props) => {
29
+ const { field, path, phoneOptions, readOnly: readOnlyFromProps, seed } = props;
30
+ const { admin: { className, description } = {}, label, localized } = field;
31
+ const passthrough = field;
32
+ const { placeholder, readOnly: readOnlyFromAdmin } = passthrough.admin ?? {};
33
+ const required = passthrough.required ?? false;
34
+ const { countries, defaultCountry, flags, isClearable, metadata: metadataSet, priorityCountries, priorityCountriesLabel, storage } = phoneOptions;
35
+ const { i18n, t } = useTranslation$1();
36
+ const { dispatchFields, setModified } = useForm();
37
+ const { customComponents: { AfterInput, BeforeInput, Description, Error: ErrorComponent, Label } = {}, disabled, errorPaths, formSubmitted, setValue, showError, value } = useField({ path });
38
+ const numberPath = `${path}.number`;
39
+ const countryPath = `${path}.country`;
40
+ const numberState = useFormFields(([fields]) => fields?.[numberPath]?.value);
41
+ const countryState = useFormFields(([fields]) => fields?.[countryPath]?.value);
42
+ const [metadata, setMetadata] = useState(null);
43
+ useEffect(() => {
44
+ let active = true;
45
+ loadMetadata(metadataSet).then((loaded) => {
46
+ if (active) setMetadata(loaded);
47
+ }).catch(() => void 0);
48
+ return () => {
49
+ active = false;
50
+ };
51
+ }, [metadataSet]);
52
+ const isE164 = storage === "e164";
53
+ const [pickedCountry, setPickedCountry] = useState(void 0);
54
+ const rawStored = isE164 ? value : numberState;
55
+ const storedNumber = typeof rawStored === "string" ? rawStored : "";
56
+ const storedCountry = useMemo(() => {
57
+ if (!isE164) return typeof countryState === "string" ? countryState : void 0;
58
+ if (!metadata || storedNumber === "") return void 0;
59
+ return parsePhone(storedNumber, {
60
+ defaultCountry: pickedCountry ?? defaultCountry,
61
+ metadata
62
+ })?.country;
63
+ }, [
64
+ countryState,
65
+ defaultCountry,
66
+ isE164,
67
+ metadata,
68
+ pickedCountry,
69
+ storedNumber
70
+ ]);
71
+ const seeded = metadata === null && seed?.number === storedNumber ? seed : null;
72
+ const display = useMemo(() => seeded?.national ?? displayFor({
73
+ country: storedCountry,
74
+ number: storedNumber || null
75
+ }, metadata), [
76
+ metadata,
77
+ seeded,
78
+ storedCountry,
79
+ storedNumber
80
+ ]);
81
+ const [draft, setDraft] = useState(display);
82
+ const editingRef = useRef(false);
83
+ useEffect(() => {
84
+ if (!editingRef.current) setDraft(display);
85
+ }, [display]);
86
+ const country = useMemo(() => metadata && isInternational(draft) ? detectCountry(draft, { metadata }) ?? provisionalCountry(draft, { metadata }) : void 0, [draft, metadata]) ?? pickedCountry ?? storedCountry ?? seeded?.country ?? defaultCountry;
87
+ const countriesKey = countries?.join();
88
+ const priorityKey = priorityCountries?.join();
89
+ const options = useMemo(() => metadata ? countryOptions({
90
+ countries,
91
+ locale: i18n.language,
92
+ metadata,
93
+ priorityCountries
94
+ }) : NO_COUNTRIES, [
95
+ countriesKey,
96
+ i18n.language,
97
+ metadata,
98
+ priorityKey
99
+ ]);
100
+ const callingCode = useMemo(() => (country && metadata ? callingCodeFor(country, metadata) : void 0) ?? seeded?.callingCode, [
101
+ country,
102
+ metadata,
103
+ seeded
104
+ ]);
105
+ const lastValidRef = useRef(null);
106
+ useEffect(() => {
107
+ if (storedNumber === "" || !metadata) return;
108
+ if (parsePhone(storedNumber, {
109
+ defaultCountry: storedCountry,
110
+ metadata
111
+ })?.valid) lastValidRef.current = {
112
+ country: storedCountry,
113
+ number: storedNumber
114
+ };
115
+ }, [
116
+ metadata,
117
+ storedCountry,
118
+ storedNumber
119
+ ]);
120
+ const write = useCallback((entry) => {
121
+ if (isE164) {
122
+ setValue(entry.number);
123
+ return;
124
+ }
125
+ dispatchFields({
126
+ type: "UPDATE",
127
+ path: numberPath,
128
+ value: entry.number
129
+ });
130
+ dispatchFields({
131
+ type: "UPDATE",
132
+ path: countryPath,
133
+ value: entry.country ?? null
134
+ });
135
+ setModified(true);
136
+ }, [
137
+ countryPath,
138
+ dispatchFields,
139
+ isE164,
140
+ numberPath,
141
+ setModified,
142
+ setValue
143
+ ]);
144
+ const commit = useCallback((raw, opts = {}) => {
145
+ const { derived, ...entry } = resolveCommit({
146
+ country: opts.country ?? country,
147
+ draft: raw,
148
+ isClearable,
149
+ lastValid: lastValidRef.current,
150
+ metadata,
151
+ picked: opts.country !== void 0,
152
+ salvage: opts.salvage === true
153
+ });
154
+ if (derived) setPickedCountry(void 0);
155
+ write(entry);
156
+ return entry;
157
+ }, [
158
+ country,
159
+ isClearable,
160
+ metadata,
161
+ write
162
+ ]);
163
+ const debounceRef = useRef(null);
164
+ const cancelPending = useCallback(() => {
165
+ if (debounceRef.current) clearTimeout(debounceRef.current);
166
+ debounceRef.current = null;
167
+ }, []);
168
+ useEffect(() => cancelPending, [cancelPending]);
169
+ const onChange = useCallback((event) => {
170
+ const input = event.target;
171
+ const raw = input.value;
172
+ const caret = input.selectionStart;
173
+ const next = formatDraft({
174
+ atEnd: caret === null || caret === raw.length,
175
+ callingCode,
176
+ country,
177
+ metadata,
178
+ previous: draft,
179
+ raw
180
+ });
181
+ if (next === null) {
182
+ const at = Math.max((caret ?? raw.length) - (raw.length - draft.length), 0);
183
+ input.value = draft;
184
+ input.setSelectionRange(at, at);
185
+ return;
186
+ }
187
+ editingRef.current = true;
188
+ setDraft(next);
189
+ cancelPending();
190
+ debounceRef.current = setTimeout(() => commit(next), COMMIT_DELAY);
191
+ }, [
192
+ callingCode,
193
+ cancelPending,
194
+ commit,
195
+ country,
196
+ draft,
197
+ metadata
198
+ ]);
199
+ const onBlur = useCallback(() => {
200
+ cancelPending();
201
+ if (!editingRef.current) return;
202
+ editingRef.current = false;
203
+ setDraft(displayFor(commit(draft, { salvage: true }), metadata));
204
+ }, [
205
+ cancelPending,
206
+ commit,
207
+ draft,
208
+ metadata
209
+ ]);
210
+ const selectCountry = useCallback((code) => {
211
+ cancelPending();
212
+ editingRef.current = false;
213
+ setPickedCountry(code);
214
+ setDraft(displayFor(commit(dropCallingCode(draft, metadata), { country: code }), metadata));
215
+ }, [
216
+ cancelPending,
217
+ commit,
218
+ draft,
219
+ metadata
220
+ ]);
221
+ const inputRef = useRef(null);
222
+ const onClear = useCallback(() => {
223
+ cancelPending();
224
+ editingRef.current = false;
225
+ setPickedCountry(void 0);
226
+ setDraft("");
227
+ write({
228
+ country: void 0,
229
+ number: null
230
+ });
231
+ inputRef.current?.focus();
232
+ }, [cancelPending, write]);
233
+ const isReadOnly = Boolean(readOnlyFromProps || disabled || readOnlyFromAdmin);
234
+ const hasError = showError || formSubmitted && (errorPaths?.length ?? 0) > 0;
235
+ const showClear = isClearable && !required && !isReadOnly && (draft !== "" || storedNumber !== "");
236
+ const showPrefix = callingCode !== void 0 && !isInternational(draft);
237
+ const styles = useMemo(() => mergeFieldStyles(field), [field]);
238
+ return /* @__PURE__ */ jsxs("div", {
239
+ className: [
240
+ fieldBaseClass,
241
+ baseClass,
242
+ className,
243
+ hasError && "error",
244
+ isReadOnly && "read-only"
245
+ ].filter(Boolean).join(" "),
246
+ style: styles,
247
+ children: [/* @__PURE__ */ jsx(RenderCustomComponent, {
248
+ CustomComponent: Label,
249
+ Fallback: /* @__PURE__ */ jsx(FieldLabel, {
250
+ label,
251
+ localized,
252
+ path,
253
+ required
254
+ })
255
+ }), /* @__PURE__ */ jsxs("div", {
256
+ className: `${fieldBaseClass}__wrap`,
257
+ children: [
258
+ /* @__PURE__ */ jsx(RenderCustomComponent, {
259
+ CustomComponent: ErrorComponent,
260
+ Fallback: /* @__PURE__ */ jsx(FieldError, {
261
+ path,
262
+ showError: hasError
263
+ })
264
+ }),
265
+ BeforeInput,
266
+ /* @__PURE__ */ jsxs("div", {
267
+ className: `${baseClass}__container`,
268
+ children: [
269
+ /* @__PURE__ */ jsx(CountryPicker, {
270
+ disabled: isReadOnly || metadata === null,
271
+ flags,
272
+ onSelect: selectCountry,
273
+ options,
274
+ priorityLabel: resolveStaticLabel(priorityCountriesLabel, i18n.language),
275
+ value: country
276
+ }),
277
+ showPrefix ? /* @__PURE__ */ jsx("span", {
278
+ className: `${baseClass}__prefix`,
279
+ children: `+${callingCode}`
280
+ }) : null,
281
+ /* @__PURE__ */ jsx("input", {
282
+ className: `${baseClass}__input`,
283
+ id: `field-${path.replace(/\./g, "__")}`,
284
+ inputMode: "tel",
285
+ name: path,
286
+ onBlur,
287
+ onChange,
288
+ placeholder: resolveStaticLabel(placeholder, i18n.language),
289
+ readOnly: isReadOnly,
290
+ ref: inputRef,
291
+ type: "tel",
292
+ value: draft
293
+ }),
294
+ showClear ? /* @__PURE__ */ jsx("button", {
295
+ "aria-label": t(keys.clearPhoneNumber),
296
+ className: `${baseClass}__clear`,
297
+ onClick: onClear,
298
+ type: "button",
299
+ children: /* @__PURE__ */ jsx(XIcon, {})
300
+ }) : null
301
+ ]
302
+ }),
303
+ AfterInput,
304
+ /* @__PURE__ */ jsx(RenderCustomComponent, {
305
+ CustomComponent: Description,
306
+ Fallback: /* @__PURE__ */ jsx(FieldDescription, {
307
+ description,
308
+ path
309
+ })
310
+ })
311
+ ]
312
+ })]
313
+ });
314
+ };
315
+ //#endregion
316
+ export { PhoneNumberField };
317
+
318
+ //# sourceMappingURL=PhoneNumberField.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PhoneNumberField.js","names":["useTranslation"],"sources":["../../../../src/fields/phoneNumber/client/PhoneNumberField.tsx"],"sourcesContent":["'use client'\n\nimport {\n\tFieldDescription,\n\tFieldError,\n\tFieldLabel,\n\tfieldBaseClass,\n\tRenderCustomComponent,\n\tuseField,\n\tuseForm,\n\tuseFormFields,\n\tXIcon,\n} from '@payloadcms/ui'\nimport { mergeFieldStyles } from '@payloadcms/ui/shared'\nimport type { GroupFieldClientProps, StaticLabel, TextFieldClientProps } from 'payload'\nimport type React from 'react'\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react'\nimport { keys } from '../../../translations/keys'\nimport { useTranslation } from '../../../translations/useTranslation'\nimport { resolveStaticLabel } from '../../../utils/resolveStaticLabel'\nimport { callingCodeFor, countryOptions } from '../engine/countries'\nimport { isInternational, type PhoneSeed, provisionalCountry } from '../engine/draft'\nimport { loadMetadata, type PhoneMetadata } from '../engine/metadata'\nimport { type CountryCode, detectCountry, parsePhone } from '../engine/phone'\nimport type { ResolvedPhoneOptions } from '../options'\nimport { type CountryOptionGroups, CountryPicker } from './CountryPicker'\nimport {\n\tdisplayFor,\n\tdropCallingCode,\n\tformatDraft,\n\ttype PhoneEntry,\n\tresolveCommit,\n} from './editModel'\nimport './phoneNumberField.css'\n\nconst baseClass = 'fields-phone'\nconst COMMIT_DELAY = 250\nconst NO_COUNTRIES: CountryOptionGroups = { priority: [], rest: [] }\n\n/**\n * Payload copies `required` and `admin.placeholder` onto the client field at runtime, but\n * `GroupFieldClient` types neither; the e164 variant is a `TextFieldClient` and types both.\n */\ntype PhonePassthrough = {\n\tadmin?: { placeholder?: StaticLabel; readOnly?: boolean }\n\trequired?: boolean\n}\n\n/**\n * `cellFormat` and `validation` are read only on the server, so they are neither shipped\n * nor readable here: reading `validation` client-side would state a verdict the server\n * has not reached.\n */\nexport type PhoneFieldClientOptions = Omit<ResolvedPhoneOptions, 'cellFormat' | 'validation'>\n\n/** A group under object storage, a text field under e164; the row is identical either way. */\nexport type PhoneNumberFieldProps = {\n\tfield: GroupFieldClientProps['field'] | TextFieldClientProps['field']\n\tphoneOptions: PhoneFieldClientOptions\n\t/** The stored row as the server already split it, painted until metadata lands. */\n\tseed?: null | PhoneSeed\n} & Omit<GroupFieldClientProps, 'field'>\n\n/**\n * One 40px row holding the country trigger, the calling-code prefix, a borderless entry and\n * a clear affordance. `phoneOptions` must already be resolved through `resolvePhoneOptionsSafe`:\n * the field's own stamped layer carries no defaults and would render the wrong chrome.\n */\nexport const PhoneNumberField: React.FC<PhoneNumberFieldProps> = (props) => {\n\tconst { field, path, phoneOptions, readOnly: readOnlyFromProps, seed } = props\n\tconst { admin: { className, description } = {}, label, localized } = field\n\tconst passthrough: PhonePassthrough = field\n\tconst { placeholder, readOnly: readOnlyFromAdmin } = passthrough.admin ?? {}\n\tconst required = passthrough.required ?? false\n\tconst {\n\t\tcountries,\n\t\tdefaultCountry,\n\t\tflags,\n\t\tisClearable,\n\t\tmetadata: metadataSet,\n\t\tpriorityCountries,\n\t\tpriorityCountriesLabel,\n\t\tstorage,\n\t} = phoneOptions\n\n\tconst { i18n, t } = useTranslation()\n\tconst { dispatchFields, setModified } = useForm()\n\tconst {\n\t\tcustomComponents: { AfterInput, BeforeInput, Description, Error: ErrorComponent, Label } = {},\n\t\tdisabled,\n\t\terrorPaths,\n\t\tformSubmitted,\n\t\tsetValue,\n\t\tshowError,\n\t\tvalue,\n\t} = useField<null | string>({ path })\n\n\t// Form state is flat by path: the group entry carries no value, so each subfield is read\n\t// at its own path and written there too.\n\tconst numberPath = `${path}.number`\n\tconst countryPath = `${path}.country`\n\tconst numberState = useFormFields(([fields]) => fields?.[numberPath]?.value)\n\tconst countryState = useFormFields(([fields]) => fields?.[countryPath]?.value)\n\n\tconst [metadata, setMetadata] = useState<null | PhoneMetadata>(null)\n\tuseEffect(() => {\n\t\tlet active = true\n\t\tloadMetadata(metadataSet)\n\t\t\t.then((loaded) => {\n\t\t\t\tif (active) setMetadata(loaded)\n\t\t\t})\n\t\t\t// A failed load degrades to unformatted entry; loadMetadata retries on a later mount\n\t\t\t.catch(() => undefined)\n\t\treturn () => {\n\t\t\tactive = false\n\t\t}\n\t}, [metadataSet])\n\n\tconst isE164 = storage === 'e164'\n\tconst [pickedCountry, setPickedCountry] = useState<CountryCode | undefined>(undefined)\n\n\tconst rawStored = isE164 ? value : numberState\n\tconst storedNumber = typeof rawStored === 'string' ? rawStored : ''\n\n\t// e164 storage has no country subfield, so the country is read back out of the number\n\tconst storedCountry = useMemo<CountryCode | undefined>(() => {\n\t\tif (!isE164) return typeof countryState === 'string' ? (countryState as CountryCode) : undefined\n\t\tif (!metadata || storedNumber === '') return undefined\n\t\treturn parsePhone(storedNumber, { defaultCountry: pickedCountry ?? defaultCountry, metadata })\n\t\t\t?.country\n\t}, [countryState, defaultCountry, isE164, metadata, pickedCountry, storedNumber])\n\n\t// The server's own derivation of the row, painted until the lazy metadata import lands so the\n\t// prefix and the entry never reflow. It describes one stored number, hence the match.\n\tconst seeded = metadata === null && seed?.number === storedNumber ? seed : null\n\n\tconst display = useMemo(\n\t\t() =>\n\t\t\tseeded?.national ??\n\t\t\tdisplayFor({ country: storedCountry, number: storedNumber || null }, metadata),\n\t\t[metadata, seeded, storedCountry, storedNumber]\n\t)\n\tconst [draft, setDraft] = useState(display)\n\tconst editingRef = useRef(false)\n\tuseEffect(() => {\n\t\tif (!editingRef.current) setDraft(display)\n\t}, [display])\n\n\t// The validated read refines the provisional one: `+1` is the United States on sight, and\n\t// stays so until enough digits arrive for libphonenumber to name Canada instead.\n\tconst draftCountry = useMemo(\n\t\t() =>\n\t\t\tmetadata && isInternational(draft)\n\t\t\t\t? (detectCountry(draft, { metadata }) ?? provisionalCountry(draft, { metadata }))\n\t\t\t\t: undefined,\n\t\t[draft, metadata]\n\t)\n\t// The pick outranks the stored country because e164 storage has no country column to write\n\t// it to: there the stored country is re-read off the number, which cannot know about a pick.\n\tconst country =\n\t\tdraftCountry ?? pickedCountry ?? storedCountry ?? seeded?.country ?? defaultCountry\n\n\tconst countriesKey = countries?.join()\n\tconst priorityKey = priorityCountries?.join()\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: a form-state round trip hands the same allowlist back as a new array, so the contents are the dependency, not the identity\n\tconst options = useMemo(\n\t\t() =>\n\t\t\tmetadata\n\t\t\t\t? countryOptions({ countries, locale: i18n.language, metadata, priorityCountries })\n\t\t\t\t: NO_COUNTRIES,\n\t\t[countriesKey, i18n.language, metadata, priorityKey]\n\t)\n\t// Off the country, not off `options`: an allowlist that does not offer the stored country\n\t// would otherwise drop the prefix once metadata lands, reflowing the row back.\n\tconst callingCode = useMemo(\n\t\t() =>\n\t\t\t(country && metadata ? callingCodeFor(country, metadata) : undefined) ?? seeded?.callingCode,\n\t\t[country, metadata, seeded]\n\t)\n\n\t// The revert target for a non-clearable commit, tracked from stored values so a picked\n\t// country, a document load and a save response all count.\n\tconst lastValidRef = useRef<null | PhoneEntry>(null)\n\tuseEffect(() => {\n\t\tif (storedNumber === '' || !metadata) return\n\t\tif (parsePhone(storedNumber, { defaultCountry: storedCountry, metadata })?.valid) {\n\t\t\tlastValidRef.current = { country: storedCountry, number: storedNumber }\n\t\t}\n\t}, [metadata, storedCountry, storedNumber])\n\n\tconst write = useCallback(\n\t\t(entry: PhoneEntry) => {\n\t\t\tif (isE164) {\n\t\t\t\tsetValue(entry.number)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tdispatchFields({ type: 'UPDATE', path: numberPath, value: entry.number })\n\t\t\tdispatchFields({ type: 'UPDATE', path: countryPath, value: entry.country ?? null })\n\t\t\t// dispatchFields, unlike setValue, leaves the form unmodified\n\t\t\tsetModified(true)\n\t\t},\n\t\t[countryPath, dispatchFields, isE164, numberPath, setModified, setValue]\n\t)\n\n\tconst commit = useCallback(\n\t\t(raw: string, opts: { country?: CountryCode; salvage?: boolean } = {}): PhoneEntry => {\n\t\t\tconst { derived, ...entry } = resolveCommit({\n\t\t\t\tcountry: opts.country ?? country,\n\t\t\t\tdraft: raw,\n\t\t\t\tisClearable,\n\t\t\t\tlastValid: lastValidRef.current,\n\t\t\t\tmetadata,\n\t\t\t\tpicked: opts.country !== undefined,\n\t\t\t\tsalvage: opts.salvage === true,\n\t\t\t})\n\t\t\t// A pick speaks for the country only until the value speaks for itself, or it would\n\t\t\t// keep overriding every number committed after it.\n\t\t\tif (derived) setPickedCountry(undefined)\n\t\t\twrite(entry)\n\t\t\treturn entry\n\t\t},\n\t\t[country, isClearable, metadata, write]\n\t)\n\n\tconst debounceRef = useRef<null | ReturnType<typeof setTimeout>>(null)\n\tconst cancelPending = useCallback(() => {\n\t\tif (debounceRef.current) clearTimeout(debounceRef.current)\n\t\tdebounceRef.current = null\n\t}, [])\n\tuseEffect(() => cancelPending, [cancelPending])\n\n\tconst onChange = useCallback(\n\t\t(event: React.ChangeEvent<HTMLInputElement>) => {\n\t\t\tconst input = event.target\n\t\t\tconst raw = input.value\n\t\t\tconst caret = input.selectionStart\n\t\t\tconst next = formatDraft({\n\t\t\t\tatEnd: caret === null || caret === raw.length,\n\t\t\t\tcallingCode,\n\t\t\t\tcountry,\n\t\t\t\tmetadata,\n\t\t\t\tprevious: draft,\n\t\t\t\traw,\n\t\t\t})\n\t\t\tif (next === null) {\n\t\t\t\t// React re-renders nothing when the state is unchanged, so the refused line is put\n\t\t\t\t// back here, with the caret where the refused characters would have gone.\n\t\t\t\tconst at = Math.max((caret ?? raw.length) - (raw.length - draft.length), 0)\n\t\t\t\tinput.value = draft\n\t\t\t\tinput.setSelectionRange(at, at)\n\t\t\t\treturn\n\t\t\t}\n\t\t\teditingRef.current = true\n\t\t\tsetDraft(next)\n\t\t\tcancelPending()\n\t\t\tdebounceRef.current = setTimeout(() => commit(next), COMMIT_DELAY)\n\t\t},\n\t\t[callingCode, cancelPending, commit, country, draft, metadata]\n\t)\n\n\tconst onBlur = useCallback(() => {\n\t\tcancelPending()\n\t\tif (!editingRef.current) return\n\t\teditingRef.current = false\n\t\tsetDraft(displayFor(commit(draft, { salvage: true }), metadata))\n\t}, [cancelPending, commit, draft, metadata])\n\n\tconst selectCountry = useCallback(\n\t\t(code: CountryCode) => {\n\t\t\tcancelPending()\n\t\t\teditingRef.current = false\n\t\t\tsetPickedCountry(code)\n\t\t\tconst entry = commit(dropCallingCode(draft, metadata), { country: code })\n\t\t\tsetDraft(displayFor(entry, metadata))\n\t\t},\n\t\t[cancelPending, commit, draft, metadata]\n\t)\n\n\t// The clear control unmounts on the same commit that empties the field, so focus moves to\n\t// the input first; otherwise a keyboard viewer is dropped back to the document body.\n\tconst inputRef = useRef<HTMLInputElement>(null)\n\tconst onClear = useCallback(() => {\n\t\tcancelPending()\n\t\teditingRef.current = false\n\t\tsetPickedCountry(undefined)\n\t\tsetDraft('')\n\t\twrite({ country: undefined, number: null })\n\t\tinputRef.current?.focus()\n\t}, [cancelPending, write])\n\n\t// renderField only maps permissions into the readOnly clientProp; admin.readOnly reaches\n\t// custom Field components solely via clientField.admin\n\tconst isReadOnly = Boolean(readOnlyFromProps || disabled || readOnlyFromAdmin)\n\t// A child subfield's error marks the group invalid without giving it a message of its own\n\tconst hasError = showError || (formSubmitted && (errorPaths?.length ?? 0) > 0)\n\tconst showClear = isClearable && !required && !isReadOnly && (draft !== '' || storedNumber !== '')\n\tconst showPrefix = callingCode !== undefined && !isInternational(draft)\n\tconst styles = useMemo(() => mergeFieldStyles(field), [field])\n\n\treturn (\n\t\t<div\n\t\t\tclassName={[\n\t\t\t\tfieldBaseClass,\n\t\t\t\tbaseClass,\n\t\t\t\tclassName,\n\t\t\t\thasError && 'error',\n\t\t\t\tisReadOnly && 'read-only',\n\t\t\t]\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(' ')}\n\t\t\tstyle={styles}\n\t\t>\n\t\t\t<RenderCustomComponent\n\t\t\t\tCustomComponent={Label}\n\t\t\t\tFallback={\n\t\t\t\t\t<FieldLabel label={label} localized={localized} path={path} required={required} />\n\t\t\t\t}\n\t\t\t/>\n\t\t\t<div className={`${fieldBaseClass}__wrap`}>\n\t\t\t\t<RenderCustomComponent\n\t\t\t\t\tCustomComponent={ErrorComponent}\n\t\t\t\t\tFallback={<FieldError path={path} showError={hasError} />}\n\t\t\t\t/>\n\t\t\t\t{BeforeInput}\n\t\t\t\t<div className={`${baseClass}__container`}>\n\t\t\t\t\t<CountryPicker\n\t\t\t\t\t\tdisabled={isReadOnly || metadata === null}\n\t\t\t\t\t\tflags={flags}\n\t\t\t\t\t\tonSelect={selectCountry}\n\t\t\t\t\t\toptions={options}\n\t\t\t\t\t\tpriorityLabel={resolveStaticLabel(priorityCountriesLabel, i18n.language)}\n\t\t\t\t\t\tvalue={country}\n\t\t\t\t\t/>\n\t\t\t\t\t{showPrefix ? <span className={`${baseClass}__prefix`}>{`+${callingCode}`}</span> : null}\n\t\t\t\t\t<input\n\t\t\t\t\t\tclassName={`${baseClass}__input`}\n\t\t\t\t\t\tid={`field-${path.replace(/\\./g, '__')}`}\n\t\t\t\t\t\tinputMode=\"tel\"\n\t\t\t\t\t\tname={path}\n\t\t\t\t\t\tonBlur={onBlur}\n\t\t\t\t\t\tonChange={onChange}\n\t\t\t\t\t\tplaceholder={resolveStaticLabel(placeholder, i18n.language)}\n\t\t\t\t\t\treadOnly={isReadOnly}\n\t\t\t\t\t\tref={inputRef}\n\t\t\t\t\t\ttype=\"tel\"\n\t\t\t\t\t\tvalue={draft}\n\t\t\t\t\t/>\n\t\t\t\t\t{showClear ? (\n\t\t\t\t\t\t<button\n\t\t\t\t\t\t\taria-label={t(keys.clearPhoneNumber)}\n\t\t\t\t\t\t\tclassName={`${baseClass}__clear`}\n\t\t\t\t\t\t\tonClick={onClear}\n\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<XIcon />\n\t\t\t\t\t\t</button>\n\t\t\t\t\t) : null}\n\t\t\t\t</div>\n\t\t\t\t{AfterInput}\n\t\t\t\t<RenderCustomComponent\n\t\t\t\t\tCustomComponent={Description}\n\t\t\t\t\tFallback={<FieldDescription description={description} path={path} />}\n\t\t\t\t/>\n\t\t\t</div>\n\t\t</div>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAmCA,MAAM,YAAY;AAClB,MAAM,eAAe;AACrB,MAAM,eAAoC;CAAE,UAAU,CAAC;CAAG,MAAM,CAAC;AAAE;;;;;;AA+BnE,MAAa,oBAAqD,UAAU;CAC3E,MAAM,EAAE,OAAO,MAAM,cAAc,UAAU,mBAAmB,SAAS;CACzE,MAAM,EAAE,OAAO,EAAE,WAAW,gBAAgB,CAAC,GAAG,OAAO,cAAc;CACrE,MAAM,cAAgC;CACtC,MAAM,EAAE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,GAAG,eAAe;aAAlC;IACC,oBAAC,uBAAD;KACC,iBAAiB;KACjB,UAAU,oBAAC,YAAD;MAAkB;MAAM,WAAW;KAAW,CAAA;IACxD,CAAA;IACA;IACD,qBAAC,OAAD;KAAK,WAAW,GAAG,UAAU;eAA7B;MACC,oBAAC,eAAD;OACC,UAAU,cAAc,aAAa;OAC9B;OACP,UAAU;OACD;OACT,eAAe,mBAAmB,wBAAwB,KAAK,QAAQ;OACvE,OAAO;MACP,CAAA;MACA,aAAa,oBAAC,QAAD;OAAM,WAAW,GAAG,UAAU;iBAAY,IAAI;MAAoB,CAAA,IAAI;MACpF,oBAAC,SAAD;OACC,WAAW,GAAG,UAAU;OACxB,IAAI,SAAS,KAAK,QAAQ,OAAO,IAAI;OACrC,WAAU;OACV,MAAM;OACE;OACE;OACV,aAAa,mBAAmB,aAAa,KAAK,QAAQ;OAC1D,UAAU;OACV,KAAK;OACL,MAAK;OACL,OAAO;MACP,CAAA;MACA,YACA,oBAAC,UAAD;OACC,cAAY,EAAE,KAAK,gBAAgB;OACnC,WAAW,GAAG,UAAU;OACxB,SAAS;OACT,MAAK;iBAEL,oBAAC,OAAD,CAAQ,CAAA;MACD,CAAA,IACL;KACA;;IACJ;IACD,oBAAC,uBAAD;KACC,iBAAiB;KACjB,UAAU,oBAAC,kBAAD;MAA+B;MAAmB;KAAO,CAAA;IACnE,CAAA;GACG;IACD;;AAEP"}
@@ -0,0 +1,86 @@
1
+ import { formatAsYouType, parsePhone, salvagePhone } from "../engine/phone.js";
2
+ import { callingCodeOf, digitCount, exceedsPhoneLength, isInternational, isPhoneInput, nationalPart } from "../engine/draft.js";
3
+ //#region src/fields/phoneNumber/client/editModel.ts
4
+ const displayFor = (entry, metadata) => {
5
+ if (!entry.number) return "";
6
+ if (!metadata) return entry.number;
7
+ const parsed = parsePhone(entry.number, {
8
+ defaultCountry: entry.country,
9
+ metadata
10
+ });
11
+ return parsed ? nationalPart(parsed) : entry.number;
12
+ };
13
+ /**
14
+ * As-you-type only while appending at the end, where the caret already sits: a mid-string
15
+ * edit or a deletion is stored exactly as typed, so the caret never moves under the viewer.
16
+ * Null refuses the input outright, leaving the draft as it was.
17
+ */
18
+ const formatDraft = (args) => {
19
+ const { atEnd, callingCode, country, metadata, previous, raw } = args;
20
+ if (!isPhoneInput(raw)) return null;
21
+ if (!metadata) return raw;
22
+ if (raw.length === previous.length + 1 && digitCount(raw) > digitCount(previous) && exceedsPhoneLength(raw, {
23
+ callingCode,
24
+ defaultCountry: country,
25
+ metadata
26
+ })) return null;
27
+ if (!atEnd || raw.length <= previous.length) return raw;
28
+ if (isInternational(raw)) return formatAsYouType(raw, void 0, { metadata });
29
+ if (callingCode === void 0) return raw;
30
+ const prefix = `+${callingCode}`;
31
+ const formatted = formatAsYouType(`${prefix}${raw}`, void 0, { metadata });
32
+ return formatted.startsWith(prefix) ? formatted.slice(prefix.length).trimStart() : formatted;
33
+ };
34
+ /**
35
+ * An international draft carries its own calling code, which a picked country replaces. A draft
36
+ * too short to parse still has to shed it, or the code left behind would name the country back.
37
+ */
38
+ const dropCallingCode = (draft, metadata) => {
39
+ if (!metadata || !isInternational(draft)) return draft;
40
+ const parsed = parsePhone(draft, { metadata });
41
+ if (parsed) return nationalPart(parsed);
42
+ const callingCode = callingCodeOf(draft, { metadata });
43
+ if (callingCode === void 0) return draft;
44
+ const digits = draft.replace(/\D/g, "");
45
+ return digits.startsWith(callingCode) ? digits.slice(callingCode.length) : draft;
46
+ };
47
+ const resolveCommit = (args) => {
48
+ const { country, draft, isClearable, lastValid, metadata, picked, salvage } = args;
49
+ const trimmed = draft.trim();
50
+ if (metadata === null) return {
51
+ country,
52
+ derived: false,
53
+ number: trimmed === "" ? null : trimmed
54
+ };
55
+ const opts = {
56
+ defaultCountry: country,
57
+ metadata
58
+ };
59
+ const direct = parsePhone(trimmed, opts);
60
+ const resolved = direct?.valid ? direct : salvage ? salvagePhone(trimmed, opts) : null;
61
+ if (resolved) {
62
+ const read = picked ? void 0 : resolved.country;
63
+ return {
64
+ country: read ?? country,
65
+ derived: read !== void 0,
66
+ number: resolved.e164
67
+ };
68
+ }
69
+ if (trimmed === "") return !isClearable && lastValid ? {
70
+ ...lastValid,
71
+ derived: true
72
+ } : {
73
+ country,
74
+ derived: false,
75
+ number: null
76
+ };
77
+ return {
78
+ country,
79
+ derived: false,
80
+ number: trimmed
81
+ };
82
+ };
83
+ //#endregion
84
+ export { displayFor, dropCallingCode, formatDraft, resolveCommit };
85
+
86
+ //# sourceMappingURL=editModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"editModel.js","names":[],"sources":["../../../../src/fields/phoneNumber/client/editModel.ts"],"sourcesContent":["import {\n\tcallingCodeOf,\n\tdigitCount,\n\texceedsPhoneLength,\n\tisInternational,\n\tisPhoneInput,\n\tnationalPart,\n} from '../engine/draft'\nimport type { PhoneMetadata } from '../engine/metadata'\nimport { type CountryCode, formatAsYouType, parsePhone, salvagePhone } from '../engine/phone'\n\n/** What one commit boundary stores: the E.164 number and the country it was read under. */\nexport type PhoneEntry = { country: CountryCode | undefined; number: null | string }\n\n/** `derived` marks a commit whose country came out of the number rather than off the row. */\nexport type PhoneCommit = PhoneEntry & { derived: boolean }\n\nexport const displayFor = (entry: PhoneEntry, metadata: null | PhoneMetadata): string => {\n\tif (!entry.number) return ''\n\tif (!metadata) return entry.number\n\tconst parsed = parsePhone(entry.number, { defaultCountry: entry.country, metadata })\n\treturn parsed ? nationalPart(parsed) : entry.number\n}\n\n/**\n * As-you-type only while appending at the end, where the caret already sits: a mid-string\n * edit or a deletion is stored exactly as typed, so the caret never moves under the viewer.\n * Null refuses the input outright, leaving the draft as it was.\n */\nexport const formatDraft = (args: {\n\tatEnd: boolean\n\tcallingCode: string | undefined\n\tcountry: CountryCode | undefined\n\tmetadata: null | PhoneMetadata\n\tprevious: string\n\traw: string\n}): null | string => {\n\tconst { atEnd, callingCode, country, metadata, previous, raw } = args\n\tif (!isPhoneInput(raw)) return null\n\tif (!metadata) return raw\n\t// The ceiling bounds typing. A bulk paste is let through so the blur salvage can still\n\t// recover a number out of a doubled or decorated one.\n\tconst oneMore = raw.length === previous.length + 1 && digitCount(raw) > digitCount(previous)\n\tif (oneMore && exceedsPhoneLength(raw, { callingCode, defaultCountry: country, metadata })) {\n\t\treturn null\n\t}\n\tif (!atEnd || raw.length <= previous.length) return raw\n\tif (isInternational(raw)) return formatAsYouType(raw, undefined, { metadata })\n\tif (callingCode === undefined) return raw\n\tconst prefix = `+${callingCode}`\n\tconst formatted = formatAsYouType(`${prefix}${raw}`, undefined, { metadata })\n\treturn formatted.startsWith(prefix) ? formatted.slice(prefix.length).trimStart() : formatted\n}\n\n/**\n * An international draft carries its own calling code, which a picked country replaces. A draft\n * too short to parse still has to shed it, or the code left behind would name the country back.\n */\nexport const dropCallingCode = (draft: string, metadata: null | PhoneMetadata): string => {\n\tif (!metadata || !isInternational(draft)) return draft\n\tconst parsed = parsePhone(draft, { metadata })\n\tif (parsed) return nationalPart(parsed)\n\tconst callingCode = callingCodeOf(draft, { metadata })\n\tif (callingCode === undefined) return draft\n\tconst digits = draft.replace(/\\D/g, '')\n\treturn digits.startsWith(callingCode) ? digits.slice(callingCode.length) : draft\n}\n\nexport const resolveCommit = (args: {\n\tcountry: CountryCode | undefined\n\tdraft: string\n\tisClearable: boolean\n\tlastValid: null | PhoneEntry\n\tmetadata: null | PhoneMetadata\n\tpicked: boolean\n\tsalvage: boolean\n}): PhoneCommit => {\n\tconst { country, draft, isClearable, lastValid, metadata, picked, salvage } = args\n\tconst trimmed = draft.trim()\n\tif (metadata === null) {\n\t\treturn { country, derived: false, number: trimmed === '' ? null : trimmed }\n\t}\n\tconst opts = { defaultCountry: country, metadata }\n\tconst direct = parsePhone(trimmed, opts)\n\tconst resolved = direct?.valid ? direct : salvage ? salvagePhone(trimmed, opts) : null\n\tif (resolved) {\n\t\t// A calling code several countries share reads back as whichever one owns the area code,\n\t\t// so re-reading it would undo the answer the viewer just gave the picker.\n\t\tconst read = picked ? undefined : resolved.country\n\t\treturn { country: read ?? country, derived: read !== undefined, number: resolved.e164 }\n\t}\n\t// isClearable false means the value cannot be removed, only replaced\n\tif (trimmed === '') {\n\t\treturn !isClearable && lastValid\n\t\t\t? { ...lastValid, derived: true }\n\t\t\t: { country, derived: false, number: null }\n\t}\n\treturn { country, derived: false, number: trimmed }\n}\n"],"mappings":";;;AAiBA,MAAa,cAAc,OAAmB,aAA2C;CACxF,IAAI,CAAC,MAAM,QAAQ,OAAO;CAC1B,IAAI,CAAC,UAAU,OAAO,MAAM;CAC5B,MAAM,SAAS,WAAW,MAAM,QAAQ;EAAE,gBAAgB,MAAM;EAAS;CAAS,CAAC;CACnF,OAAO,SAAS,aAAa,MAAM,IAAI,MAAM;AAC9C;;;;;;AAOA,MAAa,eAAe,SAOP;CACpB,MAAM,EAAE,OAAO,aAAa,SAAS,UAAU,UAAU,QAAQ;CACjE,IAAI,CAAC,aAAa,GAAG,GAAG,OAAO;CAC/B,IAAI,CAAC,UAAU,OAAO;CAItB,IADgB,IAAI,WAAW,SAAS,SAAS,KAAK,WAAW,GAAG,IAAI,WAAW,QAAQ,KAC5E,mBAAmB,KAAK;EAAE;EAAa,gBAAgB;EAAS;CAAS,CAAC,GACxF,OAAO;CAER,IAAI,CAAC,SAAS,IAAI,UAAU,SAAS,QAAQ,OAAO;CACpD,IAAI,gBAAgB,GAAG,GAAG,OAAO,gBAAgB,KAAK,KAAA,GAAW,EAAE,SAAS,CAAC;CAC7E,IAAI,gBAAgB,KAAA,GAAW,OAAO;CACtC,MAAM,SAAS,IAAI;CACnB,MAAM,YAAY,gBAAgB,GAAG,SAAS,OAAO,KAAA,GAAW,EAAE,SAAS,CAAC;CAC5E,OAAO,UAAU,WAAW,MAAM,IAAI,UAAU,MAAM,OAAO,MAAM,EAAE,UAAU,IAAI;AACpF;;;;;AAMA,MAAa,mBAAmB,OAAe,aAA2C;CACzF,IAAI,CAAC,YAAY,CAAC,gBAAgB,KAAK,GAAG,OAAO;CACjD,MA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