@rh-support/components 2.1.68 → 2.1.70

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.
@@ -202,7 +202,7 @@ export const COUNTRY_DATA = [
202
202
  '7',
203
203
  '... ...-..-..',
204
204
  1,
205
- ['310', '311', '312', '313', '315', '318', '321', '324', '325', '326', '327', '336', '7172', '73622'],
205
+ ['310', '311', '312', '313', '315', '318', '321', '324', '325', '326', '327', '336', '727', '7172', '73622'],
206
206
  ],
207
207
  ['Kenya', ['africa'], 'ke', '254'],
208
208
  ['Kiribati', ['oceania'], 'ki', '686'],
@@ -1 +1 @@
1
- {"version":3,"file":"PhoneInput.d.ts","sourceRoot":"","sources":["../../../src/PhoneInput/PhoneInput.tsx"],"names":[],"mappings":"AAAA,OAAO,mBAAmB,CAAC;AAM3B,OAAO,KAA8B,MAAM,OAAO,CAAC;AAKnD,UAAU,MAAM;IACZ,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kBAAkB,EAAE,CAAC,MAAM,KAAA,KAAK,IAAI,CAAC;IACrC,WAAW,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,SAAS,CAAC;IACtE,mBAAmB,CAAC,EAAE,CAAC,MAAM,KAAA,KAAK,IAAI,CAAC;IACvC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,UAAU,CAAC,EAAE,CAAC,OAAO,KAAA,KAAK,IAAI,CAAC;IAC/B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,uBAAuB,CAAC,EAAE,OAAO,CAAC;CACrC;AA4ED,eAAO,MAAM,WAAW,gBAAiB,MAAM;;;CAqB9C,CAAC;AAEF,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,qBAoRvC"}
1
+ {"version":3,"file":"PhoneInput.d.ts","sourceRoot":"","sources":["../../../src/PhoneInput/PhoneInput.tsx"],"names":[],"mappings":"AAAA,OAAO,mBAAmB,CAAC;AAO3B,OAAO,KAA8B,MAAM,OAAO,CAAC;AAKnD,UAAU,MAAM;IACZ,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kBAAkB,EAAE,CAAC,MAAM,KAAA,KAAK,IAAI,CAAC;IACrC,WAAW,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,SAAS,CAAC;IACtE,mBAAmB,CAAC,EAAE,CAAC,MAAM,KAAA,KAAK,IAAI,CAAC;IACvC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,UAAU,CAAC,EAAE,CAAC,OAAO,KAAA,KAAK,IAAI,CAAC;IAC/B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,uBAAuB,CAAC,EAAE,OAAO,CAAC;CACrC;AAsFD,eAAO,MAAM,WAAW,gBAAiB,MAAM;;;CAqB9C,CAAC;AAEF,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,qBAqNvC"}
@@ -3,11 +3,13 @@ import { InputGroup, InputGroupItem, Spinner, TextInput } from '@patternfly/reac
3
3
  import { Select, SelectOption, SelectVariant } from '@patternfly/react-core/deprecated';
4
4
  import isEmpty from 'lodash/isEmpty';
5
5
  import isEqual from 'lodash/isEqual';
6
+ import isUndefined from 'lodash/isUndefined';
6
7
  import React, { useEffect, useState } from 'react';
7
8
  import { useTranslation } from 'react-i18next';
8
- import { canadaAreaCodes, COUNTRY_DATA } from './CountryData';
9
+ import { COUNTRY_DATA } from './CountryData';
9
10
  const PREFIX = '+';
10
11
  const DEFAULT_MASK = '... ... ... ... ..';
12
+ const DEFAULT_FORMAT = PREFIX + DEFAULT_MASK;
11
13
  const ALWAYS_DEFAULT_MASK = false;
12
14
  // Getting Country phone format
13
15
  function getMask(prefix, dialCode, predefinedMask, defaultMask, alwaysDefaultMask) {
@@ -29,6 +31,9 @@ const initializedCountries = COUNTRY_DATA.map((country) => {
29
31
  format: getMask(PREFIX, country[3], country[4], DEFAULT_MASK, ALWAYS_DEFAULT_MASK),
30
32
  priority: country[5] || 0,
31
33
  areaCode: country[6] || [],
34
+ phoneInitials: !isEmpty(country[6]) // dialCode + areaCode
35
+ ? country[6].map((areaCode) => `${country[3]}${areaCode}`)
36
+ : [],
32
37
  };
33
38
  return countryItem;
34
39
  });
