LCOV - code coverage report
Current view: top level - packages/design-system/src/components/search-input - SearchAutocomplete.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 83.3 % 48 40
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : <template>
       2          542 :   <ComboboxRoot
       3              :     ref="comboboxRef"
       4              :     v-model:open="isOpen"
       5              :     ignore-filter
       6              :     :reset-search-term-on-blur="false"
       7              :     :reset-search-term-on-select="false"
       8              :     :disabled
       9              :     :open-on-focus
      10              :     :open-on-click="openOnFocus"
      11              :     :class="className"
      12              :     @highlight="onHighlight"
      13              :   >
      14              :     <ComboboxAnchor
      15              :       :class="
      16              :         cn(
      17              :           searchInputVariants({ size }),
      18              :           disabled && 'pointer-events-none opacity-50',
      19              :           anchorClass
      20              :         )
      21              :       "
      22              :       @click="focus"
      23              :       @keydown.capture="keepShiftSelectionNative"
      24              :     >
      25              :       <slot name="leading" />
      26              :       <Button
      27              :         v-if="modelValue"
      28              :         :class="cn('absolute', sizeConfig.clearPos)"
      29              :         variant="textonly"
      30              :         size="icon-sm"
      31              :         :aria-label="clearLabel"
      32              :         @click.stop="clearSearch"
      33              :       >
      34              :         <i :class="cn('icon-[lucide--x]', sizeConfig.icon)" />
      35              :       </Button>
      36              :       <i
      37              :         v-else-if="loading"
      38              :         :class="
      39              :           cn(
      40              :             'pointer-events-none absolute icon-[lucide--loader-circle] animate-spin',
      41              :             sizeConfig.iconPos,
      42              :             sizeConfig.icon
      43              :           )
      44              :         "
      45              :       />
      46              :       <i
      47              :         v-else
      48              :         :class="
      49              :           cn(
      50              :             'pointer-events-none absolute',
      51              :             sizeConfig.iconPos,
      52              :             sizeConfig.icon,
      53              :             icon
      54              :           )
      55              :         "
      56              :       />
      57              : 
      58              :       <ComboboxInput
      59              :         :id="inputId"
      60              :         ref="inputRef"
      61              :         v-model="modelValue"
      62              :         :class="
      63              :           cn(
      64              :             'h-full min-w-0 flex-1 border-none bg-transparent outline-none',
      65              :             sizeConfig.inputPl,
      66              :             sizeConfig.inputText
      67              :           )
      68              :         "
      69              :         :placeholder
      70              :         :aria-label="accessibleLabel"
      71              :         :auto-focus="autofocus"
      72              :         @compositionstart="isComposing = true"
      73              :         @compositionend="isComposing = false"
      74              :         @keydown.enter="onEnterKey"
      75              :       />
      76              :     </ComboboxAnchor>
      77              : 
      78              :     <ComboboxPortal :key="Number(isOpen)">
      79              :       <ComboboxContent
      80              :         v-if="suggestions.length > 0"
      81              :         position="popper"
      82              :         :side-offset="4"
      83              :         :style="contentStyle"
      84              :         :class="
      85              :           cn(
      86              :             'z-overlay max-h-60 w-(--reka-combobox-trigger-width) overflow-y-auto',
      87              :             'rounded-lg border bg-background p-1 shadow-lg'
      88              :           )
      89              :         "
      90              :       >
      91        26167 :         <ComboboxItem
      92              :           v-for="(suggestion, index) in suggestions"
      93              :           :key="suggestionKey(suggestion, index)"
      94              :           :value="suggestionValue(suggestion)"
      95              :           :data-suggestion-index="index"
      96              :           :aria-label="suggestionLabel(suggestion)"
      97              :           :class="
      98              :             cn(
      99              :               'cursor-pointer rounded-sm px-3 py-2 text-sm outline-none',
     100              :               'data-highlighted:bg-accent'
     101              :             )
     102              :           "
     103              :           @select.prevent="onSelectSuggestion(suggestion)"
     104              :         >
     105              :           <slot name="suggestion" :suggestion>
     106              :             {{ suggestionLabel(suggestion) }}
     107              :           </slot>
     108              :         </ComboboxItem>
     109              :       </ComboboxContent>
     110              :     </ComboboxPortal>
     111              :   </ComboboxRoot>
     112              : </template>
     113              : 
     114              : <script setup lang="ts" generic="T">
     115              : import type { HTMLAttributes, StyleValue } from 'vue'
     116              : 
     117              : import { cn } from '@comfyorg/tailwind-utils'
     118              : import {
     119              :   ComboboxAnchor,
     120              :   ComboboxContent,
     121              :   ComboboxInput,
     122              :   ComboboxItem,
     123              :   ComboboxRoot,
     124              :   ComboboxPortal
     125              : } from 'reka-ui'
     126              : import { computed, nextTick, ref, useTemplateRef, watch } from 'vue'
     127              : 
     128              : import Button from '@comfyorg/design-system/button/Button'
     129              : import type { SearchInputVariants } from './searchInput.variants'
     130              : import {
     131              :   searchInputSizeConfig,
     132              :   searchInputVariants
     133              : } from './searchInput.variants'
     134              : 
     135              : const {
     136              :   placeholder,
     137              :   accessibleLabel,
     138              :   clearLabel,
     139              :   icon = 'icon-[lucide--search]',
     140              :   autofocus = false,
     141              :   inputId,
     142              :   loading = false,
     143              :   disabled = false,
     144              :   openOnFocus = false,
     145              :   updateModelOnSelect = true,
     146              :   size = 'md',
     147              :   suggestions = [],
     148              :   optionLabel,
     149              :   optionKey,
     150              :   class: className,
     151              :   anchorClass,
     152              :   contentStyle
     153              : } = defineProps<{
     154              :   placeholder?: string
     155              :   accessibleLabel: string
     156              :   clearLabel: string
     157              :   icon?: string
     158              :   autofocus?: boolean
     159              :   inputId?: string
     160              :   loading?: boolean
     161              :   disabled?: boolean
     162              :   openOnFocus?: boolean
     163              :   updateModelOnSelect?: boolean
     164              :   size?: SearchInputVariants['size']
     165              :   suggestions?: T[]
     166              :   optionLabel?: keyof T & string
     167              :   optionKey?: keyof T & string
     168              :   class?: HTMLAttributes['class']
     169              :   anchorClass?: HTMLAttributes['class']
     170              :   contentStyle?: StyleValue
     171              : }>()
     172              : 
     173           49 : const emit = defineEmits<{
     174              :   select: [item: T]
     175              :   highlight: [item: T | undefined]
     176              : }>()
     177              : 
     178           49 : const sizeConfig = computed(() => searchInputSizeConfig[size])
     179              : 
     180           49 : const modelValue = defineModel<string>({ required: true })
     181              : 
     182           49 : const comboboxRef = useTemplateRef('comboboxRef')
     183           49 : const inputRef = ref<InstanceType<typeof ComboboxInput> | null>(null)
     184           49 : const isOpen = ref(false)
     185           49 : const isComposing = ref(false)
     186              : 
     187           62 : function focus() {
     188           62 :   inputRef.value?.$el?.focus()
     189              : }
     190              : 
     191           62 : function open() {
     192           62 :   isOpen.value = true
     193              : }
     194              : 
     195           49 : defineExpose({ focus, open })
     196              : 
     197            0 : function clearSearch() {
     198            0 :   modelValue.value = ''
     199            0 :   focus()
     200              : }
     201              : 
     202        78501 : function getItemProperty(item: T, key: keyof T & string): string {
     203        78501 :   if (typeof item === 'object' && item !== null) {
     204        78501 :     return String(item[key])
     205              :   }
     206            0 :   return String(item)
     207              : }
     208              : 
     209        52334 : function suggestionLabel(item: T): string {
     210        52334 :   if (optionLabel) return getItemProperty(item, optionLabel)
     211            0 :   return String(item)
     212              : }
     213              : 
     214        26167 : function suggestionKey(item: T, index: number): string {
     215        26167 :   if (optionKey) return getItemProperty(item, optionKey)
     216            0 :   return `${suggestionLabel(item)}-${index}`
     217              : }
     218              : 
     219        26167 : function suggestionValue(item: T): string {
     220        26167 :   return suggestionLabel(item)
     221              : }
     222              : 
     223            9 : function onSelectSuggestion(item: T) {
     224            9 :   if (updateModelOnSelect) modelValue.value = suggestionLabel(item)
     225            9 :   isOpen.value = false
     226            9 :   emit('select', item)
     227              : }
     228              : 
     229          481 : function onHighlight(payload: { ref: HTMLElement } | undefined) {
     230          481 :   const index = Number(payload?.ref.dataset.suggestionIndex)
     231          481 :   emit('highlight', Number.isInteger(index) ? suggestions[index] : undefined)
     232              : }
     233              : 
     234           66 : function keepShiftSelectionNative(e: KeyboardEvent) {
     235           66 :   if (e.target !== inputRef.value?.$el) return
     236           66 :   if (e.shiftKey && (e.key === 'Home' || e.key === 'End')) e.stopPropagation()
     237              : }
     238              : 
     239            1 : function onEnterKey(e: KeyboardEvent) {
     240            1 :   if (isComposing.value) {
     241            0 :     e.preventDefault()
     242            0 :     e.stopPropagation()
     243              :   }
     244              : }
     245              : 
     246           49 : watch(
     247              :   () => suggestions,
     248              :   async (items) => {
     249          356 :     isOpen.value = items.length > 0 && (openOnFocus || !!modelValue.value)
     250          356 :     if (isOpen.value) {
     251          331 :       await nextTick()
     252          331 :       comboboxRef.value?.highlightFirstItem?.()
     253              :     }
     254              :   }
     255              : )
     256              : </script>
        

Generated by: LCOV version 2.0-1