-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathLionInputTelDropdown.js
446 lines (405 loc) · 15.2 KB
/
LionInputTelDropdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import { html, css } from 'lit';
import { ref, createRef } from 'lit/directives/ref.js';
import { LionInputTel } from '@lion/ui/input-tel.js';
import { getLocalizeManager } from '@lion/ui/localize-no-side-effects.js';
import { getFlagSymbol } from './getFlagSymbol.js';
/**
* Note: one could consider to implement LionInputTelDropdown as a
* [combobox](https://www.w3.org/TR/wai-aria-practices-1.2/#combobox).
* However, the country dropdown does not directly set the textbox value, it only determines
* its region code. Therefore it does not comply to this criterium:
* "A combobox is an input widget with an associated popup that enables users to select a value for
* the combobox from a collection of possible values. In some implementations,
* the popup presents allowed values, while in other implementations, the popup presents suggested
* values, and users may either select one of the suggestions or type a value".
* We therefore decided to consider the dropdown a helper mechanism that does not set, but
* contributes to and helps format and validate the actual value.
*/
/**
* @typedef {import('lit/directives/ref.js').Ref} Ref
* @typedef {import('lit').RenderOptions} RenderOptions
* @typedef {import('../../form-core/types/FormatMixinTypes.js').FormatHost} FormatHost
* @typedef {import('../../input-tel/types/index.js').RegionCode} RegionCode
* @typedef {import('../types/index.js').TemplateDataForDropdownInputTel} TemplateDataForDropdownInputTel
* @typedef {import('../types/index.js').OnDropdownChangeEvent} OnDropdownChangeEvent
* @typedef {import('../types/index.js').DropdownRef} DropdownRef
* @typedef {import('../types/index.js').RegionMeta} RegionMeta
* @typedef {import('../../select-rich/src/LionSelectRich.js').LionSelectRich} LionSelectRich
* @typedef {import('../../overlays/src/OverlayController.js').OverlayController} OverlayController
* @typedef {TemplateDataForDropdownInputTel & {data: {regionMetaList:RegionMeta[]}}} TemplateDataForIntlInputTel
*/
/**
* LionInputTelDropdown renders a dropdown like element next to the text field, inside the
* prefix slot. This could be a LionSelect, a LionSelectRich or a native select.
* By default, the native `<select>` element is used for this, so that it's as lightweight as
* possible. Also, it doesn't need to be a `FormControl`, because it's purely a helper element
* to provide better UX: the modelValue (the text field) contains all needed info, since it's in
* `e164` format that contains all info (both region code and national phone number).
*/
export class LionInputTelDropdown extends LionInputTel {
/**
* @configure LitElement
* @type {any}
*/
static properties = {
preferredRegions: { type: Array },
};
refs = {
/** @type {DropdownRef} */
dropdown: /** @type {DropdownRef} */ (createRef()),
};
/**
* This method provides a TemplateData object to be fed to pure template functions, a.k.a.
* Pure Templates™. The goal is to totally decouple presentation from logic here, so that
* Subclassers can override all content without having to loose private info contained
* within the template function that was overridden.
*
* Subclassers would need to make sure all the contents of the TemplateData object are implemented
* by making sure they are coupled to the right 'ref' ([data-ref=dropdown] in this example),
* with the help of lit's spread operator directive.
* To enhance this process, the TemplateData object is completely typed. Ideally, this would be
* enhanced by providing linters that make sure all of their required members are implemented by
* a Subclasser.
* When a Subclasser wants to add more data, this can be done via:
* @example
* ```js
* get _templateDataDropdown() {
* return {
* ...super._templateDataDropdown,
* myExtraData: { x: 1, y: 2 },
* }
* }
* ```
* @overridable
* @type {TemplateDataForDropdownInputTel}
*/
get _templateDataDropdown() {
const localizeManager = getLocalizeManager();
const refs = {
dropdown: {
ref: this.refs.dropdown,
props: {
style: `height: 100%;`,
},
listeners: {
change: this._onDropdownValueChange,
'model-value-changed': this._onDropdownValueChange,
},
labels: {
selectCountry: localizeManager.msg('lion-input-tel:selectCountry'),
allCountries:
this._allCountriesLabel || localizeManager.msg('lion-input-tel:allCountries'),
preferredCountries:
this._preferredCountriesLabel ||
localizeManager.msg('lion-input-tel:suggestedCountries'),
},
},
input: this._inputNode,
};
return {
refs,
data: {
activeRegion: this.activeRegion,
regionMetaList: this.__regionMetaList,
regionMetaListPreferred: this.__regionMetaListPreferred,
},
};
}
static templates = {
dropdown: (/** @type {TemplateDataForDropdownInputTel} */ templateDataForDropdown) => {
const { refs, data } = templateDataForDropdown;
const renderOption = (/** @type {RegionMeta} */ regionMeta) =>
html`${this.templates.dropdownOption(templateDataForDropdown, regionMeta)} `;
// TODO: once spread directive available, use it per ref
return html`
<select
${ref(refs?.dropdown?.ref)}
aria-label="${refs?.dropdown?.labels?.selectCountry}"
@change="${refs?.dropdown?.listeners?.change}"
style="${refs?.dropdown?.props?.style}"
>
${data?.regionMetaListPreferred?.length
? html`
<optgroup label="${refs?.dropdown?.labels?.preferredCountries}">
${data.regionMetaListPreferred.map(renderOption)}
</optgroup>
<optgroup label="${refs?.dropdown?.labels?.allCountries}">
${data?.regionMetaList?.map(renderOption)}
</optgroup>
`
: html` ${data?.regionMetaList?.map(renderOption)}`}
</select>
`;
},
/**
* @param {TemplateDataForDropdownInputTel} templateDataForDropdown
* @param {RegionMeta} contextData
*/
// eslint-disable-next-line class-methods-use-this
dropdownOption: (templateDataForDropdown, { regionCode, countryCode, flagSymbol }) => html`
<option value="${regionCode}">${regionCode} (+${countryCode}) ${flagSymbol}</option>
`,
};
/**
* @configure LitElement
* @enhance LionInputTel
*/
static styles = [
super.styles,
css`
/**
* We need to align the height of the dropdown with the height of the text field.
* We target the HTMLDivElement (render wrapper from SlotMixin) here. Its child,
* [data-ref=dropdown], receives a 100% height as well via inline styles (since we
* can't target from shadow styles).
*/
::slotted([slot='prefix']) {
height: 100%;
}
`,
];
/**
* @configure SlotMixin
*/
get slots() {
return {
...super.slots,
prefix: () => {
const ctor = /** @type {typeof LionInputTelDropdown} */ (this.constructor);
const { templates } = ctor;
return {
template: templates.dropdown(this._templateDataDropdown),
renderAsDirectHostChild: true,
};
},
};
}
/**
* @configure LocalizeMixin
*/
onLocaleUpdated() {
super.onLocaleUpdated();
// @ts-expect-error relatively new platform api
this.__namesForLocale = new Intl.DisplayNames([this._langIso], {
type: 'region',
});
this.__createRegionMeta();
}
/**
* @enhance LionInputTel
*/
_onPhoneNumberUtilReady() {
super._onPhoneNumberUtilReady();
this.__createRegionMeta();
}
/**
* @lifecycle platform
*/
constructor() {
super();
/**
* Regions that will be shown on top of the dropdown
* @type {string[]}
*/
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 = '';
/**
* Contains everything needed for rendering region options:
* region code, country code, display name according to locale, display name
* @private
* @type {RegionMeta[]}
*/
this.__regionMetaList = [];
/**
* A filtered `this.__regionMetaList`, containing all regions provided in `preferredRegions`
* @private
* @type {RegionMeta[]}
*/
this.__regionMetaListPreferred = [];
/**
* @protected
* @type {EventListener}
*/
this._onDropdownValueChange = this._onDropdownValueChange.bind(this);
/**
* @private
* @type {EventListener}
*/
this.__syncRegionWithDropdown = this.__syncRegionWithDropdown.bind(this);
}
/**
* @lifecycle LitElement
* @param {import('lit-element').PropertyValues } changedProperties
*/
willUpdate(changedProperties) {
super.willUpdate(changedProperties);
if (changedProperties.has('allowedRegions')) {
this.__createRegionMeta();
}
}
/**
* @param {import('lit-element').PropertyValues } changedProperties
*/
updated(changedProperties) {
super.updated(changedProperties);
this.__syncRegionWithDropdown();
if (changedProperties.has('disabled') || changedProperties.has('readOnly')) {
if (this.disabled || this.readOnly) {
this.refs.dropdown?.value?.setAttribute('disabled', '');
} else {
this.refs.dropdown?.value?.removeAttribute('disabled');
}
}
if (changedProperties.has('_phoneUtil')) {
this._initModelValueBasedOnDropdown();
}
}
/**
* @protected
*/
_initModelValueBasedOnDropdown() {
if (!this._initialModelValue && !this.dirty && this._phoneUtil) {
const countryCode = this._phoneUtil.getCountryCodeForRegionCode(this.activeRegion);
if (this.formatCountryCodeStyle === 'parentheses') {
this.__initializedRegionCode = `(+${countryCode})`;
} else {
this.__initializedRegionCode = `+${countryCode}`;
}
// on init we set the value to the _inputNode and not the modelValue to prevent validation
this._inputNode.value = this.__initializedRegionCode;
this._initialModelValue = this.__initializedRegionCode;
this.initInteractionState();
}
}
/**
* Used for Required validation and computation of interaction states.
* We need to override this, because we prefill the input with the region code (like +31), but for proper UX,
* we don't consider this as having interaction state `prefilled`
* @param {string} modelValue
* @return {boolean}
* @protected
*/
_isEmpty(modelValue = this.modelValue) {
// the activeRegion is not synced on time, so it can't be used in this check
return super._isEmpty(modelValue) || this.value === this.__initializedRegionCode;
}
/**
* @protected
* @param {OnDropdownChangeEvent} event
*/
_onDropdownValueChange(event) {
const isInitializing = event.detail?.initialize || !this._phoneUtil;
const dropdownElement = event.target;
const dropdownValue = /** @type {RegionCode} */ (
dropdownElement.modelValue || dropdownElement.value
);
if (isInitializing || this.activeRegion === dropdownValue) {
return;
}
const prevActiveRegion = this.activeRegion;
this._setActiveRegion(dropdownValue);
// Change region code in text box
// From: https://bl00mber.github.io/react-phone-input-2.html
if (prevActiveRegion !== this.activeRegion && !this.focused && this._phoneUtil) {
const prevCountryCode = this._phoneUtil.getCountryCodeForRegionCode(prevActiveRegion);
const countryCode = this._phoneUtil.getCountryCodeForRegionCode(this.activeRegion);
if (this.value.includes(`+${prevCountryCode}`)) {
this.modelValue = this._callParser(
this.value.replace(`+${prevCountryCode}`, `+${countryCode}`),
);
} else {
// In case of dropdown has +31, and input has only +3
const valueObj = this.value.split(' ');
if (this.formatCountryCodeStyle === 'parentheses' && !this.value.includes('(')) {
this.modelValue = this._callParser(this.value.replace(valueObj[0], `(+${countryCode})`));
} else {
this.modelValue = this._callParser(this.value.replace(valueObj[0], `+${countryCode}`));
}
}
}
}
/**
* @private
*/
__syncRegionWithDropdown(regionCode = this.activeRegion) {
const dropdownElement = this.refs.dropdown?.value;
if (!dropdownElement || !regionCode) {
return;
}
const inputCountryCode = this._phoneUtil?.getCountryCodeForRegionCode(regionCode);
if ('modelValue' in dropdownElement) {
const dropdownCountryCode = this._phoneUtil?.getCountryCodeForRegionCode(
dropdownElement.modelValue,
);
if (dropdownCountryCode === inputCountryCode) {
return;
}
/** @type {* & FormatHost} */ (dropdownElement).modelValue = regionCode;
} else {
const dropdownCountryCode = this._phoneUtil?.getCountryCodeForRegionCode(
dropdownElement.value,
);
if (dropdownCountryCode === inputCountryCode) {
return;
}
/** @type {HTMLSelectElement} */ (dropdownElement).value = regionCode;
}
}
/**
* Prepares data for options, like "Greece (Ελλάδα)", where "Greece" is `nameForLocale` and
* "Ελλάδα" `nameForRegion`.
* This should be run on change of:
* - allowedRegions
* - _phoneUtil loaded
* - locale
* @private
*/
__createRegionMeta() {
if (!this._allowedOrAllRegions?.length || !this.__namesForLocale) {
return;
}
this.__regionMetaList = [];
this.__regionMetaListPreferred = [];
this._allowedOrAllRegions.forEach(regionCode => {
// @ts-ignore relatively new platform api
const namesForRegion = new Intl.DisplayNames([regionCode.toLowerCase()], {
type: 'region',
});
const countryCode =
this._phoneUtil && this._phoneUtil.getCountryCodeForRegionCode(regionCode);
const flagSymbol = getFlagSymbol(regionCode);
const destinationList = this.preferredRegions.includes(regionCode)
? this.__regionMetaListPreferred
: this.__regionMetaList;
destinationList.push({
regionCode,
countryCode,
flagSymbol,
nameForLocale: this.__namesForLocale?.of(regionCode),
nameForRegion: namesForRegion.of(regionCode),
});
});
}
/**
* Usually, we don't use composition in regular LionFields (non choice-groups). Here we use a LionSelect(Rich) inside.
* We don't want to repropagate any children, since an Application Developer is not concerned with these internals (see repropate logic in FormControlMixin)
* Also, we don't want to give (wrong) info to InteractionStateMixin, that will set the wrong interaction states based on child info.
* TODO: Make "this._repropagationRole !== 'child'" the default for FormControlMixin
* (so that FormControls used within are never repropagated for LionFields)
* @protected
* @configure FormControlMixin: don't repropagate any children
*/
// eslint-disable-next-line class-methods-use-this
_repropagationCondition() {
return false;
}
}