@transferwise/components 0.0.0-experimental-4b7aba1 → 0.0.0-experimental-16e2eec
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.
- package/build/index.esm.js +115 -368
- package/build/index.esm.js.map +1 -1
- package/build/index.js +114 -368
- package/build/index.js.map +1 -1
- package/build/main.css +0 -98
- package/build/styles/main.css +0 -98
- package/build/types/common/locale/index.d.ts +26 -43
- package/build/types/common/locale/index.d.ts.map +1 -1
- package/build/types/index.d.ts +0 -1
- package/build/types/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/PhoneNumberInput.d.ts +22 -27
- package/build/types/phoneNumberInput/PhoneNumberInput.d.ts.map +1 -1
- package/build/types/phoneNumberInput/data/countries.d.ts +5 -10
- package/build/types/phoneNumberInput/data/countries.d.ts.map +1 -1
- package/build/types/phoneNumberInput/index.d.ts +1 -1
- package/build/types/phoneNumberInput/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/cleanNumber/cleanNumber.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/cleanNumber/cleanNumber.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/cleanNumber/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/cleanNumber/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/excludeCountries/excludeCountries.d.ts +8 -1
- package/build/types/phoneNumberInput/utils/excludeCountries/excludeCountries.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/excludeCountries/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/excludeCountries/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/explodeNumberModel/index.d.ts +8 -4
- package/build/types/phoneNumberInput/utils/explodeNumberModel/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/findCountryByCode/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/findCountryByCode/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/findCountryByPrefix/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/findCountryByPrefix/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.d.ts +2 -1
- package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/index.d.ts +11 -13
- package/build/types/phoneNumberInput/utils/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/isStringNumeric/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/isStringNumeric/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/isStringNumeric/isStringNumeric.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/isStringNumeric/isStringNumeric.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/isValidPhoneNumber/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/isValidPhoneNumber/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.d.ts +6 -1
- package/build/types/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/longestMatchingPrefix/index.d.ts +2 -1
- package/build/types/phoneNumberInput/utils/longestMatchingPrefix/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/setDefaultPrefix/index.d.ts +7 -1
- package/build/types/phoneNumberInput/utils/setDefaultPrefix/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/sortArrayByProperty/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/sortArrayByProperty/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/common/locale/{index.spec.js → index.spec.ts} +4 -4
- package/src/common/locale/index.ts +96 -0
- package/src/index.ts +0 -1
- package/src/main.css +0 -98
- package/src/main.less +0 -1
- package/src/phoneNumberInput/PhoneNumberInput.spec.js +18 -22
- package/src/phoneNumberInput/PhoneNumberInput.tsx +193 -0
- package/src/phoneNumberInput/data/{countries.js → countries.ts} +9 -1
- package/src/phoneNumberInput/utils/cleanNumber/cleanNumber.ts +3 -0
- package/src/phoneNumberInput/utils/excludeCountries/{excludeCountries.spec.js → excludeCountries.spec.ts} +1 -1
- package/src/phoneNumberInput/utils/excludeCountries/{excludeCountries.js → excludeCountries.ts} +6 -5
- package/src/phoneNumberInput/utils/explodeNumberModel/{explodeNumberModel.spec.js → explodeNumberModel.spec.ts} +1 -1
- package/src/phoneNumberInput/utils/explodeNumberModel/index.ts +24 -0
- package/src/phoneNumberInput/utils/findCountryByCode/{findCountryByCode.spec.js → findCountryByCode.spec.ts} +0 -1
- package/src/phoneNumberInput/utils/findCountryByCode/index.ts +12 -0
- package/src/phoneNumberInput/utils/findCountryByPrefix/index.ts +12 -0
- package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.spec.ts +102 -0
- package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.ts +12 -0
- package/src/phoneNumberInput/utils/{index.js → index.ts} +0 -2
- package/src/phoneNumberInput/utils/isStringNumeric/{isStringNumeric.spec.js → isStringNumeric.spec.ts} +0 -1
- package/src/phoneNumberInput/utils/isStringNumeric/isStringNumeric.ts +1 -0
- package/src/phoneNumberInput/utils/isValidPhoneNumber/{isValidPhoneNumber.spec.js → isValidPhoneNumber.spec.ts} +1 -1
- package/src/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.ts +7 -0
- package/src/phoneNumberInput/utils/longestMatchingPrefix/index.ts +4 -0
- package/src/phoneNumberInput/utils/setDefaultPrefix/index.ts +20 -0
- package/src/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.ts +6 -0
- package/src/ssr.spec.js +0 -17
- package/build/styles/segmentedControl/SegmentedControl.css +0 -98
- package/build/types/phoneNumberInput/utils/filterOptionsForQuery/index.d.ts +0 -2
- package/build/types/phoneNumberInput/utils/filterOptionsForQuery/index.d.ts.map +0 -1
- package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/index.d.ts +0 -2
- package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/index.d.ts.map +0 -1
- package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.d.ts +0 -3
- package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.d.ts.map +0 -1
- package/build/types/segmentedControl/SegmentedControl.d.ts +0 -31
- package/build/types/segmentedControl/SegmentedControl.d.ts.map +0 -1
- package/build/types/segmentedControl/index.d.ts +0 -3
- package/build/types/segmentedControl/index.d.ts.map +0 -1
- package/src/common/locale/index.js +0 -139
- package/src/phoneNumberInput/PhoneNumberInput.js +0 -210
- package/src/phoneNumberInput/data/countries.spec.js +0 -12
- package/src/phoneNumberInput/utils/cleanNumber/cleanNumber.js +0 -4
- package/src/phoneNumberInput/utils/explodeNumberModel/index.js +0 -27
- package/src/phoneNumberInput/utils/filterOptionsForQuery/filterOptionsForQuery.spec.js +0 -36
- package/src/phoneNumberInput/utils/filterOptionsForQuery/index.js +0 -11
- package/src/phoneNumberInput/utils/findCountryByCode/index.js +0 -10
- package/src/phoneNumberInput/utils/findCountryByPrefix/index.js +0 -11
- package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.js +0 -26
- package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.spec.js +0 -67
- package/src/phoneNumberInput/utils/isOptionAndFitsQuery/index.js +0 -1
- package/src/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.js +0 -25
- package/src/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.spec.js +0 -66
- package/src/phoneNumberInput/utils/isStringNumeric/isStringNumeric.js +0 -1
- package/src/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.js +0 -10
- package/src/phoneNumberInput/utils/longestMatchingPrefix/index.js +0 -2
- package/src/phoneNumberInput/utils/setDefaultPrefix/index.js +0 -25
- package/src/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.js +0 -3
- package/src/segmentedControl/SegmentedControl.css +0 -98
- package/src/segmentedControl/SegmentedControl.less +0 -99
- package/src/segmentedControl/SegmentedControl.spec.tsx +0 -107
- package/src/segmentedControl/SegmentedControl.story.tsx +0 -55
- package/src/segmentedControl/SegmentedControl.tsx +0 -150
- package/src/segmentedControl/__snapshots__/SegmentedControl.spec.tsx.snap +0 -113
- package/src/segmentedControl/index.ts +0 -2
- /package/src/phoneNumberInput/{PhoneNumberInput.story.js → PhoneNumberInput.story.tsx} +0 -0
- /package/src/phoneNumberInput/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/cleanNumber/{cleanNumber.spec.js → cleanNumber.spec.ts} +0 -0
- /package/src/phoneNumberInput/utils/cleanNumber/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/excludeCountries/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/findCountryByPrefix/{findCountryByPrefix.spec.js → findCountryByPrefix.spec.ts} +0 -0
- /package/src/phoneNumberInput/utils/groupCountriesByPrefix/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/isStringNumeric/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/isValidPhoneNumber/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/longestMatchingPrefix/{longestMatchingPrefix.spec.js → longestMatchingPrefix.spec.ts} +0 -0
- /package/src/phoneNumberInput/utils/setDefaultPrefix/{setDefaultPrefix.spec.js → setDefaultPrefix.spec.ts} +0 -0
- /package/src/phoneNumberInput/utils/sortArrayByProperty/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/sortArrayByProperty/{sortArrayByProperty.spec.js → sortArrayByProperty.spec.ts} +0 -0
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param phoneNumber
|
|
4
|
+
* @returns True if number that starts with "+" and contains a mix of digits and spaces with at least 4 digits.
|
|
5
|
+
*/
|
|
6
|
+
export declare const isValidPhoneNumber: (phoneNumber: string) => boolean;
|
|
2
7
|
//# sourceMappingURL=isValidPhoneNumber.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isValidPhoneNumber.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.
|
|
1
|
+
{"version":3,"file":"isValidPhoneNumber.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,gBAAiB,MAAM,YACsC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/longestMatchingPrefix/index.
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/longestMatchingPrefix/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,eAAO,MAAM,qBAAqB,kBAAmB,OAAO,EAAE,YACa,CAAC"}
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Given a valid locale it returns the correspondent prefix if found or +44 otherwise.
|
|
3
|
+
*
|
|
4
|
+
* @param locale BCP 47 language tag of locale, e.g. `"es-ES"`.
|
|
5
|
+
* @param countryCode Two-letter country code (ISO 3166-1 alpha-2).
|
|
6
|
+
*/
|
|
7
|
+
export declare const setDefaultPrefix: (locale: string, countryCode?: string) => string;
|
|
2
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/setDefaultPrefix/index.
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/setDefaultPrefix/index.ts"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,WAAY,MAAM,gBAAgB,MAAM,WAKpE,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { sortArrayByProperty } from
|
|
1
|
+
export { sortArrayByProperty } from './sortArrayByProperty';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/sortArrayByProperty/index.
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/sortArrayByProperty/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export function sortArrayByProperty(arrayToSort:
|
|
1
|
+
export declare function sortArrayByProperty<T extends Record<PropertyKey, string>>(arrayToSort: T[], property: keyof T): T[];
|
|
2
2
|
//# sourceMappingURL=sortArrayByProperty.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sortArrayByProperty.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.
|
|
1
|
+
{"version":3,"file":"sortArrayByProperty.d.ts","sourceRoot":"","sources":["../../../../../src/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.ts"],"names":[],"mappings":"AAAA,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,EACvE,WAAW,EAAE,CAAC,EAAE,EAChB,QAAQ,EAAE,MAAM,CAAC,OAGlB"}
|
package/package.json
CHANGED
|
@@ -24,8 +24,8 @@ describe('locale utils', () => {
|
|
|
24
24
|
['zh_HK', 'zh'],
|
|
25
25
|
['ja-JP', 'ja'],
|
|
26
26
|
['zhHK', null],
|
|
27
|
-
[null, null],
|
|
28
|
-
[undefined, null],
|
|
27
|
+
[null as any, null],
|
|
28
|
+
[undefined as any, null],
|
|
29
29
|
['', null],
|
|
30
30
|
[' ', null],
|
|
31
31
|
['ar-dz', null],
|
|
@@ -51,8 +51,8 @@ describe('locale utils', () => {
|
|
|
51
51
|
['zh_HK', 'zh-HK'],
|
|
52
52
|
['ja-JP', 'ja-JP'],
|
|
53
53
|
['zhHK', null],
|
|
54
|
-
[null, null],
|
|
55
|
-
[undefined, null],
|
|
54
|
+
[null as any, null],
|
|
55
|
+
[undefined as any, null],
|
|
56
56
|
['', null],
|
|
57
57
|
[' ', null],
|
|
58
58
|
['ar-dz', 'ar-DZ'],
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Direction } from '..';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_LANG = 'en';
|
|
4
|
+
export const DEFAULT_LOCALE = 'en-GB';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Languages written right-to-left.
|
|
8
|
+
*/
|
|
9
|
+
export const RTL_LANGUAGES = ['ar', 'he'];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated The source of truth for supported languages lives in Crab.
|
|
13
|
+
*/
|
|
14
|
+
export const SUPPORTED_LANGUAGES = [
|
|
15
|
+
DEFAULT_LANG,
|
|
16
|
+
'de',
|
|
17
|
+
'es',
|
|
18
|
+
'fr',
|
|
19
|
+
'hu',
|
|
20
|
+
'id',
|
|
21
|
+
'it',
|
|
22
|
+
'ja',
|
|
23
|
+
'pl',
|
|
24
|
+
'pt',
|
|
25
|
+
'ro',
|
|
26
|
+
'ru',
|
|
27
|
+
'th',
|
|
28
|
+
'tr',
|
|
29
|
+
'uk',
|
|
30
|
+
'zh',
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Verifies and adjusts locale, replacing `_` with `-`.
|
|
35
|
+
*
|
|
36
|
+
* @param locale `es`, `es_ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.
|
|
37
|
+
* @returns `null` if locale is unrecognized by `Intl.Locale`.
|
|
38
|
+
*/
|
|
39
|
+
export function adjustLocale(locale: string) {
|
|
40
|
+
const localeTrimmed = locale?.trim();
|
|
41
|
+
if (localeTrimmed) {
|
|
42
|
+
try {
|
|
43
|
+
return new Intl.Locale(localeTrimmed.replace('_', '-')).baseName;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
// eslint-disable-next-line no-console
|
|
46
|
+
console.error(error);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Provides corresponding lang (iso2) for provided locale.
|
|
54
|
+
*
|
|
55
|
+
* @deprecated The use of this function almost always breaks language variants
|
|
56
|
+
* e.g. Simplified and Traditional Chinese.
|
|
57
|
+
* There should be no use case for this function.
|
|
58
|
+
* To select the correct translations from a translations object, pass the
|
|
59
|
+
* locale directly into Crab's getLocalisedMessages.
|
|
60
|
+
* @param locale `es`, `es-ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.
|
|
61
|
+
* @returns Two-letter ISO 639-1 language code, falling back to `null` if locale is invalid or language is unsupported.
|
|
62
|
+
*/
|
|
63
|
+
export function getLangFromLocale(locale: string) {
|
|
64
|
+
const adjustedLocale = adjustLocale(locale);
|
|
65
|
+
if (adjustedLocale != null) {
|
|
66
|
+
const { language } = new Intl.Locale(adjustedLocale);
|
|
67
|
+
if (SUPPORTED_LANGUAGES.includes(language)) {
|
|
68
|
+
return language;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Provides corresponding country code (iso2) for locales code with explicit region value.
|
|
76
|
+
*
|
|
77
|
+
* @param locale `es-ES`, `en-GB`, `ja-JP`, etc.
|
|
78
|
+
* @returns `null` if the locale is invalid or the region can‘t be identified.
|
|
79
|
+
*/
|
|
80
|
+
export function getCountryFromLocale(locale: string) {
|
|
81
|
+
const adjustedLocale = adjustLocale(locale);
|
|
82
|
+
return adjustedLocale != null ? new Intl.Locale(adjustedLocale).region ?? null : null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Provides the layout direction for a given locale.
|
|
87
|
+
*
|
|
88
|
+
* @param locale `es`, `es-ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.
|
|
89
|
+
* @returns The layout direction based on the locale, falling back to `Direction.LTR` if the locale is invalid or unsupported.
|
|
90
|
+
*/
|
|
91
|
+
export function getDirectionFromLocale(locale: string) {
|
|
92
|
+
const adjustedLocale = adjustLocale(locale);
|
|
93
|
+
return adjustedLocale != null && RTL_LANGUAGES.includes(new Intl.Locale(adjustedLocale).language)
|
|
94
|
+
? Direction.RTL
|
|
95
|
+
: Direction.LTR;
|
|
96
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -106,7 +106,6 @@ export { default as RadioGroup } from './radioGroup';
|
|
|
106
106
|
export { default as RadioOption } from './radioOption';
|
|
107
107
|
export { default as Section } from './section';
|
|
108
108
|
export { default as Select } from './select';
|
|
109
|
-
export { default as SegmentedControl } from './segmentedControl';
|
|
110
109
|
export { default as SlidingPanel } from './slidingPanel';
|
|
111
110
|
export { default as SnackbarPortal } from './snackbar/Snackbar';
|
|
112
111
|
export { default as SnackbarProvider } from './snackbar/SnackbarProvider';
|
package/src/main.css
CHANGED
|
@@ -4640,104 +4640,6 @@ html:not([dir="rtl"]) .np-navigation-option {
|
|
|
4640
4640
|
.np-theme-personal .np-dropdown-menu .np-dropdown-item--focused {
|
|
4641
4641
|
box-shadow: inset 0 0 0 2px var(--color-interactive-primary);
|
|
4642
4642
|
}
|
|
4643
|
-
.segmented-control {
|
|
4644
|
-
box-sizing: border-box;
|
|
4645
|
-
--segment-highlight-width: 0;
|
|
4646
|
-
--segment-highlight-x: var(--size-4);
|
|
4647
|
-
}
|
|
4648
|
-
.segmented-control__segments {
|
|
4649
|
-
display: inline-flex;
|
|
4650
|
-
position: relative;
|
|
4651
|
-
justify-content: center;
|
|
4652
|
-
align-items: center;
|
|
4653
|
-
background: rgba(134,167,189,0.10196);
|
|
4654
|
-
background: var(--color-background-neutral);
|
|
4655
|
-
padding: 4px;
|
|
4656
|
-
padding: var(--size-4);
|
|
4657
|
-
border-radius: 24px;
|
|
4658
|
-
border-radius: var(--size-24);
|
|
4659
|
-
width: 100%;
|
|
4660
|
-
transition: outline 300ms;
|
|
4661
|
-
outline: 2px solid transparent;
|
|
4662
|
-
}
|
|
4663
|
-
.segmented-control--input:has(:focus-visible) > .segmented-control__segments::after {
|
|
4664
|
-
outline: 2px solid var(--color-interactive-primary);
|
|
4665
|
-
}
|
|
4666
|
-
.segmented-control__segments::after {
|
|
4667
|
-
content: "";
|
|
4668
|
-
background: #ffffff;
|
|
4669
|
-
background: var(--color-background-screen);
|
|
4670
|
-
width: var(--segment-highlight-width);
|
|
4671
|
-
position: absolute;
|
|
4672
|
-
top: 4px;
|
|
4673
|
-
top: var(--size-4);
|
|
4674
|
-
bottom: 4px;
|
|
4675
|
-
bottom: var(--size-4);
|
|
4676
|
-
left: var(--segment-highlight-x);
|
|
4677
|
-
z-index: 0;
|
|
4678
|
-
border-radius: 24px;
|
|
4679
|
-
border-radius: var(--size-24);
|
|
4680
|
-
transition: left 300ms;
|
|
4681
|
-
}
|
|
4682
|
-
.segmented-control__segments--no-animate::after {
|
|
4683
|
-
transition: none !important;
|
|
4684
|
-
}
|
|
4685
|
-
.segmented-control__segment {
|
|
4686
|
-
position: relative;
|
|
4687
|
-
flex: 1 1 100%;
|
|
4688
|
-
flex-flow: column;
|
|
4689
|
-
min-width: 0;
|
|
4690
|
-
align-items: center;
|
|
4691
|
-
text-align: center;
|
|
4692
|
-
vertical-align: middle;
|
|
4693
|
-
padding: 8px 16px;
|
|
4694
|
-
padding: var(--size-8) var(--size-16);
|
|
4695
|
-
margin: 0;
|
|
4696
|
-
border-radius: 24px;
|
|
4697
|
-
border-radius: var(--size-24);
|
|
4698
|
-
z-index: 1;
|
|
4699
|
-
cursor: pointer;
|
|
4700
|
-
transition: font-weight 300ms, background 300ms;
|
|
4701
|
-
}
|
|
4702
|
-
.segmented-control__segment:hover {
|
|
4703
|
-
background: rgba(0,0,0,0.10196);
|
|
4704
|
-
background: var(--color-background-overlay);
|
|
4705
|
-
}
|
|
4706
|
-
.segmented-control__radio-input {
|
|
4707
|
-
position: fixed;
|
|
4708
|
-
opacity: 0;
|
|
4709
|
-
pointer-events: none;
|
|
4710
|
-
}
|
|
4711
|
-
.segmented-control__button {
|
|
4712
|
-
background: none;
|
|
4713
|
-
-webkit-appearance: none;
|
|
4714
|
-
-moz-appearance: none;
|
|
4715
|
-
appearance: none;
|
|
4716
|
-
border: none;
|
|
4717
|
-
outline: none;
|
|
4718
|
-
width: 100%;
|
|
4719
|
-
height: 100%;
|
|
4720
|
-
font: inherit;
|
|
4721
|
-
padding: none;
|
|
4722
|
-
outline: 2px solid transparent;
|
|
4723
|
-
}
|
|
4724
|
-
.segmented-control__button:focus {
|
|
4725
|
-
outline-offset: 0px;
|
|
4726
|
-
}
|
|
4727
|
-
.segmented-control__button:focus-visible {
|
|
4728
|
-
outline-color: var(--color-interactive-primary);
|
|
4729
|
-
}
|
|
4730
|
-
.segmented-control__selected-segment:hover {
|
|
4731
|
-
background: transparent;
|
|
4732
|
-
}
|
|
4733
|
-
.segmented-control__selected-segment {
|
|
4734
|
-
font-weight: 800;
|
|
4735
|
-
font-weight: var(--font-weight-bold);
|
|
4736
|
-
}
|
|
4737
|
-
.segmented-control__label-text {
|
|
4738
|
-
word-wrap: break-word;
|
|
4739
|
-
color: var(--color-interactive-primary);
|
|
4740
|
-
}
|
|
4741
4643
|
.np-summary {
|
|
4742
4644
|
min-width: 280px;
|
|
4743
4645
|
}
|
package/src/main.less
CHANGED
|
@@ -53,7 +53,6 @@
|
|
|
53
53
|
@import "./statusIcon/StatusIcon.less";
|
|
54
54
|
@import "./stepper/Stepper.less";
|
|
55
55
|
@import "./select/Select.less";
|
|
56
|
-
@import "./segmentedControl/SegmentedControl.less";
|
|
57
56
|
@import "./summary/Summary.less";
|
|
58
57
|
@import "./switch/Switch.less";
|
|
59
58
|
@import "./tabs/Tabs.less";
|
|
@@ -37,11 +37,7 @@ describe('Given a telephone number component', () => {
|
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
it('should set prefix control to default UK value', () => {
|
|
40
|
-
expect(select.props().value).
|
|
41
|
-
value: '+44',
|
|
42
|
-
note: 'GBR, GGY, IMN, JEY',
|
|
43
|
-
label: '+44',
|
|
44
|
-
});
|
|
40
|
+
expect(select.props().value).toBe('+44');
|
|
45
41
|
});
|
|
46
42
|
|
|
47
43
|
it('should set number control to empty', () => {
|
|
@@ -49,11 +45,11 @@ describe('Given a telephone number component', () => {
|
|
|
49
45
|
});
|
|
50
46
|
|
|
51
47
|
it('should not disable the select', () => {
|
|
52
|
-
expect(select.prop('disabled')).
|
|
48
|
+
expect(select.prop('disabled')).toBeFalsy();
|
|
53
49
|
});
|
|
54
50
|
|
|
55
51
|
it('should not disable the input', () => {
|
|
56
|
-
expect(input.prop('disabled')).
|
|
52
|
+
expect(input.prop('disabled')).toBeFalsy();
|
|
57
53
|
});
|
|
58
54
|
});
|
|
59
55
|
|
|
@@ -65,7 +61,7 @@ describe('Given a telephone number component', () => {
|
|
|
65
61
|
});
|
|
66
62
|
|
|
67
63
|
it('should set control values correctly', () => {
|
|
68
|
-
expect(select.props().value
|
|
64
|
+
expect(select.props().value).toBe('+39');
|
|
69
65
|
expect(input.prop('value')).toBe('123456789');
|
|
70
66
|
});
|
|
71
67
|
});
|
|
@@ -92,8 +88,8 @@ describe('Given a telephone number component', () => {
|
|
|
92
88
|
input = component.find(NUMBER_SELECTOR);
|
|
93
89
|
});
|
|
94
90
|
|
|
95
|
-
it('should render input with
|
|
96
|
-
expect(input.prop('id')).
|
|
91
|
+
it('should render input with unspecified id', () => {
|
|
92
|
+
expect(input.prop('id')).toBeUndefined();
|
|
97
93
|
});
|
|
98
94
|
});
|
|
99
95
|
|
|
@@ -118,7 +114,7 @@ describe('Given a telephone number component', () => {
|
|
|
118
114
|
it(`${number} code should update the value properly`, () => {
|
|
119
115
|
simulatePaste(component.find('input'), number);
|
|
120
116
|
|
|
121
|
-
expect(select().props().value
|
|
117
|
+
expect(select().props().value).toBe(countryCode);
|
|
122
118
|
expect(input().prop('value')).toBe(localNumber);
|
|
123
119
|
expect(props.onChange).toHaveBeenCalledWith(number.replace(/(\s|-)+/g, ''), countryCode);
|
|
124
120
|
});
|
|
@@ -126,28 +122,28 @@ describe('Given a telephone number component', () => {
|
|
|
126
122
|
|
|
127
123
|
it('should not paste invalid characters', () => {
|
|
128
124
|
simulatePaste(component.find('input'), '+36asdasdasd');
|
|
129
|
-
expect(select().props().value
|
|
125
|
+
expect(select().props().value).toBe('+39');
|
|
130
126
|
expect(input().prop('value')).toBe('123456789');
|
|
131
127
|
expect(props.onChange).not.toHaveBeenCalled();
|
|
132
128
|
});
|
|
133
129
|
|
|
134
130
|
it('should not paste countries which are not in the select', () => {
|
|
135
131
|
simulatePaste(component.find('input'), '+9992342343423');
|
|
136
|
-
expect(select().props().value
|
|
132
|
+
expect(select().props().value).toBe('+39');
|
|
137
133
|
expect(input().prop('value')).toBe('123456789');
|
|
138
134
|
expect(props.onChange).not.toHaveBeenCalled();
|
|
139
135
|
});
|
|
140
136
|
|
|
141
137
|
it("should not paste numbers which doesn't start with the country code", () => {
|
|
142
138
|
simulatePaste(component.find('input'), '0+36303932551');
|
|
143
|
-
expect(select().props().value
|
|
139
|
+
expect(select().props().value).toBe('+39');
|
|
144
140
|
expect(input().prop('value')).toBe('123456789');
|
|
145
141
|
expect(props.onChange).not.toHaveBeenCalled();
|
|
146
142
|
});
|
|
147
143
|
|
|
148
144
|
it("should not paste numbers which doesn't contain a country code", () => {
|
|
149
145
|
simulatePaste(component.find('input'), '06303932551');
|
|
150
|
-
expect(select().props().value
|
|
146
|
+
expect(select().props().value).toBe('+39');
|
|
151
147
|
expect(input().prop('value')).toBe('123456789');
|
|
152
148
|
expect(props.onChange).not.toHaveBeenCalled();
|
|
153
149
|
});
|
|
@@ -161,7 +157,7 @@ describe('Given a telephone number component', () => {
|
|
|
161
157
|
});
|
|
162
158
|
|
|
163
159
|
it('should set the select to the longest matching prefix', () => {
|
|
164
|
-
expect(select.props().value
|
|
160
|
+
expect(select.props().value).toBe('+1868');
|
|
165
161
|
});
|
|
166
162
|
|
|
167
163
|
it('should set the number input to the rest of the number', () => {
|
|
@@ -177,7 +173,7 @@ describe('Given a telephone number component', () => {
|
|
|
177
173
|
});
|
|
178
174
|
|
|
179
175
|
it('should empty the select', () => {
|
|
180
|
-
expect(select.props().value).
|
|
176
|
+
expect(select.props().value).toBeNull();
|
|
181
177
|
});
|
|
182
178
|
|
|
183
179
|
it('should put the whole value in the input without the plus', () => {
|
|
@@ -191,7 +187,7 @@ describe('Given a telephone number component', () => {
|
|
|
191
187
|
select = component.find(PREFIX_SELECT_SELECTOR);
|
|
192
188
|
input = component.find(NUMBER_SELECTOR);
|
|
193
189
|
|
|
194
|
-
expect(select.props().value
|
|
190
|
+
expect(select.props().value).toBe('+44');
|
|
195
191
|
expect(input.prop('value')).toBe('');
|
|
196
192
|
});
|
|
197
193
|
});
|
|
@@ -254,7 +250,7 @@ describe('Given a telephone number component', () => {
|
|
|
254
250
|
});
|
|
255
251
|
|
|
256
252
|
it('should use the prefix of the supplied value', () => {
|
|
257
|
-
expect(select.props().value
|
|
253
|
+
expect(select.props().value).toBe('+1');
|
|
258
254
|
});
|
|
259
255
|
});
|
|
260
256
|
|
|
@@ -268,7 +264,7 @@ describe('Given a telephone number component', () => {
|
|
|
268
264
|
});
|
|
269
265
|
|
|
270
266
|
it('should default the prefix to the local country', () => {
|
|
271
|
-
expect(select.props().value
|
|
267
|
+
expect(select.props().value).toBe('+34');
|
|
272
268
|
});
|
|
273
269
|
});
|
|
274
270
|
|
|
@@ -281,7 +277,7 @@ describe('Given a telephone number component', () => {
|
|
|
281
277
|
});
|
|
282
278
|
|
|
283
279
|
it('should override locale prefix with country specific prefix', () => {
|
|
284
|
-
expect(select.props().value
|
|
280
|
+
expect(select.props().value).toBe('+1');
|
|
285
281
|
});
|
|
286
282
|
});
|
|
287
283
|
});
|
|
@@ -349,7 +345,7 @@ describe('Given a telephone number component', () => {
|
|
|
349
345
|
|
|
350
346
|
it('renders Select component with expected props', () => {
|
|
351
347
|
const select = component.find(PREFIX_SELECT_SELECTOR);
|
|
352
|
-
expect(select.prop('className')).
|
|
348
|
+
expect(select.prop('className')).toBe('custom-class');
|
|
353
349
|
});
|
|
354
350
|
});
|
|
355
351
|
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { useState, useEffect, useMemo } from 'react';
|
|
2
|
+
import { useIntl } from 'react-intl';
|
|
3
|
+
|
|
4
|
+
import { Size, SizeLarge, SizeMedium, SizeSmall } from '../common';
|
|
5
|
+
import { SelectInput, SelectInputOptionContent, SelectInputProps } from '../inputs/SelectInput';
|
|
6
|
+
|
|
7
|
+
import countries from './data/countries';
|
|
8
|
+
import {
|
|
9
|
+
explodeNumberModel,
|
|
10
|
+
isValidPhoneNumber,
|
|
11
|
+
cleanNumber,
|
|
12
|
+
setDefaultPrefix,
|
|
13
|
+
sortArrayByProperty,
|
|
14
|
+
groupCountriesByPrefix,
|
|
15
|
+
excludeCountries,
|
|
16
|
+
findCountryByPrefix,
|
|
17
|
+
} from './utils';
|
|
18
|
+
import { PhoneNumber } from './utils/explodeNumberModel';
|
|
19
|
+
|
|
20
|
+
const ALLOWED_PHONE_CHARS = /^$|^[\d-\s]+$/;
|
|
21
|
+
|
|
22
|
+
export interface PhoneNumberInputProps {
|
|
23
|
+
id?: string;
|
|
24
|
+
required?: boolean;
|
|
25
|
+
disabled?: boolean;
|
|
26
|
+
initialValue?: string;
|
|
27
|
+
onChange: (value: string | null, prefix: string) => void;
|
|
28
|
+
onFocus?: React.FocusEventHandler<HTMLInputElement>;
|
|
29
|
+
onBlur?: React.FocusEventHandler<HTMLInputElement>;
|
|
30
|
+
countryCode?: string;
|
|
31
|
+
searchPlaceholder?: string;
|
|
32
|
+
size?: SizeSmall | SizeMedium | SizeLarge;
|
|
33
|
+
placeholder?: string;
|
|
34
|
+
selectProps?: Partial<SelectInputProps<string | null>>;
|
|
35
|
+
/** List of iso3 codes of countries to remove from the list */
|
|
36
|
+
disabledCountries?: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const defaultSelectProps = {} satisfies PhoneNumberInputProps['selectProps'];
|
|
40
|
+
const defaultDisabledCountries = [] satisfies PhoneNumberInputProps['disabledCountries'];
|
|
41
|
+
|
|
42
|
+
const PhoneNumberInput = ({
|
|
43
|
+
id,
|
|
44
|
+
required,
|
|
45
|
+
disabled,
|
|
46
|
+
initialValue,
|
|
47
|
+
onChange,
|
|
48
|
+
onFocus,
|
|
49
|
+
onBlur,
|
|
50
|
+
countryCode,
|
|
51
|
+
searchPlaceholder = 'Prefix',
|
|
52
|
+
size = Size.MEDIUM,
|
|
53
|
+
placeholder,
|
|
54
|
+
selectProps = defaultSelectProps,
|
|
55
|
+
disabledCountries = defaultDisabledCountries,
|
|
56
|
+
}: PhoneNumberInputProps) => {
|
|
57
|
+
const { locale } = useIntl();
|
|
58
|
+
|
|
59
|
+
const [internalValue, setInternalValue] = useState<PhoneNumber>(() => {
|
|
60
|
+
const cleanValue = initialValue ? cleanNumber(initialValue) : null;
|
|
61
|
+
|
|
62
|
+
if (!cleanValue || !isValidPhoneNumber(cleanValue)) {
|
|
63
|
+
return {
|
|
64
|
+
prefix: setDefaultPrefix(locale, countryCode),
|
|
65
|
+
suffix: '',
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return explodeNumberModel(cleanValue);
|
|
70
|
+
});
|
|
71
|
+
const [broadcastedValue, setBroadcastedValue] = useState<PhoneNumber | null>(null);
|
|
72
|
+
|
|
73
|
+
const countriesByPrefix = useMemo(
|
|
74
|
+
() =>
|
|
75
|
+
groupCountriesByPrefix(
|
|
76
|
+
sortArrayByProperty(excludeCountries(countries, disabledCountries), 'iso3'),
|
|
77
|
+
),
|
|
78
|
+
[disabledCountries],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const onSuffixChange: React.ChangeEventHandler<HTMLInputElement> = (event) => {
|
|
82
|
+
const suffix = event.target.value;
|
|
83
|
+
if (ALLOWED_PHONE_CHARS.test(suffix)) {
|
|
84
|
+
setInternalValue((prev) => ({ ...prev, suffix }));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const onPaste: React.ClipboardEventHandler<HTMLInputElement> = (event) => {
|
|
89
|
+
if (!event.nativeEvent.clipboardData) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const pastedValue = (event.nativeEvent.clipboardData.getData('text/plain') || '').replace(
|
|
94
|
+
/(\s|-)+/g,
|
|
95
|
+
'',
|
|
96
|
+
);
|
|
97
|
+
const pastedNumber = explodeNumberModel(pastedValue);
|
|
98
|
+
|
|
99
|
+
if (
|
|
100
|
+
pastedNumber.prefix != null &&
|
|
101
|
+
countriesByPrefix.has(pastedNumber.prefix) &&
|
|
102
|
+
ALLOWED_PHONE_CHARS.test(pastedNumber.suffix)
|
|
103
|
+
) {
|
|
104
|
+
setInternalValue(pastedNumber);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
if (broadcastedValue === null) {
|
|
110
|
+
return setBroadcastedValue(internalValue);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const internalPhoneNumber = `${internalValue.prefix ?? ''}${internalValue.suffix}`;
|
|
114
|
+
const broadcastedPhoneNumber = `${broadcastedValue.prefix ?? ''}${broadcastedValue.suffix}`;
|
|
115
|
+
|
|
116
|
+
if (internalPhoneNumber === broadcastedPhoneNumber) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const newValue = isValidPhoneNumber(internalPhoneNumber)
|
|
121
|
+
? cleanNumber(internalPhoneNumber)
|
|
122
|
+
: null;
|
|
123
|
+
|
|
124
|
+
onChange(
|
|
125
|
+
newValue,
|
|
126
|
+
internalValue.prefix ?? '', // TODO: Allow `null` in public API
|
|
127
|
+
);
|
|
128
|
+
setBroadcastedValue(internalValue);
|
|
129
|
+
}, [onChange, broadcastedValue, internalValue]);
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<div className="tw-telephone">
|
|
133
|
+
<div className="tw-telephone__country-select">
|
|
134
|
+
<SelectInput
|
|
135
|
+
placeholder="Select an option…"
|
|
136
|
+
items={[...countriesByPrefix].map(([prefix, countries]) => ({
|
|
137
|
+
type: 'option',
|
|
138
|
+
value: prefix,
|
|
139
|
+
filterMatchers: [
|
|
140
|
+
prefix,
|
|
141
|
+
...countries.map((country) => country.name),
|
|
142
|
+
...countries.map((country) => country.iso3),
|
|
143
|
+
],
|
|
144
|
+
}))}
|
|
145
|
+
value={internalValue.prefix}
|
|
146
|
+
renderValue={(prefix, withinTrigger) => (
|
|
147
|
+
<SelectInputOptionContent
|
|
148
|
+
title={prefix}
|
|
149
|
+
note={
|
|
150
|
+
withinTrigger
|
|
151
|
+
? undefined
|
|
152
|
+
: countriesByPrefix
|
|
153
|
+
.get(prefix)
|
|
154
|
+
?.map((country) => country.iso3)
|
|
155
|
+
.join(', ')
|
|
156
|
+
}
|
|
157
|
+
/>
|
|
158
|
+
)}
|
|
159
|
+
filterable
|
|
160
|
+
filterPlaceholder={searchPlaceholder}
|
|
161
|
+
disabled={disabled}
|
|
162
|
+
size={size}
|
|
163
|
+
onChange={(prefix) => {
|
|
164
|
+
const country = prefix != null ? findCountryByPrefix(prefix) : null;
|
|
165
|
+
setInternalValue((prev) => ({ ...prev, prefix, format: country?.phoneFormat }));
|
|
166
|
+
}}
|
|
167
|
+
{...selectProps}
|
|
168
|
+
/>
|
|
169
|
+
</div>
|
|
170
|
+
<div className="tw-telephone__number-input">
|
|
171
|
+
<div className={`input-group input-group-${size}`}>
|
|
172
|
+
<input
|
|
173
|
+
id={id}
|
|
174
|
+
autoComplete="tel-national"
|
|
175
|
+
name="phoneNumber"
|
|
176
|
+
inputMode="numeric"
|
|
177
|
+
value={internalValue.suffix}
|
|
178
|
+
className="form-control"
|
|
179
|
+
disabled={disabled}
|
|
180
|
+
required={required}
|
|
181
|
+
placeholder={placeholder}
|
|
182
|
+
onChange={onSuffixChange}
|
|
183
|
+
onPaste={onPaste}
|
|
184
|
+
onFocus={onFocus}
|
|
185
|
+
onBlur={onBlur}
|
|
186
|
+
/>
|
|
187
|
+
</div>
|
|
188
|
+
</div>
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export default PhoneNumberInput;
|