Line data Source code
1 16 : <script setup lang="ts">
2 : import { refDebounced } from '@vueuse/core'
3 : import { nextTick, onMounted, ref, toRef, toValue, watch } from 'vue'
4 : import type { HTMLAttributes, MaybeRefOrGetter } from 'vue'
5 :
6 : import { cn } from '@comfyorg/tailwind-utils'
7 :
8 : const {
9 : accessibleLabel,
10 : clearLabel,
11 : placeholder,
12 : searcher = async () => {},
13 : updateKey,
14 : autofocus = false,
15 : debounceMs = 250,
16 : debounceMaxWaitMs = 1000,
17 : class: customClass
18 : } = defineProps<{
19 : accessibleLabel: string
20 : clearLabel: string
21 : placeholder?: string
22 : searcher?: (
23 : query: string,
24 : onCleanup: (cleanupFn: () => void) => void
25 : ) => Promise<void>
26 : updateKey?: MaybeRefOrGetter<unknown>
27 : autofocus?: boolean
28 : debounceMs?: number
29 : debounceMaxWaitMs?: number
30 : class?: HTMLAttributes['class']
31 : }>()
32 2355 : const emit = defineEmits<{
33 : enter: [event: KeyboardEvent]
34 : }>()
35 :
36 434 : const searchQuery = defineModel<string>({ default: '' })
37 :
38 434 : const inputRef = ref<HTMLInputElement>()
39 434 : onMounted(() => {
40 434 : if (!autofocus) return
41 34 : void nextTick(() => inputRef.value?.focus())
42 : })
43 :
44 434 : defineExpose({
45 : focus: () => inputRef.value?.focus()
46 : })
47 :
48 434 : const isQuerying = ref(false)
49 434 : const debouncedSearchQuery = refDebounced(searchQuery, debounceMs, {
50 : maxWait: debounceMaxWaitMs
51 : })
52 434 : watch(searchQuery, (value) => {
53 24 : isQuerying.value = value !== debouncedSearchQuery.value
54 : })
55 434 : const updateKeyRef = toRef(() => toValue(updateKey))
56 :
57 434 : watch(
58 : [debouncedSearchQuery, updateKeyRef],
59 : (_, __, onCleanup) => {
60 480 : let isCleanup = false
61 480 : let cleanupFn: undefined | (() => void)
62 480 : onCleanup(() => {
63 295 : isCleanup = true
64 295 : cleanupFn?.()
65 : })
66 :
67 480 : searcher(debouncedSearchQuery.value, (cb) => (cleanupFn = cb))
68 : .catch((error) => {
69 0 : console.error('[AsyncSearchInput] searcher failed', error)
70 : })
71 : .finally(() => {
72 480 : if (!isCleanup) isQuerying.value = false
73 : })
74 : },
75 : { immediate: true }
76 : )
77 :
78 69 : function handleFocus(event: FocusEvent) {
79 69 : const target = event.target
80 69 : if (target instanceof HTMLInputElement) {
81 69 : target.select()
82 : }
83 : }
84 :
85 1 : function handleKeydownEnter(event: KeyboardEvent) {
86 1 : if (event.isComposing) return
87 1 : emit('enter', event)
88 : }
89 434 : </script>
90 :
91 : <template>
92 504 : <label
93 : :class="
94 : cn(
95 : 'group',
96 : 'rounded-lg bg-secondary transition-all duration-150',
97 : 'flex flex-1 items-center',
98 : 'border-0 text-foreground',
99 : 'focus-within:ring focus-within:ring-ring',
100 : customClass
101 : )
102 : "
103 : >
104 : <i
105 : :class="
106 : cn(
107 : 'ml-2 transition-colors duration-150',
108 : isQuerying
109 : ? 'icon-[lucide--loader-circle] animate-spin'
110 : : 'icon-[lucide--search]',
111 : searchQuery?.trim() !== ''
112 : ? 'text-foreground'
113 : : 'text-muted-foreground group-focus-within:text-foreground group-hover:text-foreground'
114 : )
115 : "
116 : />
117 : <input
118 : ref="inputRef"
119 : v-model="searchQuery"
120 : type="text"
121 : class="mx-2 my-1.5 h-5 w-full min-w-0 border-0 bg-transparent text-sm ring-0 outline-0"
122 : :placeholder
123 : :aria-label="accessibleLabel"
124 : :autofocus
125 : @focus="handleFocus"
126 : @keydown.enter="handleKeydownEnter"
127 : />
128 : <button
129 : v-if="searchQuery.trim().length > 0"
130 : class="m-0 flex shrink-0 items-center justify-center border-0 bg-transparent p-0 pr-3 pl-1 text-muted-foreground ring-0 outline-0 transition-all duration-150 hover:scale-108 hover:text-foreground"
131 : :aria-label="clearLabel"
132 : @click="searchQuery = ''"
133 : >
134 : <i :class="cn('icon-[lucide--delete] cursor-pointer')" />
135 : </button>
136 : </label>
137 : </template>
|