@oscarpalmer/oui 0.33.0 → 0.35.0
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/dist/js/combobox/constants.combobox.d.mts +2 -0
- package/dist/js/combobox/constants.combobox.mjs +46 -0
- package/dist/js/combobox/data.combobox.d.mts +18 -0
- package/dist/js/combobox/data.combobox.mjs +2 -0
- package/dist/js/combobox/dom.combobox.d.mts +110 -0
- package/dist/js/combobox/dom.combobox.mjs +2 -0
- package/dist/js/combobox/event.combobox.d.mts +17 -0
- package/dist/js/combobox/event.combobox.mjs +2 -0
- package/dist/js/combobox/form.combobox.d.mts +6 -0
- package/dist/js/combobox/form.combobox.mjs +2 -0
- package/dist/js/combobox/index.d.mts +2 -0
- package/dist/js/combobox/index.mjs +195 -0
- package/dist/js/combobox/misc.combobox.d.mts +2 -0
- package/dist/js/combobox/misc.combobox.mjs +2 -0
- package/dist/js/combobox/models.combobox.d.mts +2 -0
- package/dist/js/combobox/models.combobox.mjs +1 -0
- package/dist/js/combobox/state.combobox.d.mts +13 -0
- package/dist/js/combobox/state.combobox.mjs +2 -0
- package/dist/js/constants.combobox-h_kyDegw.d.mts +248 -0
- package/dist/js/data.combobox-B8HMkDOh.mjs +1030 -0
- package/dist/js/dialog.mjs +3 -2
- package/dist/js/floatable/floatable.embedded.mjs +4 -4
- package/dist/js/focus-trap/focus-trap.embedded.mjs +3 -3
- package/dist/js/index.d.mts +14 -643
- package/dist/js/index.mjs +19 -4875
- package/dist/js/internal/draggable.mjs +6 -6
- package/dist/js/internal/navigable.mjs +3 -3
- package/dist/js/l10n.d.mts +20 -0
- package/dist/js/l10n.mjs +80 -0
- package/dist/js/menu.d.mts +1 -1
- package/dist/js/menu.mjs +3 -2
- package/dist/js/movable/movable.embedded.mjs +3 -3
- package/dist/js/popover.mjs +1 -1
- package/dist/js/resizable/resizable.embedded.mjs +7 -7
- package/dist/js/sortable/sortable.embedded.mjs +7 -6
- package/dist/js/splitter.mjs +7 -13
- package/dist/js/toolbar.d.mts +1 -1
- package/dist/js/tooltip.mjs +3 -3
- package/package.json +4 -4
- package/src/js/combobox/constants.combobox.ts +62 -0
- package/src/js/combobox/data.combobox.ts +236 -0
- package/src/js/combobox/dom.combobox.ts +825 -0
- package/src/js/combobox/event.combobox.ts +454 -0
- package/src/js/combobox/form.combobox.ts +116 -0
- package/src/js/combobox/index.ts +313 -0
- package/src/js/combobox/misc.combobox.ts +188 -0
- package/src/js/combobox/models.combobox.ts +94 -0
- package/src/js/combobox/state.combobox.ts +247 -0
- package/src/js/index.ts +1 -1
- package/src/js/internal/draggable.ts +3 -3
- package/src/js/l10n.ts +192 -0
- package/src/js/movable/movable.embedded.ts +2 -2
- package/src/js/resizable/resizable.embedded.ts +3 -3
- package/src/js/sortable/sortable.embedded.ts +2 -2
- package/src/js/splitter.ts +2 -2
- package/dist/js/combobox.d.mts +0 -91
- package/dist/js/combobox.mjs +0 -797
- package/src/js/combobox.ts +0 -2410
|
@@ -0,0 +1,825 @@
|
|
|
1
|
+
import {setAria} from '@oscarpalmer/toretto/aria';
|
|
2
|
+
import {setAttribute} from '@oscarpalmer/toretto/attribute';
|
|
3
|
+
import {createElement} from '@oscarpalmer/toretto/create';
|
|
4
|
+
import {getData} from '@oscarpalmer/toretto/data';
|
|
5
|
+
import {
|
|
6
|
+
COMBOBOX_ATTRIBUTE_ANCHOR,
|
|
7
|
+
COMBOBOX_ATTRIBUTE_CONTENT,
|
|
8
|
+
COMBOBOX_ATTRIBUTE_FILTER,
|
|
9
|
+
COMBOBOX_ATTRIBUTE_FILTER_INPUT,
|
|
10
|
+
COMBOBOX_ATTRIBUTE_HEADER,
|
|
11
|
+
COMBOBOX_ATTRIBUTE_HIDDEN,
|
|
12
|
+
COMBOBOX_ATTRIBUTE_LIST,
|
|
13
|
+
COMBOBOX_ATTRIBUTE_MESSAGE,
|
|
14
|
+
COMBOBOX_ATTRIBUTE_OPTION,
|
|
15
|
+
COMBOBOX_ATTRIBUTE_OPTION_ACTIVE,
|
|
16
|
+
COMBOBOX_ATTRIBUTE_SELECTION,
|
|
17
|
+
COMBOBOX_ATTRIBUTE_SELECTION_ITEM,
|
|
18
|
+
comboboxScrollOptions,
|
|
19
|
+
} from './constants.combobox';
|
|
20
|
+
import {updateComboboxValidity} from './form.combobox';
|
|
21
|
+
import type {
|
|
22
|
+
OuiComboboxAutoComplete,
|
|
23
|
+
OuiComboboxAutoCompleteType,
|
|
24
|
+
OuiComboboxGroup,
|
|
25
|
+
OuiComboboxPreview,
|
|
26
|
+
OuiComboboxSelection,
|
|
27
|
+
OuiComboboxSelectionItem,
|
|
28
|
+
OuiComboboxState,
|
|
29
|
+
OuiComboboxType,
|
|
30
|
+
} from './models.combobox';
|
|
31
|
+
import {getLocalization, localize} from '../l10n';
|
|
32
|
+
import {comboboxLocalizations} from './state.combobox';
|
|
33
|
+
|
|
34
|
+
// #region Types
|
|
35
|
+
|
|
36
|
+
type CreateAnchor = {
|
|
37
|
+
container?: HTMLElement;
|
|
38
|
+
element: HTMLElement;
|
|
39
|
+
selection?: OuiComboboxSelection;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type CreatAnchorParameters = {
|
|
43
|
+
autocomplete: OuiComboboxAutoComplete;
|
|
44
|
+
id: string;
|
|
45
|
+
label: HTMLLabelElement;
|
|
46
|
+
multiple: boolean;
|
|
47
|
+
select: HTMLSelectElement;
|
|
48
|
+
type: OuiComboboxType;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type CreateContent = {
|
|
52
|
+
element: HTMLElement;
|
|
53
|
+
filter?: CreateFilter;
|
|
54
|
+
list: CreateList;
|
|
55
|
+
message: HTMLElement;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type CreateContentParameters = {
|
|
59
|
+
autocomplete: OuiComboboxAutoComplete;
|
|
60
|
+
id: string;
|
|
61
|
+
label: HTMLLabelElement;
|
|
62
|
+
multiple: boolean;
|
|
63
|
+
select: HTMLSelectElement;
|
|
64
|
+
type: OuiComboboxType;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type CreateFilter = {
|
|
68
|
+
container: HTMLElement;
|
|
69
|
+
element: HTMLInputElement;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
type CreateFilterParameters = {
|
|
73
|
+
autocomplete: OuiComboboxAutoCompleteType;
|
|
74
|
+
id: string;
|
|
75
|
+
label: HTMLLabelElement;
|
|
76
|
+
type: OuiComboboxType;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
type CreateGroupParameters = {
|
|
80
|
+
autocomplete: OuiComboboxAutoComplete;
|
|
81
|
+
group: HTMLOptGroupElement;
|
|
82
|
+
groupOptions: HTMLOptionElement[];
|
|
83
|
+
selected: HTMLElement[];
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
type CreateInputParameters = {
|
|
87
|
+
autocomplete: OuiComboboxAutoCompleteType;
|
|
88
|
+
id: string;
|
|
89
|
+
label: HTMLLabelElement;
|
|
90
|
+
type: OuiComboboxType;
|
|
91
|
+
value: string;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
type CreateList = {
|
|
95
|
+
element: HTMLElement;
|
|
96
|
+
groups?: OuiComboboxGroup[];
|
|
97
|
+
options: HTMLElement[];
|
|
98
|
+
selected: HTMLElement[];
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
type CreateListParameters = {
|
|
102
|
+
autocomplete: OuiComboboxAutoComplete;
|
|
103
|
+
id: string;
|
|
104
|
+
label: HTMLLabelElement;
|
|
105
|
+
multiple: boolean;
|
|
106
|
+
select: HTMLSelectElement;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
type CreateOptionParameters = {
|
|
110
|
+
autocomplete: OuiComboboxAutoComplete;
|
|
111
|
+
group?: string;
|
|
112
|
+
index: number;
|
|
113
|
+
option: HTMLOptionElement;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
type CreateOptionsParameters = {
|
|
117
|
+
autocomplete: OuiComboboxAutoComplete;
|
|
118
|
+
group?: string;
|
|
119
|
+
list: HTMLElement;
|
|
120
|
+
options: HTMLOptionElement[];
|
|
121
|
+
selected: HTMLElement[];
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
type CreateSelectionItemsParameters = {
|
|
125
|
+
container: HTMLElement;
|
|
126
|
+
items: OuiComboboxSelectionItem[];
|
|
127
|
+
options: HTMLElement[];
|
|
128
|
+
state?: OuiComboboxState;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// #endregion
|
|
132
|
+
|
|
133
|
+
// #region Functions
|
|
134
|
+
|
|
135
|
+
export function createComboboxAnchor(parameters: CreatAnchorParameters): CreateAnchor {
|
|
136
|
+
const {autocomplete, id, label, select, type} = parameters;
|
|
137
|
+
|
|
138
|
+
let container: HTMLElement | undefined;
|
|
139
|
+
let element: HTMLElement;
|
|
140
|
+
let selection: OuiComboboxSelection | undefined;
|
|
141
|
+
|
|
142
|
+
if (parameters.type === 'autocomplete') {
|
|
143
|
+
element = createComboboxInput({
|
|
144
|
+
id,
|
|
145
|
+
label,
|
|
146
|
+
type,
|
|
147
|
+
autocomplete: autocomplete.value,
|
|
148
|
+
value: select.selectedOptions[0]?.textContent ?? '',
|
|
149
|
+
});
|
|
150
|
+
} else {
|
|
151
|
+
({container, element, selection} = createComboboxButton(parameters));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
setAttribute(container ?? element, COMBOBOX_ATTRIBUTE_ANCHOR, '');
|
|
155
|
+
|
|
156
|
+
return {container, element, selection};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function createComboboxButton(parameters: CreatAnchorParameters): CreateAnchor {
|
|
160
|
+
const {id, label, multiple, select} = parameters;
|
|
161
|
+
|
|
162
|
+
const element = createElement('button', {
|
|
163
|
+
aria: {
|
|
164
|
+
controls: `${id}_content`,
|
|
165
|
+
expanded: false,
|
|
166
|
+
haspopup: 'listbox',
|
|
167
|
+
labelledby: `${label.id} ${id}`,
|
|
168
|
+
},
|
|
169
|
+
property: {
|
|
170
|
+
textContent: multiple ? '' : (select.selectedOptions[0]?.textContent ?? ''),
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
if (!multiple) {
|
|
175
|
+
return {element};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const container = createElement('div');
|
|
179
|
+
const selection = createComboboxSelection(id, select);
|
|
180
|
+
|
|
181
|
+
container.append(selection.container, element);
|
|
182
|
+
|
|
183
|
+
return {container, element, selection};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function createComboboxContent(parameters: CreateContentParameters): CreateContent {
|
|
187
|
+
const {autocomplete, id, label, multiple, select, type} = parameters;
|
|
188
|
+
|
|
189
|
+
const element = createElement('div', {
|
|
190
|
+
aria: {
|
|
191
|
+
labelledby: label.id,
|
|
192
|
+
},
|
|
193
|
+
attribute: {
|
|
194
|
+
[COMBOBOX_ATTRIBUTE_CONTENT]: '',
|
|
195
|
+
popover: '',
|
|
196
|
+
},
|
|
197
|
+
property: {
|
|
198
|
+
id: `${id}_content`,
|
|
199
|
+
tabIndex: -1,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const filter = createComboboxFilter({id, label, type, autocomplete: autocomplete.value});
|
|
204
|
+
const list = createComboboxList({autocomplete, id, label, multiple, select});
|
|
205
|
+
const message = createComboboxMessage();
|
|
206
|
+
|
|
207
|
+
if (filter != null) {
|
|
208
|
+
element.append(filter.container);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
element.append(list.element, message);
|
|
212
|
+
|
|
213
|
+
return {element, filter, list, message};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function createComboboxFilter(parameters: CreateFilterParameters): CreateFilter | undefined {
|
|
217
|
+
const {autocomplete, id, label, type} = parameters;
|
|
218
|
+
|
|
219
|
+
if (type === 'autocomplete') {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const container = createElement('div', {
|
|
224
|
+
attribute: {
|
|
225
|
+
[COMBOBOX_ATTRIBUTE_FILTER]: '',
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const element = createComboboxInput({autocomplete, id, label, type, value: ''});
|
|
230
|
+
|
|
231
|
+
setAttribute(element, COMBOBOX_ATTRIBUTE_FILTER_INPUT, '');
|
|
232
|
+
|
|
233
|
+
container.append(element);
|
|
234
|
+
|
|
235
|
+
return {container, element};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function createComboboxGroup(parameters: CreateGroupParameters): OuiComboboxGroup {
|
|
239
|
+
const {autocomplete, group, groupOptions, selected} = parameters;
|
|
240
|
+
|
|
241
|
+
const element = createElement('li', {
|
|
242
|
+
aria: {
|
|
243
|
+
label: group.label,
|
|
244
|
+
},
|
|
245
|
+
property: {
|
|
246
|
+
role: 'group',
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const list = createElement('ul', {
|
|
251
|
+
property: {
|
|
252
|
+
role: 'none',
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const header = createComboboxHeader(group);
|
|
257
|
+
|
|
258
|
+
element.append(header, list);
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
element,
|
|
262
|
+
header,
|
|
263
|
+
options: createComboboxOptions({
|
|
264
|
+
autocomplete,
|
|
265
|
+
list,
|
|
266
|
+
selected,
|
|
267
|
+
group: header.id,
|
|
268
|
+
options: groupOptions,
|
|
269
|
+
}),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function createComboboxHeader(group: HTMLOptGroupElement): HTMLElement {
|
|
274
|
+
return createElement('div', {
|
|
275
|
+
aria: {
|
|
276
|
+
hidden: true,
|
|
277
|
+
},
|
|
278
|
+
attribute: {
|
|
279
|
+
[COMBOBOX_ATTRIBUTE_HEADER]: '',
|
|
280
|
+
},
|
|
281
|
+
property: {
|
|
282
|
+
id: group.id,
|
|
283
|
+
textContent: group.label,
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function createComboboxInput(parameters: CreateInputParameters): HTMLInputElement {
|
|
289
|
+
const {autocomplete, id, label, type, value} = parameters;
|
|
290
|
+
|
|
291
|
+
return createElement('input', {
|
|
292
|
+
aria: {
|
|
293
|
+
autocomplete: autocomplete,
|
|
294
|
+
controls: `${id}_list`,
|
|
295
|
+
expanded: type === 'select',
|
|
296
|
+
haspopup: 'listbox',
|
|
297
|
+
labelledby: label.id,
|
|
298
|
+
},
|
|
299
|
+
attribute: {
|
|
300
|
+
autocorrect: 'off',
|
|
301
|
+
autocomplete: 'off',
|
|
302
|
+
form: '',
|
|
303
|
+
spellcheck: 'false',
|
|
304
|
+
type: 'text',
|
|
305
|
+
},
|
|
306
|
+
property: {
|
|
307
|
+
id,
|
|
308
|
+
value,
|
|
309
|
+
role: 'combobox',
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function createComboboxList(parameters: CreateListParameters): CreateList {
|
|
315
|
+
const {autocomplete, id, label, multiple, select} = parameters;
|
|
316
|
+
|
|
317
|
+
const element = createElement('ul', {
|
|
318
|
+
aria: {
|
|
319
|
+
labelledby: label.id,
|
|
320
|
+
multiselectable: multiple,
|
|
321
|
+
},
|
|
322
|
+
attribute: {
|
|
323
|
+
[COMBOBOX_ATTRIBUTE_LIST]: '',
|
|
324
|
+
tabindex: -1,
|
|
325
|
+
},
|
|
326
|
+
property: {
|
|
327
|
+
id: `${id}_list`,
|
|
328
|
+
role: 'listbox',
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const children = [...select.children];
|
|
333
|
+
const childrenLength = children.length;
|
|
334
|
+
|
|
335
|
+
const options: HTMLElement[] = [];
|
|
336
|
+
const selected: HTMLElement[] = [];
|
|
337
|
+
|
|
338
|
+
let groups: OuiComboboxGroup[] | undefined;
|
|
339
|
+
|
|
340
|
+
for (let childIndex = 0; childIndex < childrenLength; childIndex += 1) {
|
|
341
|
+
const child = children[childIndex];
|
|
342
|
+
|
|
343
|
+
if (child instanceof HTMLOptionElement) {
|
|
344
|
+
const option = createComboboxOption({
|
|
345
|
+
autocomplete,
|
|
346
|
+
index: childIndex,
|
|
347
|
+
option: child,
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
element.append(option);
|
|
351
|
+
options.push(option);
|
|
352
|
+
|
|
353
|
+
if (child.selected) {
|
|
354
|
+
selected.push(option);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (!(child instanceof HTMLOptGroupElement)) {
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const group = createComboboxGroup({
|
|
365
|
+
autocomplete,
|
|
366
|
+
selected,
|
|
367
|
+
group: child,
|
|
368
|
+
groupOptions: [...child.children] as HTMLOptionElement[],
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
groups ??= [];
|
|
372
|
+
|
|
373
|
+
element.append(group.element, ...group.options);
|
|
374
|
+
groups.push(group);
|
|
375
|
+
options.push(...group.options);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return {element, groups, options, selected};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export function createComboboxMessage(): HTMLElement {
|
|
382
|
+
return createElement('div', {
|
|
383
|
+
aria: {
|
|
384
|
+
live: 'polite',
|
|
385
|
+
},
|
|
386
|
+
attribute: {
|
|
387
|
+
[COMBOBOX_ATTRIBUTE_MESSAGE]: '',
|
|
388
|
+
},
|
|
389
|
+
property: {
|
|
390
|
+
hidden: true,
|
|
391
|
+
role: 'status',
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export function createComboboxOption(parameters: CreateOptionParameters): HTMLElement {
|
|
397
|
+
const {autocomplete, group, option} = parameters;
|
|
398
|
+
|
|
399
|
+
return createElement('li', {
|
|
400
|
+
aria: {
|
|
401
|
+
selected: !autocomplete.none && option.selected,
|
|
402
|
+
},
|
|
403
|
+
attribute: {
|
|
404
|
+
[COMBOBOX_ATTRIBUTE_OPTION]: '',
|
|
405
|
+
},
|
|
406
|
+
data: {
|
|
407
|
+
group,
|
|
408
|
+
value: option.value,
|
|
409
|
+
},
|
|
410
|
+
property: {
|
|
411
|
+
id: option.id,
|
|
412
|
+
role: 'option',
|
|
413
|
+
textContent: option.textContent,
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export function createComboboxOptions(parameters: CreateOptionsParameters): HTMLElement[] {
|
|
419
|
+
const {autocomplete, group, list, options, selected} = parameters;
|
|
420
|
+
|
|
421
|
+
const items: HTMLElement[] = [];
|
|
422
|
+
|
|
423
|
+
for (let index = 0; index < options.length; index += 1) {
|
|
424
|
+
const option = options[index];
|
|
425
|
+
|
|
426
|
+
const item = createComboboxOption({autocomplete, group, index, option});
|
|
427
|
+
|
|
428
|
+
items.push(item);
|
|
429
|
+
|
|
430
|
+
if (option.selected) {
|
|
431
|
+
selected.push(item);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
list.append(item);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return items;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export function createComboboxSelection(
|
|
441
|
+
id: string,
|
|
442
|
+
select: HTMLSelectElement,
|
|
443
|
+
): OuiComboboxSelection {
|
|
444
|
+
const container = createElement('ul', {
|
|
445
|
+
attribute: {
|
|
446
|
+
[COMBOBOX_ATTRIBUTE_SELECTION]: '',
|
|
447
|
+
},
|
|
448
|
+
property: {
|
|
449
|
+
id: `${id}_selection`,
|
|
450
|
+
},
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
const options = [...select.selectedOptions];
|
|
454
|
+
|
|
455
|
+
const items: OuiComboboxSelectionItem[] = [];
|
|
456
|
+
|
|
457
|
+
createComboboxSelectionItems({container, items, options});
|
|
458
|
+
|
|
459
|
+
const {button} = items[0] ?? {};
|
|
460
|
+
|
|
461
|
+
if (button != null) {
|
|
462
|
+
button.tabIndex = 0;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return {
|
|
466
|
+
items,
|
|
467
|
+
container: container,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function createComboboxSelectionItem(
|
|
472
|
+
option: HTMLElement,
|
|
473
|
+
state?: OuiComboboxState,
|
|
474
|
+
): OuiComboboxSelectionItem {
|
|
475
|
+
const optionLabel = option.textContent;
|
|
476
|
+
const optionValue = getData(option, 'value', false) ?? '';
|
|
477
|
+
|
|
478
|
+
const element = createElement('li');
|
|
479
|
+
|
|
480
|
+
const localization =
|
|
481
|
+
state == null
|
|
482
|
+
? option.textContent
|
|
483
|
+
: getLocalization(comboboxLocalizations, state.host, 'remove', state.localizations);
|
|
484
|
+
|
|
485
|
+
const button = createElement('button', {
|
|
486
|
+
aria: {
|
|
487
|
+
label: localize(
|
|
488
|
+
localization,
|
|
489
|
+
{
|
|
490
|
+
label: optionLabel,
|
|
491
|
+
value: optionValue,
|
|
492
|
+
},
|
|
493
|
+
[optionLabel, optionValue],
|
|
494
|
+
),
|
|
495
|
+
},
|
|
496
|
+
attribute: {
|
|
497
|
+
[COMBOBOX_ATTRIBUTE_SELECTION_ITEM]: '',
|
|
498
|
+
tabindex: -1,
|
|
499
|
+
},
|
|
500
|
+
data: {
|
|
501
|
+
option: option.id,
|
|
502
|
+
},
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
const icon = createElement('span', {
|
|
506
|
+
aria: {
|
|
507
|
+
hidden: true,
|
|
508
|
+
},
|
|
509
|
+
property: {
|
|
510
|
+
innerHTML: '×',
|
|
511
|
+
},
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
const label = createElement('span', {
|
|
515
|
+
property: {
|
|
516
|
+
textContent: option.textContent,
|
|
517
|
+
},
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
button.append(icon, label);
|
|
521
|
+
element.append(button);
|
|
522
|
+
|
|
523
|
+
return {
|
|
524
|
+
button,
|
|
525
|
+
element,
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export function createComboboxSelectionItems(parameters: CreateSelectionItemsParameters): void {
|
|
530
|
+
const {container, items, options, state} = parameters;
|
|
531
|
+
|
|
532
|
+
const {length} = options;
|
|
533
|
+
|
|
534
|
+
for (let index = 0; index < length; index += 1) {
|
|
535
|
+
const option = options[index];
|
|
536
|
+
|
|
537
|
+
const item = createComboboxSelectionItem(option, state);
|
|
538
|
+
|
|
539
|
+
container.append(item.element);
|
|
540
|
+
items.push(item);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
export function setComboboxDisabled(state: OuiComboboxState, value: boolean): void {
|
|
545
|
+
state.filter.element.readOnly = state.readonly || value;
|
|
546
|
+
|
|
547
|
+
setProperties('disabled', state, value);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function setProperties(
|
|
551
|
+
key: 'disabled' | 'readonly' | 'required',
|
|
552
|
+
state: OuiComboboxState,
|
|
553
|
+
value: boolean,
|
|
554
|
+
): void {
|
|
555
|
+
state[key] = value;
|
|
556
|
+
|
|
557
|
+
const elements = [state.host, state.anchor.element, state.filter.element];
|
|
558
|
+
const {length} = elements;
|
|
559
|
+
|
|
560
|
+
for (let index = 0; index < length; index += 1) {
|
|
561
|
+
const element = elements[index];
|
|
562
|
+
|
|
563
|
+
setAria(
|
|
564
|
+
element,
|
|
565
|
+
key,
|
|
566
|
+
key !== 'readonly' || !(element instanceof HTMLButtonElement) ? value : undefined,
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
export function setComboboxReadonly(state: OuiComboboxState, value: boolean): void {
|
|
572
|
+
state.filter.element.readOnly = state.disabled || value;
|
|
573
|
+
|
|
574
|
+
setProperties('readonly', state, value);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
export function setComboboxRequired(state: OuiComboboxState, value: boolean): void {
|
|
578
|
+
setProperties('required', state, value);
|
|
579
|
+
updateComboboxValidity(state);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
export function updateComboboxTabindices(state: OuiComboboxState, active?: HTMLElement): void {
|
|
583
|
+
if (active == null || state.selection == null) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const next = state.selection.items.find(
|
|
588
|
+
item => getData(item.button, 'option', false) === active.id,
|
|
589
|
+
);
|
|
590
|
+
|
|
591
|
+
if (next == null) {
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const {length} = state.selection.items;
|
|
596
|
+
|
|
597
|
+
for (let index = 0; index < length; index += 1) {
|
|
598
|
+
const item = state.selection.items[index];
|
|
599
|
+
|
|
600
|
+
item.button.tabIndex = item === next ? 0 : -1;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
next.button.focus();
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
export function updateComboboxAnchor(state: OuiComboboxState): void {
|
|
607
|
+
if (!state.multiple) {
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const localization = getLocalization(
|
|
612
|
+
comboboxLocalizations,
|
|
613
|
+
state.host,
|
|
614
|
+
'selected',
|
|
615
|
+
state.localizations,
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
const {length} = state.options.selected;
|
|
619
|
+
|
|
620
|
+
setAria(state.anchor.element, 'label', localize(localization, {amount: length}, [length]));
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
export function updateComboboxAutocomplete(
|
|
624
|
+
preview: OuiComboboxPreview,
|
|
625
|
+
state: OuiComboboxState,
|
|
626
|
+
active?: HTMLElement,
|
|
627
|
+
): void {
|
|
628
|
+
if (state.multiple) {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
const content = active?.textContent ?? state.filter.element.value;
|
|
633
|
+
|
|
634
|
+
if (
|
|
635
|
+
preview === 'typed' &&
|
|
636
|
+
!content.toLocaleLowerCase().startsWith(state.filter.element.value.toLocaleLowerCase())
|
|
637
|
+
) {
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
if (state.anchor.element instanceof HTMLInputElement) {
|
|
642
|
+
const contentLength = content.length;
|
|
643
|
+
const filterLength = state.filter.element.value.length;
|
|
644
|
+
|
|
645
|
+
if (preview === 'set' || preview === 'typed') {
|
|
646
|
+
state.anchor.element.value = content;
|
|
647
|
+
|
|
648
|
+
state.anchor.element.setSelectionRange(
|
|
649
|
+
preview === 'typed' ? filterLength : contentLength,
|
|
650
|
+
contentLength,
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
} else {
|
|
654
|
+
state.anchor.element.textContent = content;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
export function updateComboboxMessage(state: OuiComboboxState, visible: number): void {
|
|
659
|
+
setAttribute(state.message, COMBOBOX_ATTRIBUTE_HIDDEN, visible > 0 ? '' : undefined);
|
|
660
|
+
|
|
661
|
+
if (visible === 0) {
|
|
662
|
+
const localization = getLocalization(
|
|
663
|
+
comboboxLocalizations,
|
|
664
|
+
state.host,
|
|
665
|
+
'empty',
|
|
666
|
+
state.localizations,
|
|
667
|
+
);
|
|
668
|
+
|
|
669
|
+
state.message.textContent = localize(localization, {}, []);
|
|
670
|
+
} else {
|
|
671
|
+
const localization = getLocalization(
|
|
672
|
+
comboboxLocalizations,
|
|
673
|
+
state.host,
|
|
674
|
+
'results',
|
|
675
|
+
state.localizations,
|
|
676
|
+
);
|
|
677
|
+
|
|
678
|
+
state.message.textContent = localize(localization, {amount: visible}, [visible]);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
state.message.hidden = false;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
export function updateComboboxPreview(
|
|
685
|
+
preview: OuiComboboxPreview,
|
|
686
|
+
state: OuiComboboxState,
|
|
687
|
+
active?: HTMLElement,
|
|
688
|
+
): void {
|
|
689
|
+
const {length} = state.options.all;
|
|
690
|
+
|
|
691
|
+
for (let index = 0; index < length; index += 1) {
|
|
692
|
+
const option = state.options.all[index];
|
|
693
|
+
|
|
694
|
+
setAttribute(option, COMBOBOX_ATTRIBUTE_OPTION_ACTIVE, option === active ? '' : undefined);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
state.active = active;
|
|
698
|
+
|
|
699
|
+
setAria(state.filter.element, 'activedescendant', state.floatable.open ? active?.id : undefined);
|
|
700
|
+
|
|
701
|
+
if (state.floatable.open) {
|
|
702
|
+
active?.scrollIntoView(comboboxScrollOptions);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if (preview !== 'none') {
|
|
706
|
+
updateComboboxAutocomplete(preview, state, active);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
export function updateComboboxSelection(
|
|
711
|
+
state: OuiComboboxState,
|
|
712
|
+
active: HTMLElement | undefined,
|
|
713
|
+
added: boolean,
|
|
714
|
+
selected?: HTMLElement[],
|
|
715
|
+
): void {
|
|
716
|
+
if (state.selection == null) {
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
let options = active == null ? [] : [active];
|
|
721
|
+
|
|
722
|
+
if (added && selected != null) {
|
|
723
|
+
options = selected;
|
|
724
|
+
|
|
725
|
+
const {length} = state.selection.items;
|
|
726
|
+
|
|
727
|
+
for (let index = 0; index < length; index += 1) {
|
|
728
|
+
state.selection.items[index].element.remove();
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
state.selection.items.splice(0);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (added) {
|
|
735
|
+
createComboboxSelectionItems({
|
|
736
|
+
options,
|
|
737
|
+
state,
|
|
738
|
+
container: state.selection.container,
|
|
739
|
+
items: state.selection.items,
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
let current = state.selection.items.findIndex(
|
|
746
|
+
item => getData(item.button, 'option', false) === active?.id,
|
|
747
|
+
);
|
|
748
|
+
|
|
749
|
+
if (current === -1) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
const item = state.selection.items[current];
|
|
754
|
+
|
|
755
|
+
item.element.remove();
|
|
756
|
+
|
|
757
|
+
state.selection.items.splice(current, 1);
|
|
758
|
+
|
|
759
|
+
if (state.selection.items.length === 0) {
|
|
760
|
+
state.anchor.element.focus();
|
|
761
|
+
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
const {length} = state.selection.items;
|
|
766
|
+
|
|
767
|
+
if (current < 0) {
|
|
768
|
+
current = 0;
|
|
769
|
+
} else if (current >= length) {
|
|
770
|
+
current = length - 1;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
for (let index = 0; index < length; index += 1) {
|
|
774
|
+
const {button} = state.selection.items[index];
|
|
775
|
+
|
|
776
|
+
button.tabIndex = index === current ? 0 : -1;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
state.selection.items[current].button.focus();
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
export function updateComboboxSelectionItems(state: OuiComboboxState): void {
|
|
783
|
+
if (state.selection == null) {
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
const localization = getLocalization(
|
|
788
|
+
comboboxLocalizations,
|
|
789
|
+
state.host,
|
|
790
|
+
'remove',
|
|
791
|
+
state.localizations,
|
|
792
|
+
);
|
|
793
|
+
|
|
794
|
+
const {length} = state.selection.items;
|
|
795
|
+
|
|
796
|
+
for (let index = 0; index < length; index += 1) {
|
|
797
|
+
const item = state.selection.items[index];
|
|
798
|
+
|
|
799
|
+
const option = state.options.selected.find(
|
|
800
|
+
option => option.id === getData(item.button, 'option', false),
|
|
801
|
+
);
|
|
802
|
+
|
|
803
|
+
if (option == null) {
|
|
804
|
+
continue;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
const optionLabel = option.textContent;
|
|
808
|
+
const optionValue = getData(option, 'value', false) ?? '';
|
|
809
|
+
|
|
810
|
+
setAria(
|
|
811
|
+
item.button,
|
|
812
|
+
'label',
|
|
813
|
+
localize(
|
|
814
|
+
localization,
|
|
815
|
+
{
|
|
816
|
+
label: optionLabel,
|
|
817
|
+
value: optionValue,
|
|
818
|
+
},
|
|
819
|
+
[optionLabel, optionValue],
|
|
820
|
+
),
|
|
821
|
+
);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// #endregion
|