@@ -44,8 +49,15 @@ const removeAllSpaces = (value) => {
44
49
  return '';
45
50
  return value === null || value === void 0 ? void 0 : value.replace(/\s+/g, '');
46
51
  };
52
+ const removeAllChars = (value) => {
53
+ if (!value)
54
+ return '';
55
+ // to remove all chars, brackets and have only digits
56
+ return value === null || value === void 0 ? void 0 : value.replace(/[^0-9]/g, '');
57
+ };
47
58
  // Formatting phone numbers according to country
48
59
  const formatNumber = (fullPhoneNumber, localFormat) => {
60
+ var _a;
49
61
  if (isEmpty(fullPhoneNumber))
50
62
  return '';
51
63
  let localPhone = fullPhoneNumber === null || fullPhoneNumber === void 0 ? void 0 : fullPhoneNumber.replace(PREFIX, '');
@@ -56,7 +68,7 @@ const formatNumber = (fullPhoneNumber, localFormat) => {
56
68
  let phone = '';
57
69
  [...fullPhoneNumber].forEach((ele) => {
58
70
  if (ele >= '0' && ele <= '9') {
59
- phone = phone.concat(ele);
71
+ phone = phone === null || phone === void 0 ? void 0 : phone.concat(ele);
60
72
  }
61
73
  });
62
74
  let pattern = '';
@@ -70,26 +82,27 @@ const formatNumber = (fullPhoneNumber, localFormat) => {
70
82
  pattern += localFormat[j];
71
83
  }
72
84
  }
73
- const isPatternWithPrefix = pattern.replace(/\s+/g, '').startsWith(PREFIX);
85
+ const isPatternWithPrefix = (_a = pattern.replace(/\s+/g, '')) === null || _a === void 0 ? void 0 : _a.startsWith(PREFIX);
74
86
  pattern = isPatternWithPrefix ? pattern : !isEmpty(isPatternWithPrefix) ? PREFIX + pattern : '';
75
87
  return pattern;
76
88
  };
77
89
  // return phoneline and countrycode for any given phone
78
90
  export const getPhoneObj = (phoneNumber) => {
79
- let localPhone = phoneNumber === null || phoneNumber === void 0 ? void 0 : phoneNumber.replace(/[^0-9]/g, '');
91
+ var _a, _b, _c;
92
+ let localPhone = removeAllChars(phoneNumber);
80
93
  let countryCode = '';
81
94
  // Guessing countries according to user input
82
95
  const possibleCountries = initializedCountries.filter((country) => localPhone === null || localPhone === void 0 ? void 0 : localPhone.startsWith(country.dialCode.toString()));
83
- const exactCountry = initializedCountries.filter((country) => country.dialCode.toString() === localPhone);
96
+ const exactCountry = initializedCountries.filter((country) => { var _a; return ((_a = country.dialCode) === null || _a === void 0 ? void 0 : _a.toString()) === localPhone; });
84
97
  if (!isEmpty(exactCountry)) {
85
- countryCode = exactCountry[0].dialCode.toString();
98
+ countryCode = PREFIX + ((_a = exactCountry[0]) === null || _a === void 0 ? void 0 : _a.dialCode.toString());
86
99
  }
87
100
  else if (!isEmpty(possibleCountries)) {
88
- countryCode = possibleCountries[0].dialCode.toString();
101
+ countryCode = PREFIX + ((_b = possibleCountries[0]) === null || _b === void 0 ? void 0 : _b.dialCode.toString());
89
102
  }
90
103
  return {
91
- countryCode: PREFIX + countryCode,
92
- phoneLine: localPhone === null || localPhone === void 0 ? void 0 : localPhone.substring(countryCode.length, localPhone.length),
104
+ countryCode: countryCode,
105
+ phoneLine: localPhone === null || localPhone === void 0 ? void 0 : localPhone.substring((_c = trimAndReplacePlusFromPhone(countryCode)) === null || _c === void 0 ? void 0 : _c.length, localPhone === null || localPhone === void 0 ? void 0 : localPhone.length),
93
106
  };
94
107
  };
