tablefilter 0.7.7 → 0.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/starter.html +2 -2
- package/dist/tablefilter/tablefilter.js +1 -1
- package/dist/tablefilter/tf-1-dca7dae32ca501c46faf.js +1 -0
- package/dist/tablefilter/{tf-2-0ea691278d572212426a.js → tf-2-eaf25ea22f7cd033d053.js} +2 -2
- package/dist/tablefilter.cjs.js +300 -58
- package/dist/tablefilter.esm.js +300 -58
- package/dist/tablefilter.umd.js +300 -58
- package/package.json +8 -7
- package/src/components/tf-checklist.js +571 -0
- package/src/modules/checkList.js +405 -93
- package/src/number.js +13 -1
- package/dist/tablefilter/tf-1-7e29d0dbe6e0e0a4ff05.js +0 -1
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<tf-checklist>` custom element: multi-select filter popup.
|
|
3
|
+
*
|
|
4
|
+
* An open shadow root exposing a `<button type="button">` trigger
|
|
5
|
+
* (`part="trigger"`) and a listbox popover (`part="listbox"`), following
|
|
6
|
+
* the WAI-ARIA APG combobox pattern with a multi-select listbox.
|
|
7
|
+
*
|
|
8
|
+
* Attributes:
|
|
9
|
+
* - `trigger-text` — visible trigger text, rendered as text content.
|
|
10
|
+
* - `label` — `aria-label` on the trigger. Unset keeps the trigger's text
|
|
11
|
+
* as its accessible name.
|
|
12
|
+
* - `listbox-label` — `aria-label` on the listbox. Unset names the listbox
|
|
13
|
+
* from the trigger through `aria-labelledby`.
|
|
14
|
+
* - `open` — whether the popover is expanded. The trigger click toggles
|
|
15
|
+
* it; `aria-expanded` and the listbox's `hidden` state follow.
|
|
16
|
+
* - `disabled` — disables the trigger.
|
|
17
|
+
*
|
|
18
|
+
* Options are supplied through `setOptions()`. Each entry is either a
|
|
19
|
+
* primitive value or an object with `value`, `text`, `disabled` and
|
|
20
|
+
* `selected`; `text` defaults to the value and `setOptions()` clears the
|
|
21
|
+
* previous selection. Every entry renders as a `role="option"` element
|
|
22
|
+
* (`part="option"`) with `aria-selected`, `aria-disabled` for disabled
|
|
23
|
+
* entries, and a decorative checkbox (`part="option-checkbox"`,
|
|
24
|
+
* `aria-hidden`, non-interactive) followed by its text (`part="option-text"`),
|
|
25
|
+
* rendered as `textContent` (markup shown literally, XSS-safe). The checkbox
|
|
26
|
+
* is a styling hook; selection state is exposed through `aria-selected`
|
|
27
|
+
* and the `option` part tokens (`option-selected` / `option-disabled`).
|
|
28
|
+
*
|
|
29
|
+
* `setSelectedValues()` selects the options matching the given values
|
|
30
|
+
* and `getSelectedValues()` returns the selected values in option order.
|
|
31
|
+
* Clicking an enabled option toggles it. Option text is rendered as
|
|
32
|
+
* `textContent` (markup shown literally, matching legacy behavior).
|
|
33
|
+
*
|
|
34
|
+
* Events:
|
|
35
|
+
* - `open-change` — dispatched when the trigger toggles the popover, with
|
|
36
|
+
* `detail: {open}`, the expanded state as a boolean. Bubbles and crosses
|
|
37
|
+
* the shadow boundary.
|
|
38
|
+
* - `selection-change` — dispatched when a click toggles an option, with
|
|
39
|
+
* `detail: {values}`, the selected values in option order. Bubbles and
|
|
40
|
+
* crosses the shadow boundary. `setOptions()` and
|
|
41
|
+
* `setSelectedValues()` do not dispatch it.
|
|
42
|
+
*
|
|
43
|
+
* Content is projected into the trigger through `slot="trigger"`.
|
|
44
|
+
* `setText()`, `setLabel()`, `setListboxLabel()` and `setOpen()` reflect
|
|
45
|
+
* the matching attributes. Styling is left to the theme via `--tf-*`
|
|
46
|
+
* custom properties and `::part()`; no CSS class or presentational style
|
|
47
|
+
* is shipped.
|
|
48
|
+
*
|
|
49
|
+
* Importing this module registers the element. It is not part of the
|
|
50
|
+
* legacy webpack bundle; consumers load it as a module.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
// Ids live inside each instance's shadow root, so they are unique per
|
|
54
|
+
// component instance. The trigger names the listbox by default.
|
|
55
|
+
const TRIGGER_ID = 'trigger';
|
|
56
|
+
const LISTBOX_ID = 'listbox';
|
|
57
|
+
|
|
58
|
+
const OPTION_SELECTOR = '[part~="option"]';
|
|
59
|
+
|
|
60
|
+
// Cloned into every instance's shadow root. Structure and `part` hooks
|
|
61
|
+
// only; no class or presentational style is baked in.
|
|
62
|
+
const TEMPLATE = document.createElement('template');
|
|
63
|
+
TEMPLATE.innerHTML = `
|
|
64
|
+
<button type="button" part="trigger" id="${TRIGGER_ID}"
|
|
65
|
+
aria-haspopup="listbox" aria-expanded="false"
|
|
66
|
+
aria-controls="${LISTBOX_ID}">
|
|
67
|
+
<slot name="trigger"></slot><span part="trigger-text"></span>
|
|
68
|
+
</button>
|
|
69
|
+
<div part="listbox" id="${LISTBOX_ID}" role="listbox"
|
|
70
|
+
aria-multiselectable="true" aria-labelledby="${TRIGGER_ID}"
|
|
71
|
+
tabindex="-1" hidden></div>
|
|
72
|
+
`;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Normalize an option entry into `{value, text, disabled, selected}`.
|
|
76
|
+
* @param {*} entry Primitive value or `{value, text, disabled, selected}`
|
|
77
|
+
* @returns {Object} Normalized option
|
|
78
|
+
*/
|
|
79
|
+
function normalizeOption(entry) {
|
|
80
|
+
const isObject = entry !== null && typeof entry === 'object';
|
|
81
|
+
const raw = isObject ? entry.value : entry;
|
|
82
|
+
const value = raw == null ? '' : String(raw);
|
|
83
|
+
const text = isObject && entry.text != null ?
|
|
84
|
+
String(entry.text) : value;
|
|
85
|
+
return {
|
|
86
|
+
value,
|
|
87
|
+
text,
|
|
88
|
+
disabled: Boolean(isObject && entry.disabled),
|
|
89
|
+
selected: Boolean(isObject && entry.selected)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export class TfChecklistElement extends HTMLElement {
|
|
94
|
+
constructor() {
|
|
95
|
+
super();
|
|
96
|
+
this.attachShadow({ mode: 'open' });
|
|
97
|
+
this.shadowRoot.appendChild(TEMPLATE.content.cloneNode(true));
|
|
98
|
+
this._trigger = this.shadowRoot.querySelector('button');
|
|
99
|
+
this._triggerText =
|
|
100
|
+
this.shadowRoot.querySelector('[part="trigger-text"]');
|
|
101
|
+
this._listbox = this.shadowRoot.querySelector('[part="listbox"]');
|
|
102
|
+
// Option entries supplied through setOptions(), in render order.
|
|
103
|
+
this._options = [];
|
|
104
|
+
// Rendered option elements, aligned with _options by index.
|
|
105
|
+
this._optionEls = [];
|
|
106
|
+
this._searchString = '';
|
|
107
|
+
this._trigger.addEventListener('click', () => this._toggle());
|
|
108
|
+
this._listbox.addEventListener('click', (evt) =>
|
|
109
|
+
this._onOptionClick(evt));
|
|
110
|
+
this._trigger.addEventListener('keydown', (evt) =>
|
|
111
|
+
this._onTriggerKeydown(evt));
|
|
112
|
+
this._listbox.addEventListener('keydown', (evt) =>
|
|
113
|
+
this._onListboxKeydown(evt));
|
|
114
|
+
this._listbox.addEventListener('focusin', () => {
|
|
115
|
+
this._updateActiveDescendant();
|
|
116
|
+
});
|
|
117
|
+
this._listbox.addEventListener('focusout', () => {
|
|
118
|
+
this._listbox.removeAttribute('aria-activedescendant');
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static get observedAttributes() {
|
|
123
|
+
return ['trigger-text', 'label', 'listbox-label', 'open',
|
|
124
|
+
'disabled', 'col']; // 'col' reserved for #929 column mapping
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
connectedCallback() {
|
|
128
|
+
this._update();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
attributeChangedCallback() {
|
|
132
|
+
this._update();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Set the trigger's accessible name, rendered as `aria-label`.
|
|
137
|
+
* @param {*} label Accessible name
|
|
138
|
+
*/
|
|
139
|
+
setLabel(label) {
|
|
140
|
+
this.setAttribute('label', label == null ? '' : String(label));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Set the listbox's accessible name, rendered as `aria-label`.
|
|
145
|
+
* @param {*} label Accessible name
|
|
146
|
+
*/
|
|
147
|
+
setListboxLabel(label) {
|
|
148
|
+
this.setAttribute('listbox-label',
|
|
149
|
+
label == null ? '' : String(label));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Set the trigger text, rendered into the `trigger-text` part.
|
|
154
|
+
* @param {*} text Trigger text
|
|
155
|
+
*/
|
|
156
|
+
setText(text) {
|
|
157
|
+
this.setAttribute('trigger-text', text == null ? '' : String(text));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Expand or collapse the popover.
|
|
162
|
+
* @param {Boolean} open Whether the popover is expanded
|
|
163
|
+
*/
|
|
164
|
+
setOpen(open) {
|
|
165
|
+
if (open) {
|
|
166
|
+
this.setAttribute('open', '');
|
|
167
|
+
} else {
|
|
168
|
+
this.removeAttribute('open');
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Replace the option list. Entries are primitives or objects with
|
|
174
|
+
* `value`, `text`, `disabled` and `selected`. The previous selection
|
|
175
|
+
* is cleared.
|
|
176
|
+
* @param {Array<*>} options Option entries
|
|
177
|
+
*/
|
|
178
|
+
setOptions(options) {
|
|
179
|
+
this._options = (Array.isArray(options) ? options : [])
|
|
180
|
+
.map(normalizeOption);
|
|
181
|
+
this._renderOptions();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Select the options whose value matches an entry in `values`, and
|
|
186
|
+
* clear the selection of the rest.
|
|
187
|
+
* @param {Array<*>} values Selected values
|
|
188
|
+
*/
|
|
189
|
+
setSelectedValues(values) {
|
|
190
|
+
const selected = new Set(
|
|
191
|
+
(Array.isArray(values) ? values : []).map((v) => String(v)));
|
|
192
|
+
this._options.forEach((option, index) => {
|
|
193
|
+
this._applySelection(option, index, selected.has(option.value));
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Get the values of the selected options, in option order.
|
|
199
|
+
* @returns {Array<String>} Selected values
|
|
200
|
+
*/
|
|
201
|
+
getSelectedValues() {
|
|
202
|
+
return this._options
|
|
203
|
+
.filter((option) => option.selected)
|
|
204
|
+
.map((option) => option.value);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Toggle an option on click.
|
|
209
|
+
* @param {Event} evt Click event from inside the listbox
|
|
210
|
+
*/
|
|
211
|
+
_onOptionClick(evt) {
|
|
212
|
+
const target = evt.target instanceof Element ? evt.target : null;
|
|
213
|
+
const el = target ? target.closest(OPTION_SELECTOR) : null;
|
|
214
|
+
if (!el) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const index = Number(el.getAttribute('data-index'));
|
|
219
|
+
const option = this._options[index];
|
|
220
|
+
if (!option || option.disabled) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
this._applySelection(option, index, !option.selected);
|
|
225
|
+
this.dispatchEvent(new CustomEvent('selection-change', {
|
|
226
|
+
detail: { values: this.getSelectedValues() },
|
|
227
|
+
bubbles: true,
|
|
228
|
+
composed: true
|
|
229
|
+
}));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Render every option entry into the listbox.
|
|
234
|
+
*/
|
|
235
|
+
_renderOptions() {
|
|
236
|
+
this._listbox.textContent = '';
|
|
237
|
+
this._optionEls = this._options.map((option, index) => {
|
|
238
|
+
const el = this._createOption(option, index);
|
|
239
|
+
this._listbox.appendChild(el);
|
|
240
|
+
return el;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Build an option element for an entry and apply its selection state.
|
|
246
|
+
* Delegates selection-state updates to `_applySelection()` so the two
|
|
247
|
+
* paths cannot drift.
|
|
248
|
+
* @param {Object} option Normalized option
|
|
249
|
+
* @param {Number} index Position in the option list
|
|
250
|
+
* @returns {Element} Option element
|
|
251
|
+
*/
|
|
252
|
+
_createOption(option, index) {
|
|
253
|
+
const el = document.createElement('div');
|
|
254
|
+
el.setAttribute('role', 'option');
|
|
255
|
+
el.setAttribute('tabindex', '-1');
|
|
256
|
+
el.setAttribute('data-index', String(index));
|
|
257
|
+
el.id = `option-${index}`;
|
|
258
|
+
const checkbox = document.createElement('span');
|
|
259
|
+
checkbox.setAttribute('part', 'option-checkbox');
|
|
260
|
+
checkbox.setAttribute('aria-hidden', 'true');
|
|
261
|
+
|
|
262
|
+
const text = document.createElement('span');
|
|
263
|
+
text.setAttribute('part', 'option-text');
|
|
264
|
+
text.textContent = option.text;
|
|
265
|
+
|
|
266
|
+
el.appendChild(checkbox);
|
|
267
|
+
el.appendChild(text);
|
|
268
|
+
|
|
269
|
+
// Build part tokens directly so creation and updates share one logic.
|
|
270
|
+
const partNames = ['option'];
|
|
271
|
+
if (option.selected) {
|
|
272
|
+
partNames.push('option-selected');
|
|
273
|
+
}
|
|
274
|
+
if (option.disabled) {
|
|
275
|
+
partNames.push('option-disabled');
|
|
276
|
+
}
|
|
277
|
+
el.setAttribute('part', partNames.join(' '));
|
|
278
|
+
el.setAttribute('aria-selected', option.selected ? 'true' : 'false');
|
|
279
|
+
if (option.disabled) {
|
|
280
|
+
el.setAttribute('aria-disabled', 'true');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return el;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Set an option's selection state on the entry and its element.
|
|
288
|
+
* @param {Object} option Normalized option
|
|
289
|
+
* @param {Number} index Position in the option list
|
|
290
|
+
* @param {Boolean} selected Whether the option is selected
|
|
291
|
+
*/
|
|
292
|
+
_applySelection(option, index, selected) {
|
|
293
|
+
option.selected = selected;
|
|
294
|
+
const el = this._optionEls[index];
|
|
295
|
+
if (!el) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
el.setAttribute('aria-selected', selected ? 'true' : 'false');
|
|
299
|
+
const partNames = ['option'];
|
|
300
|
+
if (selected) {
|
|
301
|
+
partNames.push('option-selected');
|
|
302
|
+
}
|
|
303
|
+
if (option.disabled) {
|
|
304
|
+
partNames.push('option-disabled');
|
|
305
|
+
}
|
|
306
|
+
el.setAttribute('part', partNames.join(' '));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Keyboard navigation on trigger
|
|
311
|
+
*/
|
|
312
|
+
_onTriggerKeydown(evt) {
|
|
313
|
+
const open = this.hasAttribute('open');
|
|
314
|
+
if (evt.key === 'ArrowDown' || evt.key === 'ArrowUp' ||
|
|
315
|
+
evt.key === ' ' || evt.key === 'Enter') {
|
|
316
|
+
evt.preventDefault();
|
|
317
|
+
if (!open) {
|
|
318
|
+
this.setOpen(true);
|
|
319
|
+
this._setExpanded(true);
|
|
320
|
+
this.dispatchEvent(new CustomEvent('open-change', {
|
|
321
|
+
detail: { open: true }, bubbles: true, composed: true
|
|
322
|
+
}));
|
|
323
|
+
const enabled = this._optionEls.filter(
|
|
324
|
+
el => el.getAttribute('aria-disabled') !== 'true');
|
|
325
|
+
const first = enabled[0] || this._optionEls[0];
|
|
326
|
+
if (first) {
|
|
327
|
+
first.focus();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (evt.key === 'Escape' && open) {
|
|
332
|
+
evt.preventDefault();
|
|
333
|
+
this.setOpen(false);
|
|
334
|
+
this._setExpanded(false);
|
|
335
|
+
this.dispatchEvent(new CustomEvent('open-change', {
|
|
336
|
+
detail: { open: false },
|
|
337
|
+
bubbles: true,
|
|
338
|
+
composed: true
|
|
339
|
+
}));
|
|
340
|
+
this._trigger.focus();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Keyboard navigation inside listbox
|
|
346
|
+
*/
|
|
347
|
+
_onListboxKeydown(evt) {
|
|
348
|
+
const options = this._optionEls.filter(
|
|
349
|
+
el => el.getAttribute('aria-disabled') !== 'true');
|
|
350
|
+
const focused = this.shadowRoot.activeElement;
|
|
351
|
+
const index = options.indexOf(focused);
|
|
352
|
+
const k = evt.key;
|
|
353
|
+
switch (k) {
|
|
354
|
+
case 'ArrowDown': {
|
|
355
|
+
evt.preventDefault();
|
|
356
|
+
const next = options[index + 1] || options[0];
|
|
357
|
+
if (next) {
|
|
358
|
+
next.focus();
|
|
359
|
+
}
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
case 'ArrowUp': {
|
|
363
|
+
evt.preventDefault();
|
|
364
|
+
const prev = options[index - 1] ||
|
|
365
|
+
options[options.length - 1];
|
|
366
|
+
if (prev) {
|
|
367
|
+
prev.focus();
|
|
368
|
+
}
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
case 'Home': {
|
|
372
|
+
evt.preventDefault();
|
|
373
|
+
if (options[0]) {
|
|
374
|
+
options[0].focus();
|
|
375
|
+
}
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case 'End': {
|
|
379
|
+
evt.preventDefault();
|
|
380
|
+
if (options[options.length - 1]) {
|
|
381
|
+
options[options.length - 1].focus();
|
|
382
|
+
}
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
case ' ':
|
|
386
|
+
case 'Enter': {
|
|
387
|
+
evt.preventDefault();
|
|
388
|
+
const target = evt.target.closest('[part~="option"]');
|
|
389
|
+
if (target) {
|
|
390
|
+
const idx = Number(
|
|
391
|
+
target.getAttribute('data-index'));
|
|
392
|
+
const option = this._options[idx];
|
|
393
|
+
if (option && !option.disabled) {
|
|
394
|
+
this._applySelection(
|
|
395
|
+
option, idx, !option.selected);
|
|
396
|
+
this.dispatchEvent(new CustomEvent(
|
|
397
|
+
'selection-change', {
|
|
398
|
+
detail: {
|
|
399
|
+
values: this.getSelectedValues()
|
|
400
|
+
},
|
|
401
|
+
bubbles: true,
|
|
402
|
+
composed: true
|
|
403
|
+
}));
|
|
404
|
+
}
|
|
405
|
+
if (k === 'Enter' &&
|
|
406
|
+
this.hasAttribute('open')) {
|
|
407
|
+
this.setOpen(false);
|
|
408
|
+
this._setExpanded(false);
|
|
409
|
+
this.dispatchEvent(new CustomEvent(
|
|
410
|
+
'open-change', {
|
|
411
|
+
detail: { open: false },
|
|
412
|
+
bubbles: true,
|
|
413
|
+
composed: true
|
|
414
|
+
}));
|
|
415
|
+
this._trigger.focus();
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
case 'Escape': {
|
|
421
|
+
evt.preventDefault();
|
|
422
|
+
this.setOpen(false);
|
|
423
|
+
this._setExpanded(false);
|
|
424
|
+
this.dispatchEvent(new CustomEvent('open-change', {
|
|
425
|
+
detail: { open: false },
|
|
426
|
+
bubbles: true,
|
|
427
|
+
composed: true
|
|
428
|
+
}));
|
|
429
|
+
this._trigger.focus();
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
default: {
|
|
433
|
+
if (evt.key.length === 1 && !evt.ctrlKey &&
|
|
434
|
+
!evt.metaKey && !evt.altKey) {
|
|
435
|
+
evt.preventDefault();
|
|
436
|
+
this._searchString += evt.key.toLowerCase();
|
|
437
|
+
clearTimeout(this._searchTimeout);
|
|
438
|
+
this._searchTimeout = setTimeout(
|
|
439
|
+
() => {
|
|
440
|
+
this._searchString = '';
|
|
441
|
+
}, 500);
|
|
442
|
+
const focused = this.shadowRoot.activeElement;
|
|
443
|
+
const enabledEls = this._optionEls.filter(
|
|
444
|
+
el => el.getAttribute('aria-disabled') !== 'true');
|
|
445
|
+
const startIndex = focused &&
|
|
446
|
+
enabledEls.indexOf(focused) >= 0 ?
|
|
447
|
+
enabledEls.indexOf(focused) + 1 : 0;
|
|
448
|
+
const match = enabledEls.slice(
|
|
449
|
+
startIndex).find(el => {
|
|
450
|
+
if (!el.hasAttribute('data-index')) {
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
const opt = this._options[
|
|
454
|
+
Number(el.getAttribute('data-index'))] || {};
|
|
455
|
+
const text = opt.text || '';
|
|
456
|
+
return text.toLowerCase().startsWith(
|
|
457
|
+
this._searchString);
|
|
458
|
+
}) || enabledEls.find(el => {
|
|
459
|
+
if (!el.hasAttribute('data-index')) {
|
|
460
|
+
return false;
|
|
461
|
+
}
|
|
462
|
+
const opt = this._options[
|
|
463
|
+
Number(el.getAttribute('data-index'))] || {};
|
|
464
|
+
const text = opt.text || '';
|
|
465
|
+
return text.toLowerCase().startsWith(
|
|
466
|
+
this._searchString);
|
|
467
|
+
});
|
|
468
|
+
if (match) {
|
|
469
|
+
match.focus();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Toggle the popover and announce the new state.
|
|
478
|
+
*/
|
|
479
|
+
_toggle() {
|
|
480
|
+
const open = !this.hasAttribute('open');
|
|
481
|
+
this.setOpen(open);
|
|
482
|
+
this.dispatchEvent(new CustomEvent('open-change', {
|
|
483
|
+
detail: { open },
|
|
484
|
+
bubbles: true,
|
|
485
|
+
composed: true
|
|
486
|
+
}));
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
_updateActiveDescendant() {
|
|
490
|
+
const focused = this.shadowRoot.activeElement;
|
|
491
|
+
if (focused && this._optionEls.includes(focused)) {
|
|
492
|
+
this._listbox.setAttribute('aria-activedescendant', focused.id);
|
|
493
|
+
} else {
|
|
494
|
+
this._listbox.removeAttribute('aria-activedescendant');
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Render the trigger and listbox attributes and expanded state.
|
|
500
|
+
*/
|
|
501
|
+
_update() {
|
|
502
|
+
if (!this._trigger) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
this._setAriaLabel(this._trigger, 'label');
|
|
507
|
+
this._triggerText.textContent =
|
|
508
|
+
this.getAttribute('trigger-text') || '';
|
|
509
|
+
this._setListboxName();
|
|
510
|
+
this._setExpanded(this.hasAttribute('open'));
|
|
511
|
+
|
|
512
|
+
if (this.hasAttribute('disabled')) {
|
|
513
|
+
this._trigger.setAttribute('disabled', '');
|
|
514
|
+
} else {
|
|
515
|
+
this._trigger.removeAttribute('disabled');
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Name the listbox from its own attribute, or from the trigger.
|
|
521
|
+
*/
|
|
522
|
+
_setListboxName() {
|
|
523
|
+
const label = this.getAttribute('listbox-label');
|
|
524
|
+
if (label) {
|
|
525
|
+
this._listbox.setAttribute('aria-label', label);
|
|
526
|
+
this._listbox.removeAttribute('aria-labelledby');
|
|
527
|
+
} else {
|
|
528
|
+
this._listbox.removeAttribute('aria-label');
|
|
529
|
+
this._listbox.setAttribute('aria-labelledby', TRIGGER_ID);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Reflect the expanded state on the trigger and the popover.
|
|
535
|
+
* @param {Boolean} open Whether the popover is expanded
|
|
536
|
+
*/
|
|
537
|
+
_setExpanded(open) {
|
|
538
|
+
this._trigger.setAttribute('aria-expanded', open ? 'true' : 'false');
|
|
539
|
+
if (open) {
|
|
540
|
+
this._listbox.removeAttribute('hidden');
|
|
541
|
+
this._searchString = '';
|
|
542
|
+
clearTimeout(this._searchTimeout);
|
|
543
|
+
this._updateActiveDescendant();
|
|
544
|
+
} else {
|
|
545
|
+
this._listbox.setAttribute('hidden', '');
|
|
546
|
+
this._listbox.removeAttribute('aria-activedescendant');
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Render an attribute as `aria-label` on a shadow element, or remove
|
|
552
|
+
* it when the attribute is absent or empty.
|
|
553
|
+
* @param {Element} el Shadow element to name
|
|
554
|
+
* @param {String} attr Attribute holding the accessible name
|
|
555
|
+
*/
|
|
556
|
+
_setAriaLabel(el, attr) {
|
|
557
|
+
const label = this.getAttribute(attr);
|
|
558
|
+
if (label) {
|
|
559
|
+
el.setAttribute('aria-label', label);
|
|
560
|
+
} else {
|
|
561
|
+
el.removeAttribute('aria-label');
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// Register once: loading this module twice (or on a page that already
|
|
567
|
+
// defined the element) must not throw.
|
|
568
|
+
if (typeof customElements !== 'undefined' &&
|
|
569
|
+
!customElements.get('tf-checklist')) {
|
|
570
|
+
customElements.define('tf-checklist', TfChecklistElement);
|
|
571
|
+
}
|