@alfalab/core-components-intl-phone-input 6.4.0 → 6.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/dist/{component-7d88f33a.d.ts → component-fbf58e08.d.ts} +0 -0
  3. package/dist/{component-7d88f33a.js → component-fbf58e08.js} +1 -1
  4. package/dist/component.d.ts +9 -0
  5. package/dist/component.js +133 -64
  6. package/dist/components/flag-icon/component.js +1 -1
  7. package/dist/components/flag-icon/index.css +261 -261
  8. package/dist/components/index.js +1 -1
  9. package/dist/components/select/component.js +2 -2
  10. package/dist/components/select/index.css +6 -6
  11. package/dist/components/select/index.js +1 -1
  12. package/dist/components/select-field/component.js +1 -1
  13. package/dist/components/select-field/index.css +8 -8
  14. package/dist/components/select-field/index.d.ts +1 -1
  15. package/dist/components/select-field/index.js +1 -1
  16. package/dist/cssm/component.d.ts +9 -0
  17. package/dist/cssm/component.js +131 -62
  18. package/dist/cssm/index.js +1 -1
  19. package/dist/cssm/{libphonenumber-js.min-d3a9d490.d.ts → libphonenumber-js.min-903db150.d.ts} +0 -0
  20. package/dist/cssm/libphonenumber-js.min-903db150.js +2227 -0
  21. package/dist/cssm/useCaretAvoidCountryCode.js +24 -20
  22. package/dist/cssm/utils/calculateCaretPos.d.ts +7 -0
  23. package/dist/cssm/utils/calculateCaretPos.js +27 -0
  24. package/dist/esm/{component-32da0c87.d.ts → component-25b3eb47.d.ts} +0 -0
  25. package/dist/esm/{component-32da0c87.js → component-25b3eb47.js} +1 -1
  26. package/dist/esm/component.d.ts +9 -0
  27. package/dist/esm/component.js +134 -65
  28. package/dist/esm/components/flag-icon/component.js +1 -1
  29. package/dist/esm/components/flag-icon/index.css +261 -261
  30. package/dist/esm/components/index.js +1 -1
  31. package/dist/esm/components/select/component.js +2 -2
  32. package/dist/esm/components/select/index.css +6 -6
  33. package/dist/esm/components/select/index.js +1 -1
  34. package/dist/esm/components/select-field/component.js +1 -1
  35. package/dist/esm/components/select-field/index.css +8 -8
  36. package/dist/esm/components/select-field/index.d.ts +1 -1
  37. package/dist/esm/components/select-field/index.js +1 -1
  38. package/dist/esm/index.css +3 -3
  39. package/dist/esm/index.js +2 -2
  40. package/dist/esm/{libphonenumber-js.min-c49027d7.d.ts → libphonenumber-js.min-24f2609b.d.ts} +0 -0
  41. package/dist/esm/libphonenumber-js.min-24f2609b.js +2225 -0
  42. package/dist/esm/useCaretAvoidCountryCode.js +25 -21
  43. package/dist/esm/utils/calculateCaretPos.d.ts +7 -0
  44. package/dist/esm/utils/calculateCaretPos.js +23 -0
  45. package/dist/index.css +3 -3
  46. package/dist/index.js +2 -2
  47. package/dist/{libphonenumber-js.min-13d3dc00.d.ts → libphonenumber-js.min-6d8f77f6.d.ts} +0 -0
  48. package/dist/libphonenumber-js.min-6d8f77f6.js +2227 -0
  49. package/dist/modern/component.d.ts +9 -0
  50. package/dist/modern/component.js +131 -61
  51. package/dist/modern/components/flag-icon/component.js +1 -1
  52. package/dist/modern/components/flag-icon/index.css +261 -261
  53. package/dist/modern/components/select/component.js +1 -1
  54. package/dist/modern/components/select/index.css +6 -6
  55. package/dist/modern/components/select-field/component.js +1 -1
  56. package/dist/modern/components/select-field/index.css +8 -8
  57. package/dist/modern/index.css +3 -3
  58. package/dist/modern/index.js +1 -1
  59. package/dist/modern/{libphonenumber-js.min-c49027d7.d.ts → libphonenumber-js.min-24f2609b.d.ts} +0 -0
  60. package/dist/modern/libphonenumber-js.min-24f2609b.js +2225 -0
  61. package/dist/modern/useCaretAvoidCountryCode.js +25 -21
  62. package/dist/modern/utils/calculateCaretPos.d.ts +7 -0
  63. package/dist/modern/utils/calculateCaretPos.js +23 -0
  64. package/dist/useCaretAvoidCountryCode.js +24 -20
  65. package/dist/utils/calculateCaretPos.d.ts +7 -0
  66. package/dist/utils/calculateCaretPos.js +27 -0
  67. package/package.json +6 -6
  68. package/dist/cssm/libphonenumber-js.min-d3a9d490.js +0 -1569
  69. package/dist/cssm/usePreventCaretReset.d.ts +0 -8
  70. package/dist/cssm/usePreventCaretReset.js +0 -36
  71. package/dist/esm/libphonenumber-js.min-c49027d7.js +0 -1567
  72. package/dist/esm/usePreventCaretReset.d.ts +0 -8
  73. package/dist/esm/usePreventCaretReset.js +0 -32
  74. package/dist/libphonenumber-js.min-13d3dc00.js +0 -1569
  75. package/dist/modern/libphonenumber-js.min-c49027d7.js +0 -1567
  76. package/dist/modern/usePreventCaretReset.d.ts +0 -8
  77. package/dist/modern/usePreventCaretReset.js +0 -31
  78. package/dist/usePreventCaretReset.d.ts +0 -8
  79. package/dist/usePreventCaretReset.js +0 -36
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var components_selectField_component = require('../component-7d88f33a.js');
5
+ var components_selectField_component = require('../component-fbf58e08.js');
6
6
  require('react');
