-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaadin-list-box-1.js
74 lines (63 loc) · 1.78 KB
/
vaadin-list-box-1.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
import { LitElement, html, css } from 'lit-element';
import { vaadinListBoxStyles } from './vaadin-list-box-styles.js';
class VaadinListBox1 extends LitElement {
static get properties() { return {
items: { type: Array }
};}
constructor() {
super();
this.items = [];
}
static get styles() {
return [vaadinListBoxStyles, css``]
}
render() {
return html`
<ul role="listbox">
<slot></slot>
</ul>
`;
}
firstUpdated() {
this.filterItems();
this.shadowRoot.querySelector('slot').addEventListener('slotchange', event => {
this.children[0].setTabIndex(0);
});
this.addEventListener('click', event => {
const option = event.target.closest('vaadin-item-1')
if (!option) {
return;
}
option.focus()
});
this.addEventListener('keydown', event => {
if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') {
return;
}
event.preventDefault();
const option = event.target;
let selectedOption;
if (event.key === 'ArrowDown') {
selectedOption = this.items[(this.items.findIndex((item) => item.getTabIndex() == 0) + 1)];
} else {
selectedOption = this.items[(this.items.findIndex((item) => item.getTabIndex() == 0) - 1)];
}
if (selectedOption) {
selectedOption.focus();
for (var i = 0; i < this.items.length; i++) {
this.items[i].setTabIndex(-1);
}
selectedOption.setTabIndex(0);
}
});
}
filterItems() {
this.items = [];
for (var i = 0; i < this.children.length; i++) {
if (this.children[i].tagName.toLowerCase() == 'vaadin-item-1') {
this.items.push(this.children[i]);
}
}
}
}
customElements.define('vaadin-list-box-1', VaadinListBox1);