@invoicebox/ui 0.1.0-alpha.62 → 0.1.0-alpha.64

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.
@@ -1,2 +1,3 @@
1
1
  import { TCountryRule, TSupportedCountries } from './types';
2
+ export declare const MASK_DIGIT_ITEM = "9";
2
3
  export declare const allCountriesPhoneRules: Record<TSupportedCountries, TCountryRule>;
@@ -3,45 +3,53 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.allCountriesPhoneRules = void 0;
6
+ exports.allCountriesPhoneRules = exports.MASK_DIGIT_ITEM = void 0;
7
+ var MASK_DIGIT_ITEM = exports.MASK_DIGIT_ITEM = '9';
7
8
  var allCountriesPhoneRules = exports.allCountriesPhoneRules = {
8
9
  RUS: {
10
+ placeholder: '+7 (XXX) XXX-XX-XX',
9
11
  mask: '+9 (999) 999-99-99',
10
12
  startSubsequence: '7',
11
13
  regexp: /^\+?7(9\d{9})$/,
12
14
  flag: 'RUS'
13
15
  },
14
16
  BLR: {
17
+ placeholder: '+375 XX XXX-XX-XX',
15
18
  mask: '+999 99 999-99-99',
16
19
  startSubsequence: '375',
17
20
  regexp: /^(\+?375)?(24|25|29|33|44)\d{7}$/,
18
21
  flag: 'BLR'
19
22
  },
20
23
  AZE: {
24
+ placeholder: '+994 (XX) XXX-XXXX',
21
25
  mask: '+999 (99) 999-9999',
22
26
  startSubsequence: '994',
23
27
  regexp: /^\+?994\d{9}$/,
24
28
  flag: 'AZE'
25
29
  },
26
30
  ARM: {
31
+ placeholder: '+374 (XX) XXX-XXX',
27
32
  mask: '+999 (99) 999-999',
28
33
  startSubsequence: '374',
29
34
  regexp: /\+?374\d{8}/,
30
35
  flag: 'ARM'
31
36
  },
32
37
  KAZ: {
38
+ placeholder: '+7 (XXX) XXX-XX-XX',
33
39
  mask: '+9 (999) 999-99-99',
34
40
  startSubsequence: '7',
35
41
  regexp: /^(\+?7|8)?7\d{9}$/,
36
42
  flag: 'KAZ'
37
43
  },
38
44
  TJK: {
45
+ placeholder: '+992-XX-XXX-XXXX',
39
46
  mask: '+999-99-999-9999',
40
47
  startSubsequence: '992',
41
48
  regexp: /^\+?992\d{9}$/,
42
49
  flag: 'TJK'
43
50
  },
44
51
  UZB: {
52
+ placeholder: '+998 XX XXXXXXX',
45
53
  mask: '+999 99 9999999',
46
54
  startSubsequence: '998',
47
55
  regexp: /\+?998\d{9}/,
@@ -22,13 +22,14 @@ var useValidateInitialValue = exports.useValidateInitialValue = function useVali
22
22
  (0, _react.useEffect)(function () {
23
23
  if (isInitialValidateDone) return;
24
24
  if (initialValue) {
25
+ var notFormattedValue = _phoneInputLogic.phoneInputLogic.getOnlyNumbersFromString(initialValue);
25
26
  var currentCountry = Object.entries(currentCountriesPhoneRules).find(function (item) {
26
- return _phoneInputLogic.phoneInputLogic.getIsHaveStartSequenceInString(initialValue, item[1].startSubsequence);
27
+ return _phoneInputLogic.phoneInputLogic.getIsHaveStartSequenceInString(notFormattedValue, item[1].startSubsequence);
27
28
  });
28
29
  var _countryCode = currentCountry ? currentCountry[0] : _types.RUS_COUNTRY_CODE;
29
- var isValidInputValue = _phoneInputLogic.phoneInputLogic.getIsValidPhoneInput(initialValue, currentCountry ? currentCountry[1] : _constants.allCountriesPhoneRules.RUS);
30
+ var isValidInputValue = _phoneInputLogic.phoneInputLogic.getIsValidPhoneInput(notFormattedValue, currentCountry ? currentCountry[1] : _constants.allCountriesPhoneRules.RUS);
30
31
  if (isValidInputValue) {
31
- onValid(initialValue, _countryCode);
32
+ onValid(notFormattedValue, _countryCode);
32
33
  } else {
33
34
  onUnValid(_countryCode);
34
35
  }
@@ -86,23 +86,38 @@ var PhoneInput = exports.PhoneInput = function PhoneInput(_ref) {
86
86
  setCountry(countryCode);
87
87
  }, [onChange]);
88
88
  (0, _useValidateInitialValue.useValidateInitialValue)(value, currentCountriesPhoneRules, handleValid, handleUnValid);
89
- var handleInputValueChange = (0, _react.useCallback)(function (currentInputValue) {
90
- var notFormattedValue = _phoneInputLogic.phoneInputLogic.getOnlyNumbersFromString(currentInputValue);
89
+
90
+ // хак для того чтобы сдвигать каретку в случае если при вводе "9" надо заменить на "79", каретка не двигается сама и остаётся между "7" и "9" без этой функции
91
+ var moveCaretByValue = (0, _react.useCallback)(function (event) {
92
+ return function (positionCaret) {
93
+ setTimeout(function () {
94
+ event.target.setSelectionRange(positionCaret, positionCaret);
95
+ }, 0);
96
+ };
97
+ }, []);
98
+ var getIsValidValue = (0, _react.useCallback)(function (currentValue) {
91
99
  var currentCountry = currentCountriesPhoneRules[country];
92
100
  var newCountry = Object.entries(currentCountriesPhoneRules).find(function (item) {
93
- return _phoneInputLogic.phoneInputLogic.getIsHaveStartSequenceInString(notFormattedValue, item[1].startSubsequence) && currentCountry.flag !== item[1].flag;
101
+ return _phoneInputLogic.phoneInputLogic.getIsHaveStartSequenceInString(currentValue, item[1].startSubsequence) && currentCountry.flag !== item[1].flag;
94
102
  });
95
103
  var isNeedChangeCountry = newCountry && currentCountry.startSubsequence !== newCountry[1].startSubsequence;
96
- if (isNeedChangeCountry && newCountry) {
104
+ var isNeedAndCanChangeCountry = isNeedChangeCountry && !!newCountry;
105
+ if (isNeedAndCanChangeCountry) {
97
106
  handleChangeCountry(newCountry[0]);
98
107
  }
99
- var isValidInputValue = _phoneInputLogic.phoneInputLogic.getIsValidPhoneInput(notFormattedValue, isNeedChangeCountry && newCountry ? newCountry[1] : currentCountry);
108
+ return _phoneInputLogic.phoneInputLogic.getIsValidPhoneInput(currentValue, isNeedAndCanChangeCountry ? newCountry[1] : currentCountry);
109
+ }, [country, currentCountriesPhoneRules, handleChangeCountry]);
110
+ var handleInputValueChange = (0, _react.useCallback)(function (currentInputValue, event) {
111
+ var notFormattedValue = _phoneInputLogic.phoneInputLogic.getOnlyNumbersFromString(currentInputValue);
112
+ var moveCaret = event ? moveCaretByValue(event) : undefined;
113
+ var intermediateValue = country === _types.RUS_COUNTRY_CODE ? _phoneInputLogic.phoneInputLogic.formatRusPhoneToUnifiedView(notFormattedValue, moveCaret) : notFormattedValue;
114
+ var isValidInputValue = getIsValidValue(intermediateValue);
100
115
  setIsHaveInputError(!isValidInputValue);
101
- onChange(isValidInputValue ? notFormattedValue : '');
102
- setInputValue(notFormattedValue);
103
- }, [country, currentCountriesPhoneRules, handleChangeCountry, onChange]);
116
+ onChange(isValidInputValue ? intermediateValue : '');
117
+ setInputValue(intermediateValue);
118
+ }, [country, getIsValidValue, moveCaretByValue, onChange]);
104
119
  var handleChange = (0, _react.useCallback)(function (event) {
105
- handleInputValueChange(event.target.value);
120
+ handleInputValueChange(event.target.value, event);
106
121
  }, [handleInputValueChange]);
107
122
  var handleBlur = (0, _react.useCallback)(function (event) {
108
123
  blurHandler(event);
@@ -145,12 +160,14 @@ var PhoneInput = exports.PhoneInput = function PhoneInput(_ref) {
145
160
  mask: currentCountriesPhoneRules[country].mask,
146
161
  maskPlaceholder: null,
147
162
  disabled: disabled,
148
- onPaste: handlePaste
163
+ onPaste: handlePaste,
164
+ placeholder: currentCountriesPhoneRules[country].placeholder
149
165
  }, /*#__PURE__*/_react["default"].createElement(_PureInput.PureInput, {
150
166
  id: id,
151
167
  hasError: hasError || (isFirstBlur ? false : isHaveInputError),
152
168
  inFocus: inFocus,
153
169
  name: name,
154
- paddingLeft: isHaveSelectCountries ? 75 : 18
170
+ paddingLeft: isHaveSelectCountries ? 75 : 18,
171
+ autoComplete: 'off'
155
172
  }))));
156
173
  };
@@ -1,5 +1,6 @@
1
1
  import { TCountryRule, TCountrySelectOption, TSupportedCountries } from './types';
2
2
  declare class PhoneInputLogic {
3
+ private getCaretPositionByRusValue;
3
4
  getIsValidPhoneInput(value: string, country: TCountryRule): boolean;
4
5
  getCountriesSelectOptions(countries?: Array<{
5
6
  label: string;
@@ -15,6 +16,7 @@ declare class PhoneInputLogic {
15
16
  [k: string]: TCountryRule;
16
17
  };
17
18
  getIsHaveStartSequenceInString(value: string, subsequence: string): boolean;
19
+ formatRusPhoneToUnifiedView(value: string, moveCaret?: (positionCaret: number) => void): string;
18
20
  getOnlyNumbersFromString(value: string): string;
19
21
  }
20
22
  export declare const phoneInputLogic: PhoneInputLogic;
@@ -19,6 +19,22 @@ var PhoneInputLogic = /*#__PURE__*/function () {
19
19
  _classCallCheck(this, PhoneInputLogic);
20
20
  }
21
21
  _createClass(PhoneInputLogic, [{
22
+ key: "getCaretPositionByRusValue",
23
+ value: function getCaretPositionByRusValue(value) {
24
+ var mask = _constants.allCountriesPhoneRules.RUS.mask.split('');
25
+ var counter = 0;
26
+ var position = 0;
27
+ mask.forEach(function (maskItem, index) {
28
+ if (maskItem === _constants.MASK_DIGIT_ITEM) {
29
+ counter += 1;
30
+ if (counter === value.length) {
31
+ position = index + 1;
32
+ }
33
+ }
34
+ });
35
+ return position;
36
+ }
37
+ }, {
22
38
  key: "getIsValidPhoneInput",
23
39
  value: function getIsValidPhoneInput(value, country) {
24
40
  return !!value.match(country.regexp);
@@ -58,6 +74,25 @@ var PhoneInputLogic = /*#__PURE__*/function () {
58
74
  value: function getIsHaveStartSequenceInString(value, subsequence) {
59
75
  return value.startsWith(subsequence);
60
76
  }
77
+
78
+ // '8' => '+7', '9' => '+79'
79
+ }, {
80
+ key: "formatRusPhoneToUnifiedView",
81
+ value: function formatRusPhoneToUnifiedView(value, moveCaret) {
82
+ var result = value;
83
+ if (value[0] === '8') {
84
+ result = "7".concat(value.slice(1));
85
+ }
86
+ if (value[0] === '9') {
87
+ var newValue = "79".concat(value.slice(1));
88
+ result = newValue;
89
+ if (moveCaret) {
90
+ // когда пользователь вводит номер с "9", мы подставляем в инпут "79", но каретка не переносится после "9", нужно это делать вручную
91
+ moveCaret(this.getCaretPositionByRusValue(newValue));
92
+ }
93
+ }
94
+ return result;
95
+ }
61
96
  }, {
62
97
  key: "getOnlyNumbersFromString",
63
98
  value: function getOnlyNumbersFromString(value) {
@@ -2,6 +2,7 @@ export declare const RUS_COUNTRY_CODE = "RUS";
2
2
  export declare const supportedCountries: readonly ["RUS", "BLR", "AZE", "ARM", "KAZ", "TJK", "UZB"];
3
3
  export type TSupportedCountries = (typeof supportedCountries)[number];
4
4
  export type TCountryRule = {
5
+ placeholder: string;
5
6
  mask: string;
6
7
  startSubsequence: string;
7
8
  regexp: RegExp;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@invoicebox/ui",
3
- "version": "0.1.0-alpha.62",
3
+ "version": "0.1.0-alpha.64",
4
4
  "description": "Invoicebox UI",
5
5
  "license": "Apache License 2.0",
6
6
  "scripts": {