Skip to content

Commit

Permalink
search-input: Debounce immediately if the user hits "Enter"
Browse files Browse the repository at this point in the history
Small performance optimization to allow the user to give us a hint
they're done typing in the search box, so we can trigger updates that
much more quickly.
  • Loading branch information
josh-berry committed Sep 3, 2023
1 parent 8501768 commit 4c6e835
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/components/search-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
:placeholder="props.placeholder"
v-model="searchContent"
@keydown.esc.prevent="clear"
@keydown.enter.prevent="updateModelValue"
/>
<button
v-if="searchContent !== ''"
Expand Down Expand Up @@ -56,11 +57,7 @@ watch(searchContent, () => {
}

if (debounceTimeout !== undefined) clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(() => {
emit("update:modelValue", searchContent.value);
clearTimeout(debounceTimeout);
debounceTimeout = undefined;
}, props.debounceMs);
debounceTimeout = setTimeout(updateModelValue, props.debounceMs);
});

const $search = ref(undefined! as HTMLInputElement);
Expand All @@ -80,4 +77,13 @@ function clear(ev: KeyboardEvent | MouseEvent) {
searchContent.value = "";
}
}

/** Trigger a model update immediately. */
function updateModelValue() {
if (debounceTimeout !== undefined) {
clearTimeout(debounceTimeout);
debounceTimeout = undefined;
emit("update:modelValue", searchContent.value);
}
}
</script>

0 comments on commit 4c6e835

Please sign in to comment.