Fix search bar behaviour (step 3)

This commit is contained in:
Joshua Boniface 2025-03-02 14:32:44 -05:00
parent a8e3488354
commit 99372fdfe8

View File

@ -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