Skip to content

Commit 4a0c71d

Browse files
aesteves60dpellier
authored andcommitted
fix(phone-number): review
1 parent 55b0990 commit 4a0c71d

File tree

13 files changed

+89
-43
lines changed

13 files changed

+89
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
enum ODS_PHONE_NUMBER_COUTRIE {
1+
enum ODS_PHONE_NUMBER_COUNTRY_PRESET {
22
All = 'all'
33
}
44

55
export {
6-
ODS_PHONE_NUMBER_COUTRIE,
6+
ODS_PHONE_NUMBER_COUNTRY_PRESET,
77
};

packages/components/phone-number/src/components/osds-phone-number/core/controller.spec.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ODS_COUNTRY_ISO_CODE, ODS_COUNTRY_ISO_CODES, ODS_LOCALE } from '@ovhcloud/ods-common-core';
2-
import { ODS_PHONE_NUMBER_COUTRIE } from '../constants/phone-number-countries';
2+
import { ODS_PHONE_NUMBER_COUNTRY_PRESET } from '../constants/phone-number-countries';
33
import { OsdsPhoneNumber } from '../osds-phone-number';
44
import { OdsPhoneNumberController } from './controller';
55
import countriesTranslationEn from '@ovhcloud/ods-common-core/src/i18n/countries/en.json'
@@ -13,6 +13,7 @@ class OdsPhoneNumberMock extends OsdsPhoneNumber {
1313
emitChange = jest.fn();
1414
emitFocus = jest.fn();
1515
emitBlur = jest.fn();
16+
parsedCountries = ODS_COUNTRY_ISO_CODES as ODS_COUNTRY_ISO_CODE[];
1617
}
1718

1819
describe('spec:ods-phone-number-controller', () => {
@@ -63,6 +64,12 @@ describe('spec:ods-phone-number-controller', () => {
6364
const isoCode = controller.getDefaultIsoCode();
6465
expect(isoCode).toBe(ODS_COUNTRY_ISO_CODE.AD);
6566
});
67+
68+
it('should get the first element of parsedCountries as isoCode', () => {
69+
setup({ parsedCountries: [ODS_COUNTRY_ISO_CODE.CH, ODS_COUNTRY_ISO_CODE.AD, ODS_COUNTRY_ISO_CODE.AE]});
70+
const isoCode = controller.getDefaultIsoCode();
71+
expect(isoCode).toBe(ODS_COUNTRY_ISO_CODE.CH);
72+
});
6673
});
6774