95
108
  export function PhoneInput(props) {
@@ -98,78 +111,46 @@ export function PhoneInput(props) {
98
111
  const [isOpen, setIsOpen] = useState(false);
99
112
  const [iso2, setIso2] = useState('');
100
113
  const [formattedNumber, setFormattedNumber] = useState('');
101
- const [format, setFormat] = useState('');
102
- const [phoneWithoutCountryCode, setPhoneWithoutCountryCode] = useState('');
103
- const [countryObj, setCountryObj] = useState({
104
- name: '', // Country name
105
- regions: [''], // Surronding regions
106
- iso2: '', // Alpha 2 code Eg. For United States - 'US'
107
- countryCode: '', // Country Telephone Code (For US, 1)
108
- dialCode: '', // Dial Code (For US, Canada, 1)
109
- format: '', // Number Format (For US, '(...) ...-....' )
110
- priority: 0, // To prioritize countries with same dial code
111
- areaCode: [''], // Area codes of a country
112
- });
113
114
  const setLocalFormat = (phoneNumber) => {
114
- let localPhone = phoneNumber === null || phoneNumber === void 0 ? void 0 : phoneNumber.replace(PREFIX, '').trim();
115
- localPhone = localPhone === null || localPhone === void 0 ? void 0 : localPhone.replace('(', '').trim();
116
- localPhone = localPhone === null || localPhone === void 0 ? void 0 : localPhone.replace(/ +/g, '');
115
+ const localPhone = removeAllChars(phoneNumber);
117
116
  if (shouldNotSetCountryCode) {
118
- return PREFIX + DEFAULT_MASK; // default format for portal phone without country code
119
- }
120
- if (!isEmpty(countryObj) && isEqual(localPhone, countryObj.dialCode)) {
121
- // if the user selects a value from the dropdown and the country code is the same as prev, iso2 values shouldn't override
122
- onCountryCodeChange && onCountryCodeChange(PREFIX + countryObj.countryCode);
123
- return countryObj.format;
117
+ return DEFAULT_FORMAT; // default format for portal phone without country code
124
118
  }
125
119
  let localFormat;
126
120
  // Guessing countries according to user input
127
121
  const possibleCountries = initializedCountries.filter((country) => localPhone === null || localPhone === void 0 ? void 0 : localPhone.startsWith(country.dialCode.toString()));
128
- const canadaDialCode = '1';
129
- const canadaCountryPhone = canadaAreaCodes.map((areaCode) => canadaDialCode + areaCode);
130
- const isCountryCanada = canadaCountryPhone.filter((phone) => localPhone.startsWith(phone)); // checks if phone is Canadian
131
- const exactCountry = initializedCountries.filter((country) => country.dialCode.toString() === localPhone);
132
- if (!isEmpty(isCountryCanada)) {
133
- const canadaCountryObj = initializedCountries.filter((country) => country.iso2.toString() === 'ca');
134
- setIso2(canadaCountryObj[0].iso2.toString());
135
- localFormat = canadaCountryObj[0].format.toString();
136
- onCountryCodeChange && onCountryCodeChange(PREFIX + canadaCountryObj[0].dialCode.toString());
137
- setFormat(localFormat);
138
- }
139
- else if (!isEmpty(exactCountry)) {
140
- const localExactCountry = exactCountry.filter((country) => isEqual(country.priority, 0));
141
- if (isEqual(localExactCountry[0].dialCode.toString(), countryObj.dialCode)) {
142
- setIso2(countryObj.iso2);
143
- onCountryCodeChange && onCountryCodeChange(PREFIX + countryObj.countryCode);
144
- return countryObj.format;
122
+ const possibleCountriesPhoneInitials = possibleCountries === null || possibleCountries === void 0 ? void 0 : possibleCountries.map((country) => country.phoneInitials);
123
+ // Function to find the matching country index based on user phone input
124
+ const findMatchingIndex = (possibleCountry, phone) => {
125
+ for (let i = 0; i < (possibleCountry === null || possibleCountry === void 0 ? void 0 : possibleCountry.length); i++) {
126
+ const country = possibleCountry[i];
127
+ for (let j = 0; j < (country === null || country === void 0 ? void 0 : country.length); j++) {
128
+ if (phone.startsWith(country[j])) {
129
+ return i;
130
+ }
131
+ }
145
132
  }
146
- setIso2(localExactCountry[0].iso2.toString());
147
- localFormat = localExactCountry[0].format.toString();
148
- onCountryCodeChange && onCountryCodeChange(PREFIX + localExactCountry[0].dialCode.toString());
149
- setFormat(localFormat);
150
- }
151
- else if (!isEmpty(possibleCountries)) {
152
- const localPossibleCountry = possibleCountries.filter((country) => isEqual(country.priority, 0));
153
- if (isEqual(localPossibleCountry[0].dialCode.toString(), countryObj.dialCode)) {
154
- setIso2(countryObj.iso2);
155
- onCountryCodeChange && onCountryCodeChange(PREFIX + countryObj.countryCode);
156
- return countryObj.format;
157
- }
158
- setIso2(localPossibleCountry[0].iso2.toString());
159
- localFormat = localPossibleCountry[0].format.toString();
160
- onCountryCodeChange && onCountryCodeChange(PREFIX + localPossibleCountry[0].dialCode.toString());
161
- setFormat(localFormat);
133
+ return -1; // Return -1 if no match is found
134
+ };
135
+ const countryMatchingIndex = findMatchingIndex(possibleCountriesPhoneInitials, localPhone);
136
+ const defaultCountry = possibleCountries === null || possibleCountries === void 0 ? void 0 : possibleCountries.filter((country) => (localPhone === null || localPhone === void 0 ? void 0 : localPhone.startsWith(country.dialCode.toString())) && isEqual(country.priority, 0));
137
+ const possibleDefaultCountry = !isUndefined(defaultCountry[0]) ? defaultCountry[0] : []; // default country when phone didn't match with any country's area code
138
+ const exactCountry = countryMatchingIndex < 0 ? possibleDefaultCountry : possibleCountries[countryMatchingIndex];
139
+ if (!isEmpty(exactCountry)) {
140
+ setIso2(exactCountry['iso2'].toString());
141
+ localFormat = exactCountry['format'].toString();
142
+ onCountryCodeChange && onCountryCodeChange(PREFIX + exactCountry['dialCode'].toString());
162
143
  }
163
144
  else {
164
- // if the exact and possible are not the
145
+ // if there are not matched possible countries
165
146
  setIso2('');
166
- localFormat = format;
147
+ localFormat = DEFAULT_FORMAT;
167
148
  onCountryCodeChange && onCountryCodeChange('');
168
149
  }
169
150
  return localFormat;
170
151
  };
171
152
  const onPhoneChange = (phone) => {
172
- if (isEmpty(phone === null || phone === void 0 ? void 0 : phone.trim()) || isEmpty(phone === null || phone === void 0 ? void 0 : phone.replace(PREFIX, '').trim())) {
153
+ if (isEmpty(removeAllSpaces(phone)) || isEmpty(trimAndReplacePlusFromPhone(phone))) {
173
154
  setFormattedNumber('');
174
155
  onPhoneValueChange('');
175
156
  setInvalid && setInvalid(false);
@@ -180,19 +161,22 @@ export function PhoneInput(props) {
180
161
  const localFormat = setLocalFormat(phone);
181
162
  const _formattedNumber = formatNumber(phone, localFormat);
182
163
  setFormattedNumber(_formattedNumber);
183
- if (!phone.trim().startsWith(PREFIX) && regex.test(phone) && !isEqual(phone, '-')) {
184
- onPhoneValueChange(PREFIX + phone.trim());
164
+ if (!(phone === null || phone === void 0 ? void 0 : phone.trim().startsWith(PREFIX)) && regex.test(phone) && !isEqual(phone, '-')) {
165
+ onPhoneValueChange(PREFIX + (phone === null || phone === void 0 ? void 0 : phone.trim()));
185
166
  }
186
167
  else {
187
- // to allow users to edit phone from between but not from right end, 5 is max length of any country code used when local format is empty
188
- onPhoneValueChange(phone.slice(0, localFormat.length || 5));
168
+ onPhoneValueChange(phone === null || phone === void 0 ? void 0 : phone.slice(0, localFormat === null || localFormat === void 0 ? void 0 : localFormat.length));
189
169
  }
190
170
  };
191
171
  const onToggle = (isOpen) => {
192
172
  setIsOpen(isOpen);
193
173
  };
194
174
  const onCountrySelect = (event, selection) => {
195
- setCountryObj(selection);
175
+ const prevCountryCode = !shouldNotSetCountryCode // fetching previous country code to remove it from the phone
176
+ ? trimAndReplacePlusFromPhone(getPhoneObj(phoneValue).countryCode)
177
+ : '';
178
+ const phoneWithoutCountryCode = removeAllChars(phoneValue).replace(prevCountryCode, '');
179
+ onPhoneValueChange(`${PREFIX}${selection.countryCode} ${trimAndReplacePlusFromPhone(phoneWithoutCountryCode)}`);
196
180
  setIsOpen(!isOpen);
197
181
  };
198
182
  const getOptions = (options) => {
@@ -217,18 +201,6 @@ export function PhoneInput(props) {
217
201
  React.createElement("div", { className: "pf-v5-u-display-inline-flex" },
218
202
  React.createElement("div", { className: `flag ${iso2}`, style: { marginRight: '5px' } }),
219
203
  iso2.toUpperCase())));
220
- useEffect(() => {
221
- if (!isEmpty(countryObj.iso2) && `${PREFIX}${countryObj.dialCode}` !== countryCode) {
222
- if (shouldNotSetCountryCode) {
223
- onCountryCodeChange && onCountryCodeChange(PREFIX + countryObj.dialCode);
224
- }
225
- setIso2(countryObj.iso2);
226
- setFormat(countryObj.format);
227
- onPhoneValueChange(`${PREFIX}${countryObj.dialCode} ${trimAndReplacePlusFromPhone(phoneWithoutCountryCode)}`);
228
- setFormattedNumber(`${PREFIX}${countryObj.dialCode} ${trimAndReplacePlusFromPhone(phoneWithoutCountryCode)}`);
229
- }
230
- // eslint-disable-next-line react-hooks/exhaustive-deps
231
- }, [countryObj]);
232
204
  useEffect(() => {
233
205
  // when user types +, it will be replaced by empty string
234
206
  if (isEqual(phoneValue === null || phoneValue === void 0 ? void 0 : phoneValue.trim(), PREFIX) && isEmpty(iso2)) {
@@ -236,33 +208,17 @@ export function PhoneInput(props) {
236
208
  setFormattedNumber('');
237
209
  return;
238
210
  }
239
- if (isEmpty(phoneValue) && isEmpty(iso2)) {
240
- setCountryObj({
241
- name: '',
242
- regions: [''],
243
- iso2: '',
244
- countryCode: '',
245
- dialCode: '',
246
- format: '',
247
- priority: 0,
248
- areaCode: [''],
249
- });
250
- }
251
211
  if (isEmpty(phoneValue)) {
252
212
  setIso2('');
253
213
  setInvalid && setInvalid(false);
254
214
  setFormattedNumber('');
255
215
  onPhoneValueChange('');
256
- setPhoneWithoutCountryCode('');
257
216
  }
258
217
  else {
259
218
  onPhoneValueChange(phoneValue);
260
219
  const localFormat = setLocalFormat(phoneValue);
261
220
  const _formattedNumber = formatNumber(phoneValue, localFormat);
262
221
  setFormattedNumber(_formattedNumber);
263
- // to have phone without country code so it can populated with the newly selected country code
264
- // instead of keeping it just country code
265
- setPhoneWithoutCountryCode(removeAllSpaces(phoneValue).replace(countryCode, ''));
266
222
  }
267
223
  // eslint-disable-next-line react-hooks/exhaustive-deps
268
224
  }, [phoneValue]);
@@ -275,7 +231,6 @@ export function PhoneInput(props) {
275
231
  onCountryCodeChange && onCountryCodeChange('');
276
232
  }
277
233
  else {
278
- setIso2(countryObj.iso2);
279
234
  onCountryCodeChange && onCountryCodeChange(PREFIX + countryCode);
280
235
  }
281
236
  onPhoneValueChange && onPhoneValueChange(removeAllSpaces(phoneValue)); // removing trailing and in between spaces
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rh-support/components",
3
- "version": "2.1.68",
3
+ "version": "2.1.70",
4
4
  "description": "Contains all reusabel components for support app",
5
5
  "author": "Vikas Rathee <vrathee@redhat.com>",
6
6
  "license": "ISC",
@@ -109,5 +109,5 @@
109
109
  "defaults and supports es6-module",
110
110
  "maintained node versions"
111
111
  ],
112
- "gitHead": "7ee3ae4d2a39a3f70d898ea5026e558be0ba5ab4"
112
+ "gitHead": "e840019f9485b934d8d2b5163b4a94c3fc4ae2d2"
113
113
  }