@superdispatch/phones 0.25.2 → 0.26.1

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/dist-web/index.js CHANGED
@@ -13,13 +13,11 @@ var DEFAULT_COUNTRY = 'US';
13
13
  var COUNTRIES = [];
14
14
  var COUNTRY_CODE_MAP = /*#__PURE__*/new Map();
15
15
  var COUNTRY_NAME_MAP = /*#__PURE__*/new Map();
16
-
17
16
  for (var [country, code, name] of COUNTRY_CODE_METADATA) {
18
17
  COUNTRIES.push(country);
19
18
  COUNTRY_CODE_MAP.set(country, code);
20
19
  COUNTRY_NAME_MAP.set(country, name);
21
20
  }
22
-
23
21
  function listCountries() {
24
22
  return COUNTRIES;
25
23
  }
@@ -39,14 +37,17 @@ function formatCountry(country) {
39
37
  var loadError;
40
38
  var loadedAPN;
41
39
  function loadAPN() {
42
- return import(
43
- /* webpackPrefetch: true */
44
-
40
+ return import( /* webpackPrefetch: true */
45
41
  /* webpackChunkName: "apn" */
46
42
  'awesome-phonenumber').then(apn => {
47
- loadedAPN = apn.default;
43
+ loadedAPN = {
44
+ parsePhoneNumber: apn.parsePhoneNumber,
45
+ getAsYouType: apn.getAsYouType,
46
+ getExample: apn.getExample
47
+ };
48
48
  return loadedAPN;
49
49
  }, error => {
50
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
50
51
  loadError = error;
51
52
  throw error;
52
53
  });
@@ -55,95 +56,78 @@ function getAPN() {
55
56
  if (loadError) {
56
57
  throw loadError;
57
58
  }
58
-
59
59
  if (!loadedAPN) {
60
60
  // eslint-disable-next-line @typescript-eslint/no-throw-literal
61
61
  throw loadAPN();
62
62
  }
63
-
64
63
  return loadedAPN;
65
64
  }
66
65
 
67
66
  var PLUS_SIGN = '+';
68
67
  var NON_PHONE_SYMBOLS_PATTERN = /[^+\d]/g;
69
68
  var PHONE_PATTERN = /^\+?\d+/g;
70
-
71
69
  function getPrefix(country) {
72
70
  return PLUS_SIGN + String(getCountryCode(toCountryISO(country)));
73
71
  }
74
-
75
72
  function trim(input) {
76
73
  return typeof input == 'string' ? input.trim() : undefined;
77
74
  }
78
-
79
75
  function normalize(input) {
80
76
  if (typeof input == 'string') {
81
77
  var matches = input.replace(NON_PHONE_SYMBOLS_PATTERN, '').match(PHONE_PATTERN);
82
78
  if (matches !== null && matches !== void 0 && matches[0]) return matches[0];
83
79
  }
84
-
85
80
  return '';
86
81
  }
87
-
88
82
  function normalizeNationalNumber(country, input) {
89
83
  var phone = normalize(input);
90
84
  var prefix = getPrefix(country);
91
-
92
85
  if (phone.startsWith(PLUS_SIGN)) {
93
86
  return phone.slice(prefix.length);
94
87
  }
95
-
96
88
  return phone;
97
89
  }
98
-
99
90
  class PhoneService {
100
91
  static load() {
101
92
  return loadAPN().then(APN => new PhoneService(APN));
102
93
  }
103
-
104
94
  static checkPossibility(input) {
105
95
  return this.load().then(phoneService => phoneService.checkPossibility(input));
106
96
  }
107
-
108
97
  static validate(input, rules) {
109
98
  return this.load().then(phoneService => phoneService.validate(input, rules));
110
99
  }
111
-
112
100
  constructor(APN) {
113
101
  this.APN = APN;
114
102
  }
115
-
116
103
  createAPN(phone, country) {
117
104
  var asYouType = this.APN.getAsYouType(toCountryISO(country));
118
105
  asYouType.reset(normalize(phone));
119
106
  return asYouType.getPhoneNumber();
120
107
  }
121
-
122
108
  getJSON(phone, country) {
123
- return this.createAPN(phone, country).toJSON();
109
+ return this.createAPN(phone, country);
124
110
  }
125
-
126
111
  checkPossibility(input) {
127
112
  var phone = trim(input);
128
-
129
113
  if (!phone) {
130
114
  return 'unknown';
131
115
  }
132
-
133
- var apn = phone.startsWith(PLUS_SIGN) ? new this.APN(phone) : new this.APN(phone, DEFAULT_COUNTRY);
116
+ var apn = phone.startsWith(PLUS_SIGN) ? this.APN.parsePhoneNumber(phone) : this.APN.parsePhoneNumber(phone, {
117
+ regionCode: DEFAULT_COUNTRY
118
+ });
134
119
  var {
135
120
  valid,
136
121
  possible,
137
122
  possibility
138
- } = apn.toJSON(); // Avoid false positive short phone numbers.
123
+ } = apn;
139
124
 
125
+ // Avoid false positive short phone numbers.
140
126
  if (!valid && possible) {
141
127
  return 'invalid-number';
142
128
  }
143
-
144
129
  return possibility;
145
130
  }
146
-
147
131
  validate(input) {
148
132
  var {
149
133
  required,
@@ -153,40 +137,30 @@ class PhoneService {
153
137
  tooShortMessage = 'Phone number is too short'
154
138
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
155
139
  var phone = trim(input);
156
-
157
140
  if (!phone) {
158
141
  if (required) {
159
142
  return requiredMessage;
160
143
  }
161
-
162
144
  return undefined;
163
145
  }
164
-
165
146
  switch (this.checkPossibility(phone)) {
166
147
  case 'is-possible':
167
148
  return undefined;
168
-
169
149
  case 'too-long':
170
150
  return tooLongMessage;
171
-
172
151
  case 'too-short':
173
152
  return tooShortMessage;
174
153
  }
175
-
176
154
  return invalidMessage;
177
155
  }
178
-
179
156
  deletePrefix(phone, country) {
180
157
  var prefix = getPrefix(country);
181
-
182
158
  if (phone.startsWith(PLUS_SIGN)) {
183
159
  var subNumber = phone.slice(prefix.length);
184
160
  return trim(subNumber);
185
161
  }
186
-
187
162
  return phone;
188
163
  }
189
-
190
164
  getInfo(phone) {
191
165
  var {
192
166
  regionCode,
@@ -197,82 +171,70 @@ class PhoneService {
197
171
  } = this.getJSON(phone);
198
172
  var nationalNumber = '';
199
173
  var country = toCountryISO(regionCode);
200
-
201
174
  if (!internationalNumber) {
202
175
  nationalNumber = normalizeNationalNumber(country, input);
203
176
  } else {
204
177
  nationalNumber = this.deletePrefix(internationalNumber, country);
205
178
  }
206
-
207
179
  return {
208
180
  country,
209
181
  nationalNumber
210
182
  };
211
183
  }
212
-
213
184
  format(input) {
185
+ var _apn$number;
214
186
  var {
215
187
  fallback = '',
216
188
  format = 'e164',
217
189
  country: countryOption
218
190
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
219
191
  var phone = normalize(input);
220
-
221
192
  if (!phone) {
222
193
  return fallback;
223
194
  }
224
-
225
- var apn = this.createAPN(phone, countryOption);
226
- var country = countryOption || toCountryISO(apn.getRegionCode());
227
- var formatted = format === 'national' ? this.formatNationalNumber(apn, country) : apn.getNumber(format);
228
-
195
+ var apn = this.APN.parsePhoneNumber(phone, {
196
+ regionCode: countryOption
197
+ });
198
+ var country = countryOption || toCountryISO(apn.regionCode);
199
+ var international = (_apn$number = apn.number) === null || _apn$number === void 0 ? void 0 : _apn$number.international;
200
+ var formattedNumber = apn.number ? apn.number[format] : '';
201
+ var {
202
+ regionCode
203
+ } = apn;
204
+ var formatted = format === 'national' ? this.formatNationalNumber(country, international) : formattedNumber;
229
205
  if (!formatted) {
230
- return this.customFormat(apn, phone, format);
206
+ return this.customFormat(phone, format, regionCode);
231
207
  }
232
-
233
208
  return formatted;
234
209
  }
235
-
236
- formatNationalNumber(apn, country) {
237
- var internationalNumber = apn.getNumber('international');
238
-
210
+ formatNationalNumber(country, internationalNumber) {
239
211
  if (!internationalNumber) {
240
212
  return undefined;
241
213
  }
242
-
243
214
  return this.deletePrefix(internationalNumber, country);
244
215
  }
245
-
246
- customFormat(apn, phone, format) {
216
+ customFormat(phone, format, regionCode) {
247
217
  var formatted = '';
248
- var country = toCountryISO(apn.getRegionCode());
218
+ var country = toCountryISO(regionCode);
249
219
  var nationalNumber = normalizeNationalNumber(country, phone);
250
-
251
220
  if (format === 'national') {
252
221
  return nationalNumber;
253
222
  }
254
-
255
223
  var prefix = '';
256
224
  var separator = '';
257
-
258
225
  if (format === 'rfc3966') {
259
226
  prefix = 'tel:';
260
227
  separator = '-';
261
228
  }
262
-
263
229
  if (format === 'international') {
264
230
  separator = ' ';
265
231
  }
266
-
267
232
  formatted = prefix + getPrefix(country);
268
-
269
233
  if (nationalNumber) {
270
234
  formatted += separator + nationalNumber;
271
235
  }
272
-
273
236
  return formatted;
274
237
  }
275
-
276
238
  }
277
239
  function usePhoneService() {
278
240
  var APN = getAPN();
@@ -290,7 +252,6 @@ function useFormattedPhoneNumber(input) {
290
252
  if (phoneService.checkPossibility(input) !== 'is-possible') {
291
253
  return fallback;
292
254
  }
293
-
294
255
  return phoneService.format(input, {
295
256
  format,
296
257
  country,
@@ -310,14 +271,12 @@ var useStyles = /*#__PURE__*/makeStyles(theme => ({
310
271
  });
311
272
  var PhoneFieldFlag = /*#__PURE__*/forwardRef((_ref, ref) => {
312
273
  var {
313
- country,
314
- alt = country,
315
- className
316
- } = _ref,
317
- props = _objectWithoutProperties(_ref, _excluded);
318
-
274
+ country,
275
+ alt = country,
276
+ className
277
+ } = _ref,
278
+ props = _objectWithoutProperties(_ref, _excluded);
319
279
  var styles = useStyles();
320
-
321
280
  if (country === 'AC' || country === 'BQ' || country === 'EH' || country === 'TA') {
322
281
  return /*#__PURE__*/jsx(Typography, {
323
282
  ref: ref,
@@ -329,7 +288,6 @@ var PhoneFieldFlag = /*#__PURE__*/forwardRef((_ref, ref) => {
329
288
  children: country
330
289
  });
331
290
  }
332
-
333
291
  return /*#__PURE__*/jsx("img", _objectSpread(_objectSpread({}, props), {}, {
334
292
  alt: alt,
335
293
  ref: ref,
@@ -340,7 +298,7 @@ var PhoneFieldFlag = /*#__PURE__*/forwardRef((_ref, ref) => {
340
298
  if (process.env.NODE_ENV !== "production") PhoneFieldFlag.displayName = "PhoneFieldFlag";
341
299
 
342
300
  var _excluded$1 = ["country", "classes"],
343
- _excluded2 = ["flag"];
301
+ _excluded2 = ["flag"];
344
302
  var useStyles$1 = /*#__PURE__*/makeStyles(theme => ({
345
303
  dense: {},
346
304
  gutters: {},
@@ -354,19 +312,17 @@ var useStyles$1 = /*#__PURE__*/makeStyles(theme => ({
354
312
  });
355
313
  var PhoneFieldMenuItem = /*#__PURE__*/forwardRef((_ref, ref) => {
356
314
  var {
357
- country,
358
- classes
359
- } = _ref,
360
- props = _objectWithoutProperties(_ref, _excluded$1);
361
-
315
+ country,
316
+ classes
317
+ } = _ref,
318
+ props = _objectWithoutProperties(_ref, _excluded$1);
362
319
  var _useStyles = useStyles$1({
363
- classes
364
- }),
365
- {
366
- flag: flagClassName
367
- } = _useStyles,
368
- styles = _objectWithoutProperties(_useStyles, _excluded2);
369
-
320
+ classes
321
+ }),
322
+ {
323
+ flag: flagClassName
324
+ } = _useStyles,
325
+ styles = _objectWithoutProperties(_useStyles, _excluded2);
370
326
  var countryCode = useMemo(() => getCountryCode(country), [country]);
371
327
  return /*#__PURE__*/jsxs(MenuItem, _objectSpread(_objectSpread({}, props), {}, {
372
328
  ref: ref,
@@ -470,21 +426,18 @@ var PhoneFieldStartAdornment = /*#__PURE__*/forwardRef((_ref, ref) => {
470
426
  if (process.env.NODE_ENV !== "production") PhoneFieldStartAdornment.displayName = "PhoneFieldStartAdornment";
471
427
 
472
428
  var _excluded$2 = ["value", "onBlur", "onFocus", "onChange"],
473
- _excluded2$1 = ["label", "fullWidth", "helperText", "suspenseFallback"];
474
-
429
+ _excluded2$1 = ["label", "fullWidth", "helperText", "suspenseFallback"];
475
430
  function normalizeValue(value) {
476
431
  return typeof value === 'string' ? value : '';
477
432
  }
478
-
479
433
  var PhoneField = /*#__PURE__*/forwardRef((_ref, ref) => {
480
434
  var {
481
- value: valueProp,
482
- onBlur: _onBlur,
483
- onFocus: _onFocus,
484
- onChange: _onChange
485
- } = _ref,
486
- props = _objectWithoutProperties(_ref, _excluded$2);
487
-
435
+ value: valueProp,
436
+ onBlur: _onBlur,
437
+ onFocus: _onFocus,
438
+ onChange: _onChange
439
+ } = _ref,
440
+ props = _objectWithoutProperties(_ref, _excluded$2);
488
441
  var rootRef = useRef(null);
489
442
  var inputRef = useRef(null);
490
443
  var [menuAnchor, setMenuAnchor] = useState(null);
@@ -497,8 +450,10 @@ var PhoneField = /*#__PURE__*/forwardRef((_ref, ref) => {
497
450
  country,
498
451
  nationalNumber
499
452
  }, setValue] = useState(() => createState(value));
500
- var placeholder = useMemo(() => phoneService.APN.getExample(country).getNumber('international'), [country, phoneService.APN]);
501
-
453
+ var placeholder = useMemo(() => {
454
+ var _phoneService$APN$get;
455
+ return ((_phoneService$APN$get = phoneService.APN.getExample(country).number) === null || _phoneService$APN$get === void 0 ? void 0 : _phoneService$APN$get.international) || '';
456
+ }, [country, phoneService.APN]);
502
457
  function handleChange(fn, nextCountry, nextNationalNumber) {
503
458
  if (fn) {
504
459
  var nextValue = phoneService.format(nextNationalNumber, {
@@ -512,14 +467,12 @@ var PhoneField = /*#__PURE__*/forwardRef((_ref, ref) => {
512
467
  fn(nextValue);
513
468
  }
514
469
  }
515
-
516
470
  function handleChangeEvent(fn, _ref2) {
517
471
  var {
518
472
  target: {
519
473
  value: nextValue
520
474
  }
521
475
  } = _ref2;
522
-
523
476
  if (fn) {
524
477
  handleChange(fn, country, phoneService.format(nextValue, {
525
478
  country,
@@ -527,9 +480,9 @@ var PhoneField = /*#__PURE__*/forwardRef((_ref, ref) => {
527
480
  }));
528
481
  }
529
482
  }
530
-
531
483
  useEffect(() => {
532
- setValue(prev => // Ignore `props.value` changes when they were performed from inside.
484
+ setValue(prev =>
485
+ // Ignore `props.value` changes when they were performed from inside.
533
486
  prev.value === value ? prev : createState(value));
534
487
  }, [value, createState]);
535
488
  return /*#__PURE__*/jsxs(Fragment, {
@@ -565,7 +518,6 @@ var PhoneField = /*#__PURE__*/forwardRef((_ref, ref) => {
565
518
  isExpanded: !!menuAnchor,
566
519
  onClick: () => {
567
520
  var _inputRef$current;
568
-
569
521
  // `FocusTrap` inside of `Menu` will restore focus to
570
522
  // the last focused element. We want to manually focus input
571
523
  // to trick the `FocusTrap`.
@@ -580,19 +532,18 @@ var PhoneField = /*#__PURE__*/forwardRef((_ref, ref) => {
580
532
  if (process.env.NODE_ENV !== "production") PhoneField.displayName = "PhoneField";
581
533
  var SuspendedPhoneField = /*#__PURE__*/forwardRef((_ref3, ref) => {
582
534
  var {
583
- label,
584
- fullWidth,
585
- helperText,
586
- suspenseFallback = /*#__PURE__*/jsx(TextField, {
587
- disabled: true,
588
- label: label,
589
- fullWidth: fullWidth,
590
- helperText: helperText,
591
- placeholder: "Loading\u2026"
592
- })
593
- } = _ref3,
594
- props = _objectWithoutProperties(_ref3, _excluded2$1);
595
-
535
+ label,
536
+ fullWidth,
537
+ helperText,
538
+ suspenseFallback = /*#__PURE__*/jsx(TextField, {
539
+ disabled: true,
540
+ label: label,
541
+ fullWidth: fullWidth,
542
+ helperText: helperText,
543
+ placeholder: "Loading\u2026"
544
+ })
545
+ } = _ref3,
546
+ props = _objectWithoutProperties(_ref3, _excluded2$1);
596
547
  return /*#__PURE__*/jsx(Suspense, {
597
548
  fallback: suspenseFallback,
598
549
  children: /*#__PURE__*/jsx(PhoneField, _objectSpread(_objectSpread({}, props), {}, {
@@ -606,22 +557,20 @@ var SuspendedPhoneField = /*#__PURE__*/forwardRef((_ref3, ref) => {
606
557
  if (process.env.NODE_ENV !== "production") SuspendedPhoneField.displayName = "SuspendedPhoneField";
607
558
 
608
559
  var _excluded$3 = ["phone", "country", "fallback", "format"],
609
- _excluded2$2 = ["suspenseFallback"];
560
+ _excluded2$2 = ["suspenseFallback"];
610
561
  var PhoneLink = /*#__PURE__*/forwardRef((_ref, ref) => {
611
562
  var {
612
- phone,
613
- country,
614
- fallback,
615
- format = 'international'
616
- } = _ref,
617
- props = _objectWithoutProperties(_ref, _excluded$3);
618
-
563
+ phone,
564
+ country,
565
+ fallback,
566
+ format = 'international'
567
+ } = _ref,
568
+ props = _objectWithoutProperties(_ref, _excluded$3);
619
569
  var service = usePhoneService();
620
570
  var [text, href] = useMemo(() => {
621
571
  if (service.checkPossibility(phone) !== 'is-possible') {
622
572
  return [undefined, undefined];
623
573
  }
624
-
625
574
  return [service.format(phone, {
626
575
  country,
627
576
  format
@@ -639,10 +588,9 @@ var PhoneLink = /*#__PURE__*/forwardRef((_ref, ref) => {
639
588
  if (process.env.NODE_ENV !== "production") PhoneLink.displayName = "PhoneLink";
640
589
  var SuspendedPhoneLink = /*#__PURE__*/forwardRef((_ref2, ref) => {
641
590
  var {
642
- suspenseFallback = null
643
- } = _ref2,
644
- props = _objectWithoutProperties(_ref2, _excluded2$2);
645
-
591
+ suspenseFallback = null
592
+ } = _ref2,
593
+ props = _objectWithoutProperties(_ref2, _excluded2$2);
646
594
  return /*#__PURE__*/jsx(Suspense, {
647
595
  fallback: suspenseFallback,
648
596
  children: /*#__PURE__*/jsx(PhoneLink, _objectSpread(_objectSpread({}, props), {}, {
@@ -668,10 +616,9 @@ function PhoneText(_ref) {
668
616
  }
669
617
  function SuspendedPhoneText(_ref2) {
670
618
  var {
671
- suspenseFallback = null
672
- } = _ref2,
673
- props = _objectWithoutProperties(_ref2, _excluded$4);
674
-
619
+ suspenseFallback = null
620
+ } = _ref2,
621
+ props = _objectWithoutProperties(_ref2, _excluded$4);
675
622
  return /*#__PURE__*/jsx(Suspense, {
676
623
  fallback: suspenseFallback,
677
624
  children: /*#__PURE__*/jsx(PhoneText, _objectSpread({}, props))