-
Notifications
You must be signed in to change notification settings - Fork 6
/
Listbox.js
248 lines (232 loc) · 6.91 KB
/
Listbox.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
import { constructHTMLOptionsCollectionProxy } from '../dom/HTMLOptionsCollectionProxy.js';
import DelegatesFocusMixin from '../mixins/DelegatesFocusMixin.js';
import FormAssociatedMixin from '../mixins/FormAssociatedMixin.js';
import KeyboardNavMixin from '../mixins/KeyboardNavMixin.js';
import StateMixin from '../mixins/StateMixin.js';
import List from './List.js';
import ListOption from './ListOption.js';
/** -implements {HTMLSelectElement} */
export default List
.extend()
.mixin(StateMixin)
.mixin(FormAssociatedMixin)
.mixin(KeyboardNavMixin)
.mixin(DelegatesFocusMixin)
.observe({
multiple: 'boolean',
size: { type: 'integer', empty: 0 },
})
.set({
_ariaRole: 'listbox',
/** @type {HTMLCollectionOf<InstanceType<ListOption>> & HTMLOptionsCollection} */
_optionsCollection: null,
/** @type {HTMLCollectionOf<InstanceType<ListOption>>} */
_selectedOptionsCollection: null,
_handlingSelectedness: false,
_handleFormReset: true,
})
.define({
options() {
if (!this._optionsCollection) {
this._optionsCollection = constructHTMLOptionsCollectionProxy({
host: this,
collection: this.getElementsByTagName(ListOption.elementName),
OptionConstructor: ListOption,
GroupConstructor: ListOption,
});
}
return this._optionsCollection;
},
/** @return {HTMLCollectionOf<InstanceType<ListOption>>} */
selectedOptions() {
// eslint-disable-next-line no-return-assign
return (this._selectedOptionsCollection
??= (
/** @type {HTMLCollectionOf<InstanceType<ListOption>>} */
(this.getElementsByClassName('mdw-list-option__selected')))
);
},
type() { return this.multiple ? 'select-multiple' : 'select-one'; },
kbdNavQuery() { return ListOption.elementName; },
kbdNavFocusableWhenDisabled() { return true; },
})
.define({
length() { return this.options.length; },
selectedIndex: {
get() {
const [selectedItem] = this.selectedOptions;
if (!selectedItem) return -1;
return Array.prototype.indexOf.call(this.options, selectedItem);
},
set(value) {
const itemToSelect = this.options[value];
this._handlingSelectedness = true;
for (const option of this.options) {
option.selected = (option === itemToSelect);
}
this._handlingSelectedness = false;
this._value = this.value;
},
},
value: {
get() {
return this.selectedOptions[0]?.value ?? '';
},
/** @param {string} v */
set(v) {
let newValue = '';
const vString = `${v}`;
this._handlingSelectedness = true;
for (const option of this.options) {
if ((option.selected = (option.value === vString))) {
newValue = vString;
}
}
this._handlingSelectedness = false;
this._value = newValue;
},
},
add() { return this.options.add; },
})
.on({
disabledStateChanged(oldValue, newValue) {
this._kbdFocusable = !newValue;
this.tabIndex = newValue ? -1 : 0;
},
multipleChanged(oldValue, newValue) {
this.updateAriaProperty('ariaMultiSelectable', newValue ? 'true' : 'false');
},
_formResetChanged(oldValue, newValue) {
if (!newValue) return;
if (!this._handleFormReset) return;
this.value = this.defaultValue;
},
connected() {
if (!this.hasAttribute('tabindex')) {
this.tabIndex = 0;
}
},
})
.methods({
* _selectedOptionsGenerator() {
for (const el of this.options) {
if (!el.selected) continue;
yield el;
}
},
* [Symbol.iterator]() {
for (const el of this.options) {
yield el;
}
},
focus() {
this.focusCurrentOrFirst();
},
/**
* @param {number} index
* @return {ListOption|null}
*/
item(index) { return this.options[index]; },
/**
* @param {string} name ID of ListOption
* @return {ListOption|null}
*/
namedItem(name) {
for (const option of this.options) {
if (option.id === name) {
return option;
}
}
return null;
},
/** @param {Event} event */
onListboxClick(event) {
const target = event.target;
if (!(target instanceof ListOption)) return;
event.stopImmediatePropagation();
event.stopPropagation();
if (target.disabledState) return;
let sendUpdateNotifications = false;
this._handlingSelectedness = true;
// Perform unselect
if (target.selected) {
// Unselect condition
if (!this.required || (this.multiple && this.selectedOptions.length > 1)) {
sendUpdateNotifications = true;
target.selected = false;
}
} else {
if (!this.multiple) {
// Unselect all other values
for (const option of this.selectedOptions) {
option.selected = false;
}
}
target.selected = true;
sendUpdateNotifications = true;
}
this._value = this.value;
this._handlingSelectedness = false;
this._updateFormAssociatedValue();
if (sendUpdateNotifications) {
this.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
this.dispatchEvent(new Event('change', { bubbles: true }));
}
},
})
.css`
:host(:disabled) {
cursor: not-allowed;
pointer-events: none;
}
:host([internals-disabled]) {
cursor: not-allowed;
pointer-events: none;
}
`
.events({
'mdw-list-option:changed'(event) {
event.stopPropagation();
if (this.multiple) return;
if (this._handlingSelectedness) return;
const target = /** @type {InstanceType<ListOption>} */ (/** @type {unknown} */ (event.target));
if (target.selected) return;
this._handlingSelectedness = true;
// Programmatic selection of option means deselection of others
for (const option of this.selectedOptions) {
if (option !== target) {
option.selected = false;
}
}
this._value = this.value;
this._handlingSelectedness = false;
},
focus() {
// Manual delegates focus because disabled items need to be focusable
this.focusCurrentOrFirst();
},
keydown(event) {
if (event.key === 'Spacebar' || event.key === ' ') {
const target = event.target;
if (!(target instanceof ListOption)) return;
event.stopPropagation();
event.preventDefault();
this.onListboxClick.call(this, event);
}
},
click: 'onListboxClick',
})
.childEvents({
slot: {
slotchange() {
this.refreshTabIndexes();
let index = 0;
for (const el of this.options) {
el._index = index++;
}
// Refresh internal value
this._value = this.value;
},
},
})
.autoRegister('mdw-listbox');