@rh-support/components 2.0.12 → 2.1.0

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 (57) hide show
  1. package/lib/cjs/DropDownList/SearchableList.d.ts +1 -0
  2. package/lib/cjs/DropDownList/SearchableList.d.ts.map +1 -1
  3. package/lib/cjs/DropDownList/SearchableList.js +4 -2
  4. package/lib/cjs/DropDownList/dropdownList.css +1 -1
  5. package/lib/cjs/DropDownList/types.d.ts +1 -0
  6. package/lib/cjs/DropDownList/types.d.ts.map +1 -1
  7. package/lib/cjs/InlineEdit/InlineEdit.d.ts +1 -0
  8. package/lib/cjs/InlineEdit/InlineEdit.d.ts.map +1 -1
  9. package/lib/cjs/InlineEdit/InlineEdit.js +2 -2
  10. package/lib/cjs/InlineEdit/NewInlineEdit.d.ts +1 -0
  11. package/lib/cjs/InlineEdit/NewInlineEdit.d.ts.map +1 -1
  12. package/lib/cjs/InlineEdit/NewInlineEdit.js +2 -2
  13. package/lib/cjs/InlineEdit/inlineEdit.css +4 -0
  14. package/lib/cjs/InlineEdit/newInlineEdit.css +4 -0
  15. package/lib/cjs/PhoneInput/CountryData.d.ts +3 -0
  16. package/lib/cjs/PhoneInput/CountryData.d.ts.map +1 -0
  17. package/lib/cjs/PhoneInput/CountryData.js +590 -0
  18. package/lib/cjs/PhoneInput/PhoneInput.d.ts +16 -0
  19. package/lib/cjs/PhoneInput/PhoneInput.d.ts.map +1 -0
  20. package/lib/cjs/PhoneInput/PhoneInput.js +204 -0
  21. package/lib/cjs/PhoneInput/PhoneInput.scss +753 -0
  22. package/lib/cjs/PhoneInput/flags.png +0 -0
  23. package/lib/cjs/PhoneInput/index.d.ts +2 -0
  24. package/lib/cjs/PhoneInput/index.d.ts.map +1 -0
  25. package/lib/cjs/PhoneInput/index.js +13 -0
  26. package/lib/cjs/index.d.ts +1 -0
  27. package/lib/cjs/index.d.ts.map +1 -1
  28. package/lib/cjs/index.js +1 -0
  29. package/lib/esm/DropDownList/SearchableList.d.ts +1 -0
  30. package/lib/esm/DropDownList/SearchableList.d.ts.map +1 -1
  31. package/lib/esm/DropDownList/SearchableList.js +4 -2
  32. package/lib/esm/DropDownList/dropdownList.css +1 -1
  33. package/lib/esm/DropDownList/types.d.ts +1 -0
  34. package/lib/esm/DropDownList/types.d.ts.map +1 -1
  35. package/lib/esm/InlineEdit/InlineEdit.d.ts +1 -0
  36. package/lib/esm/InlineEdit/InlineEdit.d.ts.map +1 -1
  37. package/lib/esm/InlineEdit/InlineEdit.js +2 -2
  38. package/lib/esm/InlineEdit/NewInlineEdit.d.ts +1 -0
  39. package/lib/esm/InlineEdit/NewInlineEdit.d.ts.map +1 -1
  40. package/lib/esm/InlineEdit/NewInlineEdit.js +2 -2
  41. package/lib/esm/InlineEdit/inlineEdit.css +4 -0
  42. package/lib/esm/InlineEdit/newInlineEdit.css +4 -0
  43. package/lib/esm/PhoneInput/CountryData.d.ts +3 -0
  44. package/lib/esm/PhoneInput/CountryData.d.ts.map +1 -0
  45. package/lib/esm/PhoneInput/CountryData.js +588 -0
  46. package/lib/esm/PhoneInput/PhoneInput.d.ts +16 -0
  47. package/lib/esm/PhoneInput/PhoneInput.d.ts.map +1 -0
  48. package/lib/esm/PhoneInput/PhoneInput.js +178 -0
  49. package/lib/esm/PhoneInput/PhoneInput.scss +753 -0
  50. package/lib/esm/PhoneInput/flags.png +0 -0
  51. package/lib/esm/PhoneInput/index.d.ts +2 -0
  52. package/lib/esm/PhoneInput/index.d.ts.map +1 -0
  53. package/lib/esm/PhoneInput/index.js +1 -0
  54. package/lib/esm/index.d.ts +1 -0
  55. package/lib/esm/index.d.ts.map +1 -1
  56. package/lib/esm/index.js +1 -0
  57. package/package.json +12 -9
