From 99372fdfe8c661d142b9be7f64fcb83360872afc Mon Sep 17 00:00:00 2001 From: Joshua Boniface Date: Sun, 2 Mar 2025 14:32:44 -0500 Subject: [PATCH] Fix search bar behaviour (step 3) --- .../src/components/general/VMSearchBar.vue | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pvc-vue/src/components/general/VMSearchBar.vue b/pvc-vue/src/components/general/VMSearchBar.vue index 505ce56..57a8af5 100644 --- a/pvc-vue/src/components/general/VMSearchBar.vue +++ b/pvc-vue/src/components/general/VMSearchBar.vue @@ -175,12 +175,20 @@ onMounted(() => { // Set up click outside handler document.addEventListener('click', handleClickOutside); + // Restore search text from localStorage if available + const savedSearchText = localStorage.getItem('vmSearchText'); + if (savedSearchText) { + searchText.value = savedSearchText; + } + // Initialize input value based on selected VM if (!props.showList && (props.selectedVM || props.vmFromUrl)) { inputValue.value = props.selectedVM || props.vmFromUrl; } else if (props.showList && props.modelValue) { inputValue.value = props.modelValue; searchText.value = props.modelValue; + // Save to localStorage + localStorage.setItem('vmSearchText', props.modelValue); } // If the list is visible on mount, scroll to selected VM @@ -200,6 +208,9 @@ const handleSearch = (event) => { searchText.value = value; isFilterActive.value = true; + // Save search text to localStorage + localStorage.setItem('vmSearchText', value); + emit('update:modelValue', value); emit('search', value); }; @@ -248,6 +259,8 @@ const toggleList = () => { // If we're closing the list, save the search text if (props.modelValue) { searchText.value = props.modelValue; + // Save to localStorage + localStorage.setItem('vmSearchText', props.modelValue); } } else { // If we're opening the list, deactivate filtering @@ -273,6 +286,10 @@ const clearSearch = () => { inputValue.value = ''; searchText.value = ''; isFilterActive.value = false; + + // Clear search text from localStorage + localStorage.removeItem('vmSearchText'); + emit('update:modelValue', ''); emit('clear'); }; @@ -280,6 +297,14 @@ const clearSearch = () => { // Select a VM const handleVMSelect = (vm) => { console.log('Selecting VM:', vm); + + // Save the current search text before selecting a VM + if (props.modelValue) { + searchText.value = props.modelValue; + // Save to localStorage + localStorage.setItem('vmSearchText', props.modelValue); + } + emit('select-vm', vm); }; @@ -418,6 +443,12 @@ watch(() => props.showList, (isVisible) => { const effectiveVM = props.selectedVM || props.vmFromUrl; if (effectiveVM) { inputValue.value = effectiveVM; + + // Don't clear the model value when closing the list + // This ensures the search text is preserved + if (!props.modelValue && searchText.value) { + emit('update:modelValue', searchText.value); + } } } else { // When list is opened, show search text if available