Skip to content

Commit b0c5878

Browse files
committed
fix(search): 🐛 fix bug of combo
1 parent f048d8e commit b0c5878

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

components/search/Combo.vue

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<script lang="ts" setup>
22
import type { Query } from '@/composables/graphql'
33
4+
const props = defineProps<{
5+
query?: string
6+
}>()
7+
48
const router = useRouter()
59
610
const { locale } = useI18n()
711
12+
const comboEl = shallowRef<HTMLDivElement>()
813
const inputEl = shallowRef<HTMLInputElement>()
914
1015
function setCursorPos(pos: number) {
@@ -84,7 +89,7 @@ const popularTags = computed<ComboTag[]>(() =>
8489
}
8590
}) || [])
8691
87-
const query = ref('')
92+
const query = ref(props.query || '')
8893
const cursor = ref(0)
8994
9095
const queryKeywordStart = computed(() => {
@@ -157,7 +162,7 @@ function siteOrKeywordFilter(query: string) {
157162
158163
const { data: queryData, refresh, pending } = useLazyFetch<QueryResult[]>(
159164
() => `https://patchyvideo.com/be/autocomplete/ql?q=${queryKeyword.value}`,
160-
{ immediate: false, watch: false },
165+
{ immediate: Boolean(props.query), watch: false },
161166
)
162167
watchThrottled(queryKeyword, (value) => {
163168
if (value)
@@ -206,6 +211,17 @@ const completesWithKeyword = computed<ComboTag[]>(
206211
)
207212
208213
const inFocus = ref(false)
214+
function onBodyClick(event: MouseEvent) {
215+
if (comboEl.value && !comboEl.value.contains(event.target as Node))
216+
inFocus.value = false
217+
}
218+
onMounted(() => {
219+
document.addEventListener('click', onBodyClick)
220+
})
221+
onUnmounted(() => {
222+
document.removeEventListener('click', onBodyClick)
223+
})
224+
209225
const active = ref(-1)
210226
const activeCombo = computed(() => completesWithKeyword.value[active.value])
211227
@@ -258,8 +274,10 @@ function onKeyDown(e: KeyboardEvent) {
258274
e.preventDefault()
259275
e.stopPropagation()
260276
if (activeCombo.value) {
277+
// activeCombo.value.tag will be undefined after setting query.value
278+
const tag = activeCombo.value.tag
261279
query.value = `${queryPrefix.value}${activeCombo.value.tag}${querySuffix.value || ' '}`
262-
const pos = queryPrefix.value.length + activeCombo.value.tag.length + 1
280+
const pos = queryPrefix.value.length + tag.length + 1
263281
nextTick(() => setCursorPos(pos))
264282
active.value = -1
265283
}
@@ -279,15 +297,14 @@ function onComboClick(combo: ComboTag) {
279297
</script>
280298

281299
<template>
282-
<div class="relative">
300+
<div ref="comboEl" class="relative">
283301
<div class="flex items-center rounded-md border-2 border-purple-200 dark:border-gray-700 dark:bg-gray-800">
284302
<input
285303
ref="inputEl"
286304
v-model="query"
287305
class="focus:outline-none w-full ml-4 py-1 dark:bg-gray-800"
288306
placeholder="搜索你想看的内容"
289307
@focus="() => inFocus = true"
290-
@blur="() => inFocus = false"
291308
@keydown="onKeyDown"
292309
@keyup="(e) => cursor = (e.target as HTMLInputElement).selectionStart || 0"
293310
>
@@ -307,7 +324,7 @@ function onComboClick(combo: ComboTag) {
307324
<div
308325
v-show="inFocus"
309326
static
310-
class="absolute w-[calc(100%-1rem)] max-h-80vh mt-2 ml-2 rounded-lg ring-2 ring-purple-300 bg-white overflow-auto"
327+
class="absolute w-[calc(100%-1rem)] max-h-80vh mt-2 ml-2 rounded-lg ring-2 ring-purple-300 dark:ring-gray-600 bg-white dark:bg-gray-800 overflow-auto"
311328
>
312329
<div
313330
v-if="completesWithKeyword.length === 0"
@@ -319,7 +336,7 @@ function onComboClick(combo: ComboTag) {
319336
<li
320337
v-for="(combo, index) in completesWithKeyword"
321338
:key="combo.id"
322-
class="px-2 py-1 w-full list-none border-b border-purple-200 last:border-b-0 transition-colors duration-75 cursor-pointer"
339+
class="px-2 py-1 w-full list-none border-b border-purple-200 dark:border-gray-600 last:border-b-0 transition-colors duration-75 cursor-pointer"
323340
:class="{ 'bg-purple-200 bg-opacity-10': active === index }"
324341
@click="onComboClick(combo)"
325342
>

components/toolbar/Top.vue

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ const props = defineProps<{
33
fetchNote: boolean
44
}>()
55
6+
const route = useRoute()
7+
68
const showSlider = ref(false)
9+
10+
const query = computed(() => pickFirstQuery(route.query.q) || '')
711
</script>
812

913
<template>
@@ -14,7 +18,7 @@ const showSlider = ref(false)
1418

1519
<!-- Center -->
1620
<div class="flex-auto max-w-xl min-w-0">
17-
<SearchCombo />
21+
<SearchCombo :query="query" />
1822
</div>
1923

2024
<!-- End -->

0 commit comments

Comments
 (0)