/
index.js
501 lines (459 loc) · 15.2 KB
/
index.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/* eslint-disable import/no-extraneous-dependencies */
import { html, svg } from 'lit-element';
import { classMap } from 'lit-html/directives/class-map';
import { ExpandSvg } from '@axa-ch/materials/icons';
import debounce from '../../../utils/debounce';
import styles from './index.scss';
import NoShadowDOM from '../../../utils/no-shadow';
import defineOnce from '../../../utils/define-once';
import fireCustomEvent from '../../../utils/custom-event';
import createRefId from '../../../utils/create-ref-id';
import typecheck from '../../../utils/typecheck';
// module constants
const ARROW_ICON = svg([ExpandSvg]);
const DEBOUNCE_DELAY = 250; // milliseconds
const DROPDOWN_UL_MAXHEIGHT = '200px';
const EMPTY_FUNCTION = () => {};
const WORD_END = '\x00';
const INDEX_END = '\x01';
const AUTOSUGGEST_INACTIVITY_DELAY = 300; // milliseconds
// module globals
let openDropdownInstance;
// helper functions
const shouldMove = elem => {
const boundingBox = elem.getBoundingClientRect();
const bottomIsInViewport =
boundingBox.bottom <=
(window.innerHeight || document.documentElement.clientHeight);
const enoughSpaceToMove = boundingBox.top > boundingBox.height;
return !bottomIsInViewport && enoughSpaceToMove;
};
const handleViewportCheck = elem => {
if (elem && shouldMove(elem)) {
elem.style.maxHeight = DROPDOWN_UL_MAXHEIGHT;
}
};
const forEach = (array, callback, scope) => {
for (let i = 0, n = array.length; i < n; i++) {
callback.call(scope, i, array[i]); // passes back stuff we need
}
};
const nativeItemsMapper = ({ name, value, selected, disabled }, index) =>
html`
<option
class="m-dropdown__option"
value="${value}"
data-index="${index}"
data-value="${value}"
?selected="${selected}"
?disabled="${disabled}"
>${name}</option
>
`;
const contentItemsMapper = clickHandler => (
{ name, value, selected, disabled },
index
) => {
const classes = {
'm-dropdown__item': true,
'm-dropdown__item--is-selected': selected,
};
return disabled
? html``
: html`
<li class="${classMap(classes)}">
<button
type="button"
@click="${clickHandler}"
tabindex="-1"
class="m-dropdown__button js-dropdown__button"
data-index="${index}"
data-value="${value}"
>
${name}
</button>
</li>
`;
};
const defaultTitleIfNeeded = (title, anotherSelection) =>
title
? [{ name: title, disabled: true, selected: !anotherSelection, value: '' }]
: [];
// CE
class AXADropdown extends NoShadowDOM {
static get tagName() {
return 'axa-dropdown';
}
static get styles() {
return styles;
}
static get properties() {
return {
'data-test-id': { type: String, reflect: true },
maxHeight: { type: Boolean, reflect: true },
label: { type: String },
required: { type: Boolean },
items: { type: Array, /* participate in typecheck'ing */ check: true },
open: { type: Boolean, reflect: true },
value: { type: String },
name: { type: String, reflect: true },
defaultTitle: { type: String, reflect: true },
native: { type: Boolean, reflect: true },
checkMark: { type: Boolean, reflect: true },
invalid: { type: Boolean, reflect: true },
error: { type: String, reflect: true },
disabled: { type: Boolean, reflect: true },
isReact: { type: Boolean },
};
}
set value(newValue) {
const {
state: { isControlled, value, firstTime },
state,
} = this;
// first value coming in indicates controlledness?
if (!isControlled && newValue !== undefined && firstTime) {
// yes, remember in model state
state.isControlled = true;
}
// update state
state.value = newValue;
state.firstTime = false;
// manual re-render, necessary for custom setters
this.requestUpdate('value', value);
}
get value() {
return this.state.value;
}
constructor() {
super();
this.refId = `dropdown-${createRefId()}`;
this.label = '';
this.checkMark = false;
this.invalid = false;
this.disabled = false;
this.required = false;
// property defaults
this.onChange = EMPTY_FUNCTION;
this.onFocus = EMPTY_FUNCTION;
this.onBlur = EMPTY_FUNCTION;
// internal properties
this.state = { isControlled: false, firstTime: true };
this._typedSoFar = '';
// bound event handlers (so scope and de-registration work as expected)
this.handleWindowKeyDown = this.handleWindowKeyDown.bind(this);
this.handleWindowClick = this.handleWindowClick.bind(this);
this.handleDropdownItemClick = this.handleDropdownItemClick.bind(this);
this.handleDropdownClick = this.handleDropdownClick.bind(this);
this.handleResize = debounce(
() => handleViewportCheck(this.dropdown),
DEBOUNCE_DELAY
);
}
openDropdown(open) {
this.open = open;
const links = this.querySelectorAll('.js-dropdown__button');
forEach(links, (_, link) =>
link.setAttribute('tabindex', open ? '0' : '-1')
);
if (open && openDropdownInstance) {
openDropdownInstance.open = false;
}
openDropdownInstance = open ? this : /* help GC */ null;
}
handleWindowClick() {
if (this.open) {
this.openDropdown(false);
}
}
handleWindowKeyDown(e) {
const { key, keyCode } = e;
if (key === 'Escape' || key === 'Esc' || keyCode === 27) {
e.preventDefault();
return this.openDropdown(false);
}
return this.handleAutosuggest(key, e);
}
// build one long string of words paired with their indices in `items`,
// separating the elements in each pair with suitable control characters
// that do not occur in typed input
buildAutosuggestDictionary() {
this._autosuggestLetters = new Set();
const { items, _autosuggestLetters: letters } = this;
this._autosuggestDictionary =
items
.filter(item => !item.disabled)
.map((item, index) => {
// normalize word for purposes of matching
const word = item.name.toLowerCase();
// construct a set of all letters in word
// (used for an early filter over keystrokes)
word.split('').forEach(letter => {
letters.add(letter.toLowerCase());
});
return `${INDEX_END}${word}${WORD_END}${index}`;
})
.join('') + INDEX_END;
}
handleAutosuggest(key) {
// ready for autosuggestions, by having focus on the non-native UI?
if (document.activeElement !== this.button) {
// no, bail out
return;
}
// do we have even a chance of matching for the key that was just pressed?
const character = key.toLowerCase();
if (!this._autosuggestLetters.has(character)) {
// no, bail out
return;
}
this._typedSoFar += character;
// wait for certain period of keyboard inactivitity...
clearTimeout(this._timer);
this._timer = setTimeout(() => {
// ... before trying to match what was typed sofar against items
const { _autosuggestDictionary, _typedSoFar, items } = this;
const match = _autosuggestDictionary.indexOf(INDEX_END + _typedSoFar);
if (match > -1) {
// we have a match, determine the corresponding item ...
const matchIndexPos =
_autosuggestDictionary.indexOf(WORD_END, match) + 1;
const firstSelectedItem = parseInt(
_autosuggestDictionary.slice(matchIndexPos),
10
);
// ... and its value
const { value } = items[firstSelectedItem];
// re-render to visually select matched item
this.updateItems(value, 'scrollIntoView');
}
// next characters start a *new* autosuggestion
this._typedSoFar = '';
}, AUTOSUGGEST_INACTIVITY_DELAY);
}
handleDropdownClick(e) {
e.preventDefault();
e.stopPropagation();
// toggle dropdown
const open = !this.open;
this.openDropdown(open);
if (open) {
// note: without setTimeout, scrolling is off by several items, s.t. the selected
// element is not visible (at least with maxHeight being set)
setTimeout(() => this.scrollIntoView(), 1);
}
}
handleDropdownItemClick(e) {
e.preventDefault();
e.stopPropagation();
const { target } = e;
// if click 'lands' on native <select> (narrow window widths or 'native' mode),
// use DOM-API 'selectedIndex' to find out which option the click targeted
const realTarget =
target instanceof HTMLSelectElement
? target.children[target.selectedIndex]
: target;
const { value, index } = realTarget.dataset;
const {
state: { isControlled },
onChange,
selectedIndex,
} = this;
this.openDropdown(false);
// no change compared to previous selection?
// eslint-disable-next-line eqeqeq
if (selectedIndex == index) {
// ==: indices may be number or string
return;
}
this.selectedIndex = index | 0;
const [{ name }] = this.findByValue(value);
// allow idiomatic event.target.value in onChange callback!
const syntheticEvent = { target: { value, index, name } };
onChange(syntheticEvent);
if (!isControlled) {
this.value = value; // triggers re-render
const details = { value, index, name };
fireCustomEvent('axa-change', value, this, { bubbles: false });
fireCustomEvent('change', details, this, { bubbles: false });
}
}
findByValue(value, indexOnly) {
const { items } = this;
const itemIndex = items.findIndex(({selected, value: selectedValue}) =>
value === null ? selected : (selectedValue === value || selectedValue === parseInt(value, 10))
);
return indexOnly ? itemIndex : [items[itemIndex], itemIndex];
}
scrollIntoView(index) {
const realIndex =
typeof index === 'number' ? index : this.select.selectedIndex;
const itemNode = this.querySelector(
`.js-dropdown__button[data-index="${realIndex}"]`
);
if (itemNode) {
// note: IE does not interpret scrollIntoView options, therefore
// that the selected item might not be centered there.
itemNode.scrollIntoView({ block: 'center' });
}
}
updateItems(value, scrollIntoView) {
if (typeof value !== 'string') {
return;
}
const [item, itemIndex] = this.findByValue(value);
if (!item) {
return;
}
const { name } = item;
this.value = value || name || '';
// clone items array with updated selected property
// (the fact that items are cloned ensures re-render!)
this.items = this.items.map((_item, index) => {
_item.selected = index === itemIndex;
return _item;
});
if (scrollIntoView) {
this.scrollIntoView(itemIndex);
}
}
/* last overrideable lifecycle point *before* render:
put side-effects there that influence render */
shouldUpdate(changedProperties) {
typecheck(this, { items: [] });
// controlledness is only meaningful if the isReact property has been set
// via the React wrapper
this.state.isControlled = this.state.isControlled && this.isReact;
const {
state: { isControlled },
} = this;
// implicit change of value via newly selected item?
if (
!isControlled &&
changedProperties.has('items') &&
changedProperties.size === 1
) {
// make change explicit
const selectedItem = this.items.find(item => item.selected);
if (selectedItem) {
this.value = selectedItem.value;
}
}
this.updateItems(this.value);
return true;
}
render() {
const {
items = [],
name = '',
label = '',
refId = '',
defaultTitle = '',
checkMark,
invalid,
error,
required,
disabled,
handleDropdownItemClick,
handleDropdownClick,
} = this;
const [selectedItem] = this.findByValue(null);
this.title = selectedItem ? selectedItem.name : defaultTitle;
return html`
${label &&
html`
<label for="${refId}" class="m-dropdown__label">
${label}
${required
? html`
*
`
: ''}
</label>
`}
<div class="m-dropdown__wrapper">
<div class="m-dropdown__elements">
<!-- NATIVE -->
<div class="m-dropdown__select-wrapper">
<select
id="${refId}"
class="m-dropdown__select js-dropdown__select"
name="${name}"
aria-required="${required}"
?disabled="${disabled}"
@focus="${this.onFocus}"
@blur="${this.onBlur}"
@change="${handleDropdownItemClick}"
>
${defaultTitleIfNeeded(defaultTitle, selectedItem)
.concat(items)
.map(nativeItemsMapper)}
</select>
<span class="m-dropdown__select-icon">
${ARROW_ICON}
</span>
</div>
<!-- NATIVE END -->
<!-- ENHANCED -->
<button
type="button"
class="m-dropdown__toggle js-dropdown__toggle"
aria-disabled="${disabled}"
@focus="${this.onFocus}"
@blur="${this.onBlur}"
@click="${handleDropdownClick}"
>
<span class="m-dropdown__flex-wrapper">
<span class="js-dropdown__title">${this.title}</span>
<span class="m-dropdown__select-icon">${ARROW_ICON}</span>
</span>
</button>
<ul class="m-dropdown__content js-dropdown__content">
${items.map(contentItemsMapper(handleDropdownItemClick))}
</ul>
<!-- ENHANCED END -->
</div>
${checkMark
? html`
<span class="m-dropdown__check-wrapper">
<span class="m-dropdown__check js-dropdown__check"></span>
</span>
`
: ''}
</div>
${invalid
? html`
<span class="m-dropdown__error js-dropdown__error">${error}</span>
`
: ''}
`;
}
firstUpdated() {
this.button = this.querySelector('.js-dropdown__toggle');
this.dropdown = this.querySelector('.js-dropdown__content');
this.select = this.querySelector('.js-dropdown__select');
window.addEventListener('resize', this.handleResize);
window.addEventListener('keydown', this.handleWindowKeyDown);
window.addEventListener('click', this.handleWindowClick);
window.addEventListener('axa-dropdown-close', this.handleWindowClick);
}
updated(changedProperties) {
const { select, defaultTitle } = this;
// adjust native <select>
select.selectedIndex =
this.findByValue(null, true) + (defaultTitle ? 1 : 0);
if (changedProperties.has('items')) {
// rebuild keyboard-autosuggestion data structures
this.buildAutosuggestDictionary();
}
}
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener('resize', this.handleResize);
window.removeEventListener('keydown', this.handleWindowKeyDown);
window.removeEventListener('click', this.handleWindowClick);
window.removeEventListener('axa-dropdown-close', this.handleWindowClick);
}
}
defineOnce(AXADropdown.tagName, AXADropdown);
export default AXADropdown;