@redseed/redseed-ui-vue3 8.29.1 → 8.30.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redseed/redseed-ui-vue3",
3
- "version": "8.29.1",
3
+ "version": "8.30.1",
4
4
  "description": "RedSeed UI Vue 3 components",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/redseedtraining/redseed-ui",
@@ -30,6 +30,7 @@ function check(event) {
30
30
  <FormFieldSlot
31
31
  :id="$attrs.id"
32
32
  :required="$attrs.required"
33
+ :showAsterisk="false"
33
34
  class="rsui-form-field-checkbox"
34
35
  >
35
36
  <template #label>
@@ -51,12 +52,14 @@ function check(event) {
51
52
  >
52
53
  </div>
53
54
  <div v-if="$slots.label"
54
- :class="[
55
- 'rsui-form-field-checkbox__label',
56
- { 'rsui-form-field-checkbox__label--required': $attrs.required !== undefined },
57
- ]"
55
+ class="rsui-form-field-checkbox__label"
58
56
  >
59
57
  <slot name="label"></slot>
58
+ <span
59
+ v-if="$attrs.required !== undefined && $attrs.required !== false"
60
+ aria-hidden="true"
61
+ class="rsui-form-field-slot__asterisk"
62
+ >*</span>
60
63
  </div>
61
64
  </div>
62
65
  </template>
@@ -573,7 +573,7 @@ defineExpose({
573
573
  {{ option.label }}
574
574
  </div>
575
575
  <div class="rsui-form-field-combobox__option-icon" aria-hidden="true">
576
- <CheckIcon v-if="option.value === model"></CheckIcon>
576
+ <CheckIcon v-if="!navigable && option.value === model"></CheckIcon>
577
577
  </div>
578
578
  </div>
579
579
  </template>
@@ -0,0 +1,484 @@
1
+ <script setup>
2
+ import { ref, computed, onMounted, onUnmounted, watch, useAttrs } from 'vue'
3
+ import { onClickOutside, useElementBounding } from '@vueuse/core'
4
+ import FormFieldSlot from './FormFieldSlot.vue'
5
+ import { MagnifyingGlassIcon } from '@heroicons/vue/24/outline'
6
+ import { useFormFieldA11y } from '../../composables/useFormFieldA11y.js'
7
+
8
+ defineOptions({
9
+ inheritAttrs: false,
10
+ })
11
+
12
+ const props = defineProps({
13
+ searchFunction: {
14
+ type: Function,
15
+ required: true,
16
+ },
17
+ debounceMs: {
18
+ type: Number,
19
+ default: 300,
20
+ },
21
+ minSearchLength: {
22
+ type: Number,
23
+ default: 2,
24
+ },
25
+ })
26
+
27
+ const emit = defineEmits(['input', 'query-change', 'navigate', 'keyup-enter'])
28
+
29
+ const attrs = useAttrs()
30
+ const { inputId, ariaDescribedby, ariaInvalid } = useFormFieldA11y()
31
+
32
+ const query = ref('')
33
+ const isOpen = ref(false)
34
+ const inputElement = ref(null)
35
+ const rootElement = ref(null)
36
+ const dropdownElement = ref(null)
37
+ const highlightedIndex = ref(-1)
38
+
39
+ const isLoading = ref(false)
40
+ const searchError = ref(null)
41
+ const results = ref([])
42
+ const debounceTimeout = ref(null)
43
+ let latestRequestId = 0
44
+
45
+ const effectiveId = computed(() => inputId.value || attrs.id)
46
+
47
+ const liveAnnouncement = computed(() => {
48
+ if (!isOpen.value) return ''
49
+ if (isLoading.value) return 'Searching'
50
+ if (searchError.value) return 'Search failed'
51
+ if (results.value.length > 0) {
52
+ return `${results.value.length} result${results.value.length === 1 ? '' : 's'} available`
53
+ }
54
+ if (query.value.length >= props.minSearchLength) return 'No results found'
55
+ return ''
56
+ })
57
+
58
+ const groupedResults = computed(() => {
59
+ const items = results.value
60
+ if (!items.some((r) => r.group)) return null
61
+
62
+ const groups = []
63
+ let currentGroup = null
64
+
65
+ items.forEach((result, flatIndex) => {
66
+ const groupName = result.group || ''
67
+ if (!currentGroup || currentGroup.name !== groupName) {
68
+ currentGroup = { name: groupName, items: [] }
69
+ groups.push(currentGroup)
70
+ }
71
+ currentGroup.items.push({ result, flatIndex })
72
+ })
73
+
74
+ return groups
75
+ })
76
+
77
+ const dropdownContent = computed(() => {
78
+ if (isLoading.value) return 'loading'
79
+ if (searchError.value) return 'error'
80
+ if (!query.value) return 'start-typing'
81
+ if (query.value.length < props.minSearchLength) return 'min-length'
82
+ if (results.value.length === 0) return 'no-results'
83
+ return 'results'
84
+ })
85
+
86
+ function open() {
87
+ isOpen.value = true
88
+ highlightedIndex.value = -1
89
+ setTimeout(() => {
90
+ inputElement.value?.focus()
91
+ calculateDropdownPosition()
92
+ }, 1)
93
+ }
94
+
95
+ function close() {
96
+ isOpen.value = false
97
+ highlightedIndex.value = -1
98
+
99
+ if (debounceTimeout.value) {
100
+ clearTimeout(debounceTimeout.value)
101
+ debounceTimeout.value = null
102
+ }
103
+ }
104
+
105
+ function clear() {
106
+ latestRequestId++
107
+ if (debounceTimeout.value) {
108
+ clearTimeout(debounceTimeout.value)
109
+ debounceTimeout.value = null
110
+ }
111
+ query.value = ''
112
+ results.value = []
113
+ searchError.value = null
114
+ isLoading.value = false
115
+ highlightedIndex.value = -1
116
+ emit('query-change', '')
117
+ }
118
+
119
+ function navigate(result) {
120
+ emit('navigate', result.value, result)
121
+ clear()
122
+ close()
123
+ }
124
+
125
+ async function performSearch(q) {
126
+ const requestId = ++latestRequestId
127
+ isLoading.value = true
128
+ searchError.value = null
129
+
130
+ try {
131
+ const response = await props.searchFunction(q)
132
+ if (requestId !== latestRequestId) return
133
+ results.value = Array.isArray(response) ? response : []
134
+ } catch (error) {
135
+ if (requestId !== latestRequestId) return
136
+ console.error('FormFieldSearchAsync search error:', error)
137
+ searchError.value = error
138
+ results.value = []
139
+ } finally {
140
+ if (requestId === latestRequestId) {
141
+ isLoading.value = false
142
+ }
143
+ }
144
+ }
145
+
146
+ function debouncedSearch(q) {
147
+ if (debounceTimeout.value) {
148
+ clearTimeout(debounceTimeout.value)
149
+ }
150
+
151
+ debounceTimeout.value = setTimeout(() => {
152
+ if (q.length >= props.minSearchLength) {
153
+ performSearch(q)
154
+ } else {
155
+ latestRequestId++
156
+ results.value = []
157
+ isLoading.value = false
158
+ }
159
+ }, props.debounceMs)
160
+ }
161
+
162
+ function handleInput(event) {
163
+ query.value = event.target.value
164
+ if (!isOpen.value) {
165
+ isOpen.value = true
166
+ setTimeout(() => calculateDropdownPosition(), 1)
167
+ }
168
+ debouncedSearch(query.value)
169
+ emit('query-change', query.value)
170
+ emit('input', event)
171
+ }
172
+
173
+ function handleKeyup(event) {
174
+ if (event.key === 'Enter') {
175
+ if (highlightedIndex.value >= 0 && highlightedIndex.value < results.value.length) {
176
+ navigate(results.value[highlightedIndex.value])
177
+ }
178
+ emit('keyup-enter', event)
179
+ } else if (event.key === 'Escape') {
180
+ if (query.value) {
181
+ clear()
182
+ } else {
183
+ close()
184
+ }
185
+ }
186
+ }
187
+
188
+ function handleKeydown(event) {
189
+ if (event.key === 'ArrowDown') {
190
+ event.preventDefault()
191
+ if (!isOpen.value) {
192
+ open()
193
+ } else if (results.value.length > 0) {
194
+ highlightedIndex.value = Math.min(
195
+ highlightedIndex.value + 1,
196
+ results.value.length - 1
197
+ )
198
+ }
199
+ } else if (event.key === 'ArrowUp') {
200
+ event.preventDefault()
201
+ if (isOpen.value && results.value.length > 0) {
202
+ highlightedIndex.value = Math.max(highlightedIndex.value - 1, 0)
203
+ }
204
+ } else if (event.key === 'Home' && isOpen.value && results.value.length > 0) {
205
+ event.preventDefault()
206
+ highlightedIndex.value = 0
207
+ } else if (event.key === 'End' && isOpen.value && results.value.length > 0) {
208
+ event.preventDefault()
209
+ highlightedIndex.value = results.value.length - 1
210
+ }
211
+ }
212
+
213
+ watch(results, () => {
214
+ highlightedIndex.value = -1
215
+ })
216
+
217
+ onClickOutside(rootElement, () => {
218
+ if (isOpen.value) close()
219
+ })
220
+
221
+ const inputElementBounding = useElementBounding(inputElement)
222
+
223
+ function calculateDropdownPosition() {
224
+ if (!dropdownElement.value) return
225
+
226
+ const viewportHeight = window.innerHeight
227
+ const dropdownHeight = dropdownElement.value.offsetHeight
228
+ const spaceAbove = inputElementBounding.top.value
229
+ const spaceBelow = viewportHeight - inputElementBounding.bottom.value
230
+
231
+ dropdownElement.value.style.width = `${inputElementBounding.width.value}px`
232
+ dropdownElement.value.style.left = `${inputElementBounding.left.value}px`
233
+
234
+ if (spaceAbove <= dropdownHeight && spaceBelow <= dropdownHeight) {
235
+ dropdownElement.value.style.top = '0'
236
+ dropdownElement.value.style.bottom = 'auto'
237
+ } else if (spaceBelow > dropdownHeight) {
238
+ dropdownElement.value.style.top = `${inputElementBounding.bottom.value + window.scrollY}px`
239
+ dropdownElement.value.style.bottom = 'auto'
240
+ } else if (spaceAbove > dropdownHeight) {
241
+ dropdownElement.value.style.top = 'auto'
242
+ dropdownElement.value.style.bottom = `${spaceBelow + inputElementBounding.height.value + 8 - window.scrollY}px`
243
+ }
244
+ }
245
+
246
+ onMounted(() => {
247
+ window.addEventListener('resize', calculateDropdownPosition)
248
+ })
249
+
250
+ onUnmounted(() => {
251
+ window.removeEventListener('resize', calculateDropdownPosition)
252
+ })
253
+
254
+ defineExpose({
255
+ focus() {
256
+ inputElement.value?.focus()
257
+ },
258
+ open,
259
+ close,
260
+ clear,
261
+ })
262
+ </script>
263
+
264
+ <template>
265
+ <FormFieldSlot
266
+ ref="rootElement"
267
+ :id="$attrs.id"
268
+ :class="[$attrs.class, 'rsui-form-field-search-async', 'rsui-form-field-search', 'rsui-form-field-text']"
269
+ :required="$attrs.required"
270
+ :showAsterisk="$attrs.showAsterisk"
271
+ :compact="$attrs.compact"
272
+ >
273
+ <template #label v-if="$slots.label">
274
+ <slot name="label"></slot>
275
+ </template>
276
+
277
+ <div class="rsui-form-field-text__group">
278
+ <div
279
+ class="rsui-form-field-text__prefix"
280
+ aria-hidden="true"
281
+ @click.prevent="inputElement?.focus()"
282
+ >
283
+ <MagnifyingGlassIcon class="rsui-form-field-search__icon"></MagnifyingGlassIcon>
284
+ </div>
285
+
286
+ <input
287
+ ref="inputElement"
288
+ role="combobox"
289
+ class="rsui-form-field-search"
290
+ :aria-activedescendant="isOpen && dropdownContent === 'results' && highlightedIndex >= 0 ? `${effectiveId}-result-${highlightedIndex}` : undefined"
291
+ :aria-autocomplete="'list'"
292
+ :aria-busy="isLoading || undefined"
293
+ :aria-controls="`${effectiveId}-listbox`"
294
+ :aria-describedby="ariaDescribedby"
295
+ :aria-expanded="isOpen"
296
+ :aria-haspopup="'listbox'"
297
+ :aria-invalid="ariaInvalid"
298
+ :aria-required="$attrs.required || undefined"
299
+ :value="query"
300
+ :autocomplete="$attrs.autocomplete || 'off'"
301
+ :autofocus="$attrs.autofocus"
302
+ :disabled="$attrs.disabled"
303
+ :id="effectiveId"
304
+ :name="$attrs.name"
305
+ :placeholder="$attrs.placeholder || 'Search...'"
306
+ :required="$attrs.required"
307
+ type="search"
308
+ @input="handleInput"
309
+ @keyup="handleKeyup"
310
+ @keydown="handleKeydown"
311
+ @click="!isOpen && open()"
312
+ >
313
+
314
+ <Teleport to="body">
315
+ <transition
316
+ enter-active-class="enter-active-class"
317
+ enter-from-class="enter-from-class"
318
+ enter-to-class="enter-to-class"
319
+ leave-active-class="leave-active-class"
320
+ leave-from-class="leave-from-class"
321
+ leave-to-class="leave-to-class"
322
+ >
323
+ <div
324
+ ref="dropdownElement"
325
+ v-show="isOpen"
326
+ :id="`${effectiveId}-listbox`"
327
+ role="listbox"
328
+ :class="[
329
+ 'rsui-form-field-combobox__options',
330
+ { 'rsui-form-field-combobox__options--open': isOpen }
331
+ ]"
332
+ >
333
+ <div
334
+ v-if="dropdownContent === 'loading'"
335
+ class="rsui-form-field-combobox__option rsui-form-field-combobox__option--disabled"
336
+ role="option"
337
+ aria-disabled="true"
338
+ aria-selected="false"
339
+ >
340
+ <div class="rsui-form-field-combobox__option-label">
341
+ <slot name="loading-text">Searching...</slot>
342
+ </div>
343
+ <div class="rsui-form-field-combobox__option-icon">
344
+ <div class="rsui-form-field-combobox__spinner"></div>
345
+ </div>
346
+ </div>
347
+
348
+ <div
349
+ v-if="dropdownContent === 'error'"
350
+ class="rsui-form-field-combobox__option rsui-form-field-combobox__option--disabled rsui-form-field-combobox__option--error"
351
+ role="option"
352
+ aria-disabled="true"
353
+ aria-selected="false"
354
+ >
355
+ <div class="rsui-form-field-combobox__option-label">
356
+ <slot name="error-text">Search failed</slot>
357
+ </div>
358
+ </div>
359
+
360
+ <div
361
+ v-if="dropdownContent === 'start-typing'"
362
+ class="rsui-form-field-combobox__option rsui-form-field-combobox__option--disabled rsui-form-field-combobox__option--hint"
363
+ role="option"
364
+ aria-disabled="true"
365
+ aria-selected="false"
366
+ >
367
+ <div class="rsui-form-field-combobox__option-label">
368
+ <slot name="start-typing-text">Start typing to search</slot>
369
+ </div>
370
+ </div>
371
+
372
+ <div
373
+ v-if="dropdownContent === 'min-length'"
374
+ class="rsui-form-field-combobox__option rsui-form-field-combobox__option--disabled"
375
+ role="option"
376
+ aria-disabled="true"
377
+ aria-selected="false"
378
+ >
379
+ <div class="rsui-form-field-combobox__option-label">
380
+ <slot name="min-length-text">Type at least {{ minSearchLength }} characters to search</slot>
381
+ </div>
382
+ </div>
383
+
384
+ <div
385
+ v-if="dropdownContent === 'no-results'"
386
+ class="rsui-form-field-combobox__option rsui-form-field-combobox__option--disabled"
387
+ role="option"
388
+ aria-disabled="true"
389
+ aria-selected="false"
390
+ >
391
+ <div class="rsui-form-field-combobox__option-label">
392
+ <slot name="no-results-text">No results found</slot>
393
+ </div>
394
+ </div>
395
+
396
+ <template v-if="dropdownContent === 'results' && groupedResults">
397
+ <div
398
+ v-for="group in groupedResults"
399
+ :key="group.name"
400
+ role="group"
401
+ :aria-label="group.name || undefined"
402
+ >
403
+ <div v-if="group.name" class="rsui-form-field-combobox__group-header" aria-hidden="true">
404
+ {{ group.name }}
405
+ </div>
406
+ <div
407
+ v-for="{ result, flatIndex } in group.items"
408
+ :key="result.value"
409
+ :id="`${effectiveId}-result-${flatIndex}`"
410
+ role="option"
411
+ aria-selected="false"
412
+ :class="[
413
+ 'rsui-form-field-combobox__option',
414
+ { 'rsui-form-field-combobox__option--highlighted': flatIndex === highlightedIndex }
415
+ ]"
416
+ @click="navigate(result)"
417
+ >
418
+ <slot
419
+ name="result"
420
+ :result="result"
421
+ :index="flatIndex"
422
+ :highlighted="flatIndex === highlightedIndex"
423
+ >
424
+ <div
425
+ class="rsui-form-field-combobox__option-label"
426
+ :title="result.label"
427
+ >
428
+ {{ result.label }}
429
+ </div>
430
+ </slot>
431
+ </div>
432
+ </div>
433
+ </template>
434
+
435
+ <template v-if="dropdownContent === 'results' && !groupedResults">
436
+ <div
437
+ v-for="(result, index) in results"
438
+ :key="result.value"
439
+ :id="`${effectiveId}-result-${index}`"
440
+ role="option"
441
+ aria-selected="false"
442
+ :class="[
443
+ 'rsui-form-field-combobox__option',
444
+ { 'rsui-form-field-combobox__option--highlighted': index === highlightedIndex }
445
+ ]"
446
+ @click="navigate(result)"
447
+ >
448
+ <slot
449
+ name="result"
450
+ :result="result"
451
+ :index="index"
452
+ :highlighted="index === highlightedIndex"
453
+ >
454
+ <div
455
+ class="rsui-form-field-combobox__option-label"
456
+ :title="result.label"
457
+ >
458
+ {{ result.label }}
459
+ </div>
460
+ </slot>
461
+ </div>
462
+ </template>
463
+ </div>
464
+ </transition>
465
+ </Teleport>
466
+
467
+ <div
468
+ aria-live="polite"
469
+ aria-atomic="true"
470
+ class="rsui-form-field-combobox__live-region"
471
+ >
472
+ {{ liveAnnouncement }}
473
+ </div>
474
+ </div>
475
+
476
+ <template #help v-if="$slots.help">
477
+ <slot name="help"></slot>
478
+ </template>
479
+
480
+ <template #error v-if="$slots.error">
481
+ <slot name="error"></slot>
482
+ </template>
483
+ </FormFieldSlot>
484
+ </template>
@@ -7,6 +7,7 @@ import FormFieldPassword from './FormFieldPassword.vue'
7
7
  import FormFieldPasswordToggle from './FormFieldPasswordToggle.vue'
8
8
  import FormFieldRadioGroup from './FormFieldRadioGroup.vue'
9
9
  import FormFieldSearch from './FormFieldSearch.vue'
10
+ import FormFieldSearchAsync from './FormFieldSearchAsync.vue'
10
11
  import FormFieldSelect from './FormFieldSelect.vue'
11
12
  import FormFieldSlot from './FormFieldSlot.vue'
12
13
  import FormFieldText from './FormFieldText.vue'
@@ -23,6 +24,7 @@ export {
23
24
  FormFieldPasswordToggle,
24
25
  FormFieldRadioGroup,
25
26
  FormFieldSearch,
27
+ FormFieldSearchAsync,
26
28
  FormFieldSelect,
27
29
  FormFieldSlot,
28
30
  FormFieldText,