|
3 | 3 | <div v-if="showBackButton" class="back-button" @click="goBack"> |
4 | 4 | <i class="ri-arrow-left-line"></i> |
5 | 5 | </div> |
6 | | - <div class="search-box-input flex-1"> |
7 | | - <n-input |
8 | | - v-model:value="searchValue" |
9 | | - size="medium" |
10 | | - round |
11 | | - :placeholder="hotSearchKeyword" |
12 | | - class="border dark:border-gray-600 border-gray-200" |
13 | | - @keydown.enter="search" |
| 6 | + <div class="search-box-input flex-1 relative"> |
| 7 | + <n-popover |
| 8 | + trigger="manual" |
| 9 | + placement="bottom-start" |
| 10 | + :show="showSuggestions" |
| 11 | + :show-arrow="false" |
| 12 | + style="width: 100%; margin-top: 4px" |
| 13 | + content-style="padding: 0; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);" |
| 14 | + raw |
14 | 15 | > |
15 | | - <template #prefix> |
16 | | - <i class="iconfont icon-search"></i> |
| 16 | + <template #trigger> |
| 17 | + <n-input |
| 18 | + v-model:value="searchValue" |
| 19 | + size="medium" |
| 20 | + round |
| 21 | + :placeholder="hotSearchKeyword" |
| 22 | + class="border dark:border-gray-600 border-gray-200" |
| 23 | + @input="handleInput" |
| 24 | + @keydown="handleKeydown" |
| 25 | + @focus="handleFocus" |
| 26 | + @blur="handleBlur" |
| 27 | + > |
| 28 | + <template #prefix> |
| 29 | + <i class="iconfont icon-search"></i> |
| 30 | + </template> |
| 31 | + <template #suffix> |
| 32 | + <n-dropdown trigger="hover" :options="searchTypeOptions" @select="selectSearchType"> |
| 33 | + <div class="w-20 px-3 flex justify-between items-center"> |
| 34 | + <div> |
| 35 | + {{ |
| 36 | + searchTypeOptions.find((item) => item.key === searchStore.searchType)?.label |
| 37 | + }} |
| 38 | + </div> |
| 39 | + <i class="iconfont icon-xiasanjiaoxing"></i> |
| 40 | + </div> |
| 41 | + </n-dropdown> |
| 42 | + </template> |
| 43 | + </n-input> |
17 | 44 | </template> |
18 | | - <template #suffix> |
19 | | - <n-dropdown trigger="hover" :options="searchTypeOptions" @select="selectSearchType"> |
20 | | - <div class="w-20 px-3 flex justify-between items-center"> |
21 | | - <div> |
22 | | - {{ searchTypeOptions.find((item) => item.key === searchStore.searchType)?.label }} |
23 | | - </div> |
24 | | - <i class="iconfont icon-xiasanjiaoxing"></i> |
| 45 | + <!-- ==================== 搜索建议列表 ==================== --> |
| 46 | + <div class="search-suggestions-panel"> |
| 47 | + <n-scrollbar style="max-height: 300px"> |
| 48 | + <div v-if="suggestionsLoading" class="suggestion-item loading"> |
| 49 | + <n-spin size="small" /> |
25 | 50 | </div> |
26 | | - </n-dropdown> |
27 | | - </template> |
28 | | - </n-input> |
| 51 | + <div |
| 52 | + v-for="(suggestion, index) in suggestions" |
| 53 | + :key="index" |
| 54 | + class="suggestion-item" |
| 55 | + :class="{ highlighted: index === highlightedIndex }" |
| 56 | + @mousedown.prevent="selectSuggestion(suggestion)" |
| 57 | + @mouseenter="highlightedIndex = index" |
| 58 | + > |
| 59 | + <i class="ri-search-line suggestion-icon"></i> |
| 60 | + <span>{{ suggestion }}</span> |
| 61 | + </div> |
| 62 | + </n-scrollbar> |
| 63 | + </div> |
| 64 | + </n-popover> |
29 | 65 | </div> |
30 | 66 | <n-popover trigger="hover" placement="bottom" :show-arrow="false" raw> |
31 | 67 | <template #trigger> |
|
128 | 164 | </template> |
129 | 165 |
|
130 | 166 | <script lang="ts" setup> |
| 167 | +import { useDebounceFn } from '@vueuse/core'; |
131 | 168 | import { computed, onMounted, ref, watch, watchEffect } from 'vue'; |
132 | 169 | import { useI18n } from 'vue-i18n'; |
133 | 170 | import { useRouter } from 'vue-router'; |
134 | 171 |
|
135 | 172 | import { getSearchKeyword } from '@/api/home'; |
136 | 173 | import { getUserDetail } from '@/api/login'; |
| 174 | +import { getSearchSuggestions } from '@/api/search'; |
137 | 175 | import alipay from '@/assets/alipay.png'; |
138 | 176 | import wechat from '@/assets/wechat.png'; |
139 | 177 | import Coffee from '@/components/Coffee.vue'; |
@@ -250,6 +288,9 @@ const search = () => { |
250 | 288 | type: searchStore.searchType |
251 | 289 | } |
252 | 290 | }); |
| 291 | +
|
| 292 | + console.log(`[UI] 执行搜索,关键词: "${searchValue.value}"`); // <--- 日志 K |
| 293 | + showSuggestions.value = false; // 搜索后强制隐藏 |
253 | 294 | }; |
254 | 295 |
|
255 | 296 | const selectSearchType = (key: number) => { |
@@ -330,6 +371,84 @@ const toGithubRelease = () => { |
330 | 371 | window.open('https://github.com/algerkong/AlgerMusicPlayer/releases', '_blank'); |
331 | 372 | } |
332 | 373 | }; |
| 374 | +
|
| 375 | +// ==================== 搜索建议相关的状态和方法 ==================== |
| 376 | +const suggestions = ref<string[]>([]); |
| 377 | +const showSuggestions = ref(false); |
| 378 | +const suggestionsLoading = ref(false); |
| 379 | +const highlightedIndex = ref(-1); // -1 表示没有高亮项 |
| 380 | +// 使用防抖函数来避免频繁请求API |
| 381 | +const debouncedGetSuggestions = useDebounceFn(async (keyword: string) => { |
| 382 | + if (!keyword.trim()) { |
| 383 | + suggestions.value = []; |
| 384 | + showSuggestions.value = false; |
| 385 | + return; |
| 386 | + } |
| 387 | + suggestionsLoading.value = true; |
| 388 | + suggestions.value = await getSearchSuggestions(keyword); |
| 389 | + suggestionsLoading.value = false; |
| 390 | + // 只有当有建议时才显示面板 |
| 391 | + showSuggestions.value = suggestions.value.length > 0; |
| 392 | + highlightedIndex.value = -1; |
| 393 | +}, 300); // 300ms延迟 |
| 394 | +
|
| 395 | +const handleInput = (value: string) => { |
| 396 | + debouncedGetSuggestions(value); |
| 397 | +}; |
| 398 | +const handleFocus = () => { |
| 399 | + if (searchValue.value && suggestions.value.length > 0) { |
| 400 | + showSuggestions.value = true; |
| 401 | + } |
| 402 | +}; |
| 403 | +
|
| 404 | +const handleBlur = () => { |
| 405 | + setTimeout(() => { |
| 406 | + showSuggestions.value = false; |
| 407 | + }, 150); |
| 408 | +}; |
| 409 | +
|
| 410 | +const selectSuggestion = (suggestion: string) => { |
| 411 | + searchValue.value = suggestion; |
| 412 | + showSuggestions.value = false; |
| 413 | + search(); |
| 414 | +}; |
| 415 | +const handleKeydown = (event: KeyboardEvent) => { |
| 416 | + // 如果建议列表不显示,则不处理上下键 |
| 417 | + if (!showSuggestions.value || suggestions.value.length === 0) { |
| 418 | + // 如果是回车键,则正常执行搜索 |
| 419 | + if (event.key === 'Enter') { |
| 420 | + search(); |
| 421 | + } |
| 422 | + return; |
| 423 | + } |
| 424 | +
|
| 425 | + switch (event.key) { |
| 426 | + case 'ArrowDown': |
| 427 | + event.preventDefault(); // 阻止光标移动到末尾 |
| 428 | + highlightedIndex.value = (highlightedIndex.value + 1) % suggestions.value.length; |
| 429 | + break; |
| 430 | + case 'ArrowUp': |
| 431 | + event.preventDefault(); // 阻止光标移动到开头 |
| 432 | + highlightedIndex.value = |
| 433 | + (highlightedIndex.value - 1 + suggestions.value.length) % suggestions.value.length; |
| 434 | + break; |
| 435 | + case 'Enter': |
| 436 | + event.preventDefault(); // 阻止表单默认提交行为 |
| 437 | + if (highlightedIndex.value !== -1) { |
| 438 | + // 如果有高亮项,就选择它 |
| 439 | + selectSuggestion(suggestions.value[highlightedIndex.value]); |
| 440 | + } else { |
| 441 | + // 否则,执行默认搜索 |
| 442 | + search(); |
| 443 | + } |
| 444 | + break; |
| 445 | + case 'Escape': |
| 446 | + showSuggestions.value = false; // 按 Esc 隐藏建议 |
| 447 | + break; |
| 448 | + } |
| 449 | +}; |
| 450 | +
|
| 451 | +// ================================================================ |
333 | 452 | </script> |
334 | 453 |
|
335 | 454 | <style lang="scss" scoped> |
@@ -437,4 +556,22 @@ const toGithubRelease = () => { |
437 | 556 | } |
438 | 557 | } |
439 | 558 | } |
| 559 | +
|
| 560 | +.search-suggestions-panel { |
| 561 | + @apply bg-light dark:bg-dark-100 rounded-lg overflow-hidden; |
| 562 | + .suggestion-item { |
| 563 | + @apply flex items-center px-4 py-2 cursor-pointer; |
| 564 | + @apply text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800; |
| 565 | + &.highlighted { |
| 566 | + @apply bg-gray-100 dark:bg-gray-800; |
| 567 | + } |
| 568 | + &.loading { |
| 569 | + @apply justify-center; |
| 570 | + } |
| 571 | +
|
| 572 | + .suggestion-icon { |
| 573 | + @apply mr-2 text-gray-400; |
| 574 | + } |
| 575 | + } |
| 576 | +} |
440 | 577 | </style> |
0 commit comments