@10x-media/fields 0.1.0-beta.1 → 0.1.0-beta.10
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.
- package/CHANGELOG.md +120 -0
- package/README.md +7 -3
- package/dist/exports/client.d.ts +10 -1
- package/dist/exports/client.js +9 -1
- package/dist/exports/color-utils.d.ts +2 -1
- package/dist/exports/color-utils.js +2 -1
- package/dist/exports/color.d.ts +2 -1
- package/dist/exports/color.js +2 -1
- package/dist/exports/encrypted.d.ts +7 -4
- package/dist/exports/encrypted.js +6 -3
- package/dist/exports/measurement-utils.d.ts +9 -0
- package/dist/exports/measurement-utils.js +8 -0
- package/dist/exports/measurement.d.ts +11 -0
- package/dist/exports/measurement.js +10 -0
- package/dist/exports/phone-utils.d.ts +4 -0
- package/dist/exports/phone-utils.js +4 -0
- package/dist/exports/phone.d.ts +6 -0
- package/dist/exports/phone.js +3 -0
- package/dist/exports/rsc.d.ts +5 -1
- package/dist/exports/rsc.js +5 -1
- package/dist/exports/types.d.ts +3 -2
- package/dist/fields/color/client/ColorField.d.ts +2 -0
- package/dist/fields/color/client/ColorField.js +35 -293
- package/dist/fields/color/client/ColorField.js.map +1 -1
- package/dist/fields/color/client/ColorInput.d.ts +44 -0
- package/dist/fields/color/client/ColorInput.js +374 -0
- package/dist/fields/color/client/ColorInput.js.map +1 -0
- package/dist/fields/color/client/ColorPickerPanel.d.ts +3 -1
- package/dist/fields/color/client/ColorPickerPanel.js +29 -4
- package/dist/fields/color/client/ColorPickerPanel.js.map +1 -1
- package/dist/fields/color/client/colorField.css +30 -0
- package/dist/fields/color/colorField.js +36 -8
- package/dist/fields/color/colorField.js.map +1 -1
- package/dist/fields/color/presetChip.js +10 -9
- package/dist/fields/color/presetChip.js.map +1 -1
- package/dist/fields/color/presetReference.d.ts +13 -0
- package/dist/fields/color/presetReference.js +94 -0
- package/dist/fields/color/presetReference.js.map +1 -0
- package/dist/fields/color/server/ColorCell.js +7 -1
- package/dist/fields/color/server/ColorCell.js.map +1 -1
- package/dist/fields/color/server/colorCell.css +5 -0
- package/dist/fields/color/valueIssue.js +23 -0
- package/dist/fields/color/valueIssue.js.map +1 -0
- package/dist/fields/encrypted/boot.js +2 -1
- package/dist/fields/encrypted/boot.js.map +1 -1
- package/dist/fields/encrypted/components/EncryptedInput.js +4 -1
- package/dist/fields/encrypted/components/EncryptedInput.js.map +1 -1
- package/dist/fields/encrypted/components/EncryptedTextarea.js +6 -2
- package/dist/fields/encrypted/components/EncryptedTextarea.js.map +1 -1
- package/dist/fields/encrypted/components/ProtectedCell.d.ts +15 -6
- package/dist/fields/encrypted/components/ProtectedCell.js +37 -8
- package/dist/fields/encrypted/components/ProtectedCell.js.map +1 -1
- package/dist/fields/encrypted/components/ProtectedField.css +134 -39
- package/dist/fields/encrypted/components/ProtectedField.d.ts +3 -0
- package/dist/fields/encrypted/components/ProtectedField.js +14 -0
- package/dist/fields/encrypted/components/ProtectedField.js.map +1 -1
- package/dist/fields/encrypted/components/WriteOnlyActions.js +65 -0
- package/dist/fields/encrypted/components/WriteOnlyActions.js.map +1 -0
- package/dist/fields/encrypted/components/WriteOnlyField.js +286 -0
- package/dist/fields/encrypted/components/WriteOnlyField.js.map +1 -0
- package/dist/fields/encrypted/components/writeOnlyIntent.js +69 -0
- package/dist/fields/encrypted/components/writeOnlyIntent.js.map +1 -0
- package/dist/fields/encrypted/crypto/wire.d.ts +10 -1
- package/dist/fields/encrypted/crypto/wire.js +1 -1
- package/dist/fields/encrypted/encryptedField.js +63 -6
- package/dist/fields/encrypted/encryptedField.js.map +1 -1
- package/dist/fields/encrypted/generateSecret.d.ts +10 -0
- package/dist/fields/encrypted/generateSecret.js +41 -0
- package/dist/fields/encrypted/generateSecret.js.map +1 -0
- package/dist/fields/encrypted/hint.js +67 -0
- package/dist/fields/encrypted/hint.js.map +1 -0
- package/dist/fields/encrypted/hooks.js +59 -31
- package/dist/fields/encrypted/hooks.js.map +1 -1
- package/dist/fields/encrypted/queryRewrite.d.ts +10 -2
- package/dist/fields/encrypted/queryRewrite.js +34 -8
- package/dist/fields/encrypted/queryRewrite.js.map +1 -1
- package/dist/fields/encrypted/rawRead.d.ts +24 -0
- package/dist/fields/encrypted/rawRead.js +40 -0
- package/dist/fields/encrypted/rawRead.js.map +1 -0
- package/dist/fields/encrypted/scan.js +21 -4
- package/dist/fields/encrypted/scan.js.map +1 -1
- package/dist/fields/encrypted/types.d.ts +67 -2
- package/dist/fields/encrypted/types.js +8 -1
- package/dist/fields/encrypted/types.js.map +1 -1
- package/dist/fields/encrypted/utils/decryptAllData.js +2 -2
- package/dist/fields/encrypted/utils/decryptAllData.js.map +1 -1
- package/dist/fields/encrypted/utils/decryptFieldValue.d.ts +30 -0
- package/dist/fields/encrypted/utils/decryptFieldValue.js +56 -0
- package/dist/fields/encrypted/utils/decryptFieldValue.js.map +1 -0
- package/dist/fields/encrypted/utils/readEncryptedField.d.ts +51 -0
- package/dist/fields/encrypted/utils/readEncryptedField.js +96 -0
- package/dist/fields/encrypted/utils/readEncryptedField.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/lucide/generated/nodes.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/nodes.js.map +1 -1
- package/dist/fields/icon/adapters/tabler/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/tabler/generated/nodes.js.map +1 -1
- package/dist/fields/icon/client/icon-field.css +4 -2
- package/dist/fields/icon/codegen/emit.js +2 -2
- package/dist/fields/icon/codegen/emit.js.map +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js.map +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js.map +1 -1
- package/dist/fields/icon/shared/iconLabel.js +1 -1
- package/dist/fields/icon/shared/iconLabel.js.map +1 -1
- package/dist/fields/measurement/client/MeasurementCell.d.ts +18 -0
- package/dist/fields/measurement/client/MeasurementCell.js +47 -0
- package/dist/fields/measurement/client/MeasurementCell.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementField.d.ts +13 -0
- package/dist/fields/measurement/client/MeasurementField.js +70 -0
- package/dist/fields/measurement/client/MeasurementField.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementInput.d.ts +42 -0
- package/dist/fields/measurement/client/MeasurementInput.js +327 -0
- package/dist/fields/measurement/client/MeasurementInput.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.d.ts +24 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js +61 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js.map +1 -0
- package/dist/fields/measurement/client/editModel.js +124 -0
- package/dist/fields/measurement/client/editModel.js.map +1 -0
- package/dist/fields/measurement/client/measurementCell.css +4 -0
- package/dist/fields/measurement/client/measurementField.css +215 -0
- package/dist/fields/measurement/client/useMeasurementModel.d.ts +19 -0
- package/dist/fields/measurement/client/useMeasurementModel.js +85 -0
- package/dist/fields/measurement/client/useMeasurementModel.js.map +1 -0
- package/dist/fields/measurement/engine/convert.d.ts +8 -0
- package/dist/fields/measurement/engine/convert.js +21 -0
- package/dist/fields/measurement/engine/convert.js.map +1 -0
- package/dist/fields/measurement/engine/format.d.ts +37 -0
- package/dist/fields/measurement/engine/format.js +156 -0
- package/dist/fields/measurement/engine/format.js.map +1 -0
- package/dist/fields/measurement/engine/locale.d.ts +25 -0
- package/dist/fields/measurement/engine/locale.js +79 -0
- package/dist/fields/measurement/engine/locale.js.map +1 -0
- package/dist/fields/measurement/engine/precision.d.ts +34 -0
- package/dist/fields/measurement/engine/precision.js +63 -0
- package/dist/fields/measurement/engine/precision.js.map +1 -0
- package/dist/fields/measurement/engine/registry.d.ts +55 -0
- package/dist/fields/measurement/engine/registry.js +117 -0
- package/dist/fields/measurement/engine/registry.js.map +1 -0
- package/dist/fields/measurement/engine/units.d.ts +178 -0
- package/dist/fields/measurement/engine/units.js +198 -0
- package/dist/fields/measurement/engine/units.js.map +1 -0
- package/dist/fields/measurement/measurementField.d.ts +17 -0
- package/dist/fields/measurement/measurementField.js +139 -0
- package/dist/fields/measurement/measurementField.js.map +1 -0
- package/dist/fields/measurement/options.d.ts +76 -0
- package/dist/fields/measurement/options.js +9 -0
- package/dist/fields/measurement/options.js.map +1 -0
- package/dist/fields/measurement/presets.d.ts +117 -0
- package/dist/fields/measurement/presets.js +136 -0
- package/dist/fields/measurement/presets.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementCellServer.d.ts +21 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js +53 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.d.ts +16 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js +45 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js.map +1 -0
- package/dist/fields/measurement/server/getUserUnits.js +39 -0
- package/dist/fields/measurement/server/getUserUnits.js.map +1 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js +24 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js.map +1 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js +26 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryFlag.d.ts +16 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js +40 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryPicker.d.ts +21 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js +255 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js.map +1 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.d.ts +26 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js +318 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/client/editModel.js +86 -0
- package/dist/fields/phoneNumber/client/editModel.js.map +1 -0
- package/dist/fields/phoneNumber/client/phoneNumberField.css +305 -0
- package/dist/fields/phoneNumber/engine/countries.d.ts +36 -0
- package/dist/fields/phoneNumber/engine/countries.js +68 -0
- package/dist/fields/phoneNumber/engine/countries.js.map +1 -0
- package/dist/fields/phoneNumber/engine/draft.d.ts +13 -0
- package/dist/fields/phoneNumber/engine/draft.js +72 -0
- package/dist/fields/phoneNumber/engine/draft.js.map +1 -0
- package/dist/fields/phoneNumber/engine/metadata.d.ts +11 -0
- package/dist/fields/phoneNumber/engine/metadata.js +32 -0
- package/dist/fields/phoneNumber/engine/metadata.js.map +1 -0
- package/dist/fields/phoneNumber/engine/phone.d.ts +37 -0
- package/dist/fields/phoneNumber/engine/phone.js +59 -0
- package/dist/fields/phoneNumber/engine/phone.js.map +1 -0
- package/dist/fields/phoneNumber/options.d.ts +72 -0
- package/dist/fields/phoneNumber/options.js +37 -0
- package/dist/fields/phoneNumber/options.js.map +1 -0
- package/dist/fields/phoneNumber/phoneNumberField.d.ts +14 -0
- package/dist/fields/phoneNumber/phoneNumberField.js +167 -0
- package/dist/fields/phoneNumber/phoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.d.ts +19 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js +73 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.d.ts +16 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js +57 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js +51 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js.map +1 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js +18 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js.map +1 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js +33 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js.map +1 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +35 -1
- package/dist/index.js.map +1 -1
- package/dist/translations/ar.js +62 -0
- package/dist/translations/ar.js.map +1 -0
- package/dist/translations/de.js +19 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +19 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +62 -0
- package/dist/translations/es.js.map +1 -0
- package/dist/translations/fr.js +62 -0
- package/dist/translations/fr.js.map +1 -0
- package/dist/translations/id.js +62 -0
- package/dist/translations/id.js.map +1 -0
- package/dist/translations/index.d.ts +10 -1
- package/dist/translations/index.js +19 -1
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +19 -1
- package/dist/translations/keys.js +20 -2
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +62 -0
- package/dist/translations/ko.js.map +1 -0
- package/dist/translations/pt.js +62 -0
- package/dist/translations/pt.js.map +1 -0
- package/dist/translations/ru.js +62 -0
- package/dist/translations/ru.js.map +1 -0
- package/dist/translations/uk.js +62 -0
- package/dist/translations/uk.js.map +1 -0
- package/dist/translations/zh.js +62 -0
- package/dist/translations/zh.js.map +1 -0
- package/dist/types.d.ts +41 -1
- package/dist/utils/resolveStaticLabel.js +13 -0
- package/dist/utils/resolveStaticLabel.js.map +1 -0
- package/package.json +28 -6
- package/dist/fields/icon/shared/resolveStaticLabel.js +0 -16
- package/dist/fields/icon/shared/resolveStaticLabel.js.map +0 -1
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { keys } from "../../../translations/keys.js";
|
|
3
|
+
import { useTranslation as useTranslation$1 } from "../../../translations/useTranslation.js";
|
|
4
|
+
import { CountryFlag } from "./CountryFlag.js";
|
|
5
|
+
import { ChevronIcon, Popup, SearchIcon } from "@payloadcms/ui";
|
|
6
|
+
import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
7
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
import "./phoneNumberField.css";
|
|
9
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
10
|
+
//#region src/fields/phoneNumber/client/CountryPicker.tsx
|
|
11
|
+
const baseClass = "fields-phone";
|
|
12
|
+
const ROW_HEIGHT = 34;
|
|
13
|
+
const estimateRow = () => ROW_HEIGHT;
|
|
14
|
+
const LIST_MAX_HEIGHT = ROW_HEIGHT * 8;
|
|
15
|
+
const matchesQuery = (option, query) => {
|
|
16
|
+
if (option.name.toLowerCase().includes(query)) return true;
|
|
17
|
+
if (option.code.toLowerCase().startsWith(query)) return true;
|
|
18
|
+
const digits = query.replace(/\D/g, "");
|
|
19
|
+
return digits !== "" && option.callingCode.startsWith(digits);
|
|
20
|
+
};
|
|
21
|
+
const filterOptions = (options, query) => query === "" ? options : options.filter((option) => matchesQuery(option, query));
|
|
22
|
+
/**
|
|
23
|
+
* The priority heading renders only with a caller-given label; the plain divider before
|
|
24
|
+
* `rest` carries the split otherwise. Neither appears when either group is empty, since
|
|
25
|
+
* there is then nothing to separate from.
|
|
26
|
+
*/
|
|
27
|
+
const buildRows = (options, query, priorityLabel) => {
|
|
28
|
+
const priority = filterOptions(options.priority, query);
|
|
29
|
+
const rest = filterOptions(options.rest, query);
|
|
30
|
+
const split = priority.length > 0 && rest.length > 0;
|
|
31
|
+
return [
|
|
32
|
+
...split && priorityLabel !== void 0 ? [{
|
|
33
|
+
kind: "heading",
|
|
34
|
+
label: priorityLabel
|
|
35
|
+
}] : [],
|
|
36
|
+
...priority.map((option) => ({
|
|
37
|
+
kind: "option",
|
|
38
|
+
option
|
|
39
|
+
})),
|
|
40
|
+
...split ? [{ kind: "divider" }] : [],
|
|
41
|
+
...rest.map((option) => ({
|
|
42
|
+
kind: "option",
|
|
43
|
+
option
|
|
44
|
+
}))
|
|
45
|
+
];
|
|
46
|
+
};
|
|
47
|
+
const CountryPanel = ({ close, flags, onSelect, options, priorityLabel, value }) => {
|
|
48
|
+
const { t } = useTranslation$1();
|
|
49
|
+
const [query, setQuery] = useState("");
|
|
50
|
+
const listRef = useRef(null);
|
|
51
|
+
const searchRef = useRef(null);
|
|
52
|
+
const listId = useId();
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
searchRef.current?.focus({ preventScroll: true });
|
|
55
|
+
}, []);
|
|
56
|
+
const rows = useMemo(() => buildRows(options, query.trim().toLowerCase(), priorityLabel), [
|
|
57
|
+
options,
|
|
58
|
+
priorityLabel,
|
|
59
|
+
query
|
|
60
|
+
]);
|
|
61
|
+
const optionRows = useMemo(() => rows.flatMap((row, index) => row.kind === "option" ? [index] : []), [rows]);
|
|
62
|
+
const virtualizer = useVirtualizer({
|
|
63
|
+
count: rows.length,
|
|
64
|
+
estimateSize: estimateRow,
|
|
65
|
+
getScrollElement: () => listRef.current,
|
|
66
|
+
overscan: 6
|
|
67
|
+
});
|
|
68
|
+
const [activeRow, setActiveRow] = useState(-1);
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
const selected = rows.findIndex((row) => row.kind === "option" && row.option.code === value);
|
|
71
|
+
const next = selected >= 0 ? selected : optionRows[0] ?? -1;
|
|
72
|
+
setActiveRow(next);
|
|
73
|
+
if (next >= 0) virtualizer.scrollToIndex(next, { align: "center" });
|
|
74
|
+
}, [query]);
|
|
75
|
+
const select = useCallback((code) => {
|
|
76
|
+
onSelect(code);
|
|
77
|
+
close();
|
|
78
|
+
}, [close, onSelect]);
|
|
79
|
+
const move = useCallback((step) => {
|
|
80
|
+
const position = optionRows.indexOf(activeRow);
|
|
81
|
+
const next = optionRows[Math.min(Math.max(position + step, 0), optionRows.length - 1)];
|
|
82
|
+
if (next === void 0) return;
|
|
83
|
+
setActiveRow(next);
|
|
84
|
+
virtualizer.scrollToIndex(next);
|
|
85
|
+
}, [
|
|
86
|
+
activeRow,
|
|
87
|
+
optionRows,
|
|
88
|
+
virtualizer
|
|
89
|
+
]);
|
|
90
|
+
const onKeyDown = (event) => {
|
|
91
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
92
|
+
event.preventDefault();
|
|
93
|
+
event.stopPropagation();
|
|
94
|
+
move(event.key === "ArrowDown" ? 1 : -1);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (event.key === "Enter") {
|
|
98
|
+
event.preventDefault();
|
|
99
|
+
const row = rows[activeRow];
|
|
100
|
+
if (row?.kind === "option") select(row.option.code);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const optionId = (index) => `${listId}-option-${index}`;
|
|
104
|
+
const populated = rows.length > 0;
|
|
105
|
+
const listA11y = populated ? {
|
|
106
|
+
"aria-label": t(keys.selectCountry),
|
|
107
|
+
role: "listbox"
|
|
108
|
+
} : {};
|
|
109
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
110
|
+
className: `${baseClass}__panel`,
|
|
111
|
+
"data-popup-prevent-close": true,
|
|
112
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
113
|
+
className: `${baseClass}__search`,
|
|
114
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
115
|
+
"aria-hidden": "true",
|
|
116
|
+
className: `${baseClass}__search-icon`,
|
|
117
|
+
children: /* @__PURE__ */ jsx(SearchIcon, {})
|
|
118
|
+
}), /* @__PURE__ */ jsx("input", {
|
|
119
|
+
"aria-activedescendant": rows[activeRow]?.kind === "option" ? optionId(activeRow) : void 0,
|
|
120
|
+
"aria-autocomplete": "list",
|
|
121
|
+
"aria-controls": populated ? listId : void 0,
|
|
122
|
+
"aria-expanded": populated,
|
|
123
|
+
"aria-label": t(keys.searchCountries),
|
|
124
|
+
className: `${baseClass}__search-input`,
|
|
125
|
+
onChange: (event) => setQuery(event.target.value),
|
|
126
|
+
onKeyDown,
|
|
127
|
+
placeholder: t(keys.searchCountries),
|
|
128
|
+
ref: searchRef,
|
|
129
|
+
role: "combobox",
|
|
130
|
+
type: "text",
|
|
131
|
+
value: query
|
|
132
|
+
})]
|
|
133
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
134
|
+
className: `${baseClass}__list`,
|
|
135
|
+
id: listId,
|
|
136
|
+
ref: listRef,
|
|
137
|
+
style: { maxHeight: LIST_MAX_HEIGHT },
|
|
138
|
+
...listA11y,
|
|
139
|
+
children: populated ? /* @__PURE__ */ jsx("div", {
|
|
140
|
+
role: "presentation",
|
|
141
|
+
style: {
|
|
142
|
+
height: virtualizer.getTotalSize(),
|
|
143
|
+
position: "relative",
|
|
144
|
+
width: "100%"
|
|
145
|
+
},
|
|
146
|
+
children: virtualizer.getVirtualItems().map((item) => {
|
|
147
|
+
const row = rows[item.index];
|
|
148
|
+
if (!row) return null;
|
|
149
|
+
const style = {
|
|
150
|
+
height: `${item.size}px`,
|
|
151
|
+
left: 0,
|
|
152
|
+
position: "absolute",
|
|
153
|
+
top: 0,
|
|
154
|
+
transform: `translateY(${item.start}px)`,
|
|
155
|
+
width: "100%"
|
|
156
|
+
};
|
|
157
|
+
if (row.kind === "heading") return /* @__PURE__ */ jsx("div", {
|
|
158
|
+
className: `${baseClass}__group`,
|
|
159
|
+
role: "presentation",
|
|
160
|
+
style,
|
|
161
|
+
children: row.label
|
|
162
|
+
}, "heading");
|
|
163
|
+
if (row.kind === "divider") return /* @__PURE__ */ jsx("div", {
|
|
164
|
+
className: `${baseClass}__group`,
|
|
165
|
+
role: "presentation",
|
|
166
|
+
style
|
|
167
|
+
}, "divider");
|
|
168
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
169
|
+
"aria-posinset": optionRows.indexOf(item.index) + 1,
|
|
170
|
+
"aria-selected": row.option.code === value,
|
|
171
|
+
"aria-setsize": optionRows.length,
|
|
172
|
+
className: `${baseClass}__option`,
|
|
173
|
+
"data-active": item.index === activeRow || void 0,
|
|
174
|
+
id: optionId(item.index),
|
|
175
|
+
onClick: () => select(row.option.code),
|
|
176
|
+
role: "option",
|
|
177
|
+
style,
|
|
178
|
+
tabIndex: -1,
|
|
179
|
+
type: "button",
|
|
180
|
+
children: [
|
|
181
|
+
/* @__PURE__ */ jsx(CountryFlag, {
|
|
182
|
+
code: row.option.code,
|
|
183
|
+
mode: flags
|
|
184
|
+
}),
|
|
185
|
+
/* @__PURE__ */ jsx("span", {
|
|
186
|
+
className: `${baseClass}__option-name`,
|
|
187
|
+
children: row.option.name
|
|
188
|
+
}),
|
|
189
|
+
/* @__PURE__ */ jsx("span", {
|
|
190
|
+
className: `${baseClass}__option-code`,
|
|
191
|
+
children: `+${row.option.callingCode}`
|
|
192
|
+
})
|
|
193
|
+
]
|
|
194
|
+
}, row.option.code);
|
|
195
|
+
})
|
|
196
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
197
|
+
className: `${baseClass}__empty`,
|
|
198
|
+
children: t(keys.noCountriesFound)
|
|
199
|
+
})
|
|
200
|
+
})]
|
|
201
|
+
});
|
|
202
|
+
};
|
|
203
|
+
const CountryPicker = ({ disabled, flags, onSelect, options, priorityLabel, value }) => {
|
|
204
|
+
const { t } = useTranslation$1();
|
|
205
|
+
const [open, setOpen] = useState(false);
|
|
206
|
+
const selected = useMemo(() => [...options.priority, ...options.rest].find((option) => option.code === value), [options, value]);
|
|
207
|
+
const label = value === void 0 ? t(keys.selectCountry) : t(keys.phoneCountryFor, { country: selected?.name ?? value });
|
|
208
|
+
const renderFace = () => {
|
|
209
|
+
if (value === void 0) return null;
|
|
210
|
+
if (flags === "none") return /* @__PURE__ */ jsx("span", {
|
|
211
|
+
className: `${baseClass}__code`,
|
|
212
|
+
children: value
|
|
213
|
+
});
|
|
214
|
+
return /* @__PURE__ */ jsx(CountryFlag, {
|
|
215
|
+
code: value,
|
|
216
|
+
mode: flags
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
return /* @__PURE__ */ jsx(Popup, {
|
|
220
|
+
button: /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
221
|
+
renderFace(),
|
|
222
|
+
/* @__PURE__ */ jsx("span", {
|
|
223
|
+
className: `${baseClass}__sr-only`,
|
|
224
|
+
children: label
|
|
225
|
+
}),
|
|
226
|
+
/* @__PURE__ */ jsx("span", {
|
|
227
|
+
"aria-hidden": "true",
|
|
228
|
+
className: `${baseClass}__caret`,
|
|
229
|
+
children: /* @__PURE__ */ jsx(ChevronIcon, { size: "small" })
|
|
230
|
+
})
|
|
231
|
+
] }),
|
|
232
|
+
buttonClassName: `${baseClass}__trigger`,
|
|
233
|
+
buttonType: "default",
|
|
234
|
+
caret: false,
|
|
235
|
+
className: `${baseClass}__picker`,
|
|
236
|
+
disabled,
|
|
237
|
+
horizontalAlign: "left",
|
|
238
|
+
onToggleClose: () => setOpen(false),
|
|
239
|
+
onToggleOpen: () => setOpen(true),
|
|
240
|
+
render: ({ close }) => open ? /* @__PURE__ */ jsx(CountryPanel, {
|
|
241
|
+
close,
|
|
242
|
+
flags,
|
|
243
|
+
onSelect,
|
|
244
|
+
options,
|
|
245
|
+
priorityLabel,
|
|
246
|
+
value
|
|
247
|
+
}) : null,
|
|
248
|
+
size: "fit-content",
|
|
249
|
+
verticalAlign: "bottom"
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
//#endregion
|
|
253
|
+
export { CountryPicker };
|
|
254
|
+
|
|
255
|
+
//# sourceMappingURL=CountryPicker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CountryPicker.js","names":["useTranslation"],"sources":["../../../../src/fields/phoneNumber/client/CountryPicker.tsx"],"sourcesContent":["'use client'\n\nimport { ChevronIcon, Popup, SearchIcon } from '@payloadcms/ui'\nimport { useVirtualizer } from '@tanstack/react-virtual'\nimport type React from 'react'\nimport { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'\nimport { keys } from '../../../translations/keys'\nimport { useTranslation } from '../../../translations/useTranslation'\nimport type { CountryOption } from '../engine/countries'\nimport type { CountryCode } from '../engine/phone'\nimport type { PhoneFlagMode } from '../options'\nimport { CountryFlag } from './CountryFlag'\nimport './phoneNumberField.css'\n\nconst baseClass = 'fields-phone'\n\n// Headings, the divider, and options share one height, so the virtualizer's estimate is\n// exact and the rows it positions can never drift from what the CSS paints.\nconst ROW_HEIGHT = 34\nconst estimateRow = () => ROW_HEIGHT\n\n// The cap is published from here rather than written into the stylesheet: a fractional one\n// clips the last row against the panel edge, which reads as tighter padding at the bottom.\nconst VISIBLE_ROWS = 8\nconst LIST_MAX_HEIGHT = ROW_HEIGHT * VISIBLE_ROWS\n\nexport type CountryOptionGroups = {\n\tpriority: CountryOption[]\n\trest: CountryOption[]\n}\n\ntype PickerRow =\n\t| { kind: 'divider' }\n\t| { kind: 'heading'; label: string }\n\t| { kind: 'option'; option: CountryOption }\n\nconst matchesQuery = (option: CountryOption, query: string): boolean => {\n\tif (option.name.toLowerCase().includes(query)) return true\n\tif (option.code.toLowerCase().startsWith(query)) return true\n\tconst digits = query.replace(/\\D/g, '')\n\treturn digits !== '' && option.callingCode.startsWith(digits)\n}\n\nconst filterOptions = (options: CountryOption[], query: string): CountryOption[] =>\n\tquery === '' ? options : options.filter((option) => matchesQuery(option, query))\n\n/**\n * The priority heading renders only with a caller-given label; the plain divider before\n * `rest` carries the split otherwise. Neither appears when either group is empty, since\n * there is then nothing to separate from.\n */\nconst buildRows = (\n\toptions: CountryOptionGroups,\n\tquery: string,\n\tpriorityLabel: string | undefined\n): PickerRow[] => {\n\tconst priority = filterOptions(options.priority, query)\n\tconst rest = filterOptions(options.rest, query)\n\tconst split = priority.length > 0 && rest.length > 0\n\treturn [\n\t\t...(split && priorityLabel !== undefined\n\t\t\t? [{ kind: 'heading', label: priorityLabel } as const]\n\t\t\t: []),\n\t\t...priority.map((option) => ({ kind: 'option', option }) as const),\n\t\t...(split ? [{ kind: 'divider' } as const] : []),\n\t\t...rest.map((option) => ({ kind: 'option', option }) as const),\n\t]\n}\n\ntype CountryPanelProps = {\n\tclose: () => void\n\tflags: PhoneFlagMode\n\tonSelect: (code: CountryCode) => void\n\toptions: CountryOptionGroups\n\tpriorityLabel?: string\n\tvalue: CountryCode | undefined\n}\n\nconst CountryPanel: React.FC<CountryPanelProps> = ({\n\tclose,\n\tflags,\n\tonSelect,\n\toptions,\n\tpriorityLabel,\n\tvalue,\n}) => {\n\tconst { t } = useTranslation()\n\tconst [query, setQuery] = useState('')\n\tconst listRef = useRef<HTMLDivElement>(null)\n\tconst searchRef = useRef<HTMLInputElement>(null)\n\tconst listId = useId()\n\n\t// The panel is portalled to the body and absolutely positioned, so a plain focus scrolls\n\t// the page to it and drags the row the viewer is reading out from under them.\n\tuseEffect(() => {\n\t\tsearchRef.current?.focus({ preventScroll: true })\n\t}, [])\n\n\tconst rows = useMemo(\n\t\t() => buildRows(options, query.trim().toLowerCase(), priorityLabel),\n\t\t[options, priorityLabel, query]\n\t)\n\tconst optionRows = useMemo(\n\t\t() => rows.flatMap((row, index) => (row.kind === 'option' ? [index] : [])),\n\t\t[rows]\n\t)\n\n\tconst virtualizer = useVirtualizer({\n\t\tcount: rows.length,\n\t\testimateSize: estimateRow,\n\t\tgetScrollElement: () => listRef.current,\n\t\toverscan: 6,\n\t})\n\n\tconst [activeRow, setActiveRow] = useState(-1)\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: the query is the trigger; re-seeding on every parent render would fight the arrow keys\n\tuseEffect(() => {\n\t\tconst selected = rows.findIndex((row) => row.kind === 'option' && row.option.code === value)\n\t\tconst next = selected >= 0 ? selected : (optionRows[0] ?? -1)\n\t\tsetActiveRow(next)\n\t\t// aria-activedescendant has to name a rendered element, and the virtualizer only\n\t\t// renders what is in view: opening on the 200th country must scroll to it.\n\t\tif (next >= 0) virtualizer.scrollToIndex(next, { align: 'center' })\n\t}, [query])\n\n\tconst select = useCallback(\n\t\t(code: CountryCode) => {\n\t\t\tonSelect(code)\n\t\t\tclose()\n\t\t},\n\t\t[close, onSelect]\n\t)\n\n\tconst move = useCallback(\n\t\t(step: number) => {\n\t\t\tconst position = optionRows.indexOf(activeRow)\n\t\t\tconst next = optionRows[Math.min(Math.max(position + step, 0), optionRows.length - 1)]\n\t\t\tif (next === undefined) return\n\t\t\tsetActiveRow(next)\n\t\t\tvirtualizer.scrollToIndex(next)\n\t\t},\n\t\t[activeRow, optionRows, virtualizer]\n\t)\n\n\tconst onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {\n\t\tif (event.key === 'ArrowDown' || event.key === 'ArrowUp') {\n\t\t\t// The arrows drive this list, so they stop here: Payload's Popup otherwise walks\n\t\t\t// focus between the panel's tabbables, which is why rows carry tabIndex={-1}.\n\t\t\tevent.preventDefault()\n\t\t\tevent.stopPropagation()\n\t\t\tmove(event.key === 'ArrowDown' ? 1 : -1)\n\t\t\treturn\n\t\t}\n\t\tif (event.key === 'Enter') {\n\t\t\tevent.preventDefault()\n\t\t\tconst row = rows[activeRow]\n\t\t\tif (row?.kind === 'option') select(row.option.code)\n\t\t}\n\t}\n\n\tconst optionId = (index: number) => `${listId}-option-${index}`\n\tconst populated = rows.length > 0\n\t// A listbox must own option children, so the list claims the role only once it holds\n\t// options; while empty it is a plain container around the no-results message.\n\tconst listA11y: React.HTMLAttributes<HTMLDivElement> = populated\n\t\t? { 'aria-label': t(keys.selectCountry), role: 'listbox' }\n\t\t: {}\n\n\treturn (\n\t\t// Popup closes itself on any click reaching a button inside it, which unmounts this\n\t\t// panel before React dispatches the row's own onClick and loses the selection\n\t\t// entirely. The rows close through `select` instead, so it opts out.\n\t\t<div className={`${baseClass}__panel`} data-popup-prevent-close>\n\t\t\t<div className={`${baseClass}__search`}>\n\t\t\t\t<span aria-hidden=\"true\" className={`${baseClass}__search-icon`}>\n\t\t\t\t\t<SearchIcon />\n\t\t\t\t</span>\n\t\t\t\t<input\n\t\t\t\t\taria-activedescendant={\n\t\t\t\t\t\trows[activeRow]?.kind === 'option' ? optionId(activeRow) : undefined\n\t\t\t\t\t}\n\t\t\t\t\taria-autocomplete=\"list\"\n\t\t\t\t\taria-controls={populated ? listId : undefined}\n\t\t\t\t\taria-expanded={populated}\n\t\t\t\t\taria-label={t(keys.searchCountries)}\n\t\t\t\t\tclassName={`${baseClass}__search-input`}\n\t\t\t\t\tonChange={(event) => setQuery(event.target.value)}\n\t\t\t\t\tonKeyDown={onKeyDown}\n\t\t\t\t\tplaceholder={t(keys.searchCountries)}\n\t\t\t\t\tref={searchRef}\n\t\t\t\t\trole=\"combobox\"\n\t\t\t\t\ttype=\"text\"\n\t\t\t\t\tvalue={query}\n\t\t\t\t/>\n\t\t\t</div>\n\t\t\t<div\n\t\t\t\tclassName={`${baseClass}__list`}\n\t\t\t\tid={listId}\n\t\t\t\tref={listRef}\n\t\t\t\tstyle={{ maxHeight: LIST_MAX_HEIGHT }}\n\t\t\t\t{...listA11y}\n\t\t\t>\n\t\t\t\t{populated ? (\n\t\t\t\t\t<div\n\t\t\t\t\t\trole=\"presentation\"\n\t\t\t\t\t\tstyle={{ height: virtualizer.getTotalSize(), position: 'relative', width: '100%' }}\n\t\t\t\t\t>\n\t\t\t\t\t\t{virtualizer.getVirtualItems().map((item) => {\n\t\t\t\t\t\t\tconst row = rows[item.index]\n\t\t\t\t\t\t\tif (!row) return null\n\t\t\t\t\t\t\tconst style: React.CSSProperties = {\n\t\t\t\t\t\t\t\theight: `${item.size}px`,\n\t\t\t\t\t\t\t\tleft: 0,\n\t\t\t\t\t\t\t\tposition: 'absolute',\n\t\t\t\t\t\t\t\ttop: 0,\n\t\t\t\t\t\t\t\ttransform: `translateY(${item.start}px)`,\n\t\t\t\t\t\t\t\twidth: '100%',\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (row.kind === 'heading') {\n\t\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\t\t\tclassName={`${baseClass}__group`}\n\t\t\t\t\t\t\t\t\t\tkey=\"heading\"\n\t\t\t\t\t\t\t\t\t\trole=\"presentation\"\n\t\t\t\t\t\t\t\t\t\tstyle={style}\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t{row.label}\n\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (row.kind === 'divider') {\n\t\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\t\t\tclassName={`${baseClass}__group`}\n\t\t\t\t\t\t\t\t\t\tkey=\"divider\"\n\t\t\t\t\t\t\t\t\t\trole=\"presentation\"\n\t\t\t\t\t\t\t\t\t\tstyle={style}\n\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t<button\n\t\t\t\t\t\t\t\t\t// Only a dozen rows are ever in the DOM, so a screen reader would otherwise\n\t\t\t\t\t\t\t\t\t// count the window instead of the list and announce \"1 of 12\" for 250 countries.\n\t\t\t\t\t\t\t\t\taria-posinset={optionRows.indexOf(item.index) + 1}\n\t\t\t\t\t\t\t\t\taria-selected={row.option.code === value}\n\t\t\t\t\t\t\t\t\taria-setsize={optionRows.length}\n\t\t\t\t\t\t\t\t\tclassName={`${baseClass}__option`}\n\t\t\t\t\t\t\t\t\tdata-active={item.index === activeRow || undefined}\n\t\t\t\t\t\t\t\t\tid={optionId(item.index)}\n\t\t\t\t\t\t\t\t\tkey={row.option.code}\n\t\t\t\t\t\t\t\t\tonClick={() => select(row.option.code)}\n\t\t\t\t\t\t\t\t\trole=\"option\"\n\t\t\t\t\t\t\t\t\tstyle={style}\n\t\t\t\t\t\t\t\t\ttabIndex={-1}\n\t\t\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t<CountryFlag code={row.option.code} mode={flags} />\n\t\t\t\t\t\t\t\t\t<span className={`${baseClass}__option-name`}>{row.option.name}</span>\n\t\t\t\t\t\t\t\t\t<span\n\t\t\t\t\t\t\t\t\t\tclassName={`${baseClass}__option-code`}\n\t\t\t\t\t\t\t\t\t>{`+${row.option.callingCode}`}</span>\n\t\t\t\t\t\t\t\t</button>\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t})}\n\t\t\t\t\t</div>\n\t\t\t\t) : (\n\t\t\t\t\t<div className={`${baseClass}__empty`}>{t(keys.noCountriesFound)}</div>\n\t\t\t\t)}\n\t\t\t</div>\n\t\t</div>\n\t)\n}\n\nexport type CountryPickerProps = {\n\tdisabled: boolean\n\tflags: PhoneFlagMode\n\tonSelect: (code: CountryCode) => void\n\toptions: CountryOptionGroups\n\tpriorityLabel?: string\n\tvalue: CountryCode | undefined\n}\n\nexport const CountryPicker: React.FC<CountryPickerProps> = ({\n\tdisabled,\n\tflags,\n\tonSelect,\n\toptions,\n\tpriorityLabel,\n\tvalue,\n}) => {\n\tconst { t } = useTranslation()\n\t// Payload's Popup renders its panel whether or not it is open, so the list is gated on\n\t// this instead: a closed field builds no rows, and reopening starts from a clean search.\n\tconst [open, setOpen] = useState(false)\n\n\tconst selected = useMemo(\n\t\t() => [...options.priority, ...options.rest].find((option) => option.code === value),\n\t\t[options, value]\n\t)\n\tconst label =\n\t\tvalue === undefined\n\t\t\t? t(keys.selectCountry)\n\t\t\t: t(keys.phoneCountryFor, { country: selected?.name ?? value })\n\n\tconst renderFace = () => {\n\t\tif (value === undefined) return null\n\t\tif (flags === 'none') return <span className={`${baseClass}__code`}>{value}</span>\n\t\treturn <CountryFlag code={value} mode={flags} />\n\t}\n\n\treturn (\n\t\t<Popup\n\t\t\tbutton={\n\t\t\t\t<>\n\t\t\t\t\t{renderFace()}\n\t\t\t\t\t<span className={`${baseClass}__sr-only`}>{label}</span>\n\t\t\t\t\t<span aria-hidden=\"true\" className={`${baseClass}__caret`}>\n\t\t\t\t\t\t{/* Unsized, the icon is --base tall and overflows its 12px box downwards */}\n\t\t\t\t\t\t<ChevronIcon size=\"small\" />\n\t\t\t\t\t</span>\n\t\t\t\t</>\n\t\t\t}\n\t\t\tbuttonClassName={`${baseClass}__trigger`}\n\t\t\tbuttonType=\"default\"\n\t\t\tcaret={false}\n\t\t\tclassName={`${baseClass}__picker`}\n\t\t\tdisabled={disabled}\n\t\t\thorizontalAlign=\"left\"\n\t\t\tonToggleClose={() => setOpen(false)}\n\t\t\tonToggleOpen={() => setOpen(true)}\n\t\t\trender={({ close }) =>\n\t\t\t\topen ? (\n\t\t\t\t\t<CountryPanel\n\t\t\t\t\t\tclose={close}\n\t\t\t\t\t\tflags={flags}\n\t\t\t\t\t\tonSelect={onSelect}\n\t\t\t\t\t\toptions={options}\n\t\t\t\t\t\tpriorityLabel={priorityLabel}\n\t\t\t\t\t\tvalue={value}\n\t\t\t\t\t/>\n\t\t\t\t) : null\n\t\t\t}\n\t\t\tsize=\"fit-content\"\n\t\t\tverticalAlign=\"bottom\"\n\t\t/>\n\t)\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAM,YAAY;AAIlB,MAAM,aAAa;AACnB,MAAM,oBAAoB;AAK1B,MAAM,kBAAkB,aAAa;AAYrC,MAAM,gBAAgB,QAAuB,UAA2B;CACvE,IAAI,OAAO,KAAK,YAAY,EAAE,SAAS,KAAK,GAAG,OAAO;CACtD,IAAI,OAAO,KAAK,YAAY,EAAE,WAAW,KAAK,GAAG,OAAO;CACxD,MAAM,SAAS,MAAM,QAAQ,OAAO,EAAE;CACtC,OAAO,WAAW,MAAM,OAAO,YAAY,WAAW,MAAM;AAC7D;AAEA,MAAM,iBAAiB,SAA0B,UAChD,UAAU,KAAK,UAAU,QAAQ,QAAQ,WAAW,aAAa,QAAQ,KAAK,CAAC;;;;;;AAOhF,MAAM,aACL,SACA,OACA,kBACiB;CACjB,MAAM,WAAW,cAAc,QAAQ,UAAU,KAAK;CACtD,MAAM,OAAO,cAAc,QAAQ,MAAM,KAAK;CAC9C,MAAM,QAAQ,SAAS,SAAS,KAAK,KAAK,SAAS;CACnD,OAAO;EACN,GAAI,SAAS,kBAAkB,KAAA,IAC5B,CAAC;GAAE,MAAM;GAAW,O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;GACG;GACD;GACM;GACR;EACP,CAAA,IACE;EAEL,MAAK;EACL,eAAc;CACd,CAAA;AAEH"}
|
|
@@ -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
|