@rh-support/components 2.1.68 → 2.1.69
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;
|
|
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 {
|
|
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,6 +49,12 @@ 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) => {
|
|
49
60
|
if (isEmpty(fullPhoneNumber))
|
|
@@ -76,20 +87,20 @@ const formatNumber = (fullPhoneNumber, localFormat) => {
|
|
|
76
87
|
};
|
|
77
88
|
// return phoneline and countrycode for any given phone
|
|
78
89
|
export const getPhoneObj = (phoneNumber) => {
|
|
79
|
-
let localPhone = phoneNumber
|
|
90
|
+
let localPhone = removeAllChars(phoneNumber);
|
|
80
91
|
let countryCode = '';
|
|
81
92
|
// Guessing countries according to user input
|
|
82
93
|
const possibleCountries = initializedCountries.filter((country) => localPhone === null || localPhone === void 0 ? void 0 : localPhone.startsWith(country.dialCode.toString()));
|
|
83
94
|
const exactCountry = initializedCountries.filter((country) => country.dialCode.toString() === localPhone);
|
|
84
95
|
if (!isEmpty(exactCountry)) {
|
|
85
|
-
countryCode = exactCountry[0].dialCode.toString();
|
|
96
|
+
countryCode = PREFIX + exactCountry[0].dialCode.toString();
|
|
86
97
|
}
|
|
87
98
|
else if (!isEmpty(possibleCountries)) {
|
|
88
|
-
countryCode = possibleCountries[0].dialCode.toString();
|
|
99
|
+
countryCode = PREFIX + possibleCountries[0].dialCode.toString();
|
|
89
100
|
}
|
|
90
101
|
return {
|
|
91
|
-
countryCode:
|
|
92
|
-
phoneLine: localPhone === null || localPhone === void 0 ? void 0 : localPhone.substring(countryCode.length, localPhone.length),
|
|
102
|
+
countryCode: countryCode,
|
|
103
|
+
phoneLine: localPhone === null || localPhone === void 0 ? void 0 : localPhone.substring(trimAndReplacePlusFromPhone(countryCode).length, localPhone.length),
|
|
93
104
|
};
|
|
94
105
|
};
|
|
95
106
|
export function PhoneInput(props) {
|
|
@@ -98,78 +109,46 @@ export function PhoneInput(props) {
|
|
|
98
109
|
const [isOpen, setIsOpen] = useState(false);
|
|
99
110
|
const [iso2, setIso2] = useState('');
|
|
100
111
|
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
112
|
const setLocalFormat = (phoneNumber) => {
|
|
114
|
-
|
|
115
|
-
localPhone = localPhone === null || localPhone === void 0 ? void 0 : localPhone.replace('(', '').trim();
|
|
116
|
-
localPhone = localPhone === null || localPhone === void 0 ? void 0 : localPhone.replace(/ +/g, '');
|
|
113
|
+
const localPhone = removeAllChars(phoneNumber);
|
|
117
114
|
if (shouldNotSetCountryCode) {
|
|
118
|
-
return
|
|
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;
|
|
115
|
+
return DEFAULT_FORMAT; // default format for portal phone without country code
|
|
124
116
|
}
|
|
125
117
|
let localFormat;
|
|
126
118
|
// Guessing countries according to user input
|
|
127
119
|
const possibleCountries = initializedCountries.filter((country) => localPhone === null || localPhone === void 0 ? void 0 : localPhone.startsWith(country.dialCode.toString()));
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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;
|
|
120
|
+
const possibleCountriesPhoneInitials = possibleCountries.map((country) => country.phoneInitials);
|
|
121
|
+
// Function to find the matching country index based on user phone input
|
|
122
|
+
const findMatchingIndex = (possibleCountry, phone) => {
|
|
123
|
+
for (let i = 0; i < possibleCountry.length; i++) {
|
|
124
|
+
const country = possibleCountry[i];
|
|
125
|
+
for (let j = 0; j < country.length; j++) {
|
|
126
|
+
if (phone.startsWith(country[j])) {
|
|
127
|
+
return i;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
145
130
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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);
|
|
131
|
+
return -1; // Return -1 if no match is found
|
|
132
|
+
};
|
|
133
|
+
const countryMatchingIndex = findMatchingIndex(possibleCountriesPhoneInitials, localPhone);
|
|
134
|
+
const defaultCountry = possibleCountries.filter((country) => (localPhone === null || localPhone === void 0 ? void 0 : localPhone.startsWith(country.dialCode.toString())) && isEqual(country.priority, 0));
|
|
135
|
+
const possibleDefaultCountry = !isUndefined(defaultCountry[0]) ? defaultCountry[0] : []; // default country when phone didn't match with any country's area code
|
|
136
|
+
const exactCountry = countryMatchingIndex < 0 ? possibleDefaultCountry : possibleCountries[countryMatchingIndex];
|
|
137
|
+
if (!isEmpty(exactCountry)) {
|
|
138
|
+
setIso2(exactCountry['iso2'].toString());
|
|
139
|
+
localFormat = exactCountry['format'].toString();
|
|
140
|
+
onCountryCodeChange && onCountryCodeChange(PREFIX + exactCountry['dialCode'].toString());
|
|
162
141
|
}
|
|
163
142
|
else {
|
|
164
|
-
// if
|
|
143
|
+
// if there are not matched possible countries
|
|
165
144
|
setIso2('');
|
|
166
|
-
localFormat =
|
|
145
|
+
localFormat = DEFAULT_FORMAT;
|
|
167
146
|
onCountryCodeChange && onCountryCodeChange('');
|
|
168
147
|
}
|
|
169
148
|
return localFormat;
|
|
170
149
|
};
|
|
171
150
|
const onPhoneChange = (phone) => {
|
|
172
|
-
if (isEmpty(phone
|
|
151
|
+
if (isEmpty(removeAllSpaces(phone)) || isEmpty(trimAndReplacePlusFromPhone(phone))) {
|
|
173
152
|
setFormattedNumber('');
|
|
174
153
|
onPhoneValueChange('');
|
|
175
154
|
setInvalid && setInvalid(false);
|
|
@@ -184,15 +163,18 @@ export function PhoneInput(props) {
|
|
|
184
163
|
onPhoneValueChange(PREFIX + phone.trim());
|
|
185
164
|
}
|
|
186
165
|
else {
|
|
187
|
-
|
|
188
|
-
onPhoneValueChange(phone.slice(0, localFormat.length || 5));
|
|
166
|
+
onPhoneValueChange(phone.slice(0, localFormat.length));
|
|
189
167
|
}
|
|
190
168
|
};
|
|
191
169
|
const onToggle = (isOpen) => {
|
|
192
170
|
setIsOpen(isOpen);
|
|
193
171
|
};
|
|
194
172
|
const onCountrySelect = (event, selection) => {
|
|
195
|
-
|
|
173
|
+
const prevCountryCode = !shouldNotSetCountryCode // fetching previous country code to remove it from the phone
|
|
174
|
+
? trimAndReplacePlusFromPhone(getPhoneObj(phoneValue).countryCode)
|
|
175
|
+
: '';
|
|
176
|
+
const phoneWithoutCountryCode = removeAllChars(phoneValue).replace(prevCountryCode, '');
|
|
177
|
+
onPhoneValueChange(`${PREFIX}${selection.countryCode} ${trimAndReplacePlusFromPhone(phoneWithoutCountryCode)}`);
|
|
196
178
|
setIsOpen(!isOpen);
|
|
197
179
|
};
|
|
198
180
|
const getOptions = (options) => {
|
|
@@ -217,18 +199,6 @@ export function PhoneInput(props) {
|
|
|
217
199
|
React.createElement("div", { className: "pf-v5-u-display-inline-flex" },
|
|
218
200
|
React.createElement("div", { className: `flag ${iso2}`, style: { marginRight: '5px' } }),
|
|
219
201
|
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
202
|
useEffect(() => {
|
|
233
203
|
// when user types +, it will be replaced by empty string
|
|
234
204
|
if (isEqual(phoneValue === null || phoneValue === void 0 ? void 0 : phoneValue.trim(), PREFIX) && isEmpty(iso2)) {
|
|
@@ -236,33 +206,17 @@ export function PhoneInput(props) {
|
|
|
236
206
|
setFormattedNumber('');
|
|
237
207
|
return;
|
|
238
208
|
}
|
|
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
209
|
if (isEmpty(phoneValue)) {
|
|
252
210
|
setIso2('');
|
|
253
211
|
setInvalid && setInvalid(false);
|
|
254
212
|
setFormattedNumber('');
|
|
255
213
|
onPhoneValueChange('');
|
|
256
|
-
setPhoneWithoutCountryCode('');
|
|
257
214
|
}
|
|
258
215
|
else {
|
|
259
216
|
onPhoneValueChange(phoneValue);
|
|
260
217
|
const localFormat = setLocalFormat(phoneValue);
|
|
261
218
|
const _formattedNumber = formatNumber(phoneValue, localFormat);
|
|
262
219
|
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
220
|
}
|
|
267
221
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
268
222
|
}, [phoneValue]);
|
|
@@ -275,7 +229,6 @@ export function PhoneInput(props) {
|
|
|
275
229
|
onCountryCodeChange && onCountryCodeChange('');
|
|
276
230
|
}
|
|
277
231
|
else {
|
|
278
|
-
setIso2(countryObj.iso2);
|
|
279
232
|
onCountryCodeChange && onCountryCodeChange(PREFIX + countryCode);
|
|
280
233
|
}
|
|
281
234
|
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.
|
|
3
|
+
"version": "2.1.69",
|
|
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": "
|
|
112
|
+
"gitHead": "8720a44261dd585266b2cfdf2bf5092a0cec88d0"
|
|
113
113
|
}
|