@bwui/vue 0.1.14 → 0.1.15
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 +3 -0
- package/dist/chrome.d.ts +183 -0
- package/dist/chrome.js +287 -0
- package/dist/combobox.d.ts +223 -8
- package/dist/combobox.js +571 -85
- package/dist/date-picker.d.ts +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +2 -1
- package/dist/pages.js +10 -4
- package/dist/primitives.d.ts +174 -15
- package/dist/primitives.js +178 -15
- package/package.json +10 -7
- package/src/chrome.ts +385 -0
- package/src/combobox.ts +625 -61
- package/src/index.ts +33 -1
- package/src/pages.ts +10 -4
- package/src/primitives.ts +278 -32
package/dist/combobox.js
CHANGED
|
@@ -1,119 +1,605 @@
|
|
|
1
|
-
import { computed, defineComponent, h, onMounted, onUnmounted, ref } from 'vue';
|
|
1
|
+
import { computed, defineComponent, h, nextTick, onMounted, onUnmounted, ref, useId, watch, } from 'vue';
|
|
2
2
|
import { cn } from './cn';
|
|
3
|
+
function defaultResultsLabel(count) {
|
|
4
|
+
return count === 1 ? '1 result available' : `${count} results available`;
|
|
5
|
+
}
|
|
6
|
+
function CloseIcon() {
|
|
7
|
+
return h('svg', {
|
|
8
|
+
class: 'bw-icon',
|
|
9
|
+
viewBox: '0 0 24 24',
|
|
10
|
+
fill: 'none',
|
|
11
|
+
stroke: 'currentColor',
|
|
12
|
+
'stroke-width': '2',
|
|
13
|
+
'stroke-linecap': 'round',
|
|
14
|
+
'stroke-linejoin': 'round',
|
|
15
|
+
'aria-hidden': 'true',
|
|
16
|
+
}, [h('path', { d: 'M18 6 6 18' }), h('path', { d: 'm6 6 12 12' })]);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Combobox (docs/components/combobox.md, GAP-CMB-18). Mirrors the React Early
|
|
20
|
+
* surface: single and multiple, size, groups, clear, readonly, invalid, client
|
|
21
|
+
* and server filter hooks, allow-create. The floor blank option uses
|
|
22
|
+
* `blankLabel` (falling back to `placeholder`); no hard-coded English
|
|
23
|
+
* "Choose" (CMB-072, CB-14). English string props remain overridable defaults,
|
|
24
|
+
* matching React.
|
|
25
|
+
*/
|
|
3
26
|
export const Combobox = defineComponent({
|
|
4
27
|
name: 'BwCombobox',
|
|
28
|
+
inheritAttrs: false,
|
|
5
29
|
props: {
|
|
6
30
|
id: String,
|
|
7
31
|
name: String,
|
|
8
32
|
options: { type: Array, required: true },
|
|
9
|
-
modelValue: { type: String, default:
|
|
33
|
+
modelValue: { type: [String, Array], default: undefined },
|
|
34
|
+
defaultValue: { type: [String, Array], default: undefined },
|
|
35
|
+
multiple: { type: Boolean, default: false },
|
|
10
36
|
placeholder: { type: String, default: 'Search' },
|
|
37
|
+
/**
|
|
38
|
+
* Text of the blank option on the hidden floor select (CMB-072). Never shown
|
|
39
|
+
* to a user; defaults to `placeholder` so no English originates here. Single
|
|
40
|
+
* mode only.
|
|
41
|
+
*/
|
|
42
|
+
blankLabel: { type: String, default: undefined },
|
|
11
43
|
disabled: Boolean,
|
|
44
|
+
readOnly: Boolean,
|
|
12
45
|
required: Boolean,
|
|
46
|
+
invalid: Boolean,
|
|
47
|
+
size: { type: String, default: 'md' },
|
|
48
|
+
filterMode: { type: String, default: 'client' },
|
|
49
|
+
debounceMs: { type: Number, default: undefined },
|
|
50
|
+
loading: Boolean,
|
|
51
|
+
error: Boolean,
|
|
52
|
+
allowCreate: Boolean,
|
|
13
53
|
emptyLabel: { type: String, default: 'No matches' },
|
|
54
|
+
errorLabel: {
|
|
55
|
+
type: String,
|
|
56
|
+
default: 'Options could not be loaded. Type to try again.',
|
|
57
|
+
},
|
|
58
|
+
createLabel: { type: String, default: 'Create' },
|
|
59
|
+
clearLabel: { type: String, default: 'Clear selection' },
|
|
60
|
+
clearAllLabel: { type: String, default: 'Clear all' },
|
|
61
|
+
removeLabel: { type: String, default: 'Remove' },
|
|
62
|
+
loadingLabel: { type: String, default: 'Loading options' },
|
|
63
|
+
resultsLabel: {
|
|
64
|
+
type: Function,
|
|
65
|
+
default: defaultResultsLabel,
|
|
66
|
+
},
|
|
67
|
+
addedLabel: { type: String, default: '{label} added' },
|
|
68
|
+
removedLabel: { type: String, default: '{label} removed' },
|
|
69
|
+
clearedLabel: { type: String, default: 'Selection cleared' },
|
|
70
|
+
ariaLabel: { type: String, default: undefined },
|
|
71
|
+
ariaLabelledby: { type: String, default: undefined },
|
|
72
|
+
ariaDescribedby: { type: String, default: undefined },
|
|
73
|
+
},
|
|
74
|
+
emits: {
|
|
75
|
+
'update:modelValue': (_value) => true,
|
|
76
|
+
queryChange: (_query) => true,
|
|
77
|
+
create: (_query) => true,
|
|
14
78
|
},
|
|
15
|
-
emits: ['update:modelValue'],
|
|
16
79
|
setup(props, { emit, attrs }) {
|
|
80
|
+
const listboxId = useId();
|
|
17
81
|
const open = ref(false);
|
|
18
82
|
const query = ref('');
|
|
19
|
-
const
|
|
83
|
+
const typed = ref(false);
|
|
84
|
+
const active = ref(-1);
|
|
85
|
+
const held = ref(false);
|
|
86
|
+
const status = ref('');
|
|
87
|
+
const labelledBy = ref(undefined);
|
|
20
88
|
const root = ref(null);
|
|
21
|
-
const
|
|
89
|
+
const floor = ref(null);
|
|
90
|
+
const inputEl = ref(null);
|
|
91
|
+
const labelCache = ref(new Map());
|
|
92
|
+
const debounceTimer = ref(null);
|
|
93
|
+
const initial = props.defaultValue;
|
|
94
|
+
const uncontrolled = ref(Array.isArray(initial) ? initial : initial ? [initial] : []);
|
|
95
|
+
const controlled = computed(() => props.modelValue !== undefined);
|
|
96
|
+
const selected = computed(() => {
|
|
97
|
+
if (controlled.value) {
|
|
98
|
+
const v = props.modelValue;
|
|
99
|
+
if (Array.isArray(v))
|
|
100
|
+
return v;
|
|
101
|
+
return v ? [v] : [];
|
|
102
|
+
}
|
|
103
|
+
return uncontrolled.value;
|
|
104
|
+
});
|
|
105
|
+
const selectedKey = computed(() => JSON.stringify(selected.value));
|
|
106
|
+
const blank = computed(() => props.blankLabel ?? props.placeholder);
|
|
107
|
+
const operable = computed(() => !props.disabled && !props.readOnly);
|
|
108
|
+
const committedLabel = computed(() => !props.multiple && selected.value.length
|
|
109
|
+
? labelOf(selected.value[0])
|
|
110
|
+
: '');
|
|
111
|
+
function labelOf(value) {
|
|
112
|
+
return labelCache.value.get(value) ?? value;
|
|
113
|
+
}
|
|
114
|
+
watch(() => props.options, (opts) => {
|
|
115
|
+
const next = new Map(labelCache.value);
|
|
116
|
+
for (const opt of opts)
|
|
117
|
+
next.set(opt.value, opt.label);
|
|
118
|
+
labelCache.value = next;
|
|
119
|
+
}, { immediate: true, deep: true });
|
|
120
|
+
const floorOptions = computed(() => {
|
|
121
|
+
const present = new Set(props.options.map((o) => o.value));
|
|
122
|
+
const extra = selected.value
|
|
123
|
+
.filter((v) => !present.has(v))
|
|
124
|
+
.map((v) => ({ value: v, label: labelCache.value.get(v) ?? v }));
|
|
125
|
+
return [...props.options, ...extra];
|
|
126
|
+
});
|
|
22
127
|
const filtered = computed(() => {
|
|
23
|
-
const q = query.value.trim().toLowerCase();
|
|
128
|
+
const q = typed.value && props.filterMode === 'client' ? query.value.trim().toLowerCase() : '';
|
|
24
129
|
if (!q)
|
|
25
130
|
return props.options;
|
|
26
|
-
return props.options.filter((
|
|
131
|
+
return props.options.filter((opt) => opt.label.toLowerCase().includes(q));
|
|
132
|
+
});
|
|
133
|
+
const createVisible = computed(() => Boolean(props.allowCreate) && typed.value && query.value.trim() !== '');
|
|
134
|
+
const rows = computed(() => {
|
|
135
|
+
const list = filtered.value.map((opt) => ({ kind: 'option', opt }));
|
|
136
|
+
if (createVisible.value)
|
|
137
|
+
list.push({ kind: 'create' });
|
|
138
|
+
return list;
|
|
27
139
|
});
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
140
|
+
function rowId(index) {
|
|
141
|
+
return `${listboxId}-row-${index}`;
|
|
142
|
+
}
|
|
143
|
+
function isDisabledRow(row) {
|
|
144
|
+
return row?.kind === 'option' && Boolean(row.opt.disabled);
|
|
145
|
+
}
|
|
146
|
+
function nextEnabled(from, step) {
|
|
147
|
+
const count = rows.value.length;
|
|
148
|
+
let index = from;
|
|
149
|
+
for (let tried = 0; tried < count; tried += 1) {
|
|
150
|
+
index = (((index + step) % count) + count) % count;
|
|
151
|
+
if (!isDisabledRow(rows.value[index]))
|
|
152
|
+
return index;
|
|
153
|
+
}
|
|
154
|
+
return -1;
|
|
155
|
+
}
|
|
156
|
+
function hide() {
|
|
157
|
+
open.value = false;
|
|
158
|
+
active.value = -1;
|
|
159
|
+
typed.value = false;
|
|
160
|
+
held.value = false;
|
|
161
|
+
}
|
|
162
|
+
function announceText(text) {
|
|
163
|
+
status.value = text;
|
|
164
|
+
}
|
|
165
|
+
function announceResults(nextQuery) {
|
|
166
|
+
const q = nextQuery.trim().toLowerCase();
|
|
167
|
+
const count = q && props.filterMode === 'client'
|
|
168
|
+
? props.options.filter((o) => o.label.toLowerCase().includes(q)).length
|
|
169
|
+
: props.options.length;
|
|
170
|
+
announceText(count === 0 ? props.emptyLabel : props.resultsLabel(count));
|
|
171
|
+
}
|
|
172
|
+
function scheduleQuery(next) {
|
|
173
|
+
if (debounceTimer.value)
|
|
174
|
+
clearTimeout(debounceTimer.value);
|
|
175
|
+
let wait = props.debounceMs;
|
|
176
|
+
if (wait === undefined) {
|
|
177
|
+
const raw = root.value
|
|
178
|
+
? getComputedStyle(root.value).getPropertyValue('--bw-debounce-search').trim()
|
|
179
|
+
: '';
|
|
180
|
+
const parsed = parseFloat(raw);
|
|
181
|
+
wait = Number.isFinite(parsed)
|
|
182
|
+
? raw.endsWith('ms')
|
|
183
|
+
? parsed
|
|
184
|
+
: raw.endsWith('s')
|
|
185
|
+
? parsed * 1000
|
|
186
|
+
: parsed
|
|
187
|
+
: 300;
|
|
188
|
+
}
|
|
189
|
+
debounceTimer.value = setTimeout(() => {
|
|
190
|
+
debounceTimer.value = null;
|
|
191
|
+
emit('queryChange', next);
|
|
192
|
+
}, wait);
|
|
193
|
+
}
|
|
194
|
+
function emitSelection(next) {
|
|
195
|
+
if (JSON.stringify(next) === selectedKey.value)
|
|
196
|
+
return;
|
|
197
|
+
if (!controlled.value)
|
|
198
|
+
uncontrolled.value = next;
|
|
199
|
+
emit('update:modelValue', props.multiple ? next : (next[0] ?? ''));
|
|
200
|
+
nextTick(() => {
|
|
201
|
+
floor.value?.dispatchEvent(new Event('change', { bubbles: true }));
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
function commitSingle(next) {
|
|
205
|
+
emitSelection(next === '' ? [] : [next]);
|
|
206
|
+
query.value = next === '' ? '' : labelOf(next);
|
|
207
|
+
hide();
|
|
208
|
+
}
|
|
209
|
+
function toggleMultiple(value) {
|
|
210
|
+
const label = labelOf(value);
|
|
211
|
+
if (selected.value.includes(value)) {
|
|
212
|
+
emitSelection(selected.value.filter((v) => v !== value));
|
|
213
|
+
announceText(props.removedLabel.replace('{label}', label));
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
emitSelection([...selected.value, value]);
|
|
217
|
+
announceText(props.addedLabel.replace('{label}', label));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function activate(row) {
|
|
221
|
+
if (!row)
|
|
222
|
+
return;
|
|
223
|
+
if (row.kind === 'create') {
|
|
224
|
+
emit('create', query.value.trim());
|
|
225
|
+
if (!props.multiple)
|
|
226
|
+
hide();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (row.opt.disabled)
|
|
230
|
+
return;
|
|
231
|
+
if (props.multiple)
|
|
232
|
+
toggleMultiple(row.opt.value);
|
|
233
|
+
else
|
|
234
|
+
commitSingle(row.opt.value);
|
|
235
|
+
}
|
|
236
|
+
function removeChip(value) {
|
|
237
|
+
if (!operable.value)
|
|
238
|
+
return;
|
|
239
|
+
toggleMultiple(value);
|
|
240
|
+
inputEl.value?.focus();
|
|
241
|
+
}
|
|
242
|
+
function clearAll() {
|
|
243
|
+
if (!operable.value)
|
|
244
|
+
return;
|
|
245
|
+
emitSelection([]);
|
|
246
|
+
query.value = '';
|
|
247
|
+
typed.value = false;
|
|
248
|
+
held.value = false;
|
|
249
|
+
active.value = -1;
|
|
31
250
|
open.value = false;
|
|
251
|
+
scheduleQuery('');
|
|
252
|
+
announceText(props.clearedLabel);
|
|
253
|
+
inputEl.value?.focus();
|
|
32
254
|
}
|
|
255
|
+
watch(() => [props.multiple, open.value, held.value, committedLabel.value], () => {
|
|
256
|
+
if (props.multiple || open.value || held.value)
|
|
257
|
+
return;
|
|
258
|
+
query.value = committedLabel.value;
|
|
259
|
+
}, { immediate: true });
|
|
260
|
+
watch(() => props.loading, (loading) => {
|
|
261
|
+
if (loading)
|
|
262
|
+
announceText(props.loadingLabel);
|
|
263
|
+
});
|
|
264
|
+
watch(() => props.error, (err) => {
|
|
265
|
+
if (err)
|
|
266
|
+
announceText(props.errorLabel);
|
|
267
|
+
});
|
|
33
268
|
function onPointer(e) {
|
|
34
269
|
if (!root.value?.contains(e.target))
|
|
35
|
-
|
|
270
|
+
hide();
|
|
36
271
|
}
|
|
37
|
-
onMounted(() =>
|
|
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
|
-
commit(opt.value);
|
|
92
|
-
}
|
|
93
|
-
else if (e.key === 'Escape') {
|
|
94
|
-
open.value = false;
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
}),
|
|
98
|
-
h('ul', { class: 'bw-combobox__listbox', role: 'listbox' }, [
|
|
99
|
-
...filtered.value.map((opt, idx) => h('li', {
|
|
100
|
-
class: 'bw-combobox__option',
|
|
272
|
+
onMounted(() => {
|
|
273
|
+
document.addEventListener('pointerdown', onPointer);
|
|
274
|
+
if (props.ariaLabel || props.ariaLabelledby)
|
|
275
|
+
return;
|
|
276
|
+
const floorEl = floor.value;
|
|
277
|
+
if (!floorEl)
|
|
278
|
+
return;
|
|
279
|
+
const ids = Array.from(floorEl.labels ?? []).map((label, i) => {
|
|
280
|
+
if (!label.id)
|
|
281
|
+
label.id = `${floorEl.id || listboxId}-label-${i}`;
|
|
282
|
+
return label.id;
|
|
283
|
+
});
|
|
284
|
+
labelledBy.value = ids.length ? ids.join(' ') : undefined;
|
|
285
|
+
});
|
|
286
|
+
onUnmounted(() => {
|
|
287
|
+
document.removeEventListener('pointerdown', onPointer);
|
|
288
|
+
if (debounceTimer.value)
|
|
289
|
+
clearTimeout(debounceTimer.value);
|
|
290
|
+
});
|
|
291
|
+
return () => {
|
|
292
|
+
const inputValue = props.multiple
|
|
293
|
+
? query.value
|
|
294
|
+
: open.value || held.value
|
|
295
|
+
? query.value
|
|
296
|
+
: committedLabel.value || query.value;
|
|
297
|
+
const showClear = operable.value && (selected.value.length > 0 || inputValue !== '');
|
|
298
|
+
const activeRow = active.value >= 0 ? rows.value[active.value] : undefined;
|
|
299
|
+
// Groups: adjacent options sharing a `group` render under one heading.
|
|
300
|
+
const rendered = [];
|
|
301
|
+
let position = 0;
|
|
302
|
+
let groupLabel;
|
|
303
|
+
let groupItems = [];
|
|
304
|
+
let groupIndex = 0;
|
|
305
|
+
const flushGroup = () => {
|
|
306
|
+
if (groupLabel !== undefined && groupItems.length) {
|
|
307
|
+
const labelId = `${listboxId}-group-${groupIndex}`;
|
|
308
|
+
rendered.push(h('li', { key: `group-${groupIndex}`, class: 'bw-combobox__group', role: 'presentation' }, [
|
|
309
|
+
h('span', { class: 'bw-combobox__group-label', id: labelId }, groupLabel),
|
|
310
|
+
h('ul', { class: 'bw-combobox__group-options', role: 'group', 'aria-labelledby': labelId }, groupItems),
|
|
311
|
+
]));
|
|
312
|
+
groupIndex += 1;
|
|
313
|
+
}
|
|
314
|
+
groupItems = [];
|
|
315
|
+
};
|
|
316
|
+
for (const row of rows.value) {
|
|
317
|
+
const index = position;
|
|
318
|
+
position += 1;
|
|
319
|
+
if (row.kind === 'create') {
|
|
320
|
+
flushGroup();
|
|
321
|
+
groupLabel = undefined;
|
|
322
|
+
rendered.push(h('li', {
|
|
323
|
+
key: 'create',
|
|
324
|
+
id: rowId(index),
|
|
325
|
+
class: 'bw-combobox__option bw-combobox__option--create',
|
|
101
326
|
role: 'option',
|
|
102
|
-
'
|
|
103
|
-
'
|
|
104
|
-
|
|
327
|
+
'aria-selected': false,
|
|
328
|
+
...(index === active.value ? { 'data-bw-active': '' } : {}),
|
|
329
|
+
'data-bw-combobox-create': '',
|
|
105
330
|
onMousemove: () => {
|
|
106
|
-
active.value =
|
|
331
|
+
active.value = index;
|
|
107
332
|
},
|
|
108
333
|
onMousedown: (e) => {
|
|
109
334
|
e.preventDefault();
|
|
110
|
-
|
|
111
|
-
|
|
335
|
+
activate(row);
|
|
336
|
+
},
|
|
337
|
+
}, [
|
|
338
|
+
`${props.createLabel} `,
|
|
339
|
+
h('span', { class: 'bw-combobox__create-query' }, query.value.trim()),
|
|
340
|
+
]));
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const opt = row.opt;
|
|
344
|
+
const item = h('li', {
|
|
345
|
+
key: opt.value,
|
|
346
|
+
id: rowId(index),
|
|
347
|
+
class: 'bw-combobox__option',
|
|
348
|
+
role: 'option',
|
|
349
|
+
'data-bw-value': opt.value,
|
|
350
|
+
'aria-selected': selected.value.includes(opt.value),
|
|
351
|
+
'aria-disabled': opt.disabled || undefined,
|
|
352
|
+
...(index === active.value ? { 'data-bw-active': '' } : {}),
|
|
353
|
+
onMousemove: () => {
|
|
354
|
+
if (!opt.disabled)
|
|
355
|
+
active.value = index;
|
|
356
|
+
},
|
|
357
|
+
onMousedown: (e) => {
|
|
358
|
+
e.preventDefault();
|
|
359
|
+
if (!opt.disabled)
|
|
360
|
+
activate(row);
|
|
361
|
+
},
|
|
362
|
+
}, opt.label);
|
|
363
|
+
if (opt.group !== undefined) {
|
|
364
|
+
if (groupLabel !== opt.group) {
|
|
365
|
+
flushGroup();
|
|
366
|
+
groupLabel = opt.group;
|
|
367
|
+
}
|
|
368
|
+
groupItems.push(item);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
flushGroup();
|
|
372
|
+
groupLabel = undefined;
|
|
373
|
+
rendered.push(item);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
flushGroup();
|
|
377
|
+
// Floor optgroups mirror the option groups for the no-script control.
|
|
378
|
+
const floorNodes = [];
|
|
379
|
+
{
|
|
380
|
+
let current;
|
|
381
|
+
let bucket = [];
|
|
382
|
+
const flush = () => {
|
|
383
|
+
if (current !== undefined && bucket.length) {
|
|
384
|
+
floorNodes.push(h('optgroup', { key: `og-${current}`, label: current }, bucket));
|
|
385
|
+
}
|
|
386
|
+
bucket = [];
|
|
387
|
+
};
|
|
388
|
+
for (const opt of floorOptions.value) {
|
|
389
|
+
const node = h('option', {
|
|
390
|
+
key: opt.value,
|
|
391
|
+
value: opt.value,
|
|
392
|
+
disabled: opt.disabled,
|
|
393
|
+
selected: selected.value.includes(opt.value) || undefined,
|
|
394
|
+
}, opt.label);
|
|
395
|
+
if (opt.group !== undefined) {
|
|
396
|
+
if (current !== opt.group) {
|
|
397
|
+
flush();
|
|
398
|
+
current = opt.group;
|
|
399
|
+
}
|
|
400
|
+
bucket.push(node);
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
flush();
|
|
404
|
+
current = undefined;
|
|
405
|
+
floorNodes.push(node);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
flush();
|
|
409
|
+
}
|
|
410
|
+
const { class: cls, ...restAttrs } = attrs;
|
|
411
|
+
return h('div', {
|
|
412
|
+
ref: root,
|
|
413
|
+
class: cn('bw-combobox', `bw-combobox--size-${props.size}`, props.multiple && 'bw-combobox--multiple', String(cls || '')),
|
|
414
|
+
'data-bw-combobox': '',
|
|
415
|
+
...(open.value ? { 'data-bw-open': '' } : {}),
|
|
416
|
+
...restAttrs,
|
|
417
|
+
}, [
|
|
418
|
+
h('select', {
|
|
419
|
+
ref: floor,
|
|
420
|
+
class: 'bw-input bw-combobox__floor',
|
|
421
|
+
id: props.id,
|
|
422
|
+
name: props.name,
|
|
423
|
+
multiple: props.multiple || undefined,
|
|
424
|
+
value: props.multiple ? selected.value : (selected.value[0] ?? ''),
|
|
425
|
+
disabled: props.disabled || props.readOnly || undefined,
|
|
426
|
+
tabindex: -1,
|
|
427
|
+
'aria-hidden': 'true',
|
|
428
|
+
'aria-invalid': props.invalid || undefined,
|
|
429
|
+
'aria-readonly': props.readOnly || undefined,
|
|
430
|
+
hidden: true,
|
|
431
|
+
onChange: (e) => {
|
|
432
|
+
const next = Array.from(e.target.selectedOptions)
|
|
433
|
+
.map((o) => o.value)
|
|
434
|
+
.filter((v) => v !== '');
|
|
435
|
+
if (JSON.stringify(next) !== selectedKey.value)
|
|
436
|
+
emitSelection(next);
|
|
437
|
+
},
|
|
438
|
+
}, [
|
|
439
|
+
!props.multiple ? h('option', { value: '' }, blank.value) : null,
|
|
440
|
+
...floorNodes,
|
|
441
|
+
]),
|
|
442
|
+
props.readOnly && props.name
|
|
443
|
+
? selected.value.map((value) => h('input', { key: value, type: 'hidden', name: props.name, value }))
|
|
444
|
+
: null,
|
|
445
|
+
h('div', { class: 'bw-combobox__field' }, [
|
|
446
|
+
props.multiple
|
|
447
|
+
? selected.value.map((value) => h('span', { key: value, class: 'bw-combobox__chip', 'data-bw-value': value }, [
|
|
448
|
+
labelOf(value),
|
|
449
|
+
h('button', {
|
|
450
|
+
type: 'button',
|
|
451
|
+
class: 'bw-combobox__chip-remove',
|
|
452
|
+
'aria-label': `${props.removeLabel} ${labelOf(value)}`,
|
|
453
|
+
onMousedown: (e) => e.preventDefault(),
|
|
454
|
+
onClick: () => removeChip(value),
|
|
455
|
+
}, [CloseIcon()]),
|
|
456
|
+
]))
|
|
457
|
+
: null,
|
|
458
|
+
h('input', {
|
|
459
|
+
ref: inputEl,
|
|
460
|
+
class: 'bw-input bw-combobox__input',
|
|
461
|
+
role: 'combobox',
|
|
462
|
+
'aria-expanded': open.value,
|
|
463
|
+
'aria-controls': listboxId,
|
|
464
|
+
'aria-autocomplete': 'list',
|
|
465
|
+
'aria-activedescendant': open.value && activeRow ? rowId(active.value) : undefined,
|
|
466
|
+
'aria-label': props.ariaLabel,
|
|
467
|
+
'aria-labelledby': props.ariaLabelledby ?? labelledBy.value,
|
|
468
|
+
'aria-describedby': props.ariaDescribedby,
|
|
469
|
+
'aria-required': props.required || undefined,
|
|
470
|
+
'aria-invalid': props.invalid || undefined,
|
|
471
|
+
'aria-readonly': props.readOnly || undefined,
|
|
472
|
+
autocomplete: 'off',
|
|
473
|
+
disabled: props.disabled || undefined,
|
|
474
|
+
readonly: props.readOnly || undefined,
|
|
475
|
+
placeholder: props.placeholder,
|
|
476
|
+
value: inputValue,
|
|
477
|
+
onClick: () => {
|
|
478
|
+
if (operable.value)
|
|
479
|
+
open.value = true;
|
|
480
|
+
},
|
|
481
|
+
onBlur: hide,
|
|
482
|
+
onInput: (e) => {
|
|
483
|
+
if (!operable.value)
|
|
484
|
+
return;
|
|
485
|
+
const next = e.target.value;
|
|
486
|
+
query.value = next;
|
|
487
|
+
typed.value = true;
|
|
488
|
+
held.value = false;
|
|
489
|
+
open.value = true;
|
|
490
|
+
active.value = -1;
|
|
491
|
+
announceResults(next);
|
|
492
|
+
scheduleQuery(next);
|
|
493
|
+
},
|
|
494
|
+
onKeydown: (e) => {
|
|
495
|
+
if (!operable.value && e.key !== 'Tab')
|
|
496
|
+
return;
|
|
497
|
+
if (e.key === 'ArrowDown') {
|
|
498
|
+
e.preventDefault();
|
|
499
|
+
if (!open.value) {
|
|
500
|
+
open.value = true;
|
|
501
|
+
if (e.altKey)
|
|
502
|
+
return;
|
|
503
|
+
const selectedAt = rows.value.findIndex((r) => r.kind === 'option' && selected.value.includes(r.opt.value));
|
|
504
|
+
active.value =
|
|
505
|
+
selectedAt >= 0 && !isDisabledRow(rows.value[selectedAt])
|
|
506
|
+
? selectedAt
|
|
507
|
+
: nextEnabled(-1, 1);
|
|
508
|
+
}
|
|
509
|
+
else if (!e.altKey) {
|
|
510
|
+
const next = nextEnabled(active.value, 1);
|
|
511
|
+
if (next !== -1)
|
|
512
|
+
active.value = next;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
else if (e.key === 'ArrowUp') {
|
|
516
|
+
e.preventDefault();
|
|
517
|
+
if (!open.value) {
|
|
518
|
+
open.value = true;
|
|
519
|
+
active.value = nextEnabled(0, -1);
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
const next = nextEnabled(active.value === -1 ? 0 : active.value, -1);
|
|
523
|
+
if (next !== -1)
|
|
524
|
+
active.value = next;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
else if (e.key === 'Enter') {
|
|
528
|
+
if (open.value && activeRow) {
|
|
529
|
+
e.preventDefault();
|
|
530
|
+
activate(activeRow);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
else if (e.key === 'Escape') {
|
|
534
|
+
if (open.value) {
|
|
535
|
+
e.preventDefault();
|
|
536
|
+
open.value = false;
|
|
537
|
+
active.value = -1;
|
|
538
|
+
if (!props.multiple)
|
|
539
|
+
held.value = true;
|
|
540
|
+
}
|
|
541
|
+
else if (!props.multiple && held.value) {
|
|
542
|
+
e.preventDefault();
|
|
543
|
+
held.value = false;
|
|
544
|
+
}
|
|
545
|
+
else if (props.multiple && query.value !== '') {
|
|
546
|
+
e.preventDefault();
|
|
547
|
+
query.value = '';
|
|
548
|
+
typed.value = false;
|
|
549
|
+
scheduleQuery('');
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
else if (e.key === 'Tab') {
|
|
553
|
+
hide();
|
|
554
|
+
}
|
|
555
|
+
else if (e.key === 'Backspace') {
|
|
556
|
+
if (props.multiple && query.value === '' && selected.value.length) {
|
|
557
|
+
e.preventDefault();
|
|
558
|
+
toggleMultiple(selected.value[selected.value.length - 1]);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
112
561
|
},
|
|
113
|
-
}
|
|
114
|
-
|
|
562
|
+
}),
|
|
563
|
+
showClear
|
|
564
|
+
? h('button', {
|
|
565
|
+
type: 'button',
|
|
566
|
+
class: 'bw-combobox__clear',
|
|
567
|
+
'aria-label': props.multiple ? props.clearAllLabel : props.clearLabel,
|
|
568
|
+
onMousedown: (e) => e.preventDefault(),
|
|
569
|
+
onClick: clearAll,
|
|
570
|
+
}, [CloseIcon()])
|
|
571
|
+
: null,
|
|
572
|
+
h('ul', {
|
|
573
|
+
class: 'bw-combobox__listbox',
|
|
574
|
+
role: 'listbox',
|
|
575
|
+
id: listboxId,
|
|
576
|
+
'aria-multiselectable': props.multiple || undefined,
|
|
577
|
+
'aria-busy': props.loading || undefined,
|
|
578
|
+
onMousedown: (e) => e.preventDefault(),
|
|
579
|
+
}, [
|
|
580
|
+
...rendered,
|
|
581
|
+
h('li', {
|
|
582
|
+
class: 'bw-combobox__empty',
|
|
583
|
+
role: 'option',
|
|
584
|
+
'aria-disabled': 'true',
|
|
585
|
+
hidden: filtered.value.length > 0 || Boolean(props.error),
|
|
586
|
+
}, props.emptyLabel),
|
|
587
|
+
props.error
|
|
588
|
+
? h('li', {
|
|
589
|
+
class: 'bw-combobox__error',
|
|
590
|
+
role: 'option',
|
|
591
|
+
'aria-disabled': 'true',
|
|
592
|
+
}, props.errorLabel)
|
|
593
|
+
: null,
|
|
594
|
+
]),
|
|
595
|
+
h('div', {
|
|
596
|
+
class: 'bw-combobox__status bw-visually-hidden',
|
|
597
|
+
role: 'status',
|
|
598
|
+
'aria-live': 'polite',
|
|
599
|
+
'data-bw-combobox-status': '',
|
|
600
|
+
}, status.value),
|
|
115
601
|
]),
|
|
116
|
-
])
|
|
117
|
-
|
|
602
|
+
]);
|
|
603
|
+
};
|
|
118
604
|
},
|
|
119
605
|
});
|