1
1
import type { OdsPhoneNumberAttribute } from './interfaces/attributes' ;
2
- import { ODS_COUNTRY_ISO_CODE , ODS_LOCALE } from '@ovhcloud/ods-common-core' ;
2
+ import { OdsLogger , ODS_COUNTRY_ISO_CODE , ODS_LOCALE } from '@ovhcloud/ods-common-core' ;
3
3
import { Component , Host , h , Prop , State , Watch } from '@stencil/core' ;
4
4
import { ODS_INPUT_TYPE } from '@ovhcloud/ods-component-input' ;
5
5
import { DEFAULT_ATTRIBUTE } from './constants/default-attributes' ;
6
6
import { OdsPhoneNumberController } from './core/controller' ;
7
7
import { ODS_THEME_COLOR_INTENT } from '@ovhcloud/ods-common-theming' ;
8
- import { ODS_PHONE_NUMBER_COUTRIE } from './constants/phone-number-countries' ;
8
+ import { ODS_PHONE_NUMBER_COUNTRY_PRESET } from './constants/phone-number-countries' ;
9
9
10
10
/**
11
11
* @slot (unnamed) - Phone Number content
@@ -14,16 +14,17 @@ import { ODS_PHONE_NUMBER_COUTRIE } from './constants/phone-number-countries';
14
14
tag : 'osds-phone-number' ,
15
15
styleUrl : 'osds-phone-number.scss' ,
16
16
shadow : true ,
17
- assetsDirs : [ '../../assets' ] ,
18
17
} )
19
18
export class OsdsPhoneNumber implements OdsPhoneNumberAttribute {
19
+ private logger = new OdsLogger ( 'OsdsPhoneNumber' ) ;
20
20
controller = new OdsPhoneNumberController ( this ) ;
21
+ parsedCountries : ODS_COUNTRY_ISO_CODE [ ] = [ ] ;
21
22
22
23
/** @see OdsPhoneNumberAttribute.clearable */
23
24
@Prop ( { reflect : true } ) clearable ?: boolean = DEFAULT_ATTRIBUTE . clearable ;
24
25
25
26
/** @see OdsPhoneNumberAttribute.countries */
26
- @Prop ( { reflect : true , mutable : true } ) countries ?: ODS_COUNTRY_ISO_CODE [ ] | ODS_PHONE_NUMBER_COUTRIE . All = DEFAULT_ATTRIBUTE . countries ;
27
+ @Prop ( { reflect : true , mutable : true } ) countries ?: ODS_COUNTRY_ISO_CODE [ ] | ODS_PHONE_NUMBER_COUNTRY_PRESET . All | string = DEFAULT_ATTRIBUTE . countries ;
27
28
28
29
/** @see OdsPhoneNumberAttribute.disabled */
29
30
@Prop ( { reflect : true } ) disabled ?: boolean = DEFAULT_ATTRIBUTE . disabled ;
@@ -42,14 +43,14 @@ export class OsdsPhoneNumber implements OdsPhoneNumberAttribute {
42
43
43
44
@State ( ) i18nCountriesMap ! : Map < string , { isoCode : string , name : string } > ;
44
45
45
- @State ( ) countriesList : readonly ODS_COUNTRY_ISO_CODE [ ] = [ ] ;
46
46
@State ( ) hasCountries : boolean = false ;
47
47
48
48
componentWillLoad ( ) : void {
49
+ // order matter
50
+ this . handlerCountries ( ) ;
49
51
this . isoCode = this . controller . getDefaultIsoCode ( ) ;
50
52
this . locale = this . controller . getDefaultLocale ( ) ;
51
53
this . handlerLocale ( this . locale ) ;
52
- this . handlerCountries ( ) ;
53
54
}
54
55
55
56
@Watch ( 'locale' )
@@ -63,8 +64,22 @@ export class OsdsPhoneNumber implements OdsPhoneNumberAttribute {
63
64
64
65
@Watch ( 'countries' )
65
66
handlerCountries ( ) : void {
66
- this . hasCountries = Boolean ( this . countries ?. length ) ;
67
- this . countriesList = this . controller . getCountriesList ( ) ;
67
+ const countriesList = this . controller . getCountriesList ( ) ;
68
+ this . parseCountries ( countriesList ) ;
69
+ this . hasCountries = ! ! this . parsedCountries ?. length ;
70
+ }
71
+
72
+ private parseCountries ( countries : readonly ODS_COUNTRY_ISO_CODE [ ] | string ) {
73
+ if ( typeof countries === 'string' ) {
74
+ try {
75
+ this . parsedCountries = JSON . parse ( countries ) ;
76
+ } catch ( err ) {
77
+ this . logger . warn ( '[OsdsPhoneNumber] countries string could not be parsed.' ) ;
78
+ this . parsedCountries = [ ] ;
79
+ }
80
+ } else {
81
+ this . parsedCountries = [ ...countries ] ;
82
+ }
68
83
}
69
84
70
85
render ( ) {
@@ -77,9 +92,9 @@ export class OsdsPhoneNumber implements OdsPhoneNumberAttribute {
77
92
disabled = { this . disabled }
78
93
error = { this . error }
79
94
value = { this . isoCode } >
80
- { this . countriesList ?. map ( ( country ) => < osds-select-option value = { country } >
95
+ { this . parsedCountries ?. map ( ( country ) => < osds-select-option value = { country } >
81
96
< div class = "phone-number__select__option" >
82
- < osds-flag iso = { country } > </ osds-flag >
97
+ < osds-flag iso = { country } class = "phone-number__select__option__flag" > </ osds-flag >
83
98
< osds-text > { this . i18nCountriesMap ?. get ( country ) ?. name } </ osds-text >
84
99
</ div >
85
100
</ osds-select-option > ) }
0 commit comments