From 807188dea0eb5b353a21e4c89f66dccbbef641f2 Mon Sep 17 00:00:00 2001 From: starknt <1431880400@qq.com> Date: Sun, 31 Dec 2023 18:06:22 +0800 Subject: [PATCH] feat(search-bar): adjust search history style --- src/components/SearchBar/SearchBar.vue | 184 ++++++++---------- .../SearchBar/searchHistoryProvider.ts | 4 + 2 files changed, 84 insertions(+), 104 deletions(-) diff --git a/src/components/SearchBar/SearchBar.vue b/src/components/SearchBar/SearchBar.vue index 620614c5..d350d919 100644 --- a/src/components/SearchBar/SearchBar.vue +++ b/src/components/SearchBar/SearchBar.vue @@ -4,6 +4,7 @@ import { addSearchHistory, getSearchHistory, removeSearchHistory, + clearSearchHistory, } from './searchHistoryProvider' defineProps<{ @@ -16,12 +17,12 @@ const isFocus = ref(false) const keyword = ref('') const suggestions = reactive([]) const selectedIndex = ref(-1) -const searchHistory = reactive([]) +const searchHistory = ref([]) const historyItemRef = ref([]) const suggestionItemRef = ref([]) onMounted(() => { - Object.assign(searchHistory, getSearchHistory()) + searchHistory.value = getSearchHistory() }) function handleInput() { @@ -49,7 +50,7 @@ function navigateToSearchResultPage(keyword: string) { timestamp: Number(new Date()), } addSearchHistory(searchItem) - Object.assign(searchHistory, getSearchHistory()) + searchHistory.value = getSearchHistory() } } @@ -94,14 +95,14 @@ function handleKeyDown() { if ( isShowSuggestion - && selectedIndex.value >= suggestions.length - 1 + && selectedIndex.value >= suggestions.length - 1 ) { selectedIndex.value = suggestions.length - 1 return } if ( !isShowSuggestion - && selectedIndex.value >= searchHistory.length - 1 + && selectedIndex.value >= searchHistory.length - 1 ) { selectedIndex.value = searchHistory.length - 1 return @@ -124,119 +125,74 @@ function handleKeyDown() { else item.classList.remove('active') }) } + +function handleClearSearchHistory() { + clearSearchHistory() + searchHistory.value = getSearchHistory() +}