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