Skip to content

Commit

Permalink
fix: pressing enter should search immediately (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethidden authored and Frodigo committed Jun 22, 2022
1 parent 8b09e76 commit 7ce80de
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/theme/components/Header/SearchBar/SearchBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
aria-label="Search"
class="sf-header__search"
:value="term"
@input="handleSearch"
@keydown.enter="handleSearch($event)"
@input="debouncedHandleSearch($event)"
@keyup.enter="handleKeydownEnter($event.target.value) /* https://github.com/vuestorefront/storefront-ui/issues/2453#issuecomment-1160231619 */"
@keydown.tab="hideSearch"
@focus="showSearch"
@click="showSearch"
Expand Down Expand Up @@ -130,7 +130,7 @@ export default defineComponent({
}
};
const handleSearch = debounce(async (searchTerm: string) => {
const rawHandleSearch = async (searchTerm: string) => {
term.value = searchTerm;
if (term.value.length < props.minTermLen) return;
Expand All @@ -141,7 +141,15 @@ export default defineComponent({
}) as unknown as Products;
emit('set-search-results', productList!.items);
}, 1000);
};
const debouncedHandleSearch = debounce(rawHandleSearch, 1000);
const handleKeydownEnter = (searchTerm: string) => {
// cancel debounce timeout started by typing into the searchbar - this is to avoid making two network requests instead of one
debouncedHandleSearch.cancel();
rawHandleSearch(searchTerm);
};
watch(route, () => {
hideSearch();
Expand All @@ -153,7 +161,9 @@ export default defineComponent({
showSearch,
hideSearch,
toggleSearch,
handleSearch,
rawHandleSearch,
debouncedHandleSearch,
handleKeydownEnter,
term,
};
},
Expand Down

0 comments on commit 7ce80de

Please sign in to comment.