@panneau/fields 5.0.0-alpha.12 → 5.0.0-alpha.13
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/README.md +13 -1
- package/dist/country/CountryField.d.ts +17 -0
- package/dist/country/CountryField.d.ts.map +1 -0
- package/dist/country/CountryField.js +151 -0
- package/dist/country/CountryField.js.map +1 -0
- package/dist/country/codes.d.ts +6 -0
- package/dist/country/codes.d.ts.map +1 -0
- package/dist/country/codes.js +261 -0
- package/dist/country/codes.js.map +1 -0
- package/dist/country/countries.d.ts +32 -0
- package/dist/country/countries.d.ts.map +1 -0
- package/dist/country/countries.js +60 -0
- package/dist/country/countries.js.map +1 -0
- package/dist/country/index.d.ts +3 -0
- package/dist/country/index.d.ts.map +1 -0
- package/dist/country/index.js +4 -0
- package/dist/region/RegionField.d.ts +22 -0
- package/dist/region/RegionField.d.ts.map +1 -0
- package/dist/region/RegionField.js +136 -0
- package/dist/region/RegionField.js.map +1 -0
- package/dist/region/data.d.ts +5 -0
- package/dist/region/data.d.ts.map +1 -0
- package/dist/region/data.js +513 -0
- package/dist/region/data.js.map +1 -0
- package/dist/region/index.d.ts +3 -0
- package/dist/region/index.d.ts.map +1 -0
- package/dist/region/index.js +3 -0
- package/dist/region/regions.d.ts +15 -0
- package/dist/region/regions.d.ts.map +1 -0
- package/dist/region/regions.js +29 -0
- package/dist/region/regions.js.map +1 -0
- package/dist/shared/CodeComboBox.d.ts +20 -0
- package/dist/shared/CodeComboBox.d.ts.map +1 -0
- package/dist/shared/CodeComboBox.js +238 -0
- package/dist/shared/CodeComboBox.js.map +1 -0
- package/dist/shared/builtIn.d.ts.map +1 -1
- package/dist/shared/builtIn.js +5 -1
- package/dist/shared/builtIn.js.map +1 -1
- package/dist/shared/entry.d.ts.map +1 -1
- package/dist/shared/entry.js +1 -1
- package/dist/shared/entry.js.map +1 -1
- package/dist/shared/flow.d.ts.map +1 -1
- package/dist/shared/flow.js +2 -0
- package/dist/shared/flow.js.map +1 -1
- package/dist/tel/TelField.d.ts +16 -5
- package/dist/tel/TelField.d.ts.map +1 -1
- package/dist/tel/TelField.js +253 -51
- package/dist/tel/TelField.js.map +1 -1
- package/dist/tel/index.d.ts +1 -0
- package/dist/tel/index.d.ts.map +1 -1
- package/dist/tel/index.js +3 -1
- package/dist/tel/phone.d.ts +31 -0
- package/dist/tel/phone.d.ts.map +1 -0
- package/dist/tel/phone.js +60 -0
- package/dist/tel/phone.js.map +1 -0
- package/package.json +15 -4
package/dist/tel/TelField.js
CHANGED
|
@@ -1,85 +1,287 @@
|
|
|
1
|
-
import { getFieldProps, isString } from "../shared/entry.js";
|
|
1
|
+
import { getFieldProps, getSetting, isString } from "../shared/entry.js";
|
|
2
|
+
import { messages } from "../shared/messages.js";
|
|
2
3
|
import { TextField } from "../text/TextField.js";
|
|
4
|
+
import { getCountryFlag, getCountryName, getLocaleCountry } from "../country/countries.js";
|
|
5
|
+
import { formatPhoneInput, getCallingCode, getPhoneCountries, parsePhone, readPhone } from "./phone.js";
|
|
3
6
|
import { c } from "react/compiler-runtime";
|
|
7
|
+
import { useState } from "react";
|
|
8
|
+
import { Select, usePanneauIntl } from "@panneau/ui";
|
|
4
9
|
import { jsx } from "react/jsx-runtime";
|
|
5
10
|
//#region src/tel/TelField.tsx
|
|
11
|
+
/** The text shown for a value: a number of the country in its format, any other as it is. */
|
|
12
|
+
function display(value, country) {
|
|
13
|
+
const number = parsePhone(value);
|
|
14
|
+
if (number === null) return value ?? "";
|
|
15
|
+
return number.country !== null && number.country === country ? number.national : number.international;
|
|
16
|
+
}
|
|
6
17
|
/**
|
|
7
|
-
* A phone number,
|
|
8
|
-
*
|
|
18
|
+
* A phone number, in E.164 (`+15145550199`). The country of the number, its flag and its calling
|
|
19
|
+
* code, is in a menu at the start of the input, and the number shows in the format of its country
|
|
20
|
+
* as it is typed. The country follows the number: typing `+33…` picks France.
|
|
21
|
+
*
|
|
22
|
+
* Until a number is complete, the value is the text as typed, which the `tel` rule refuses. A value
|
|
23
|
+
* that isn't E.164, kept from before, shows as it is, and becomes E.164 when the field is left, if
|
|
24
|
+
* it reads as a number of the chosen country.
|
|
9
25
|
*/
|
|
10
26
|
function TelField(t0) {
|
|
11
|
-
const $ = c(
|
|
27
|
+
const $ = c(69);
|
|
28
|
+
let defaultCountry;
|
|
29
|
+
let disabled;
|
|
12
30
|
let onBlur;
|
|
13
31
|
let onChange;
|
|
14
32
|
let props;
|
|
33
|
+
let readOnly;
|
|
15
34
|
let value;
|
|
16
35
|
if ($[0] !== t0) {
|
|
17
|
-
({value, onChange, onBlur, ...props} = t0);
|
|
36
|
+
({value, onChange, onBlur, country: defaultCountry, disabled, readOnly, ...props} = t0);
|
|
18
37
|
$[0] = t0;
|
|
19
|
-
$[1] =
|
|
20
|
-
$[2] =
|
|
21
|
-
$[3] =
|
|
22
|
-
$[4] =
|
|
38
|
+
$[1] = defaultCountry;
|
|
39
|
+
$[2] = disabled;
|
|
40
|
+
$[3] = onBlur;
|
|
41
|
+
$[4] = onChange;
|
|
42
|
+
$[5] = props;
|
|
43
|
+
$[6] = readOnly;
|
|
44
|
+
$[7] = value;
|
|
23
45
|
} else {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
46
|
+
defaultCountry = $[1];
|
|
47
|
+
disabled = $[2];
|
|
48
|
+
onBlur = $[3];
|
|
49
|
+
onChange = $[4];
|
|
50
|
+
props = $[5];
|
|
51
|
+
readOnly = $[6];
|
|
52
|
+
value = $[7];
|
|
28
53
|
}
|
|
54
|
+
const intl = usePanneauIntl();
|
|
55
|
+
const [chosen, setChosen] = useState(null);
|
|
56
|
+
const [draft, setDraft] = useState(null);
|
|
29
57
|
let t1;
|
|
30
|
-
if ($[
|
|
31
|
-
t1 = ()
|
|
32
|
-
|
|
33
|
-
|
|
58
|
+
if ($[8] !== value) {
|
|
59
|
+
t1 = parsePhone(value);
|
|
60
|
+
$[8] = value;
|
|
61
|
+
$[9] = t1;
|
|
62
|
+
} else t1 = $[9];
|
|
63
|
+
const number = t1;
|
|
64
|
+
let t2;
|
|
65
|
+
if ($[10] !== chosen || $[11] !== defaultCountry || $[12] !== intl.locale || $[13] !== number?.country) {
|
|
66
|
+
t2 = number?.country ?? chosen ?? defaultCountry ?? getLocaleCountry(intl.locale) ?? null;
|
|
67
|
+
$[10] = chosen;
|
|
68
|
+
$[11] = defaultCountry;
|
|
69
|
+
$[12] = intl.locale;
|
|
70
|
+
$[13] = number?.country;
|
|
71
|
+
$[14] = t2;
|
|
72
|
+
} else t2 = $[14];
|
|
73
|
+
const country = t2;
|
|
74
|
+
let t3;
|
|
75
|
+
if ($[15] !== country || $[16] !== draft || $[17] !== value) {
|
|
76
|
+
t3 = draft ?? display(value, country);
|
|
77
|
+
$[15] = country;
|
|
78
|
+
$[16] = draft;
|
|
79
|
+
$[17] = value;
|
|
80
|
+
$[18] = t3;
|
|
81
|
+
} else t3 = $[18];
|
|
82
|
+
const shown = t3;
|
|
83
|
+
let t4;
|
|
84
|
+
if ($[19] !== onChange || $[20] !== value) {
|
|
85
|
+
t4 = (next) => {
|
|
86
|
+
if (next !== (value ?? null)) onChange?.(next);
|
|
87
|
+
};
|
|
88
|
+
$[19] = onChange;
|
|
89
|
+
$[20] = value;
|
|
90
|
+
$[21] = t4;
|
|
91
|
+
} else t4 = $[21];
|
|
92
|
+
const emit = t4;
|
|
93
|
+
let T0;
|
|
94
|
+
let t10;
|
|
95
|
+
let t5;
|
|
96
|
+
let t6;
|
|
97
|
+
let t7;
|
|
98
|
+
let t8;
|
|
99
|
+
let t9;
|
|
100
|
+
if ($[22] !== disabled || $[23] !== intl || $[24] !== readOnly) {
|
|
101
|
+
const collator = new Intl.Collator(intl.locale);
|
|
102
|
+
let t11;
|
|
103
|
+
if ($[32] !== intl.locale) {
|
|
104
|
+
t11 = (code) => ({
|
|
105
|
+
code,
|
|
106
|
+
name: getCountryName(code, intl.locale)
|
|
107
|
+
});
|
|
108
|
+
$[32] = intl.locale;
|
|
109
|
+
$[33] = t11;
|
|
110
|
+
} else t11 = $[33];
|
|
111
|
+
const countries = getPhoneCountries().map(t11).sort((a, b) => collator.compare(a.name, b.name));
|
|
112
|
+
T0 = Select;
|
|
113
|
+
if ($[34] !== intl) {
|
|
114
|
+
t5 = intl.formatMessage(messages.telCountry);
|
|
115
|
+
$[34] = intl;
|
|
116
|
+
$[35] = t5;
|
|
117
|
+
} else t5 = $[35];
|
|
118
|
+
t6 = "bare";
|
|
119
|
+
t7 = true;
|
|
120
|
+
t8 = disabled === true || readOnly === true;
|
|
121
|
+
t9 = "+";
|
|
122
|
+
t10 = countries.map(_temp);
|
|
123
|
+
$[22] = disabled;
|
|
124
|
+
$[23] = intl;
|
|
125
|
+
$[24] = readOnly;
|
|
126
|
+
$[25] = T0;
|
|
127
|
+
$[26] = t10;
|
|
128
|
+
$[27] = t5;
|
|
129
|
+
$[28] = t6;
|
|
130
|
+
$[29] = t7;
|
|
131
|
+
$[30] = t8;
|
|
132
|
+
$[31] = t9;
|
|
133
|
+
} else {
|
|
134
|
+
T0 = $[25];
|
|
135
|
+
t10 = $[26];
|
|
136
|
+
t5 = $[27];
|
|
137
|
+
t6 = $[28];
|
|
138
|
+
t7 = $[29];
|
|
139
|
+
t8 = $[30];
|
|
140
|
+
t9 = $[31];
|
|
141
|
+
}
|
|
142
|
+
let t11;
|
|
143
|
+
if ($[36] !== emit || $[37] !== number?.nationalNumber || $[38] !== shown) {
|
|
144
|
+
t11 = (next_0) => {
|
|
145
|
+
const code_1 = next_0 !== null ? String(next_0) : null;
|
|
146
|
+
setChosen(code_1);
|
|
147
|
+
setDraft(null);
|
|
148
|
+
const digits = number?.nationalNumber ?? shown.replace(/^\+/, "");
|
|
149
|
+
emit(readPhone(digits, code_1).value);
|
|
150
|
+
};
|
|
151
|
+
$[36] = emit;
|
|
152
|
+
$[37] = number?.nationalNumber;
|
|
153
|
+
$[38] = shown;
|
|
154
|
+
$[39] = t11;
|
|
155
|
+
} else t11 = $[39];
|
|
156
|
+
let t12;
|
|
157
|
+
if ($[40] !== T0 || $[41] !== country || $[42] !== t10 || $[43] !== t11 || $[44] !== t5 || $[45] !== t6 || $[46] !== t7 || $[47] !== t8 || $[48] !== t9) {
|
|
158
|
+
t12 = /* @__PURE__ */ jsx(T0, {
|
|
159
|
+
"aria-label": t5,
|
|
160
|
+
appearance: t6,
|
|
161
|
+
isRequired: t7,
|
|
162
|
+
isDisabled: t8,
|
|
163
|
+
placeholder: t9,
|
|
164
|
+
options: t10,
|
|
165
|
+
value: country,
|
|
166
|
+
renderValue: _temp2,
|
|
167
|
+
onChange: t11
|
|
168
|
+
});
|
|
169
|
+
$[40] = T0;
|
|
170
|
+
$[41] = country;
|
|
171
|
+
$[42] = t10;
|
|
172
|
+
$[43] = t11;
|
|
173
|
+
$[44] = t5;
|
|
174
|
+
$[45] = t6;
|
|
175
|
+
$[46] = t7;
|
|
176
|
+
$[47] = t8;
|
|
177
|
+
$[48] = t9;
|
|
178
|
+
$[49] = t12;
|
|
179
|
+
} else t12 = $[49];
|
|
180
|
+
const picker = t12;
|
|
181
|
+
let t13;
|
|
182
|
+
if ($[50] !== country || $[51] !== emit || $[52] !== shown) {
|
|
183
|
+
t13 = (text) => {
|
|
184
|
+
const typed = text ?? "";
|
|
185
|
+
const next_1 = typed.length > shown.length && typed.startsWith(shown) ? formatPhoneInput(typed, country) : typed;
|
|
186
|
+
setDraft(next_1);
|
|
187
|
+
const read = readPhone(next_1, country);
|
|
188
|
+
if (read.country !== null && read.country !== country) setChosen(read.country);
|
|
189
|
+
emit(read.value);
|
|
190
|
+
};
|
|
191
|
+
$[50] = country;
|
|
192
|
+
$[51] = emit;
|
|
193
|
+
$[52] = shown;
|
|
194
|
+
$[53] = t13;
|
|
195
|
+
} else t13 = $[53];
|
|
196
|
+
let t14;
|
|
197
|
+
if ($[54] !== country || $[55] !== disabled || $[56] !== emit || $[57] !== onBlur || $[58] !== readOnly || $[59] !== value) {
|
|
198
|
+
t14 = () => {
|
|
199
|
+
setDraft(null);
|
|
200
|
+
if (readOnly !== true && disabled !== true && isString(value) && parsePhone(value) === null) emit(readPhone(value, country).value);
|
|
34
201
|
onBlur?.();
|
|
35
202
|
};
|
|
36
|
-
$[
|
|
37
|
-
$[
|
|
38
|
-
$[
|
|
39
|
-
$[
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
203
|
+
$[54] = country;
|
|
204
|
+
$[55] = disabled;
|
|
205
|
+
$[56] = emit;
|
|
206
|
+
$[57] = onBlur;
|
|
207
|
+
$[58] = readOnly;
|
|
208
|
+
$[59] = value;
|
|
209
|
+
$[60] = t14;
|
|
210
|
+
} else t14 = $[60];
|
|
211
|
+
let t15;
|
|
212
|
+
if ($[61] !== disabled || $[62] !== picker || $[63] !== props || $[64] !== readOnly || $[65] !== shown || $[66] !== t13 || $[67] !== t14) {
|
|
213
|
+
t15 = /* @__PURE__ */ jsx(TextField, {
|
|
44
214
|
autoComplete: "tel",
|
|
45
215
|
...props,
|
|
216
|
+
disabled,
|
|
217
|
+
readOnly,
|
|
46
218
|
type: "tel",
|
|
47
219
|
inputMode: "tel",
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
220
|
+
prefix: picker,
|
|
221
|
+
value: shown,
|
|
222
|
+
onChange: t13,
|
|
223
|
+
onBlur: t14
|
|
51
224
|
});
|
|
52
|
-
$[
|
|
53
|
-
$[
|
|
54
|
-
$[
|
|
55
|
-
$[
|
|
56
|
-
$[
|
|
57
|
-
|
|
58
|
-
|
|
225
|
+
$[61] = disabled;
|
|
226
|
+
$[62] = picker;
|
|
227
|
+
$[63] = props;
|
|
228
|
+
$[64] = readOnly;
|
|
229
|
+
$[65] = shown;
|
|
230
|
+
$[66] = t13;
|
|
231
|
+
$[67] = t14;
|
|
232
|
+
$[68] = t15;
|
|
233
|
+
} else t15 = $[68];
|
|
234
|
+
return t15;
|
|
235
|
+
}
|
|
236
|
+
/** The `tel` type: `settings.country` is the country of a new number. */
|
|
237
|
+
function _temp2(option) {
|
|
238
|
+
return `+${getCallingCode(String(option.value)) ?? ""}`;
|
|
239
|
+
}
|
|
240
|
+
function _temp(t0) {
|
|
241
|
+
const { code: code_0, name } = t0;
|
|
242
|
+
return {
|
|
243
|
+
value: code_0,
|
|
244
|
+
label: `${name} +${getCallingCode(code_0) ?? ""}`,
|
|
245
|
+
prefix: getCountryFlag(code_0)
|
|
246
|
+
};
|
|
59
247
|
}
|
|
60
|
-
/** The `tel` type. */
|
|
61
248
|
function TelFieldEntry(props) {
|
|
62
|
-
const $ = c(
|
|
249
|
+
const $ = c(11);
|
|
63
250
|
let t0;
|
|
64
|
-
if ($[0] !== props) {
|
|
65
|
-
t0 =
|
|
66
|
-
$[0] = props;
|
|
251
|
+
if ($[0] !== props.field) {
|
|
252
|
+
t0 = getSetting(props.field, "country", isString);
|
|
253
|
+
$[0] = props.field;
|
|
67
254
|
$[1] = t0;
|
|
68
255
|
} else t0 = $[1];
|
|
69
|
-
const
|
|
256
|
+
const country = t0;
|
|
257
|
+
let t1;
|
|
258
|
+
if ($[2] !== props) {
|
|
259
|
+
t1 = getFieldProps(props);
|
|
260
|
+
$[2] = props;
|
|
261
|
+
$[3] = t1;
|
|
262
|
+
} else t1 = $[3];
|
|
70
263
|
let t2;
|
|
71
|
-
if ($[
|
|
72
|
-
t2 =
|
|
73
|
-
|
|
74
|
-
value: t1,
|
|
75
|
-
onChange: props.onChange
|
|
76
|
-
});
|
|
77
|
-
$[2] = props.onChange;
|
|
78
|
-
$[3] = t0;
|
|
79
|
-
$[4] = t1;
|
|
264
|
+
if ($[4] !== country) {
|
|
265
|
+
t2 = country !== void 0 ? { country } : {};
|
|
266
|
+
$[4] = country;
|
|
80
267
|
$[5] = t2;
|
|
81
268
|
} else t2 = $[5];
|
|
82
|
-
|
|
269
|
+
const t3 = isString(props.value) ? props.value : null;
|
|
270
|
+
let t4;
|
|
271
|
+
if ($[6] !== props.onChange || $[7] !== t1 || $[8] !== t2 || $[9] !== t3) {
|
|
272
|
+
t4 = /* @__PURE__ */ jsx(TelField, {
|
|
273
|
+
...t1,
|
|
274
|
+
...t2,
|
|
275
|
+
value: t3,
|
|
276
|
+
onChange: props.onChange
|
|
277
|
+
});
|
|
278
|
+
$[6] = props.onChange;
|
|
279
|
+
$[7] = t1;
|
|
280
|
+
$[8] = t2;
|
|
281
|
+
$[9] = t3;
|
|
282
|
+
$[10] = t4;
|
|
283
|
+
} else t4 = $[10];
|
|
284
|
+
return t4;
|
|
83
285
|
}
|
|
84
286
|
//#endregion
|
|
85
287
|
export { TelField, TelFieldEntry };
|
package/dist/tel/TelField.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TelField.js","names":["getFieldProps","isString","FieldEntryProps","TextField","TextFieldProps","TelFieldProps","Omit","TelField","t0","$","_c","onBlur","onChange","props","value","t1","trimmed","trim","t2","TelFieldEntry","_c"],"sources":["../../src/tel/TelField.tsx"],"sourcesContent":["import { getFieldProps, isString } from '../shared/entry';\nimport type { FieldEntryProps } from '../shared/types';\nimport { TextField, type TextFieldProps } from '../text/TextField';\n\nexport type TelFieldProps = Omit<TextFieldProps, 'type' | 'inputMode'>;\n\n/**\n * A phone number, as typed, with the phone keyboard on phones. Spaces around it are removed on\n * blur. A `pattern` rule constrains its format when a project needs one.\n */\nexport function TelField({ value, onChange, onBlur, ...props }: TelFieldProps) {\n return (\n <TextField\n autoComplete=\"tel\"\n {...props}\n type=\"tel\"\n inputMode=\"tel\"\n value={value}\n onChange={onChange}\n onBlur={() => {\n const trimmed = value?.trim() ?? null;\n if (trimmed !== value) {\n onChange?.(trimmed === '' ? null : trimmed);\n }\n onBlur?.();\n }}\n />\n );\n}\n\n/** The `tel` type. */\nexport function TelFieldEntry(props: FieldEntryProps) {\n return (\n <TelField\n {...getFieldProps(props)}\n value={isString(props.value) ? props.value : null}\n onChange={props.onChange}\n />\n );\n}\n"],"mappings":";;;;;;;;;AAUA,SAAOO,SAAAC,IAAA;CAAA,MA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KAAAD;CAAA,OAAAA,KAAAC,EAAA;CACjB,MAAAM,KAAAd,SAASY,MAAKC,KAA2B,IAAjBD,MAAKC,QAA7B;CAA0C,IAAAI;CAAA,IAAAT,EAAA,OAAAI,MAAAD,YAAAH,EAAA,OAAAD,MAAAC,EAAA,OAAAM,IAAA;EAFrDG,KAAA,oBAAC,UAAD;GAAS,GACDV;GACG,OAAAO;GACG,UAAAF,MAAKD;EAAS,CAAA;EAC1BH,EAAA,KAAAI,MAAAD;EAAAH,EAAA,KAAAD;EAAAC,EAAA,KAAAM;EAAAN,EAAA,KAAAS;CAAA,OAAAA,KAAAT,EAAA;CAAA,OAJFS;AAIE"}
|
|
1
|
+
{"version":3,"file":"TelField.js","names":["useState","Select","usePanneauIntl","getCountryFlag","getCountryName","getLocaleCountry","getFieldProps","getSetting","isString","messages","FieldEntryProps","TextField","TextFieldProps","formatPhoneInput","getCallingCode","getPhoneCountries","parsePhone","readPhone","TelFieldProps","Omit","country","display","value","number","national","international","TelField","t0","$","_c","defaultCountry","disabled","onBlur","onChange","props","readOnly","intl","chosen","setChosen","draft","setDraft","t1","t2","locale","t3","shown","t4","next","emit","T0","t10","t5","t6","t7","t8","t9","collator","Intl","Collator","t11","code","name","countries","map","sort","a","b","compare","formatMessage","telCountry","_temp","nationalNumber","next_0","code_1","String","digits","replace","t12","_temp2","picker","t13","text","typed","next_1","length","startsWith","read","t14","t15","option","code_0","label","prefix","TelFieldEntry","field","undefined","_c"],"sources":["../../src/tel/TelField.tsx"],"sourcesContent":["import { useState } from 'react';\n\nimport { Select, usePanneauIntl } from '@panneau/ui';\n\nimport { getCountryFlag, getCountryName, getLocaleCountry } from '../country/countries';\nimport { getFieldProps, getSetting, isString } from '../shared/entry';\nimport { messages } from '../shared/messages';\nimport type { FieldEntryProps } from '../shared/types';\nimport { TextField, type TextFieldProps } from '../text/TextField';\nimport {\n formatPhoneInput,\n getCallingCode,\n getPhoneCountries,\n parsePhone,\n readPhone,\n} from './phone';\n\nexport interface TelFieldProps extends Omit<\n TextFieldProps,\n 'type' | 'inputMode' | 'prefix' | 'maxLength' | 'secret'\n> {\n /**\n * The country of a new number (`CA`). Without it, the region of the user's locale (`fr-CA`\n * gives `CA`).\n */\n country?: string | null;\n}\n\n/** The text shown for a value: a number of the country in its format, any other as it is. */\nfunction display(value: string | null | undefined, country: string | null): string {\n const number = parsePhone(value);\n if (number === null) {\n return value ?? '';\n }\n return number.country !== null && number.country === country\n ? number.national\n : number.international;\n}\n\n/**\n * A phone number, in E.164 (`+15145550199`). The country of the number, its flag and its calling\n * code, is in a menu at the start of the input, and the number shows in the format of its country\n * as it is typed. The country follows the number: typing `+33…` picks France.\n *\n * Until a number is complete, the value is the text as typed, which the `tel` rule refuses. A value\n * that isn't E.164, kept from before, shows as it is, and becomes E.164 when the field is left, if\n * it reads as a number of the chosen country.\n */\nexport function TelField({\n value,\n onChange,\n onBlur,\n country: defaultCountry,\n disabled,\n readOnly,\n ...props\n}: TelFieldProps) {\n const intl = usePanneauIntl();\n // The country picked in the menu, or found in a number typed with its calling code.\n const [chosen, setChosen] = useState<string | null>(null);\n // The text being typed, while the input has the focus.\n const [draft, setDraft] = useState<string | null>(null);\n const number = parsePhone(value);\n const country =\n number?.country ?? chosen ?? defaultCountry ?? getLocaleCountry(intl.locale) ?? null;\n const shown = draft ?? display(value, country);\n\n const emit = (next: string | null) => {\n if (next !== (value ?? null)) {\n onChange?.(next);\n }\n };\n\n const collator = new Intl.Collator(intl.locale);\n const countries = getPhoneCountries()\n .map((code) => ({ code, name: getCountryName(code, intl.locale) }))\n .sort((a, b) => collator.compare(a.name, b.name));\n\n const picker = (\n <Select\n aria-label={intl.formatMessage(messages.telCountry)}\n appearance=\"bare\"\n isRequired\n isDisabled={disabled === true || readOnly === true}\n placeholder=\"+\"\n options={countries.map(({ code, name }) => ({\n value: code,\n label: `${name} +${getCallingCode(code) ?? ''}`,\n prefix: getCountryFlag(code),\n }))}\n value={country}\n renderValue={(option) => `+${getCallingCode(String(option.value)) ?? ''}`}\n onChange={(next) => {\n const code = next !== null ? String(next) : null;\n setChosen(code);\n setDraft(null);\n // The same digits, in the country picked.\n const digits = number?.nationalNumber ?? shown.replace(/^\\+/, '');\n emit(readPhone(digits, code).value);\n }}\n />\n );\n\n return (\n <TextField\n autoComplete=\"tel\"\n {...props}\n disabled={disabled}\n readOnly={readOnly}\n type=\"tel\"\n inputMode=\"tel\"\n prefix={picker}\n value={shown}\n onChange={(text) => {\n const typed = text ?? '';\n // Characters added at the end are formatted as they are typed; other edits stay as\n // they are, so that the cursor doesn't jump.\n const next =\n typed.length > shown.length && typed.startsWith(shown)\n ? formatPhoneInput(typed, country)\n : typed;\n setDraft(next);\n const read = readPhone(next, country);\n if (read.country !== null && read.country !== country) {\n setChosen(read.country);\n }\n emit(read.value);\n }}\n onBlur={() => {\n setDraft(null);\n // A value as typed becomes E.164 when it reads as a number of the country, unless\n // the field can't be changed.\n if (\n readOnly !== true &&\n disabled !== true &&\n isString(value) &&\n parsePhone(value) === null\n ) {\n emit(readPhone(value, country).value);\n }\n onBlur?.();\n }}\n />\n );\n}\n\n/** The `tel` type: `settings.country` is the country of a new number. */\nexport function TelFieldEntry(props: FieldEntryProps) {\n const country = getSetting(props.field, 'country', isString);\n return (\n <TelField\n {...getFieldProps(props)}\n {...(country !== undefined ? { country } : {})}\n value={isString(props.value) ? props.value : null}\n onChange={props.onChange}\n />\n );\n}\n"],"mappings":";;;;;;;;;;;AA6BA,SAASqB,QAAQC,OAAkCF,SAAgC;CAC/E,MAAMG,SAASP,WAAWM,KAAK;CAC/B,IAAIC,WAAW,MACX,OAAOD,SAAS;CAEpB,OAAOC,OAAOH,YAAY,QAAQG,OAAOH,YAAYA,UAC/CG,OAAOC,WACPD,OAAOE;AACjB;;;;;;;;;;AAWA,SAAOC,SAAAC,IAAA;CAAA,MAAAC,IAAAC,EAAA,EAAA;CAAA,IAAAC;CAAA,IAAAC;CAAA,IAAAC;CAAA,IAAAC;CAAA,IAAAC;CAAA,IAA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QAAQ;EAACoB,EAAA,KAAAM,MAAA4D;EAAAlE,EAAA,KAAAD;CAAA,OAAAA,KAAAC,EAAA;CAA5D,MAAAR,UAAgBO;CAA6C,IAAAc;CAAA,IAAAb,EAAA,OAAAM,OAAA;EAGjDO,KAAAnC,cAAc4B,KAAK;EAACN,EAAA,KAAAM;EAAAN,EAAA,KAAAa;CAAA,OAAAA,KAAAb,EAAA;CAAA,IAAAc;CAAA,IAAAd,EAAA,OAAAR,SAAA;EACnBsB,KAAAtB,YAAY2E,KAAAA,IAAZ,EAAA3E,QAAuC,IAAvC,CAAuC;EAACQ,EAAA,KAAAR;EAAAQ,EAAA,KAAAc;CAAA,OAAAA,KAAAd,EAAA;CACtC,MAAAgB,KAAApC,SAAS0B,MAAKZ,KAA2B,IAAjBY,MAAKZ,QAA7B;CAA0C,IAAAwB;CAAA,IAAAlB,EAAA,OAAAM,MAAAD,YAAAL,EAAA,OAAAa,MAAAb,EAAA,OAAAc,MAAAd,EAAA,OAAAgB,IAAA;EAHrDE,KAAA,oBAAC,UAAD;GAAS,GACDL;GAAoB,GACnBC;GACE,OAAAE;GACG,UAAAV,MAAKD;EAAS,CAAA;EAC1BL,EAAA,KAAAM,MAAAD;EAAAL,EAAA,KAAAa;EAAAb,EAAA,KAAAc;EAAAd,EAAA,KAAAgB;EAAAhB,EAAA,MAAAkB;CAAA,OAAAA,KAAAlB,EAAA;CAAA,OALFkB;AAKE"}
|
package/dist/tel/index.d.ts
CHANGED
package/dist/tel/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tel/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tel/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACzE,OAAO,EACH,KAAK,WAAW,EAChB,gBAAgB,EAChB,cAAc,EACd,iBAAiB,EACjB,UAAU,EACV,SAAS,GACZ,MAAM,SAAS,CAAC"}
|
package/dist/tel/index.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
import { formatPhoneInput, getCallingCode, getPhoneCountries, parsePhone, readPhone } from "./phone.js";
|
|
2
|
+
import { TelField, TelFieldEntry } from "./TelField.js";
|
|
3
|
+
export { TelField, TelFieldEntry, formatPhoneInput, getCallingCode, getPhoneCountries, parsePhone, readPhone };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** A phone number read from an E.164 value. */
|
|
2
|
+
export interface PhoneNumber {
|
|
3
|
+
/** The value, in E.164: `+15145550199`. */
|
|
4
|
+
number: string;
|
|
5
|
+
/** The country of the number, or `null` for a number of no country (`+800`). */
|
|
6
|
+
country: string | null;
|
|
7
|
+
/** The digits after the calling code: `5145550199`. */
|
|
8
|
+
nationalNumber: string;
|
|
9
|
+
/** In the format of its country: `(514) 555-0199`. */
|
|
10
|
+
national: string;
|
|
11
|
+
/** With its calling code: `+1 514 555 0199`. */
|
|
12
|
+
international: string;
|
|
13
|
+
}
|
|
14
|
+
/** A value in E.164 that is a valid number, read. `null` for any other value. */
|
|
15
|
+
export declare function parsePhone(value: unknown): PhoneNumber | null;
|
|
16
|
+
/**
|
|
17
|
+
* The value of a typed number: in E.164 once it is a valid number of its country, the text as
|
|
18
|
+
* typed (trimmed) until then, or `null` when empty. A number typed with its calling code (`+33…`)
|
|
19
|
+
* gives its own country.
|
|
20
|
+
*/
|
|
21
|
+
export declare function readPhone(text: string, country: string | null): {
|
|
22
|
+
value: string | null;
|
|
23
|
+
country: string | null;
|
|
24
|
+
};
|
|
25
|
+
/** A number being typed, in the format of its country: `514555` gives `(514) 555`. */
|
|
26
|
+
export declare function formatPhoneInput(text: string, country: string | null): string;
|
|
27
|
+
/** The calling code of a country, without `+` (`1` for `CA`), or `null`. */
|
|
28
|
+
export declare function getCallingCode(country: string | null | undefined): string | null;
|
|
29
|
+
/** The countries that have calling codes. */
|
|
30
|
+
export declare function getPhoneCountries(): string[];
|
|
31
|
+
//# sourceMappingURL=phone.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"phone.d.ts","sourceRoot":"","sources":["../../src/tel/phone.ts"],"names":[],"mappings":"AAYA,+CAA+C;AAC/C,MAAM,WAAW,WAAW;IACxB,2CAA2C;IAC3C,MAAM,EAAE,MAAM,CAAC;IACf,gFAAgF;IAChF,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,uDAAuD;IACvD,cAAc,EAAE,MAAM,CAAC;IACvB,sDAAsD;IACtD,QAAQ,EAAE,MAAM,CAAC;IACjB,gDAAgD;IAChD,aAAa,EAAE,MAAM,CAAC;CACzB;AAQD,iFAAiF;AACjF,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,GAAG,IAAI,CAe7D;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CACrB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,GAAG,IAAI,GACvB;IAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAelD;AAED,sFAAsF;AACtF,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAE7E;AAED,4EAA4E;AAC5E,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAGhF;AAED,6CAA6C;AAC7C,wBAAgB,iBAAiB,IAAI,MAAM,EAAE,CAE5C"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { AsYouType, getCountries, getCountryCallingCode, isSupportedCountry, parsePhoneNumberFromString } from "libphonenumber-js/min";
|
|
2
|
+
//#region src/tel/phone.ts
|
|
3
|
+
/** An international number in E.164, as the `tel` rule of the protocol checks it. */
|
|
4
|
+
var E164 = /^\+[1-9]\d{6,14}$/;
|
|
5
|
+
function asCountry(country) {
|
|
6
|
+
return country !== null && country !== void 0 && isSupportedCountry(country) ? country : void 0;
|
|
7
|
+
}
|
|
8
|
+
/** A value in E.164 that is a valid number, read. `null` for any other value. */
|
|
9
|
+
function parsePhone(value) {
|
|
10
|
+
if (typeof value !== "string" || !E164.test(value)) return null;
|
|
11
|
+
const number = parsePhoneNumberFromString(value);
|
|
12
|
+
if (number === void 0 || !number.isValid()) return null;
|
|
13
|
+
return {
|
|
14
|
+
number: number.number,
|
|
15
|
+
country: number.country ?? null,
|
|
16
|
+
nationalNumber: number.nationalNumber,
|
|
17
|
+
national: number.formatNational(),
|
|
18
|
+
international: number.formatInternational()
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The value of a typed number: in E.164 once it is a valid number of its country, the text as
|
|
23
|
+
* typed (trimmed) until then, or `null` when empty. A number typed with its calling code (`+33…`)
|
|
24
|
+
* gives its own country.
|
|
25
|
+
*/
|
|
26
|
+
function readPhone(text, country) {
|
|
27
|
+
const trimmed = text.trim();
|
|
28
|
+
if (trimmed === "") return {
|
|
29
|
+
value: null,
|
|
30
|
+
country: null
|
|
31
|
+
};
|
|
32
|
+
const typer = new AsYouType(asCountry(country));
|
|
33
|
+
typer.input(trimmed);
|
|
34
|
+
const number = typer.getNumber();
|
|
35
|
+
if (number?.isValid() === true) return {
|
|
36
|
+
value: number.number,
|
|
37
|
+
country: number.country ?? null
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
value: trimmed,
|
|
41
|
+
country: typer.isInternational() ? typer.getCountry() ?? null : null
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** A number being typed, in the format of its country: `514555` gives `(514) 555`. */
|
|
45
|
+
function formatPhoneInput(text, country) {
|
|
46
|
+
return new AsYouType(asCountry(country)).input(text);
|
|
47
|
+
}
|
|
48
|
+
/** The calling code of a country, without `+` (`1` for `CA`), or `null`. */
|
|
49
|
+
function getCallingCode(country) {
|
|
50
|
+
const code = asCountry(country);
|
|
51
|
+
return code !== void 0 ? getCountryCallingCode(code) : null;
|
|
52
|
+
}
|
|
53
|
+
/** The countries that have calling codes. */
|
|
54
|
+
function getPhoneCountries() {
|
|
55
|
+
return getCountries();
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { formatPhoneInput, getCallingCode, getPhoneCountries, parsePhone, readPhone };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=phone.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"phone.js","names":["AsYouType","CountryCode","getCountries","getCountryCallingCode","isSupportedCountry","parsePhoneNumberFromString","E164","PhoneNumber","number","country","nationalNumber","national","international","asCountry","undefined","parsePhone","value","test","isValid","formatNational","formatInternational","readPhone","text","trimmed","trim","typer","input","getNumber","isInternational","getCountry","formatPhoneInput","getCallingCode","code","getPhoneCountries"],"sources":["../../src/tel/phone.ts"],"sourcesContent":["import {\n AsYouType,\n type CountryCode,\n getCountries,\n getCountryCallingCode,\n isSupportedCountry,\n parsePhoneNumberFromString,\n} from 'libphonenumber-js/min';\n\n/** An international number in E.164, as the `tel` rule of the protocol checks it. */\nconst E164 = /^\\+[1-9]\\d{6,14}$/;\n\n/** A phone number read from an E.164 value. */\nexport interface PhoneNumber {\n /** The value, in E.164: `+15145550199`. */\n number: string;\n /** The country of the number, or `null` for a number of no country (`+800`). */\n country: string | null;\n /** The digits after the calling code: `5145550199`. */\n nationalNumber: string;\n /** In the format of its country: `(514) 555-0199`. */\n national: string;\n /** With its calling code: `+1 514 555 0199`. */\n international: string;\n}\n\nfunction asCountry(country: string | null | undefined): CountryCode | undefined {\n return country !== null && country !== undefined && isSupportedCountry(country)\n ? country\n : undefined;\n}\n\n/** A value in E.164 that is a valid number, read. `null` for any other value. */\nexport function parsePhone(value: unknown): PhoneNumber | null {\n if (typeof value !== 'string' || !E164.test(value)) {\n return null;\n }\n const number = parsePhoneNumberFromString(value);\n if (number === undefined || !number.isValid()) {\n return null;\n }\n return {\n number: number.number,\n country: number.country ?? null,\n nationalNumber: number.nationalNumber,\n national: number.formatNational(),\n international: number.formatInternational(),\n };\n}\n\n/**\n * The value of a typed number: in E.164 once it is a valid number of its country, the text as\n * typed (trimmed) until then, or `null` when empty. A number typed with its calling code (`+33…`)\n * gives its own country.\n */\nexport function readPhone(\n text: string,\n country: string | null,\n): { value: string | null; country: string | null } {\n const trimmed = text.trim();\n if (trimmed === '') {\n return { value: null, country: null };\n }\n const typer = new AsYouType(asCountry(country));\n typer.input(trimmed);\n const number = typer.getNumber();\n if (number?.isValid() === true) {\n return { value: number.number, country: number.country ?? null };\n }\n return {\n value: trimmed,\n country: typer.isInternational() ? (typer.getCountry() ?? null) : null,\n };\n}\n\n/** A number being typed, in the format of its country: `514555` gives `(514) 555`. */\nexport function formatPhoneInput(text: string, country: string | null): string {\n return new AsYouType(asCountry(country)).input(text);\n}\n\n/** The calling code of a country, without `+` (`1` for `CA`), or `null`. */\nexport function getCallingCode(country: string | null | undefined): string | null {\n const code = asCountry(country);\n return code !== undefined ? getCountryCallingCode(code) : null;\n}\n\n/** The countries that have calling codes. */\nexport function getPhoneCountries(): string[] {\n return getCountries();\n}\n"],"mappings":";;;AAUA,IAAMM,OAAO;AAgBb,SAASO,UAAUJ,SAA6D;CAC5E,OAAOA,YAAY,QAAQA,YAAYK,KAAAA,KAAaV,mBAAmBK,OAAO,IACxEA,UACAK,KAAAA;AACV;;AAGA,SAAgBC,WAAWC,OAAoC;CAC3D,IAAI,OAAOA,UAAU,YAAY,CAACV,KAAKW,KAAKD,KAAK,GAC7C,OAAO;CAEX,MAAMR,SAASH,2BAA2BW,KAAK;CAC/C,IAAIR,WAAWM,KAAAA,KAAa,CAACN,OAAOU,QAAQ,GACxC,OAAO;CAEX,OAAO;EACHV,QAAQA,OAAOA;EACfC,SAASD,OAAOC,WAAW;EAC3BC,gBAAgBF,OAAOE;EACvBC,UAAUH,OAAOW,eAAe;EAChCP,eAAeJ,OAAOY,oBAAoB;CAC9C;AACJ;;;;;;AAOA,SAAgBC,UACZC,MACAb,SACgD;CAChD,MAAMc,UAAUD,KAAKE,KAAK;CAC1B,IAAID,YAAY,IACZ,OAAO;EAAEP,OAAO;EAAMP,SAAS;CAAK;CAExC,MAAMgB,QAAQ,IAAIzB,UAAUa,UAAUJ,OAAO,CAAC;CAC9CgB,MAAMC,MAAMH,OAAO;CACnB,MAAMf,SAASiB,MAAME,UAAU;CAC/B,IAAInB,QAAQU,QAAQ,MAAM,MACtB,OAAO;EAAEF,OAAOR,OAAOA;EAAQC,SAASD,OAAOC,WAAW;CAAK;CAEnE,OAAO;EACHO,OAAOO;EACPd,SAASgB,MAAMG,gBAAgB,IAAKH,MAAMI,WAAW,KAAK,OAAQ;CACtE;AACJ;;AAGA,SAAgBC,iBAAiBR,MAAcb,SAAgC;CAC3E,OAAO,IAAIT,UAAUa,UAAUJ,OAAO,CAAC,CAAC,CAACiB,MAAMJ,IAAI;AACvD;;AAGA,SAAgBS,eAAetB,SAAmD;CAC9E,MAAMuB,OAAOnB,UAAUJ,OAAO;CAC9B,OAAOuB,SAASlB,KAAAA,IAAYX,sBAAsB6B,IAAI,IAAI;AAC9D;;AAGA,SAAgBC,oBAA8B;CAC1C,OAAO/B,aAAa;AACxB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@panneau/fields",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.13",
|
|
4
4
|
"description": "Controlled field components of Panneau for the protocol's built-in field types, one subpath per field, and uploads to storage (Uppy 6 in its own entry point).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -108,6 +108,16 @@
|
|
|
108
108
|
"types": "./dist/embed/index.d.ts",
|
|
109
109
|
"default": "./dist/embed/index.js"
|
|
110
110
|
},
|
|
111
|
+
"./country": {
|
|
112
|
+
"@panneau/source": "./src/country/index.ts",
|
|
113
|
+
"types": "./dist/country/index.d.ts",
|
|
114
|
+
"default": "./dist/country/index.js"
|
|
115
|
+
},
|
|
116
|
+
"./region": {
|
|
117
|
+
"@panneau/source": "./src/region/index.ts",
|
|
118
|
+
"types": "./dist/region/index.d.ts",
|
|
119
|
+
"default": "./dist/region/index.js"
|
|
120
|
+
},
|
|
111
121
|
"./location": {
|
|
112
122
|
"@panneau/source": "./src/location/index.ts",
|
|
113
123
|
"types": "./dist/location/index.d.ts",
|
|
@@ -178,10 +188,11 @@
|
|
|
178
188
|
},
|
|
179
189
|
"dependencies": {
|
|
180
190
|
"@internationalized/date": "^3.12.4",
|
|
181
|
-
"@panneau/intl": "5.0.0-alpha.
|
|
182
|
-
"@panneau/protocol": "5.0.0-alpha.
|
|
183
|
-
"@panneau/ui": "5.0.0-alpha.
|
|
191
|
+
"@panneau/intl": "5.0.0-alpha.13",
|
|
192
|
+
"@panneau/protocol": "5.0.0-alpha.13",
|
|
193
|
+
"@panneau/ui": "5.0.0-alpha.13",
|
|
184
194
|
"leaflet": "^1.9.4",
|
|
195
|
+
"libphonenumber-js": "^1.13.15",
|
|
185
196
|
"lucide-react": "^1.57.0"
|
|
186
197
|
},
|
|
187
198
|
"devDependencies": {
|