Skip to content

Commit

Permalink
Make the search feature act as soon as the user starts typing - Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubka committed Feb 25, 2025
1 parent 11db01e commit 530b1a5
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions resources/js/components/SearchBox.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup>
const keyword = defineModel('keyword')
const props = defineProps({
keyword: String,
hasNoBackground: {
type: Boolean,
default: false
Expand All @@ -10,28 +10,56 @@
const searchInput = ref(null)
onMounted(() => {
document.addEventListener('keydown', keyListener)
document.addEventListener('keydown', ctrlFHandler)
document.addEventListener('keypress', anyPrintableKeyHandler)
})
onUnmounted(() => {
document.removeEventListener('keydown', keyListener)
document.removeEventListener('keydown', ctrlFHandler)
document.removeEventListener('keypress', anyPrintableKeyHandler)
})
/**
* Attach an event listen for ctrl+F
*/
function keyListener(e) {
function ctrlFHandler(e) {
if (e.key === "f" && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
e.preventDefault()
searchInput.value?.focus()
}
}
/**
* Clear the search field
*/
function clearSearch() {
keyword.value = ''
}
/**
* Attach an event listen for any key press to automatically focus on search
* without having to ctrl+F
*/
function anyPrintableKeyHandler(e) {
if (e.key === 'Enter') {
return
}
keyword.value = e.key
searchInput.value?.setSelectionRange(1, 1)
searchInput.value?.focus()
e.preventDefault()
}
</script>
<template>
<div role="search" class="field">
<div class="control has-icons-right">
<input
v-model="keyword"
@keyup.esc.prevent="(event) => { clearSearch(); event.target.blur() }"
@keyup.enter.prevent="(event) => event.target.blur()"
@keypress.stop
ref="searchInput"
id="txtSearch"
type="search"
Expand All @@ -40,11 +68,9 @@
:title="$t('commons.search')"
:placeholder="placeholder"
class="input is-rounded is-search"
:class="{ 'has-no-background': hasNoBackground }"
:value="keyword"
v-on:keyup="$emit('update:keyword', $event.target.value)">
:class="{ 'has-no-background': hasNoBackground }">
<span class="icon is-small is-right">
<button type="button" v-if="keyword != ''" id="btnClearSearch" tabindex="1" :title="$t('commons.clear_search')" class="clear-selection delete" @click="$emit('update:keyword','')"></button>
<button type="button" v-if="keyword != ''" id="btnClearSearch" tabindex="1" :title="$t('commons.clear_search')" class="clear-selection delete" @click="clearSearch"></button>
<FontAwesomeIcon v-else :icon="['fas', 'search']" />
</span>
</div>
Expand Down

0 comments on commit 530b1a5

Please sign in to comment.