@@ -0,0 +1,178 @@
1
+ import './PhoneInput.scss';
2
+ import { InputGroup, Select, SelectOption, SelectVariant, Spinner, TextInput } from '@patternfly/react-core';
3
+ import { isEmpty, isEqual } from 'lodash';
4
+ import React, { useEffect, useState } from 'react';
5
+ import { useTranslation } from 'react-i18next';
6
+ import CountryData from './CountryData';
7
+ export function PhoneInput(props) {
8
+ const { phoneValue, countryCode, onCountryCodeChange, onPhoneValueChange, validations, setInvalid } = props;
9
+ const { t } = useTranslation();
10
+ const [isOpen, setIsOpen] = useState(false);
11
+ const [iso2, setIso2] = useState('');
12
+ const [formattedNumber, setFormattedNumber] = useState('');
13
+ const [format, setFormat] = useState('');
14
+ const PREFIX = '+';
15
+ const DEFAULT_MASK = '... ... ... ... ..';
16
+ const alwaysDefaultMask = false;
17
+ const [countryObj, setCountryObj] = useState({
18
+ name: '',
19
+ regions: [''],
20
+ iso2: '',
21
+ countryCode: '',
22
+ dialCode: '',
23
+ format: '', // Number Format (For US, '(...) ...-....' )
24
+ });
25
+ // Getting Country phone format
26
+ function getMask(prefix, dialCode, predefinedMask, defaultMask, alwaysDefaultMask) {
27
+ if (!predefinedMask || alwaysDefaultMask) {
28
+ return prefix + ''.padEnd(dialCode.length, '.') + ' ' + defaultMask;
29
+ }
30
+ else {
31
+ return prefix + ''.padEnd(dialCode.length, '.') + ' ' + predefinedMask;
32
+ }
33
+ }
34
+ const initializedCountries = CountryData.map((country) => {
35
+ const countryItem = {
36
+ name: country[0],
37
+ regions: country[1],
38
+ iso2: country[2],
39
+ countryCode: country[3],
40
+ dialCode: country[3],
41
+ format: getMask(PREFIX, country[3], country[4], DEFAULT_MASK, alwaysDefaultMask),
42
+ priority: country[5] || 0,
43
+ };
44
+ return countryItem;
45
+ });
46
+ const setLocalFormat = (phoneNumber) => {
47
+ let localPhone = phoneNumber.replace(PREFIX, '');
48
+ let localFormat;
49
+ // Guessing countries according to user input
50
+ const possibleCountries = initializedCountries.filter((country) => localPhone === null || localPhone === void 0 ? void 0 : localPhone.startsWith(country.dialCode.toString()));
51
+ const exactCountry = initializedCountries.filter((country) => country.dialCode.toString() === localPhone);
52
+ if (!isEmpty(exactCountry)) {
53
+ setIso2(exactCountry[0].iso2.toString());
54
+ localFormat = exactCountry[0].format.toString();
55
+ onCountryCodeChange && onCountryCodeChange(PREFIX + exactCountry[0].dialCode.toString());
56
+ setFormat(localFormat);
57
+ }
58
+ else if (!isEmpty(possibleCountries)) {
59
+ setIso2(possibleCountries[0].iso2.toString());
60
+ localFormat = possibleCountries[0].format.toString();
61
+ onCountryCodeChange && onCountryCodeChange(PREFIX + possibleCountries[0].dialCode.toString());
62
+ setFormat(localFormat);
63
+ }
64
+ else {
65
+ // if the exact and possible are not the
66
+ setIso2('');
67
+ localFormat = format;
68
+ onCountryCodeChange && onCountryCodeChange('');
69
+ }
70
+ return localFormat;
71
+ };
72
+ const onPhoneChange = (phone) => {
73
+ if (isEmpty(phone.replace(PREFIX, ''))) {
74
+ setFormattedNumber('');
75
+ onPhoneValueChange('');
76
+ }
77
+ const regex = /^[\d ()+-]+$/;
78
+ if (setInvalid)
79
+ regex.test(phone) ? setInvalid(false) : setInvalid(true);
80
+ const localFormat = setLocalFormat(phone);
81
+ const _formattedNumber = formatNumber(phone, localFormat);
82
+ setFormattedNumber(_formattedNumber);
83
+ if (!phoneValue.startsWith(PREFIX) && regex.test(phone) && !isEqual(phone, '-')) {
84
+ onPhoneValueChange(phone.startsWith(PREFIX) ? phone : PREFIX + phone);
85
+ }
86
+ else if (phone.length <= _formattedNumber.length) {
87
+ onPhoneValueChange(phone);
88
+ }
89
+ };
90
+ const onToggle = (isOpen) => {
91
+ setIsOpen(isOpen);
92
+ };
93
+ const onCountrySelect = (event, selection) => {
94
+ setCountryObj(selection);
95
+ setIsOpen(!isOpen);
96
+ };
97
+ const getOptions = (options) => {
98
+ return options.map((country) => (React.createElement(SelectOption, { key: country.iso2, value: country },
99
+ React.createElement("div", { className: "pf-u-display-inline-flex" },
100
+ React.createElement("div", { className: `flag ${country.iso2}`, style: { marginRight: '8px' } }),
101
+ country.name,
102
+ " (+",
103
+ country.dialCode,
104
+ ")"))));
105
+ };
106
+ const onFilter = (_, value) => {
107
+ if (!value)
108
+ return getOptions(initializedCountries);
109
+ const newOptions = initializedCountries.filter((country) => {
110
+ const countryStr = `${country.name.toString()} (+${country.countryCode.toString()})`.toLowerCase();
111
+ return countryStr.includes(value.toLowerCase());
112
+ });
113
+ return getOptions(newOptions);
114
+ };
115
+ // Formatting phone numbers according to country
116
+ const formatNumber = (fullPhoneNumber, localFormat) => {
117
+ if (isEmpty(fullPhoneNumber))
118
+ return '';
119
+ let localPhone = fullPhoneNumber.replace(PREFIX, '');
120
+ if (isEmpty(localPhone))
121
+ return '';
122
+ // Formatting inputted number
123
+ let phone = '';
124
+ [...fullPhoneNumber].forEach((ele) => {
125
+ if (ele >= '0' && ele <= '9') {
126
+ phone = phone.concat(ele);
127
+ }
128
+ });
129
+ let pattern = '';
130
+ let i = 0;
131
+ for (let j = 0; j < localFormat.length && i < phone.length; j++) {
132
+ if (localFormat[j] === '.') {
133
+ pattern += phone[i];
134
+ i++;
135
+ }
136
+ else {
137
+ pattern += localFormat[j];
138
+ }
139
+ }
140
+ return pattern;
141
+ };
142
+ const placeholder = (React.createElement("div", { className: "phone-number-select-placeholder" },
143
+ React.createElement("div", { className: "pf-u-display-inline-flex" },
144
+ React.createElement("div", { className: `flag ${iso2}`, style: { marginRight: '5px' } }),
145
+ iso2.toUpperCase())));
146
+ useEffect(() => {
147
+ if (isEmpty(phoneValue))
148
+ return;
149
+ if (isEmpty(iso2)) {
150
+ const localFormat = setLocalFormat(phoneValue);
151
+ const _formattedNumber = formatNumber(phoneValue, localFormat);
152
+ setFormattedNumber(_formattedNumber);
153
+ }
154
+ // eslint-disable-next-line react-hooks/exhaustive-deps
155
+ }, []);
156
+ useEffect(() => {
157
+ if (!isEmpty(countryObj.iso2) && `${PREFIX}${countryObj.dialCode}` !== countryCode) {
158
+ setIso2(countryObj.iso2);
159
+ setFormat(countryObj.format);
160
+ onPhoneValueChange(`${PREFIX}${countryObj.dialCode}`);
161
+ setFormattedNumber(`${PREFIX}${countryObj.dialCode}`);
162
+ }
163
+ // eslint-disable-next-line react-hooks/exhaustive-deps
164
+ }, [countryObj]);
165
+ useEffect(() => {
166
+ if (isEmpty(phoneValue)) {
167
+ setIso2('');
168
+ setInvalid && setInvalid(false);
169
+ setFormattedNumber('');
170
+ }
171
+ // eslint-disable-next-line react-hooks/exhaustive-deps
172
+ }, [phoneValue]);
173
+ return (React.createElement(InputGroup, { className: "phone-number-input" },
174
+ React.createElement("div", null,
175
+ React.createElement(Select, { variant: SelectVariant.single, "data-tracking-id": "case-phone-number-country-code", className: "phone-number-select", onToggle: onToggle, onSelect: onCountrySelect, isOpen: isOpen, placeholderText: iso2 && phoneValue !== '' ? placeholder : 'Country Code', onFilter: onFilter, hasInlineFilter: true, inlineFilterPlaceholderText: t('Search'), isDisabled: props.isDisabled }, getOptions(initializedCountries))),
176
+ React.createElement(TextInput, { id: "case-phone-number", "data-tracking-id": "case-phone-number", placeholder: "55-555-5555", value: formattedNumber ? formattedNumber : phoneValue, onChange: onPhoneChange, validated: validations, isDisabled: props.isDisabled }),
177
+ props.isLoading && React.createElement(Spinner, { isSVG: true, size: "lg", className: "pf-u-ml-2xl pf-u-mr-xl phone-spinner" })));
178
+ }