6875
describe('methods:getDefaultLocale', () => {
@@ -99,7 +106,7 @@ describe('spec:ods-phone-number-controller', () => {
99106

100107
describe('methods:getCountriesList', () => {
101108
it('should get all countries', () => {
102-
setup({ countries: ODS_PHONE_NUMBER_COUTRIE.All });
109+
setup({ countries: ODS_PHONE_NUMBER_COUNTRY_PRESET.All });
103110
const countries = controller.getCountriesList();
104111
expect(countries).toEqual(ODS_COUNTRY_ISO_CODES);
105112
});

packages/components/phone-number/src/components/osds-phone-number/core/controller.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OsdsPhoneNumber } from '../osds-phone-number';
22
import { ODS_COUNTRY_ISO_CODE, ODS_COUNTRY_ISO_CODES, ODS_LOCALE, ODS_LOCALES } from '@ovhcloud/ods-common-core';
3-
import { ODS_PHONE_NUMBER_COUTRIE } from '../constants/phone-number-countries';
3+
import { ODS_PHONE_NUMBER_COUNTRY_PRESET } from '../constants/phone-number-countries';
44
import countriesTranslationEn from '@ovhcloud/ods-common-core/src/i18n/countries/en.json'
55
import countriesTranslationFr from '@ovhcloud/ods-common-core/src/i18n/countries/fr.json'
66

@@ -21,13 +21,13 @@ class OdsPhoneNumberController {
2121
return this.getValueOrNavigatorOrDefault({
2222
value: this.component.isoCode,
2323
list: ODS_COUNTRY_ISO_CODES,
24-
defaultValue: ODS_COUNTRY_ISO_CODE.AD,
24+
defaultValue: this.component.parsedCountries[0],
2525
guard: this.isOdsCountryCode,
2626
});
2727
}
2828

29-
getCountriesList(): readonly ODS_COUNTRY_ISO_CODE[] {
30-
if (this.component.countries === ODS_PHONE_NUMBER_COUTRIE.All) {
29+
getCountriesList(): readonly ODS_COUNTRY_ISO_CODE[] | string {
30+
if (this.component.countries === ODS_PHONE_NUMBER_COUNTRY_PRESET.All) {
3131
return ODS_COUNTRY_ISO_CODES;
3232
}
3333
return this.component.countries || [];

packages/components/phone-number/src/components/osds-phone-number/interfaces/attributes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { ODS_COUNTRY_ISO_CODE, ODS_LOCALE } from "@ovhcloud/ods-common-core";
2-
import { ODS_PHONE_NUMBER_COUTRIE } from "../constants/phone-number-countries";
2+
import { ODS_PHONE_NUMBER_COUNTRY_PRESET } from "../constants/phone-number-countries";
33

44
interface OdsPhoneNumberAttribute {
55
/** Ability to clear the phone number value */
66
clearable?: boolean;
77
/** A specific subset of countries to display in the select instead of the whole list */
8-
countries?: ODS_COUNTRY_ISO_CODE[] | ODS_PHONE_NUMBER_COUTRIE.All;
8+
countries?: ODS_COUNTRY_ISO_CODE[] | ODS_PHONE_NUMBER_COUNTRY_PRESET.All | string;
99
disabled?: boolean;
1010
/** Indicates if the phone number shows error or not */
1111
error?: boolean;

packages/components/phone-number/src/components/osds-phone-number/osds-phone-number.e2e.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { odsComponentAttributes2StringAttributes, odsStringAttributes2Str } from
66
import { DEFAULT_ATTRIBUTE } from './constants/default-attributes';
77
import { ODS_COUNTRY_ISO_CODE } from '@ovhcloud/ods-common-core';
88
import { odsSetE2eInterceptRequest } from '@ovhcloud/ods-common-stencil';
9-
import { ODS_PHONE_NUMBER_COUTRIE } from './constants/phone-number-countries';
9+
import { ODS_PHONE_NUMBER_COUNTRY_PRESET } from './constants/phone-number-countries';
1010

1111
describe('e2e:osds-phone-number', () => {
1212
const baseAttribute = { value: '' };
@@ -83,7 +83,7 @@ describe('e2e:osds-phone-number', () => {
8383
});
8484

8585
it('should display select because of countries all', async () => {
86-
await setup({ attributes: { countries: ODS_PHONE_NUMBER_COUTRIE.All }, cbkInterceptorRequest: myCbk });
86+
await setup({ attributes: { countries: ODS_PHONE_NUMBER_COUNTRY_PRESET.All }, cbkInterceptorRequest: myCbk });
8787

8888
expect(select).not.toBeNull();
8989
expect(select).toHaveClass('hydrated');

packages/components/phone-number/src/components/osds-phone-number/osds-phone-number.scss

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
&__option {
1616
display: flex;
17+
align-items: center;
1718

1819
&__flag {
1920
display: inline-block;

packages/components/phone-number/src/components/osds-phone-number/osds-phone-number.spec.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import { odsComponentAttributes2StringAttributes, odsStringAttributes2Str, odsUn
55
import { DEFAULT_ATTRIBUTE } from './constants/default-attributes';
66
import { OsdsPhoneNumber } from './osds-phone-number';
77
import { ODS_COUNTRY_ISO_CODE, ODS_COUNTRY_ISO_CODES, ODS_LOCALE } from '@ovhcloud/ods-common-core';
8-
import { ODS_PHONE_NUMBER_COUTRIE } from './constants/phone-number-countries';
9-
import { OdsPhoneNumberController } from './core/controller';
8+
import { ODS_PHONE_NUMBER_COUNTRY_PRESET } from './constants/phone-number-countries';
109

1110
describe('spec:osds-phone-number', () => {
1211
const baseAttribute = { ariaLabel: '', forbiddenValues: [], value: '' };
1312
let page: SpecPage;
1413
let root: HTMLElement | undefined;
1514
let instance: OsdsPhoneNumber;
16-
let controller: OdsPhoneNumberController;
1715
let select: HTMLElement;
1816

1917
afterEach(() => {
@@ -63,7 +61,7 @@ describe('spec:osds-phone-number', () => {
6361
name: 'countries',
6462
defaultValue: DEFAULT_ATTRIBUTE.countries,
6563
newValue: [ODS_COUNTRY_ISO_CODE.FR, ODS_COUNTRY_ISO_CODE.GB],
66-
value: ODS_PHONE_NUMBER_COUTRIE.All,
64+
value: ODS_PHONE_NUMBER_COUNTRY_PRESET.All,
6765
setup: (countries) => setup({ attributes: { countries } }),
6866
...config,
6967
exclude: [OdsUnitTestAttributeType.REFLECTED, OdsUnitTestAttributeType.MUTABLE],
@@ -133,14 +131,14 @@ describe('spec:osds-phone-number', () => {
133131
it('should get countries list with default value & not display select', async () => {
134132
await setup();
135133
instance.handlerCountries();
136-
expect(instance.countriesList).toEqual([]);
134+
expect(instance.parsedCountries).toEqual([]);
137135
expect(select).toBe(null);
138136
});
139137

140138
it('should get countries list with all', async () => {
141-
await setup({ attributes: { countries: ODS_PHONE_NUMBER_COUTRIE.All} });
139+
await setup({ attributes: { countries: ODS_PHONE_NUMBER_COUNTRY_PRESET.All } });
142140
instance.handlerCountries();
143-
expect(instance.countriesList).toEqual(ODS_COUNTRY_ISO_CODES);
141+
expect(instance.parsedCountries).toEqual(ODS_COUNTRY_ISO_CODES);
144142
expect(select).toBeDefined();
145143
});
146144

@@ -149,9 +147,18 @@ describe('spec:osds-phone-number', () => {
149147
await setup({ });
150148
instance.countries = countries;
151149
instance.handlerCountries();
152-
expect(instance.countriesList).toEqual(countries);
150+
expect(instance.parsedCountries).toEqual(countries);
153151
expect(select).toBeDefined();
154152
});
153+
154+
it('should parsed string coutries', async () => {
155+
await setup();
156+
instance.countries = JSON.stringify(["fr", "gb"]);
157+
instance.handlerCountries();
158+
expect(instance.parsedCountries).toEqual([ODS_COUNTRY_ISO_CODE.FR, ODS_COUNTRY_ISO_CODE.GB]);
159+
expect(select).toBeDefined();
160+
});
161+
155162
});
156163

157164
describe('methods:handlerLocale', () => {

packages/components/phone-number/src/components/osds-phone-number/osds-phone-number.tsx

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
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';
33
import { Component, Host, h, Prop, State, Watch } from '@stencil/core';
44
import { ODS_INPUT_TYPE } from '@ovhcloud/ods-component-input';
55
import { DEFAULT_ATTRIBUTE } from './constants/default-attributes';
66
import { OdsPhoneNumberController } from './core/controller';
77
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';
99

1010
/**
1111
* @slot (unnamed) - Phone Number content
@@ -14,16 +14,17 @@ import { ODS_PHONE_NUMBER_COUTRIE } from './constants/phone-number-countries';
1414
tag: 'osds-phone-number',
1515
styleUrl: 'osds-phone-number.scss',
1616
shadow: true,
17-
assetsDirs: ['../../assets'],
1817
})
1918
export class OsdsPhoneNumber implements OdsPhoneNumberAttribute {
19+
private logger = new OdsLogger('OsdsPhoneNumber');
2020
controller = new OdsPhoneNumberController(this);
21+
parsedCountries: ODS_COUNTRY_ISO_CODE[] = [];
2122

2223
/** @see OdsPhoneNumberAttribute.clearable */
2324
@Prop({ reflect: true }) clearable?: boolean = DEFAULT_ATTRIBUTE.clearable;
2425

2526
/** @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;
2728

2829
/** @see OdsPhoneNumberAttribute.disabled */
2930
@Prop({ reflect: true }) disabled?: boolean = DEFAULT_ATTRIBUTE.disabled;
@@ -42,14 +43,14 @@ export class OsdsPhoneNumber implements OdsPhoneNumberAttribute {
4243

4344
@State() i18nCountriesMap!: Map<string, { isoCode: string , name: string }>;
4445

45-
@State() countriesList: readonly ODS_COUNTRY_ISO_CODE[] = [];
4646
@State() hasCountries: boolean = false;
4747

4848
componentWillLoad(): void {
49+
// order matter
50+
this.handlerCountries();
4951
this.isoCode = this.controller.getDefaultIsoCode();
5052
this.locale = this.controller.getDefaultLocale();
5153
this.handlerLocale(this.locale);
52-
this.handlerCountries();
5354
}
5455

5556
@Watch('locale')
@@ -63,8 +64,22 @@ export class OsdsPhoneNumber implements OdsPhoneNumberAttribute {
6364

6465
@Watch('countries')
6566
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+
}
6883
}
6984

7085
render() {
@@ -77,9 +92,9 @@ export class OsdsPhoneNumber implements OdsPhoneNumberAttribute {
7792
disabled={this.disabled}
7893
error={this.error}
7994
value={this.isoCode}>
80-
{ this.countriesList?.map((country) => <osds-select-option value={ country }>
95+
{ this.parsedCountries?.map((country) => <osds-select-option value={ country }>
8196
<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>
8398
<osds-text>{ this.i18nCountriesMap?.get(country)?.name }</osds-text>
8499
</div>
85100
</osds-select-option>) }
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export type { OdsPhoneNumberAttribute } from './interfaces/attributes';
22
export { OsdsPhoneNumber } from './osds-phone-number';
3-
export { ODS_PHONE_NUMBER_COUTRIE } from './constants/phone-number-countries';
3+
export { ODS_PHONE_NUMBER_COUNTRY_PRESET } from './constants/phone-number-countries';

packages/components/phone-number/src/docs/osds-phone-number/usage.mdx

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import GenericStyle from '@ovhcloud/ods-common-core/docs/generic-style.mdx';
2020

2121

2222
### With specific countries
23-
<osds-phone-number countries="['fr', 'gb']" value="+33647652334"></osds-phone-number>
23+
<osds-phone-number countries='["fr", "gb"]' value="+33647652334"></osds-phone-number>
2424

2525
```html
26-
<osds-phone-number countries="['fr', 'gb']" value="+33647652334"></osds-phone-number>
26+
<osds-phone-number countries='["fr", "gb"]' value="+33647652334"></osds-phone-number>
27+
2728
```

packages/components/phone-number/src/global.dev.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ odsSetup();
1515

1616
const logger = new OdsLogger('global-dev');
1717
logger.log('init');
18-
Ods.instance().assetPath('../../../packages/tools/stories/public/flags/');
18+
Ods.instance().assetPath('../flags/flags-4x3/');
1919

2020
(window as any).globalMethod = async function () {
2121
logger.log('globalMethod');

packages/components/phone-number/src/index.html

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<osds-phone-number clearable value="+33 6426658925"></osds-phone-number>
1919
<br/>
2020
<br/>
21-
<osds-phone-number id="phoneNumberWithSetCountries" clearable value="+33 6426658925"></osds-phone-number>
21+
<osds-phone-number countries='["fr", "gb"]' clearable value="+33 6426658925"></osds-phone-number>
2222

2323
<p>Error</p>
2424
<osds-phone-number countries="all" error value="+33 6426658925"></osds-phone-number>
@@ -31,12 +31,5 @@
3131
<br/>
3232
<br/>
3333
<osds-phone-number disabled value="+33 6426658925"></osds-phone-number>
34-
35-
36-
<script>
37-
const phoneNumberWithSetCountries = document.getElementById('phoneNumberWithSetCountries');
38-
phoneNumberWithSetCountries.countries = ['fr', 'gb'];
39-
phoneNumberWithSetCountries.addEventListener('odsValueChange', () => console.log('odsValueChange'))
40-
</script>
4134
</body>
4235
</html>

packages/components/phone-number/stencil.config.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as jestConfig from './jest.config';
44

55
const args = process.argv.slice(2);
66

7-
export const config: Config = getStencilConfig({
7+
const config: Config = getStencilConfig({
88
namespace: 'osds-phone-number',
99
args,
1010
jestConfig: jestConfig.default,
@@ -26,3 +26,25 @@ export const config: Config = getStencilConfig({
2626
globalScript: 'src/global.test.ts'
2727
},
2828
});
29+
30+
31+
config.outputTargets?.forEach(output => {
32+
if (output.type === 'dist-custom-elements' || output.type === 'www') {
33+
output.copy = output.copy || [];
34+
output.copy.push(
35+
{
36+
src: '../../../../node_modules/flag-icons/flags/4x3/',
37+
dest: `${output.type === 'dist-custom-elements' ? 'dist/' : ''}flags/flags-4x3/`,
38+
}
39+
);
40+
}
41+
if (output.type === 'www') {
42+
output.copy = output.copy || [];
43+
output.copy.push({
44+
src: '../../../../node_modules/flag-icons/flags/4x3/',
45+
dest: 'flags-custom-path/'
46+
});
47+
}
48+
});
49+
50+
export { config };

0 commit comments

Comments
 (0)