All files / src/components MultiReferenceSelect.vue

91.83% Statements 45/49
84.31% Branches 43/51
89.47% Functions 17/19
97.36% Lines 37/38

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164  7x     8x                       1x                 1x                                 5x                                                     5x   5x                                                             5x         5x   5x 5x 5x 5x   5x 5x 8x 6x       5x 6x 6x 6x 11x 5x 1x 11x       5x 5x 5x 4x       1x 1x 1x 1x     1x 1x       1x   2x                      
<template>
  <div class="multi-reference-select">
    <!-- Selected chips -->
    <div v-if="selectedOptions.length > 0" class="selected-chips mb-2">
      <span
        v-for="option in selectedOptions"
        :key="option.value"
        class="badge bg-secondary me-1 mb-1 d-inline-flex align-items-center gap-1"
      >
        {{ option.label }}
        <button
          v-if="!disabled"
          type="button"
          class="btn-close btn-close-white"
          style="font-size: 0.6rem"
          :aria-label="t('common.remove')"
          @click="removeValue(option.value)"
        />
      </span>
    </div>
 
    <!-- Add picker (hidden when max reached) -->
    <template v-if="canAddMore">
      <input
        v-if="showSearch"
        v-model="query"
        type="search"
        class="form-control form-control-sm mb-2"
        :placeholder="resolvedSearchPlaceholder"
        :disabled="disabled || loading"
      />
      <select
        class="form-select form-select-sm"
        :value="''"
        :disabled="disabled || loading || availableOptions.length === 0"
        @change="handleChange"
        @blur="$emit('blur')"
      >
        <option value="" disabled>{{ resolvedAddLabel }}</option>
        <option v-if="loading && availableOptions.length === 0" value="" disabled>
          {{ resolvedLoadingLabel }}
        </option>
        <option
          v-for="option in availableOptions"
          :key="option.value"
          :value="option.value"
          :disabled="option.disabled"
        >
          {{ option.label }}
          <template v-if="option.description"> - {{ option.description }}</template>
        </option>
        <option v-if="!loading && availableOptions.length === 0" value="" disabled>
          {{ resolvedNoResultsLabel }}
        </option>
      </select>
    </template>
 
    <!-- Validation hints -->
    <div v-if="minValues && modelValue.length < minValues" class="text-warning small mt-1">
      {{ t('field.referenceMinValuesHint').replace('{min}', String(minValues)) }}
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import type { ReferenceOption } from '@/composables/useFlowMemberReference'
 
const { t } = useUILanguage()
 
const props = withDefaults(
  defineProps<{
    modelValue: string[]
    options: ReferenceOption[]
    inputId?: string
    required?: boolean
    disabled?: boolean
    loading?: boolean
    showSearch?: boolean
    searchPlaceholder?: string
    addLabel?: string
    loadingLabel?: string
    noResultsLabel?: string
    minValues?: number
    maxValues?: number
  }>(),
  {
    inputId: '',
    required: false,
    disabled: false,
    loading: false,
    showSearch: true,
    searchPlaceholder: '',
    addLabel: '',
    loadingLabel: '',
    noResultsLabel: '',
    minValues: undefined,
    maxValues: undefined
  }
)
 
const emit = defineEmits<{
  'update:modelValue': [value: string[]]
  blur: []
}>()
 
const query = ref('')
 
const resolvedSearchPlaceholder = computed(() => props.searchPlaceholder || t('common.search'))
const resolvedAddLabel = computed(() => props.addLabel || t('field.referenceAddValue'))
const resolvedLoadingLabel = computed(() => props.loadingLabel || t('common.loading'))
const resolvedNoResultsLabel = computed(() => props.noResultsLabel || t('common.noResults'))
 
const selectedOptions = computed(() =>
  props.modelValue
    .map((v) => props.options.find((o) => o.value === v))
    .filter((o): o is ReferenceOption => !!o)
)
 
// Options not yet selected, filtered by search query
const availableOptions = computed(() => {
  const selectedSet = new Set(props.modelValue)
  const search = query.value.trim().toLowerCase()
  return props.options.filter((option) => {
    if (selectedSet.has(option.value)) return false
    if (!search) return true
    const haystack = `${option.label} ${option.description || ''}`.toLowerCase()
    return haystack.includes(search)
  })
})
 
const canAddMore = computed(() => {
  Iif (props.disabled) return false
  if (props.maxValues !== undefined && props.modelValue.length >= props.maxValues) return false
  return true
})
 
function handleChange(event: Event) {
  const value = (event.target as HTMLSelectElement).value
  Iif (!value) return
  Eif (!props.modelValue.includes(value)) {
    emit('update:modelValue', [...props.modelValue, value])
  }
  // Reset picker
  ;(event.target as HTMLSelectElement).value = ''
  query.value = ''
}
 
function removeValue(value: string) {
  emit(
    'update:modelValue',
    props.modelValue.filter((v) => v !== value)
  )
}
</script>
 
<style scoped>
.selected-chips {
  display: flex;
  flex-wrap: wrap;
}
</style>