@splendidlabz/svelte 4.15.3 → 4.17.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/README.md +44 -0
- package/components/Form/Combobox/Autocomplete.svelte +208 -0
- package/components/Form/Combobox/AutocompleteOptions.svelte +99 -14
- package/components/Form/Combobox/Chips.svelte +58 -0
- package/components/Form/Combobox/ComboboxOptions.svelte +44 -1
- package/components/Form/Combobox/Select.svelte +195 -0
- package/components/Form/Combobox/SelectOptions.svelte +57 -0
- package/components/Form/Combobox/autocomplete.svelte.js +517 -96
- package/components/Form/Combobox/combobox.svelte.js +395 -49
- package/components/Form/Combobox/index.js +6 -2
- package/components/Form/Combobox/listbox.js +119 -22
- package/components/Form/Combobox/match.js +405 -0
- package/components/Form/Combobox/matching.js +83 -0
- package/components/Form/Combobox/select.svelte.js +533 -0
- package/components/Form/FieldMeta.svelte +50 -0
- package/components/Form/Honeypot.svelte +3 -1
- package/components/Form/{Select.svelte → NativeSelect.svelte} +20 -15
- package/components/Form/SearchInput.svelte +6 -0
- package/components/Form/TextInput.svelte +42 -22
- package/components/Form/Textarea.svelte +22 -34
- package/components/Form/index.js +1 -1
- package/package.json +3 -4
- package/components/Form/Combobox/AutocompleteInput.svelte +0 -26
- package/components/Form/Combobox/ComboboxInput.svelte +0 -37
- package/components/Form/Combobox/autocomplete-matching.js +0 -69
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Holds what a select or autocomplete has selected.
|
|
3
|
+
*
|
|
4
|
+
* The selected options live in `state.selected` as `{ value, label }`, which is what the chips render and what the hidden inputs submit. The DOM says the same thing through `aria-selected`, and both are written in the same pass.
|
|
5
|
+
*/
|
|
6
|
+
import { omitEmpty } from '@splendidlabz/utils'
|
|
7
|
+
import { addListeners, removeListeners } from '@splendidlabz/utils/dom'
|
|
8
|
+
import {
|
|
9
|
+
closeCombobox,
|
|
10
|
+
commitSelection,
|
|
11
|
+
highlight,
|
|
12
|
+
initCombobox,
|
|
13
|
+
isComboboxOpen,
|
|
14
|
+
openCombobox,
|
|
15
|
+
stopClosingCombobox,
|
|
16
|
+
} from './combobox.svelte.js'
|
|
17
|
+
import {
|
|
18
|
+
getAdjacentOption,
|
|
19
|
+
getAllOptions,
|
|
20
|
+
getHighlightedOption,
|
|
21
|
+
getOptionStartingWith,
|
|
22
|
+
getVisibleOptions,
|
|
23
|
+
} from './listbox.js'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @import { Combobox } from './combobox.svelte.js'
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
// ========================================
|
|
30
|
+
// Actions
|
|
31
|
+
// ========================================
|
|
32
|
+
/**
|
|
33
|
+
* selectTrigger
|
|
34
|
+
* Lets a button open its options, select them, and jump to one by typing its first letters.
|
|
35
|
+
*
|
|
36
|
+
* `comboboxInput` runs first on the same button. A key it already handled arrives prevented and is left alone, so an Enter that selected an option doesn't open the list again.
|
|
37
|
+
*
|
|
38
|
+
* @see https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/
|
|
39
|
+
*
|
|
40
|
+
* @param {HTMLButtonElement} node
|
|
41
|
+
* @param {object} [props]
|
|
42
|
+
* @param {object} [props.options]
|
|
43
|
+
* @param {(item: any) => void} [props.options.onselect] Receives the item, or the option element
|
|
44
|
+
* @param {boolean} [props.options.multiple] Let more than one option be selected
|
|
45
|
+
* @param {number} [props.options.max] Most options a multi select takes
|
|
46
|
+
* @param {SelectEntry | SelectEntry[] | null} [props.options.selected] Selection to start with
|
|
47
|
+
*/
|
|
48
|
+
export function selectTrigger(node, props = {}) {
|
|
49
|
+
const id = /** @type {string} */ (node.getAttribute('target'))
|
|
50
|
+
let onselect = props.options?.onselect
|
|
51
|
+
|
|
52
|
+
const combobox = initSelection({
|
|
53
|
+
id,
|
|
54
|
+
options: props.options,
|
|
55
|
+
onselect: selectItem,
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
let typedText = ''
|
|
59
|
+
/** @type {ReturnType<typeof setTimeout> | undefined} */
|
|
60
|
+
let typingTimer
|
|
61
|
+
|
|
62
|
+
// On the box, not the button: chips share the field with the button, so a gesture that starts or ends on one still belongs to the field.
|
|
63
|
+
const box = node.closest('.input-group') ?? node
|
|
64
|
+
|
|
65
|
+
const listeners = [
|
|
66
|
+
{ event: 'keydown', node, handler: onKeydown },
|
|
67
|
+
{ event: 'keyup', node, handler: preventSpaceClick },
|
|
68
|
+
{ event: 'pointerdown', node: box, handler: onPointerdown },
|
|
69
|
+
{ event: 'pointerup', node: box, handler: onPointerup },
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
addListeners(listeners)
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
/**
|
|
76
|
+
* update
|
|
77
|
+
* Takes the button's new options.
|
|
78
|
+
*
|
|
79
|
+
* @param {typeof props} [nextProps]
|
|
80
|
+
*/
|
|
81
|
+
update(nextProps = {}) {
|
|
82
|
+
onselect = nextProps.options?.onselect
|
|
83
|
+
Object.assign(combobox.options, omitEmpty(nextProps.options), {
|
|
84
|
+
onselect: selectItem,
|
|
85
|
+
})
|
|
86
|
+
},
|
|
87
|
+
destroy() {
|
|
88
|
+
removeListeners(listeners)
|
|
89
|
+
clearTimeout(typingTimer)
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* selectItem
|
|
95
|
+
* Records the selection, then hands the item back to whoever owns it.
|
|
96
|
+
*
|
|
97
|
+
* @param {HTMLElement} option
|
|
98
|
+
*/
|
|
99
|
+
function selectItem(option) {
|
|
100
|
+
toggleSelected(combobox, getOptionData(option))
|
|
101
|
+
sendItem({ combobox, option, onselect })
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* onKeydown
|
|
106
|
+
* Opens the list and selects from it, jumps to either end, and jumps to typed text.
|
|
107
|
+
*
|
|
108
|
+
* @param {KeyboardEvent} event
|
|
109
|
+
*/
|
|
110
|
+
function onKeydown(event) {
|
|
111
|
+
const { listbox } = combobox.nodes
|
|
112
|
+
if (event.defaultPrevented || !listbox) return
|
|
113
|
+
|
|
114
|
+
const { key } = event
|
|
115
|
+
const isOpen = isComboboxOpen(combobox)
|
|
116
|
+
|
|
117
|
+
// A space typed right after other characters is part of the typed text
|
|
118
|
+
if (key === ' ' && typedText) return jumpToTypedText(event)
|
|
119
|
+
|
|
120
|
+
if (key === 'Enter' || key === ' ') {
|
|
121
|
+
event.preventDefault()
|
|
122
|
+
if (!isOpen) return openList()
|
|
123
|
+
|
|
124
|
+
const option = getHighlightedOption(listbox)
|
|
125
|
+
if (option) commitSelection(combobox, option)
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if ((key === 'Home' || key === 'End') && isOpen) {
|
|
130
|
+
event.preventDefault()
|
|
131
|
+
const options = getVisibleOptions(listbox)
|
|
132
|
+
highlight(combobox, options[key === 'Home' ? 0 : options.length - 1], {
|
|
133
|
+
behaviour: 'instant',
|
|
134
|
+
})
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const isCharacter =
|
|
139
|
+
key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey
|
|
140
|
+
if (isCharacter) jumpToTypedText(event)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* jumpToTypedText
|
|
145
|
+
* Opens the list and highlights the option starting with what was typed in the last 500ms. The same character again moves on to the next option starting with it.
|
|
146
|
+
*
|
|
147
|
+
* @param {KeyboardEvent} event
|
|
148
|
+
*/
|
|
149
|
+
function jumpToTypedText(event) {
|
|
150
|
+
event.preventDefault()
|
|
151
|
+
|
|
152
|
+
clearTimeout(typingTimer)
|
|
153
|
+
typingTimer = setTimeout(() => (typedText = ''), 500)
|
|
154
|
+
typedText += event.key
|
|
155
|
+
|
|
156
|
+
// Typed text means the options are wanted, so it also ends a close still fading
|
|
157
|
+
stopClosingCombobox(combobox)
|
|
158
|
+
if (!isComboboxOpen(combobox)) openList()
|
|
159
|
+
|
|
160
|
+
const { listbox } = combobox.nodes
|
|
161
|
+
const isRepeat = [...typedText].every(char => char === typedText[0])
|
|
162
|
+
const option = isRepeat
|
|
163
|
+
? getOptionStartingWith({
|
|
164
|
+
listbox,
|
|
165
|
+
text: typedText[0],
|
|
166
|
+
from: getAdjacentOption({ listbox, direction: 'next', loop: true }),
|
|
167
|
+
})
|
|
168
|
+
: getOptionStartingWith({
|
|
169
|
+
listbox,
|
|
170
|
+
text: typedText,
|
|
171
|
+
from: getHighlightedOption(listbox),
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
if (option) highlight(combobox, option, { behaviour: 'smooth' })
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* openList
|
|
179
|
+
* Opens the options, with the selected option highlighted.
|
|
180
|
+
*/
|
|
181
|
+
function openList() {
|
|
182
|
+
openCombobox(combobox)
|
|
183
|
+
|
|
184
|
+
const [firstSelected] = combobox.state.selected
|
|
185
|
+
if (!firstSelected) return
|
|
186
|
+
|
|
187
|
+
const option = getOptionByValue({ combobox, value: firstSelected.value })
|
|
188
|
+
if (option) highlight(combobox, option, { behaviour: 'instant' })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* onPointerdown
|
|
193
|
+
* Opens or closes the options the moment a mouse presses the box, the way a native select does.
|
|
194
|
+
*
|
|
195
|
+
* @param {PointerEvent} event
|
|
196
|
+
*/
|
|
197
|
+
function onPointerdown(event) {
|
|
198
|
+
if (event.pointerType !== 'mouse') return
|
|
199
|
+
|
|
200
|
+
// Safari takes focus off a pressed button, and that focusout closes the list before the gesture finishes
|
|
201
|
+
event.preventDefault()
|
|
202
|
+
toggleList(event)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* onPointerup
|
|
207
|
+
* Opens or closes the options on a tap. A pen or a finger has to lift before it counts, so a scroll that starts on the box ends in `pointercancel` and leaves the list alone.
|
|
208
|
+
*
|
|
209
|
+
* @param {PointerEvent} event
|
|
210
|
+
*/
|
|
211
|
+
function onPointerup(event) {
|
|
212
|
+
if (event.pointerType === 'mouse') return
|
|
213
|
+
toggleList(event)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* toggleList
|
|
218
|
+
* Opens or closes the options.
|
|
219
|
+
*
|
|
220
|
+
* @param {PointerEvent} event
|
|
221
|
+
*/
|
|
222
|
+
function toggleList(event) {
|
|
223
|
+
// A chip's remove button sits in the box and answers its own press
|
|
224
|
+
if (
|
|
225
|
+
/** @type {HTMLElement} */ (event.target).closest('.chip-remove-button')
|
|
226
|
+
)
|
|
227
|
+
return
|
|
228
|
+
|
|
229
|
+
// Safari doesn't focus a pressed button, and Escape and clicking away need focus
|
|
230
|
+
node.focus()
|
|
231
|
+
|
|
232
|
+
// A press on a list still fading closes it rather than bringing it back
|
|
233
|
+
if (combobox.state.phase === 'closed') openList()
|
|
234
|
+
else closeCombobox(combobox)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* preventSpaceClick
|
|
239
|
+
* Stops Space from clicking the button once keydown has used it.
|
|
240
|
+
*
|
|
241
|
+
* @param {KeyboardEvent} event
|
|
242
|
+
*/
|
|
243
|
+
function preventSpaceClick(event) {
|
|
244
|
+
if (event.key === ' ') event.preventDefault()
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* selectOptions
|
|
250
|
+
* Registers the items the options were rendered from, so selecting one can return the original.
|
|
251
|
+
*
|
|
252
|
+
* @param {HTMLElement} node Wrapper holding the listbox
|
|
253
|
+
* @param {object} [props]
|
|
254
|
+
* @param {any[]} [props.items]
|
|
255
|
+
*/
|
|
256
|
+
export function selectOptions(node, props = {}) {
|
|
257
|
+
const combobox = initCombobox({
|
|
258
|
+
id: node.id,
|
|
259
|
+
state: { items: props.items },
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
applySelection(combobox)
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
/**
|
|
266
|
+
* update
|
|
267
|
+
* Takes the new items, and marks the options holding a selected value.
|
|
268
|
+
*
|
|
269
|
+
* @param {typeof props} [nextProps]
|
|
270
|
+
*/
|
|
271
|
+
update(nextProps = {}) {
|
|
272
|
+
combobox.state.items = nextProps.items
|
|
273
|
+
applySelection(combobox)
|
|
274
|
+
},
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// ========================================
|
|
279
|
+
// Methods
|
|
280
|
+
// ========================================
|
|
281
|
+
/**
|
|
282
|
+
* initSelection
|
|
283
|
+
* Registers the selection a select or autocomplete starts with, and the options its input part was given.
|
|
284
|
+
*
|
|
285
|
+
* @param {object} opts
|
|
286
|
+
* @param {string} opts.id Id of the options wrapper
|
|
287
|
+
* @param {object} [opts.state]
|
|
288
|
+
* @param {Record<string, any>} [opts.options] Options for the input part
|
|
289
|
+
* @param {Record<string, any>} [opts.defaults] Defaults for those options
|
|
290
|
+
* @param {(option: HTMLElement) => void} opts.onselect Receives the chosen option
|
|
291
|
+
* @returns {Combobox}
|
|
292
|
+
*/
|
|
293
|
+
export function initSelection({ id, state, options, defaults, onselect }) {
|
|
294
|
+
const combobox = initCombobox({
|
|
295
|
+
id,
|
|
296
|
+
state,
|
|
297
|
+
options: { ...defaults, ...omitEmpty(options), onselect },
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
// An empty array doesn't survive the merge into a registered part, so it's written here
|
|
301
|
+
combobox.state.selected ??= toSelectedOptions(options?.selected)
|
|
302
|
+
|
|
303
|
+
// The options can register first, before the selection was known
|
|
304
|
+
applySelection(combobox)
|
|
305
|
+
|
|
306
|
+
// The binding gets the selection in the shape of the mode from the start
|
|
307
|
+
sendSelected(combobox)
|
|
308
|
+
return combobox
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* getValueAndLabel
|
|
313
|
+
* Reads an item or a selected entry. A bare string is both value and label, and a missing value falls back to the label.
|
|
314
|
+
*
|
|
315
|
+
* @typedef {{ value: string, label: string }} SelectedOption
|
|
316
|
+
* @typedef {string | { value?: string, label: string }} SelectEntry An item, or a selected option passed in.
|
|
317
|
+
*
|
|
318
|
+
* @param {SelectEntry} entry
|
|
319
|
+
* @returns {SelectedOption}
|
|
320
|
+
*/
|
|
321
|
+
export function getValueAndLabel(entry) {
|
|
322
|
+
if (typeof entry === 'string') return { value: entry, label: entry }
|
|
323
|
+
return { value: entry.value ?? entry.label, label: entry.label }
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* toSelectedOptions
|
|
328
|
+
* Reads the `selected` prop into a list: one entry for a single select, an array for a multi select.
|
|
329
|
+
*
|
|
330
|
+
* @param {SelectEntry | SelectEntry[] | null} [selected]
|
|
331
|
+
* @returns {SelectedOption[]}
|
|
332
|
+
*/
|
|
333
|
+
export function toSelectedOptions(selected) {
|
|
334
|
+
if (!selected) return []
|
|
335
|
+
if (!Array.isArray(selected)) return [getValueAndLabel(selected)]
|
|
336
|
+
return selected.map(getValueAndLabel)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* getOptionData
|
|
341
|
+
* Reads the value and label an option carries.
|
|
342
|
+
*
|
|
343
|
+
* @param {HTMLElement} option
|
|
344
|
+
* @returns {SelectedOption}
|
|
345
|
+
*/
|
|
346
|
+
export function getOptionData(option) {
|
|
347
|
+
const { value, label } = /** @type {{ value: string, label?: string }} */ (
|
|
348
|
+
option.dataset
|
|
349
|
+
)
|
|
350
|
+
return { value, label: label ?? value ?? '' }
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* sendItem
|
|
355
|
+
* Hands the chosen item to the caller's `onselect`: the original item when options came from `items`, or else the option element.
|
|
356
|
+
*
|
|
357
|
+
* @param {object} opts
|
|
358
|
+
* @param {Combobox} opts.combobox
|
|
359
|
+
* @param {HTMLElement} opts.option
|
|
360
|
+
* @param {(item: any) => void} [opts.onselect]
|
|
361
|
+
* @returns {void}
|
|
362
|
+
*/
|
|
363
|
+
export function sendItem({ combobox, option, onselect }) {
|
|
364
|
+
if (!onselect) return
|
|
365
|
+
|
|
366
|
+
const { index } = option.dataset
|
|
367
|
+
if (index === undefined) return onselect(option)
|
|
368
|
+
onselect($state.snapshot(combobox.state.items[Number(index)]))
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* canSelectOption
|
|
373
|
+
* Reads whether an option can still be chosen. A full multi select refuses a new option, and still lets a chosen one be taken off.
|
|
374
|
+
*
|
|
375
|
+
* @param {Combobox} combobox
|
|
376
|
+
* @param {HTMLElement} option
|
|
377
|
+
* @returns {boolean}
|
|
378
|
+
*/
|
|
379
|
+
export function canSelectOption(combobox, option) {
|
|
380
|
+
const { max, multiple } = combobox.options
|
|
381
|
+
if (!multiple || !max) return true
|
|
382
|
+
if (isSelected({ combobox, value: getOptionData(option).value })) return true
|
|
383
|
+
return combobox.state.selected.length < max
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* toggleSelected
|
|
388
|
+
* Selects an option. In a multi select, an option already selected is deselected instead.
|
|
389
|
+
*
|
|
390
|
+
* @param {Combobox} combobox
|
|
391
|
+
* @param {SelectedOption} selectedOption
|
|
392
|
+
* @returns {void}
|
|
393
|
+
*/
|
|
394
|
+
export function toggleSelected(combobox, selectedOption) {
|
|
395
|
+
const { value } = selectedOption
|
|
396
|
+
if (combobox.options.multiple && isSelected({ combobox, value }))
|
|
397
|
+
return deselect({ combobox, value })
|
|
398
|
+
addSelected(combobox, selectedOption)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* addSelected
|
|
403
|
+
* Adds a selected option. A single select holds one, so the new one replaces it.
|
|
404
|
+
*
|
|
405
|
+
* @param {Combobox} combobox
|
|
406
|
+
* @param {SelectedOption} selectedOption
|
|
407
|
+
* @returns {void}
|
|
408
|
+
*/
|
|
409
|
+
export function addSelected(combobox, selectedOption) {
|
|
410
|
+
if (combobox.options.multiple) combobox.state.selected.push(selectedOption)
|
|
411
|
+
else combobox.state.selected = [selectedOption]
|
|
412
|
+
applySelection(combobox)
|
|
413
|
+
sendSelected(combobox)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* deselect
|
|
418
|
+
* Takes a value off the selection, and off the option holding it.
|
|
419
|
+
*
|
|
420
|
+
* @param {object} opts
|
|
421
|
+
* @param {Combobox} opts.combobox
|
|
422
|
+
* @param {string} opts.value
|
|
423
|
+
* @returns {void}
|
|
424
|
+
*/
|
|
425
|
+
export function deselect({ combobox, value }) {
|
|
426
|
+
const index = combobox.state.selected.findIndex(
|
|
427
|
+
/** @param {SelectedOption} selectedOption */
|
|
428
|
+
selectedOption =>
|
|
429
|
+
isSameValue({ combobox, one: selectedOption.value, other: value }),
|
|
430
|
+
)
|
|
431
|
+
if (index === -1) return
|
|
432
|
+
|
|
433
|
+
combobox.state.selected.splice(index, 1)
|
|
434
|
+
getOptionByValue({ combobox, value })?.setAttribute('aria-selected', 'false')
|
|
435
|
+
applySelection(combobox)
|
|
436
|
+
sendSelected(combobox)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* isSelected
|
|
441
|
+
* Reads whether a value is already selected.
|
|
442
|
+
*
|
|
443
|
+
* @param {object} opts
|
|
444
|
+
* @param {Combobox} opts.combobox
|
|
445
|
+
* @param {string} opts.value
|
|
446
|
+
* @returns {boolean}
|
|
447
|
+
*/
|
|
448
|
+
export function isSelected({ combobox, value }) {
|
|
449
|
+
return combobox.state.selected.some(
|
|
450
|
+
/** @param {SelectedOption} selectedOption */
|
|
451
|
+
selectedOption =>
|
|
452
|
+
isSameValue({ combobox, one: selectedOption.value, other: value }),
|
|
453
|
+
)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* getOptionByValue
|
|
458
|
+
* Gets the option holding a value.
|
|
459
|
+
*
|
|
460
|
+
* @param {object} opts
|
|
461
|
+
* @param {Combobox} opts.combobox
|
|
462
|
+
* @param {string} opts.value
|
|
463
|
+
* @returns {HTMLElement | null}
|
|
464
|
+
*/
|
|
465
|
+
export function getOptionByValue({ combobox, value }) {
|
|
466
|
+
const options = getAllOptions(combobox.nodes.listbox)
|
|
467
|
+
return (
|
|
468
|
+
options.find(option =>
|
|
469
|
+
isSameValue({ combobox, one: option.dataset.value, other: value }),
|
|
470
|
+
) ?? null
|
|
471
|
+
)
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* sendSelected
|
|
476
|
+
* Hands the selection to `onselectedchange`: one option or `null` for a single select, an array for a multi select.
|
|
477
|
+
*
|
|
478
|
+
* @param {Combobox} combobox
|
|
479
|
+
* @returns {void}
|
|
480
|
+
*/
|
|
481
|
+
function sendSelected(combobox) {
|
|
482
|
+
const copies = combobox.state.selected.map(
|
|
483
|
+
/** @param {SelectedOption} option */ option => ({ ...option }),
|
|
484
|
+
)
|
|
485
|
+
combobox.options.onselectedchange?.(
|
|
486
|
+
combobox.options.multiple ? copies : (copies[0] ?? null),
|
|
487
|
+
)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* applySelection
|
|
492
|
+
* Writes the selection onto the options: which ones are selected, and which ones a full selection refuses.
|
|
493
|
+
*
|
|
494
|
+
* @param {Combobox} combobox
|
|
495
|
+
* @returns {void}
|
|
496
|
+
*/
|
|
497
|
+
function applySelection(combobox) {
|
|
498
|
+
const { listbox } = combobox.nodes
|
|
499
|
+
if (!listbox || !combobox.state.selected) return
|
|
500
|
+
|
|
501
|
+
for (const selectedOption of combobox.state.selected) {
|
|
502
|
+
getOptionByValue({ combobox, value: selectedOption.value })?.setAttribute(
|
|
503
|
+
'aria-selected',
|
|
504
|
+
'true',
|
|
505
|
+
)
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const { max, multiple } = combobox.options
|
|
509
|
+
if (!multiple || !max) return
|
|
510
|
+
|
|
511
|
+
const isFull = combobox.state.selected.length >= max
|
|
512
|
+
for (const option of getAllOptions(listbox)) {
|
|
513
|
+
const isRefused = isFull && option.getAttribute('aria-selected') !== 'true'
|
|
514
|
+
if (isRefused) option.setAttribute('aria-disabled', 'true')
|
|
515
|
+
else option.removeAttribute('aria-disabled')
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* isSameValue
|
|
521
|
+
* Compares two values the way this select matches text.
|
|
522
|
+
*
|
|
523
|
+
* @param {object} opts
|
|
524
|
+
* @param {Combobox} opts.combobox
|
|
525
|
+
* @param {string} [opts.one]
|
|
526
|
+
* @param {string} [opts.other]
|
|
527
|
+
* @returns {boolean}
|
|
528
|
+
*/
|
|
529
|
+
function isSameValue({ combobox, one, other }) {
|
|
530
|
+
if (one === undefined || other === undefined) return false
|
|
531
|
+
if (combobox.options.caseSensitive) return one.trim() === other.trim()
|
|
532
|
+
return one.trim().toLowerCase() === other.trim().toLowerCase()
|
|
533
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import Markdown from '../Markdown/Markdown.svelte'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {object} FieldMetaProps
|
|
6
|
+
* @property {string} id Id of the field
|
|
7
|
+
* @property {string} [label]
|
|
8
|
+
* @property {boolean} [labelVisible]
|
|
9
|
+
* @property {string | null} [hint]
|
|
10
|
+
* @property {string | null} [error]
|
|
11
|
+
* @property {string} [labelClass]
|
|
12
|
+
* @property {string} [hintClass]
|
|
13
|
+
* @property {string} [errorClass]
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** @type {FieldMetaProps} */
|
|
17
|
+
let {
|
|
18
|
+
id,
|
|
19
|
+
label = '',
|
|
20
|
+
labelVisible = true,
|
|
21
|
+
hint = null,
|
|
22
|
+
error = null,
|
|
23
|
+
labelClass,
|
|
24
|
+
hintClass,
|
|
25
|
+
errorClass,
|
|
26
|
+
} = $props()
|
|
27
|
+
</script>
|
|
28
|
+
|
|
29
|
+
<!-- input-meta is deprecated in favour of field-meta, and goes in the next major -->
|
|
30
|
+
{#if label && labelVisible}
|
|
31
|
+
<label class="label field-meta input-meta {labelClass}" for={id}>
|
|
32
|
+
<Markdown content={label} inline />
|
|
33
|
+
</label>
|
|
34
|
+
{/if}
|
|
35
|
+
{#if hint}
|
|
36
|
+
<div id="{id}-hint" class="hint field-meta input-meta {hintClass}">
|
|
37
|
+
<Markdown content={hint} inline />
|
|
38
|
+
</div>
|
|
39
|
+
{/if}
|
|
40
|
+
<output
|
|
41
|
+
id="{id}-error"
|
|
42
|
+
class="error field-meta input-meta {errorClass}"
|
|
43
|
+
class:sr-only={!error}
|
|
44
|
+
for={id}
|
|
45
|
+
role="alert"
|
|
46
|
+
>
|
|
47
|
+
{#if error}
|
|
48
|
+
<Markdown content={error} inline />
|
|
49
|
+
{/if}
|
|
50
|
+
</output>
|
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
} = $props()
|
|
10
10
|
</script>
|
|
11
11
|
|
|
12
|
-
<!-- `autocomplete="off"` keeps the browser from filling a field named after a real one, which would reject a real person as a bot. -->
|
|
12
|
+
<!-- `autocomplete="off"` keeps the browser from filling a field named after a real one, which would reject a real person as a bot. `tabindex` and `aria-hidden` keep keyboard and screen reader users out of the field. -->
|
|
13
13
|
<TextInput
|
|
14
14
|
{...rest}
|
|
15
15
|
{name}
|
|
16
16
|
class="form-field-helper {klass}"
|
|
17
17
|
autocomplete="off"
|
|
18
|
+
tabindex="-1"
|
|
19
|
+
aria-hidden="true"
|
|
18
20
|
bind:value
|
|
19
21
|
/>
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { randomString } from '@splendidlabz/utils/dom'
|
|
4
4
|
import { init } from '../../utils'
|
|
5
5
|
import Icon from '../Icon/Icon.svelte'
|
|
6
|
+
import FieldMeta from './FieldMeta.svelte'
|
|
6
7
|
import HiddenInput from './HiddenInput.svelte'
|
|
7
8
|
|
|
8
9
|
let {
|
|
@@ -19,6 +20,8 @@
|
|
|
19
20
|
labelVisible = true,
|
|
20
21
|
hint = null,
|
|
21
22
|
hintClass,
|
|
23
|
+
error = null,
|
|
24
|
+
errorClass,
|
|
22
25
|
fieldClass = null,
|
|
23
26
|
id = randomString(8),
|
|
24
27
|
required = null,
|
|
@@ -31,8 +34,10 @@
|
|
|
31
34
|
} = $props()
|
|
32
35
|
|
|
33
36
|
const isSingle = $derived(!multiple)
|
|
37
|
+
// IDs for aria-describedby
|
|
34
38
|
const hintId = $derived(hint ? id + '-hint' : '')
|
|
35
|
-
const
|
|
39
|
+
const errorId = $derived(id + '-error')
|
|
40
|
+
const describedBy = $derived(`${hintId} ${errorId}`.trim() || null)
|
|
36
41
|
|
|
37
42
|
let name = $state('')
|
|
38
43
|
|
|
@@ -88,7 +93,9 @@
|
|
|
88
93
|
name,
|
|
89
94
|
class: 'input ' + fieldClass,
|
|
90
95
|
disabled: disabled || readonly,
|
|
91
|
-
'aria-describedby':
|
|
96
|
+
'aria-describedby': describedBy,
|
|
97
|
+
'aria-invalid': error ? true : null,
|
|
98
|
+
'aria-label': !labelVisible && label ? label : null,
|
|
92
99
|
onchange: handleChange,
|
|
93
100
|
required,
|
|
94
101
|
size,
|
|
@@ -114,13 +121,7 @@
|
|
|
114
121
|
|
|
115
122
|
<!-- Svelte requires multiple to be passed as a standard attribute for it to bind to an array value, hence the duplication here -->
|
|
116
123
|
{#snippet multiselect()}
|
|
117
|
-
<select
|
|
118
|
-
{...properties}
|
|
119
|
-
bind:this={ref}
|
|
120
|
-
aria-describedby={hintDescription}
|
|
121
|
-
multiple
|
|
122
|
-
bind:value={selected}
|
|
123
|
-
>
|
|
124
|
+
<select {...properties} bind:this={ref} multiple bind:value={selected}>
|
|
124
125
|
{#each items as option (option.value)}
|
|
125
126
|
{@const { label: optionLabel, value, ...optionRest } = option}
|
|
126
127
|
<option {...optionRest} {value}>{optionLabel}</option>
|
|
@@ -145,12 +146,16 @@
|
|
|
145
146
|
{/snippet}
|
|
146
147
|
|
|
147
148
|
<div class="input-container select {klass}">
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
149
|
+
<FieldMeta
|
|
150
|
+
{id}
|
|
151
|
+
{error}
|
|
152
|
+
{errorClass}
|
|
153
|
+
{hint}
|
|
154
|
+
{hintClass}
|
|
155
|
+
{label}
|
|
156
|
+
{labelClass}
|
|
157
|
+
{labelVisible}
|
|
158
|
+
/>
|
|
154
159
|
|
|
155
160
|
<div class="input-group items-center stack" class:disabled class:readonly>
|
|
156
161
|
<!-- Single Select -->
|