7
7
  require('classnames');
8
8
  require('react-merge-refs');
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var components_selectField_component = require('../../component-7d88f33a.js');
5
+ var components_selectField_component = require('../../component-fbf58e08.js');
6
6
  var React = require('react');
7
7
  require('classnames');
8
8
  require('react-merge-refs');
@@ -14,7 +14,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
14
14
 
15
15
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
16
16
 
17
- var styles = {"component":"intl-phone-input__component_1701x","option":"intl-phone-input__option_1701x","flag":"intl-phone-input__flag_1701x","countryName":"intl-phone-input__countryName_1701x","dialCode":"intl-phone-input__dialCode_1701x"};
17
+ var styles = {"component":"intl-phone-input__component_1c2vg","option":"intl-phone-input__option_1c2vg","flag":"intl-phone-input__flag_1c2vg","countryName":"intl-phone-input__countryName_1c2vg","dialCode":"intl-phone-input__dialCode_1c2vg"};
18
18
  require('./index.css')
19
19
 
20
20
  var CountriesSelect = function (_a) {
@@ -1,4 +1,4 @@
1
- /* hash: 99zrq */
1
+ /* hash: yz6kc */
2
2
  :root {
3
3
  --color-light-text-secondary: #546272;
4
4
  }
@@ -17,24 +17,24 @@
17
17
  :root {
18
18
  --text-secondary-color: var(--color-light-text-secondary);
19
19
  }
20
- .intl-phone-input__component_1701x {
20
+ .intl-phone-input__component_1c2vg {
21
21
  position: static;
22
22
  display: flex;
23
23
  min-width: initial;
24
24
  height: 100%;
25
25
  }
26
- .intl-phone-input__option_1701x {
26
+ .intl-phone-input__option_1c2vg {
27
27
  display: flex;
28
28
  align-items: flex-start;
29
29
  }
30
- .intl-phone-input__flag_1701x {
30
+ .intl-phone-input__flag_1c2vg {
31
31
  flex-shrink: 0;
32
32
  margin-top: var(--gap-2xs);
33
33
  margin-right: var(--gap-xs);
34
34
  }
35
- .intl-phone-input__countryName_1701x {
35
+ .intl-phone-input__countryName_1c2vg {
36
36
  margin-right: var(--gap-xs);
37
37
  }
38
- .intl-phone-input__dialCode_1701x {
38
+ .intl-phone-input__dialCode_1c2vg {
39
39
  color: var(--text-secondary-color);
40
40
  }
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- require('../../component-7d88f33a.js');
5
+ require('../../component-fbf58e08.js');
6
6
  require('react');
7
7
  require('classnames');
8
8
  require('react-merge-refs');
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var components_selectField_component = require('../../component-7d88f33a.js');
5
+ var components_selectField_component = require('../../component-fbf58e08.js');
6
6
  require('react');
7
7
  require('classnames');
8
8
  require('react-merge-refs');
@@ -1,4 +1,4 @@
1
- /* hash: 52j3d */
1
+ /* hash: 1eegc */
2
2
  :root {
3
3
  --color-light-border-link: #007aff;
4
4
  }
@@ -19,13 +19,13 @@
19
19
  --focus-color: var(--color-light-border-link);
20
20
  --disabled-cursor: not-allowed;
21
21
  }
22
- .intl-phone-input__component_15ahh {
22
+ .intl-phone-input__component_14z08 {
23
23
  height: 100%;
24
24
  cursor: pointer;
25
25
  outline: none;
26
26
  position: relative;
27
27
  }
28
- .intl-phone-input__flagIconContainer_15ahh {
28
+ .intl-phone-input__flagIconContainer_14z08 {
29
29
  display: flex;
30
30
  justify-content: center;
31
31
  align-items: center;
@@ -33,10 +33,10 @@
33
33
  height: 24px;
34
34
  margin-right: var(--gap-2xs);
35
35
  }
36
- .intl-phone-input__disabled_15ahh {
36
+ .intl-phone-input__disabled_14z08 {
37
37
  cursor: var(--disabled-cursor);
38
38
  }
39
- .intl-phone-input__inner_15ahh {
39
+ .intl-phone-input__inner_14z08 {
40
40
  position: relative;
41
41
  display: flex;
42
42
  align-items: center;
@@ -44,11 +44,11 @@
44
44
  padding-left: var(--gap-s);
45
45
  outline: none;
46
46
  }
47
- .intl-phone-input__l_15ahh .intl-phone-input__inner_15ahh,
48
- .intl-phone-input__xl_15ahh .intl-phone-input__inner_15ahh {
47
+ .intl-phone-input__l_14z08 .intl-phone-input__inner_14z08,
48
+ .intl-phone-input__xl_14z08 .intl-phone-input__inner_14z08 {
49
49
  padding-left: var(--gap-m);
50
50
  }
51
- .intl-phone-input__focusVisible_15ahh {
51
+ .intl-phone-input__focusVisible_14z08 {
52
52
  outline: 2px solid var(--focus-color);
53
53
  outline-offset: 2px;
54
54
  }
@@ -1 +1 @@
1
- export * from "../../component-7d88f33a";
1
+ export * from "../../component-fbf58e08";
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var components_selectField_component = require('../../component-7d88f33a.js');
5
+ var components_selectField_component = require('../../component-fbf58e08.js');
6
6
  require('react');
7
7
  require('classnames');
8
8
  require('react-merge-refs');
@@ -4,6 +4,7 @@ import { CountryCode } from 'libphonenumber-js';
4
4
  import { SelectProps } from "@alfalab/core-components-select";
5
5
  import { Country } from '@alfalab/utils';
6
6
  import { InputAutocompleteProps } from "@alfalab/core-components-input-autocomplete";
7
+ type MaxPhoneLenByCountry = Record<string, number>;
7
8
  type IntlPhoneInputProps = Partial<Omit<InputAutocompleteProps, 'onChange'>> & Pick<SelectProps, 'preventFlip'> & {
8
9
  /**
9
10
  * Значение
@@ -33,6 +34,10 @@ type IntlPhoneInputProps = Partial<Omit<InputAutocompleteProps, 'onChange'>> & P
33
34
  * Возможность стереть код страны
34
35
  */
35
36
  clearableCountryCode?: boolean;
37
+ /**
38
+ * Ограничение длин вводимых номеров по странам.
39
+ */
40
+ maxPhoneLen?: MaxPhoneLenByCountry;
36
41
  };
37
42
  declare const IntlPhoneInput: React.ForwardRefExoticComponent<Partial<Pick<InputAutocompleteProps, "selected" | "valueRenderer" | "Optgroup" | "size" | "value" | "disabled" | "multiple" | "name" | "placeholder" | "readOnly" | "className" | "id" | "onFocus" | "onBlur" | "onInput" | "onScroll" | "optionsListWidth" | "options" | "visibleOptions" | "open" | "label" | "error" | "hint" | "Arrow" | "dataTestId" | "fieldClassName" | "success" | "block" | "optionGroupClassName" | "Option" | "inputProps" | "optionsListClassName" | "optionClassName" | "popperClassName" | "autocomplete" | "allowUnselect" | "closeOnSelect" | "circularNavigation" | "defaultOpen" | "popoverPosition" | "preventFlip" | "optionsSize" | "fieldProps" | "optionsListProps" | "optionProps" | "onOpen" | "OptionsList" | "updatePopover" | "zIndexPopover" | "showEmptyOptionsList" | "Input">> & Pick<SelectProps, "preventFlip"> & {
38
43
  /**
@@ -63,5 +68,9 @@ declare const IntlPhoneInput: React.ForwardRefExoticComponent<Partial<Pick<Input
63
68
  * Возможность стереть код страны
64
69
  */
65
70
  clearableCountryCode?: boolean | undefined;
71
+ /**
72
+ * Ограничение длин вводимых номеров по странам.
73
+ */
74
+ maxPhoneLen?: Record<string, number> | undefined;
66
75
  } & React.RefAttributes<HTMLInputElement>>;
67
76
  export { IntlPhoneInputProps, IntlPhoneInput };
@@ -16,8 +16,8 @@ require('@alfalab/core-components-select/dist/cssm');
16
16
  require('./components/select/index.module.css');
17
17
  var components_select_component = require('./components/select/component.js');
18
18
  var utils_formatPhoneWithUnclearableCountryCode = require('./utils/format-phone-with-unclearable-country-code.js');
19
+ var utils_calculateCaretPos = require('./utils/calculateCaretPos.js');
19
20
  var useCaretAvoidCountryCode = require('./useCaretAvoidCountryCode.js');
20
- var usePreventCaretReset = require('./usePreventCaretReset.js');
21
21
  var styles = require('./index.module.css');
22
22
 
23
23
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -28,90 +28,84 @@ var styles__default = /*#__PURE__*/_interopDefaultLegacy(styles);
28
28
 
29
29
  var countriesHash = utils.getCountriesHash();
30
30
  var MAX_DIAL_CODE_LENGTH = 4;
31
+ var MASK_SYMBOLS = [' ', '-', '(', ')'];
32
+ var MAX_PHONE_LEN = 15;
33
+ var DEFAULT_MAX_PHONE_LEN_BY_COUNTRY = { RU: 11 };
31
34
  var IntlPhoneInput = React.forwardRef(function (_a, ref) {
32
- var _b = _a.disabled, disabled = _b === void 0 ? false : _b, _c = _a.readOnly, readOnly = _c === void 0 ? false : _c, _d = _a.size, size = _d === void 0 ? 'm' : _d, _e = _a.colors, colors = _e === void 0 ? 'default' : _e, _f = _a.options, options = _f === void 0 ? [] : _f, _g = _a.countries, countries = _g === void 0 ? utils.getCountries() : _g, _h = _a.clearableCountryCode, clearableCountryCode = _h === void 0 ? true : _h, className = _a.className, value = _a.value, onChange = _a.onChange, onCountryChange = _a.onCountryChange, _j = _a.defaultCountryIso2, defaultCountryIso2 = _j === void 0 ? 'ru' : _j, preventFlip = _a.preventFlip, inputProps = _a.inputProps, restProps = components_selectField_component.__rest(_a, ["disabled", "readOnly", "size", "colors", "options", "countries", "clearableCountryCode", "className", "value", "onChange", "onCountryChange", "defaultCountryIso2", "preventFlip", "inputProps"]);
33
- var _k = React.useState(defaultCountryIso2.toLowerCase()), countryIso2 = _k[0], setCountryIso2 = _k[1];
35
+ var _b = _a.disabled, disabled = _b === void 0 ? false : _b, _c = _a.readOnly, readOnly = _c === void 0 ? false : _c, _d = _a.size, size = _d === void 0 ? 'm' : _d, _e = _a.colors, colors = _e === void 0 ? 'default' : _e, _f = _a.options, options = _f === void 0 ? [] : _f, _g = _a.countries, countries = _g === void 0 ? utils.getCountries() : _g, _h = _a.clearableCountryCode, clearableCountryCode = _h === void 0 ? true : _h, className = _a.className, value = _a.value, onChange = _a.onChange, onCountryChange = _a.onCountryChange, _j = _a.defaultCountryIso2, defaultCountryIso2 = _j === void 0 ? 'ru' : _j, preventFlip = _a.preventFlip, inputProps = _a.inputProps, _k = _a.maxPhoneLen, maxPhoneLen = _k === void 0 ? DEFAULT_MAX_PHONE_LEN_BY_COUNTRY : _k, restProps = components_selectField_component.__rest(_a, ["disabled", "readOnly", "size", "colors", "options", "countries", "clearableCountryCode", "className", "value", "onChange", "onCountryChange", "defaultCountryIso2", "preventFlip", "inputProps", "maxPhoneLen"]);
36
+ var _l = React.useState(defaultCountryIso2.toLowerCase()), countryIso2 = _l[0], setCountryIso2 = _l[1];
34
37
  var inputRef = React.useRef(null);
35
- var _l = React.useState(null), inputWrapperRef = _l[0], setInputWrapperRef = _l[1];
38
+ var _m = React.useState(null), inputWrapperRef = _m[0], setInputWrapperRef = _m[1];
39
+ var _o = React.useState(), caretPos = _o[0], setCaretPos = _o[1];
36
40
  var phoneLibUtils = React.useRef();
37
- var setCountryByIso2 = React.useCallback(function (iso2) {
38
- var country = countriesHash[iso2];
39
- var inputValue = "+" + country.dialCode;
40
- onChange(inputValue);
41
- setCountryIso2(country.iso2);
42
- return country;
43
- }, [onChange]);
44
- var setValue = React.useCallback(function (inputValue) {
41
+ var formatPhone = function (inputValue) {
45
42
  var newValue = inputValue;
46
43
  if (phoneLibUtils.current) {
47
44
  var Utils = phoneLibUtils.current;
48
45
  var utils = new Utils(countryIso2.toUpperCase());
49
46
  newValue = utils.input(inputValue);
50
47
  }
51
- onChange(newValue);
52
- }, [countryIso2, onChange]);
53
- var handleCountryChange = React.useCallback(function (countryCode) {
48
+ return newValue;
49
+ };
50
+ var setCountryByIso2 = function (iso2) {
51
+ var country = countriesHash[iso2];
52
+ var inputValue = "+" + country.dialCode;
53
+ onChange(inputValue);
54
+ setCountryIso2(country.iso2);
55
+ return country;
56
+ };
57
+ var handleCountryChange = function (countryCode) {
54
58
  if (onCountryChange) {
55
59
  onCountryChange(countryCode.toUpperCase());
56
60
  }
57
- }, [onCountryChange]);
58
- var setCountryByDialCode = React.useCallback(function (inputValue) {
61
+ };
62
+ var setCountryByDialCode = function (inputValue) {
59
63
  for (var i = 0; i < countries.length; i++) {
60
64
  var country_1 = countries[i];
61
65
  if (new RegExp("^\\+" + country_1.dialCode).test(inputValue)) {
62
66
  // Сначала проверяем, если приоритет не указан
63
67
  if (country_1.priority === undefined) {
64
- setValue(inputValue);
68
+ onChange(formatPhone(inputValue));
65
69
  setCountryIso2(country_1.iso2);
66
70
  handleCountryChange(country_1.iso2);
67
71
  break;
68
72
  }
69
73
  // Если страна уже была выставлена через селект, и коды совпадают
70
74
  if (countryIso2 === country_1.iso2) {
71
- setValue(inputValue);
75
+ onChange(formatPhone(inputValue));
72
76
  setCountryIso2(country_1.iso2);
73
77
  handleCountryChange(country_1.iso2);
74
78
  break;
75
79
  // Если не совпадают - выбираем по приоритету
76
80
  }
77
81
  else if (country_1.priority === 0) {
78
- setValue(inputValue);
82
+ onChange(formatPhone(inputValue));
79
83
  setCountryIso2(country_1.iso2);
80
84
  handleCountryChange(country_1.iso2);
81
85
  break;
82
86
  }
83
87
  }
84
88
  }
85
- }, [countries, countryIso2, setValue, handleCountryChange]);
86
- var loadPhoneUtils = React.useCallback(function () {
87
- // prettier-ignore
88
- return Promise.resolve().then(function () { return require(/* webpackChunkName: "libphonenumber" */ './libphonenumber-js.min-d3a9d490.js'); })
89
- .then(function (utils) {
90
- phoneLibUtils.current = utils.AsYouType;
91
- })
92
- .catch(function (error) { return "An error occurred while loading libphonenumber-js:\n" + error; });
93
- }, []);
94
- var handleInputChange = React.useCallback(function (event) {
95
- var targetValue = event.target.value;
89
+ };
90
+ var setCountryByDialCodeWithLengthCheck = function (inputValue) {
91
+ if (value.length < MAX_DIAL_CODE_LENGTH) {
92
+ setCountryByDialCode(inputValue);
93
+ }
94
+ };
95
+ var addCountryCode = function (inputValue) {
96
96
  var country = countriesHash[countryIso2];
97
97
  if (clearableCountryCode) {
98
- var newValue = targetValue.length === 1 && targetValue !== '+'
99
- ? "+" + targetValue
100
- : targetValue;
101
- setValue(newValue);
102
- if (value.length < MAX_DIAL_CODE_LENGTH) {
103
- setCountryByDialCode(newValue);
104
- }
105
- return;
106
- }
107
- if (!clearableCountryCode) {
108
- var newValue = utils_formatPhoneWithUnclearableCountryCode.formatPhoneWithUnclearableCountryCode(targetValue, country);
109
- setValue(newValue);
110
- return;
98
+ return inputValue.length === 1 && inputValue !== '+'
99
+ ? "+" + inputValue
100
+ : inputValue;
111
101
  }
112
- setValue(targetValue);
113
- }, [clearableCountryCode, countryIso2, setCountryByDialCode, setValue, value.length]);
114
- var handleSelectChange = React.useCallback(function (_a) {
102
+ return utils_formatPhoneWithUnclearableCountryCode.formatPhoneWithUnclearableCountryCode(inputValue, country);
103
+ };
104
+ var handleInputChange = function (event) {
105
+ setCountryByDialCodeWithLengthCheck(event.target.value);
106
+ onChange(formatPhone(addCountryCode(event.target.value)));
107
+ };
108
+ var handleSelectChange = function (_a) {
115
109
  var selected = _a.selected;
116
110
  if (selected) {
117
111
  var country_2 = setCountryByIso2(selected.value);
@@ -122,28 +116,103 @@ var IntlPhoneInput = React.forwardRef(function (_a, ref) {
122
116
  }
123
117
  handleCountryChange(country_2.iso2);
124
118
  }
125
- }, [setCountryByIso2, handleCountryChange]);
126
- var handleChange = React.useCallback(function (payload) {
119
+ };
120
+ var handleChange = function (payload) {
127
121
  var selected = payload.selected;
128
122
  if (!selected)
129
123
  return;
130
- if (selected.key.length < MAX_DIAL_CODE_LENGTH) {
131
- setCountryByDialCode(selected.key);
124
+ setCountryByDialCodeWithLengthCheck(selected.key);
125
+ onChange(formatPhone(selected.key));
126
+ };
127
+ var country = countriesHash[countryIso2];
128
+ var countryCodeLength = ("+" + country.dialCode).length;
129
+ var handleInputNewChar = function (event, caretPosition) {
130
+ var input = event.target;
131
+ var currentValue = input.value;
132
+ var maxPhoneLength = (maxPhoneLen === null || maxPhoneLen === void 0 ? void 0 : maxPhoneLen[countryIso2.toUpperCase()]) || MAX_PHONE_LEN;
133
+ // Если номер полностью заполнен, то перезатираем цифры, если каретка не в самом конце.
134
+ var shouldReplace = maxPhoneLength === currentValue.replace(/\D/g, '').length;
135
+ var endPhonePart = currentValue.slice(caretPosition);
136
+ if (shouldReplace) {
137
+ var cursor = 0;
138
+ while (MASK_SYMBOLS.includes(endPhonePart.charAt(cursor))) {
139
+ cursor += 1;
140
+ }
141
+ endPhonePart = endPhonePart.slice(cursor + 1);
142
+ }
143
+ var newValue = currentValue.slice(0, caretPosition) + event.key + endPhonePart;
144
+ // Запрещаем ввод, если номер уже заполнен.
145
+ if (newValue.replace(/\D/g, '').length > maxPhoneLength) {
146
+ newValue = newValue.slice(0, -1);
147
+ }
148
+ newValue = formatPhone(addCountryCode(newValue));
149
+ var phonePartWithoutMask = currentValue.slice(0, caretPosition).replace(/\D/g, '') + event.key;
150
+ if (shouldReplace && phonePartWithoutMask.length > maxPhoneLength) {
151
+ phonePartWithoutMask = phonePartWithoutMask.slice(0, -1);
152
+ }
153
+ setCaretPos(utils_calculateCaretPos.calculateCaretPos(phonePartWithoutMask, newValue));
154
+ setCountryByDialCodeWithLengthCheck(newValue);
155
+ onChange(newValue);
156
+ };
157
+ var handleDeleteChar = function (event, caretPosition) {
158
+ var input = event.target;
159
+ if (!clearableCountryCode && caretPosition <= countryCodeLength)
160
+ return;
161
+ var currentValue = input.value;
162
+ var isMaskSymbol = function (count) {
163
+ var isMask = MASK_SYMBOLS.includes(currentValue.charAt(caretPosition - count));
164
+ var isPossibleToRemove = clearableCountryCode
165
+ ? caretPosition - count > 0
166
+ : caretPosition - count > countryCodeLength;
167
+ return isMask && isPossibleToRemove;
168
+ };
169
+ var deletedCharsCount = 1;
170
+ // Высчитываем кол-во символов, которые нужно удалить.
171
+ while (isMaskSymbol(deletedCharsCount)) {
172
+ deletedCharsCount += 1;
173
+ }
174
+ var phonePart = currentValue.slice(0, caretPosition - deletedCharsCount);
175
+ var newValue = formatPhone(addCountryCode(phonePart + currentValue.slice(caretPosition)));
176
+ var phonePartWithoutMask = phonePart.replace(/[^0-9+]+/g, '');
177
+ setCaretPos(utils_calculateCaretPos.calculateCaretPos(phonePartWithoutMask, newValue));
178
+ setCountryByDialCodeWithLengthCheck(newValue);
179
+ onChange(newValue);
180
+ };
181
+ var handleKeyDown = function (event) {
182
+ var input = event.target;
183
+ var caretPosition = input.selectionStart || 0;
184
+ // Нажат только Backspace, не сочетание клавиш с ним.
185
+ if (!event.shiftKey && !event.ctrlKey && !event.metaKey && event.key === 'Backspace') {
186
+ if (!caretPosition)
187
+ return;
188
+ event.preventDefault();
189
+ handleDeleteChar(event, caretPosition);
132
190
  }
133
- setValue(selected.key);
134
- }, [setValue, setCountryByDialCode]);
191
+ if (event.key.length === 1 && /[0-9+]/.test(event.key)) {
192
+ event.preventDefault();
193
+ handleInputNewChar(event, caretPosition);
194
+ }
195
+ };
135
196
  React.useEffect(function () {
136
- if (!phoneLibUtils.current) {
137
- loadPhoneUtils().then(function () {
138
- setCountryByDialCode(value);
139
- });
197
+ if (inputRef.current && caretPos !== undefined) {
198
+ inputRef.current.setSelectionRange(caretPos, caretPos);
199
+ setCaretPos(undefined);
140
200
  }
141
- }, [countryIso2, loadPhoneUtils, setCountryByDialCode, value]);
142
- var country = countriesHash[countryIso2];
143
- var countryCodeLength = ("+" + country.dialCode).length;
201
+ }, [caretPos]);
202
+ React.useEffect(function () {
203
+ if (phoneLibUtils.current)
204
+ return;
205
+ Promise.resolve().then(function () { return require(
206
+ /* webpackChunkName: "libphonenumber" */ './libphonenumber-js.min-903db150.js'); })
207
+ .then(function (utils) {
208
+ phoneLibUtils.current = utils.AsYouType;
209
+ setCountryByDialCode(value);
210
+ })
211
+ .catch(function (error) { return "An error occurred while loading libphonenumber-js:\n" + error; });
212
+ /* eslint-disable-next-line react-hooks/exhaustive-deps */
213
+ }, [value]);
144
214
  useCaretAvoidCountryCode.useCaretAvoidCountryCode({ inputRef: inputRef, countryCodeLength: countryCodeLength, clearableCountryCode: clearableCountryCode });
145
- usePreventCaretReset.usePreventCaretReset({ inputRef: inputRef, countryCodeLength: countryCodeLength, clearableCountryCode: clearableCountryCode });
146
- return (React__default['default'].createElement(coreComponentsInputAutocomplete.InputAutocomplete, components_selectField_component.__assign({}, restProps, { ref: ref, inputProps: components_selectField_component.__assign(components_selectField_component.__assign({}, inputProps), { ref: inputRef, wrapperRef: setInputWrapperRef, type: 'tel', colors: colors, className: cn__default['default'](className, styles__default['default'][size]), addonsClassName: styles__default['default'].addons, leftAddons: countries.length > 1 && (React__default['default'].createElement(components_select_component.CountriesSelect, { dataTestId: 'countries-select', disabled: disabled || readOnly, size: size, selected: countryIso2, countries: countries, onChange: handleSelectChange, fieldWidth: inputWrapperRef && inputWrapperRef.getBoundingClientRect().width, preventFlip: preventFlip })) }), optionsListWidth: 'field', closeOnSelect: true, onInput: handleInputChange, onChange: handleChange, options: options, disabled: disabled, readOnly: readOnly, size: size, className: className, value: value })));
215
+ return (React__default['default'].createElement(coreComponentsInputAutocomplete.InputAutocomplete, components_selectField_component.__assign({}, restProps, { ref: ref, inputProps: components_selectField_component.__assign(components_selectField_component.__assign({}, inputProps), { ref: inputRef, wrapperRef: setInputWrapperRef, type: 'tel', colors: colors, className: cn__default['default'](className, styles__default['default'][size]), addonsClassName: styles__default['default'].addons, onKeyDown: handleKeyDown, leftAddons: countries.length > 1 && (React__default['default'].createElement(components_select_component.CountriesSelect, { dataTestId: 'countries-select', disabled: disabled || readOnly, size: size, selected: countryIso2, countries: countries, onChange: handleSelectChange, fieldWidth: inputWrapperRef && inputWrapperRef.getBoundingClientRect().width, preventFlip: preventFlip })) }), optionsListWidth: 'field', closeOnSelect: true, onInput: handleInputChange, onChange: handleChange, options: options, disabled: disabled, readOnly: readOnly, size: size, className: className, value: value })));
147
216
  });
148
217
  IntlPhoneInput.defaultProps = {
149
218
  size: 'm',
@@ -16,8 +16,8 @@ require('@alfalab/core-components-select/dist/cssm');
16
16
  require('./components/select/index.module.css');
17
17
  require('./components/select/component.js');
18
18
  require('./utils/format-phone-with-unclearable-country-code.js');
19
+ require('./utils/calculateCaretPos.js');
19
20
  require('./useCaretAvoidCountryCode.js');
20
- require('./usePreventCaretReset.js');
21
21
  require('./index.module.css');
22
22
  var component = require('./component.js');
23
23