All files / src/components ReferenceSelect.vue

97.29% Statements 36/37
95.12% Branches 39/41
92.85% Functions 13/14
96.66% Lines 29/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119  8x     2x                                       8x                                         6x   6x                                                     6x         6x   6x 6x 6x 6x 6x   6x 8x 8x 5x   3x 4x 4x       6x 6x   6x 5x 5x   1x 1x 1x             1x 1x      
<template>
  <div class="reference-select">
    <input
      v-if="showSearch"
      v-model="query"
      type="search"
      class="form-control form-control-sm mb-2"
      :placeholder="resolvedSearchPlaceholder"
      :disabled="disabled || loading"
    />
 
    <select
      :id="inputId"
      class="form-select form-select-sm"
      :value="normalizedValue"
      :required="required"
      :disabled="disabled || loading"
      @change="handleChange"
      @blur="$emit('blur')"
    >
      <option v-if="!required" value="">{{ resolvedEmptyLabel }}</option>
      <option v-if="loading && filteredOptions.length === 0" value="" disabled>
        {{ resolvedLoadingLabel }}
      </option>
      <option
        v-for="option in filteredOptions"
        :key="option.value"
        :value="option.value"
        :disabled="option.disabled"
      >
        {{ option.label }}
        <template v-if="option.description"> - {{ option.description }}</template>
      </option>
      <option v-if="!loading && filteredOptions.length === 0" value="" disabled>
        {{ resolvedNoResultsLabel }}
      </option>
    </select>
  </div>
</template>
 
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import type { ReferenceOption } from '@/composables/useFlowMemberReference'
 
const { t } = useUILanguage()
 
const props = withDefaults(
  defineProps<{
    modelValue: string | null | undefined
    options: ReferenceOption[]
    inputId?: string
    required?: boolean
    disabled?: boolean
    loading?: boolean
    showSearch?: boolean
    searchPlaceholder?: string
    emptyLabel?: string
    loadingLabel?: string
    noResultsLabel?: string
  }>(),
  {
    inputId: '',
    required: false,
    disabled: false,
    loading: false,
    showSearch: true,
    searchPlaceholder: '',
    emptyLabel: '',
    loadingLabel: '',
    noResultsLabel: ''
  }
)
 
const emit = defineEmits<{
  'update:modelValue': [value: string | null]
  blur: []
}>()
 
const query = ref('')
 
const normalizedValue = computed(() => props.modelValue ?? '')
const resolvedSearchPlaceholder = computed(() => props.searchPlaceholder || t('common.search'))
const resolvedEmptyLabel = computed(() => props.emptyLabel || t('common.select'))
const resolvedLoadingLabel = computed(() => props.loadingLabel || t('common.loading'))
const resolvedNoResultsLabel = computed(() => props.noResultsLabel || t('common.noResults'))
 
const filteredOptions = computed(() => {
  const search = query.value.trim().toLowerCase()
  if (!search) {
    return props.options
  }
  return props.options.filter((option) => {
    const haystack = `${option.label} ${option.description || ''}`.toLowerCase()
    return haystack.includes(search)
  })
})
 
watch(
  () => props.modelValue,
  (value) => {
    if (!value) {
      query.value = ''
      return
    }
    const selected = props.options.find((option) => option.value === value)
    Eif (selected) {
      query.value = selected.label
    }
  },
  { immediate: true }
)
 
function handleChange(event: Event) {
  const target = event.target as HTMLSelectElement
  emit('update:modelValue', target.value || null)
}
</script>