feat(input-tel-dropdown): add optgroups when preferredCountries are shown

This commit is contained in:
gvangeest 2022-05-23 14:49:48 +02:00
parent 51a0c064c7
commit 01fd0d2023
6 changed files with 79 additions and 9 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/input-tel-dropdown': patch
---
Add optgroups when preferredCountries are shown

View file

@ -0,0 +1,5 @@
---
'@lion/input-tel': patch
---
Export formatPhoneNumber

View file

@ -51,8 +51,7 @@ export const allowedRegions = () => html`
## Preferred regions ## Preferred regions
When `.preferredRegions` is configured, they will show up on top of the dropdown list to When `.preferredRegions` is configured, they will show up on top of the dropdown list to enhance user experience.
enhance user experience.
```js preview-story ```js preview-story
export const preferredRegionCodes = () => html` export const preferredRegionCodes = () => html`
@ -61,8 +60,8 @@ export const preferredRegionCodes = () => html`
help-text="Preferred regions show on top" help-text="Preferred regions show on top"
.modelValue=${'+31612345678'} .modelValue=${'+31612345678'}
name="phoneNumber" name="phoneNumber"
.allowedRegions=${['NL', 'DE', 'GB', 'BE', 'US', 'CA']} .allowedRegions=${['BE', 'CA', 'DE', 'GB', 'NL', 'US']}
.preferredRegions=${['NL', 'DE']} .preferredRegions=${['DE', 'NL']}
></lion-input-tel-dropdown> ></lion-input-tel-dropdown>
<h-output <h-output
.show="${['modelValue', 'activeRegion']}" .show="${['modelValue', 'activeRegion']}"

View file

@ -52,7 +52,9 @@ export class LionInputTelDropdown extends LionInputTel {
* @configure LitElement * @configure LitElement
* @type {any} * @type {any}
*/ */
static properties = { preferredRegions: { type: Array } }; static properties = {
preferredRegions: { type: Array },
};
refs = { refs = {
/** @type {DropdownRef} */ /** @type {DropdownRef} */
@ -97,6 +99,9 @@ export class LionInputTelDropdown extends LionInputTel {
}, },
labels: { labels: {
selectCountry: localize.msg('lion-input-tel:selectCountry'), selectCountry: localize.msg('lion-input-tel:selectCountry'),
// TODO: add translations
allCountries: this._allCountriesLabel || 'All countries',
preferredCountries: this._preferredCountriesLabel || 'Suggested countries',
}, },
}, },
}; };
@ -126,9 +131,12 @@ export class LionInputTelDropdown extends LionInputTel {
> >
${data?.regionMetaListPreferred?.length ${data?.regionMetaListPreferred?.length
? html` ? html`
${data.regionMetaListPreferred.map(renderOption)} <optgroup label="${refs?.dropdown?.labels?._preferredCountries}">
<option disabled>---------------</option> ${data.regionMetaListPreferred.map(renderOption)}
${data?.regionMetaList?.map(renderOption)} </optgroup>
<optgroup label="${refs?.dropdown?.labels?._allCountries}">
${data?.regionMetaList?.map(renderOption)}
</optgroup>
` `
: html` ${data?.regionMetaList?.map(renderOption)}`} : html` ${data?.regionMetaList?.map(renderOption)}`}
</select> </select>
@ -206,6 +214,16 @@ export class LionInputTelDropdown extends LionInputTel {
* @type {string[]} * @type {string[]}
*/ */
this.preferredRegions = []; this.preferredRegions = [];
/**
* Group label for all countries, when preferredCountries are shown
* @protected
*/
this._allCountriesLabel = '';
/**
* Group label for preferred countries, when preferredCountries are shown
* @protected
*/
this._preferredCountriesLabel = '';
/** @type {HTMLDivElement} */ /** @type {HTMLDivElement} */
this.__dropdownRenderParent = document.createElement('div'); this.__dropdownRenderParent = document.createElement('div');

View file

@ -112,7 +112,11 @@ export function runInputTelDropdownSuite({ klass } = { klass: LionInputTelDropdo
}, },
refs: { refs: {
dropdown: { dropdown: {
labels: { selectCountry: 'Select country' }, labels: {
selectCountry: 'Select country',
allCountries: 'All countries',
preferredCountries: 'Suggested countries',
},
listeners: { listeners: {
// @ts-expect-error [allow-protected] // @ts-expect-error [allow-protected]
change: el._onDropdownValueChange, change: el._onDropdownValueChange,
@ -125,6 +129,44 @@ export function runInputTelDropdownSuite({ klass } = { klass: LionInputTelDropdo
}, },
}), }),
); );
spy.restore();
});
it('can override "all-countries-label"', async () => {
const el = fixtureSync(html` <${tag}
.preferredRegions="${['PH']}"
></${tag}> `);
const spy = sinon.spy(
/** @type {typeof LionInputTelDropdown} */ (el.constructor).templates,
'dropdown',
);
// @ts-expect-error [allow-protected]
el._allCountriesLabel = 'foo';
await el.updateComplete;
const templateDataForDropdown = /** @type {TemplateDataForDropdownInputTel} */ (
spy.args[0][0]
);
expect(templateDataForDropdown.refs.dropdown.labels.allCountries).to.eql('foo');
spy.restore();
});
it('can override "preferred-countries-label"', async () => {
const el = fixtureSync(html` <${tag}
.preferredRegions="${['PH']}"
></${tag}> `);
const spy = sinon.spy(
/** @type {typeof LionInputTelDropdown} */ (el.constructor).templates,
'dropdown',
);
// @ts-expect-error [allow-protected]
el._preferredCountriesLabel = 'foo';
await el.updateComplete;
const templateDataForDropdown = /** @type {TemplateDataForDropdownInputTel} */ (
spy.args[0][0]
);
expect(templateDataForDropdown.refs.dropdown.labels.preferredCountries).to.eql('foo');
spy.restore();
}); });
it('syncs dropdown value initially from activeRegion', async () => { it('syncs dropdown value initially from activeRegion', async () => {

View file

@ -1,3 +1,4 @@
export { formatPhoneNumber } from './src/formatters.js';
export { LionInputTel } from './src/LionInputTel.js'; export { LionInputTel } from './src/LionInputTel.js';
export { PhoneNumber } from './src/validators.js'; export { PhoneNumber } from './src/validators.js';
export { PhoneUtilManager } from './src/PhoneUtilManager.js'; export { PhoneUtilManager } from './src/PhoneUtilManager.js';