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() +}