SearchBar.vue
1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<script lang="ts" setup>
import { useDebounceFn } from "@vueuse/core";
// #region --import--.
import { SearchIcon, XIcon } from "lucide-vue-next";
import { ref } from "vue";
// #endregion
// #region --Props--.
withDefaults(defineProps<{
placeholder?: string;
}>(), {
placeholder: "",
});
// #endregion
// #region --Emits--.
const emit = defineEmits<{
eventSearch: [search: string];
eventClearSearch: [];
}>();
// #endregion
// #region --Data/refs--.
const searchQuery = ref("");
// #endregion
// #region --Function--.
/**
* Debounced function
*/
const handleSearchEvent = useDebounceFn(() => {
emit("eventSearch", searchQuery.value);
}, 500);
function handleClearSearchEvent() {
searchQuery.value = "";
emit("eventClearSearch");
}
// #endregion
</script>
<template>
<!-- Barre de recherche -->
<section class="mb-8">
<div class="relative max-w-xl mx-auto">
<input
v-model="searchQuery"
:placeholder="placeholder"
class="w-full px-4 py-3 bg-gray-800 rounded-full text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-primary"
type="text"
@input="handleSearchEvent"
>
<button
v-if="searchQuery"
class="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-white"
@click="handleClearSearchEvent"
>
<XIcon :size="20" />
</button>
<button
v-else
class="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400"
>
<SearchIcon :size="20" />
</button>
</div>
</section>
</template>
<style scoped></style>