Skip to content

Commit

Permalink
feat: clear search text when loading a division
Browse files Browse the repository at this point in the history
  • Loading branch information
themightychris committed Nov 11, 2024
1 parent 701f49a commit 6e204cf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SearchResults from './components/SearchResults.vue'
const searchResults = ref({ matches: [], divisions: [], searchTerm: '' })
const isResultsExpanded = ref(false)
const searchQuery = ref('')
function handleSearch(results) {
searchResults.value = results
Expand All @@ -20,13 +21,18 @@ function handleMapSearch(results) {
// For map searches, ensure we clear any previous search term
searchResults.value = { ...results, searchTerm: '' }
isResultsExpanded.value = true
// Clear the search box without triggering a search clear
searchQuery.value = ''
}
</script>

<template>
<div class="app">
<CountdownBar />
<SearchBox @search="handleSearch" />
<SearchBox
v-model="searchQuery"
@search="handleSearch"
/>
<div class="map-wrapper">
<MapView
:searchResults="searchResults"
Expand Down
38 changes: 31 additions & 7 deletions src/components/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
import { ref, watch } from 'vue'
import { createClient } from '@supabase/supabase-js'
const searchQuery = ref('')
const emit = defineEmits(['search'])
const props = defineProps({
modelValue: {
type: String,
default: ''
}
})
const searchQuery = ref(props.modelValue)
const emit = defineEmits(['search', 'update:modelValue'])
// Initialize Supabase client
const supabase = createClient(
Expand All @@ -25,17 +32,34 @@ function debounce(func, wait) {
}
}
// Watch for changes in search query with debounce
watch(searchQuery, debounce(async (newQuery) => {
console.log('Search query changed:', newQuery)
// Watch for external value changes
watch(() => props.modelValue, (newValue) => {
if (newValue !== searchQuery.value) {
searchQuery.value = newValue
}
})
// Watch for internal value changes
watch(searchQuery, (newValue) => {
emit('update:modelValue', newValue)
})
// Watch for search query changes with debounce
const debouncedSearch = debounce(async (query) => {
console.log('Search query changed:', query)
if (newQuery.length >= 3) {
if (query.length >= 3) {
await handleSearch()
} else if (query.length === 0) {
// Don't clear results if the query was cleared externally
console.log('Query cleared')
} else {
console.log('Query too short, clearing results')
emit('search', { matches: [], divisions: [], searchTerm: '' })
}
}, 300))
}, 300)
watch(searchQuery, debouncedSearch)
async function handleSearch() {
if (searchQuery.value.length < 3) return
Expand Down

0 comments on commit 6e204cf

Please sign in to comment.