Skip to content

Commit e167794

Browse files
committed
feat(components): create the search input component
1 parent ccd5d49 commit e167794

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

components/SearchInput.vue

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<template>
2+
<div class="max-w-md mx-auto">
3+
<div
4+
class="relative flex items-center w-full h-12 rounded-lg focus-within:shadow-lg bg-white overflow-hidden"
5+
>
6+
<div class="grid place-items-center h-full w-12 text-gray-300">
7+
<svg
8+
xmlns="http://www.w3.org/2000/svg"
9+
class="h-6 w-6"
10+
fill="none"
11+
viewBox="0 0 24 24"
12+
stroke="currentColor"
13+
>
14+
<path
15+
stroke-linecap="round"
16+
stroke-linejoin="round"
17+
stroke-width="2"
18+
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
19+
/>
20+
</svg>
21+
</div>
22+
23+
<input
24+
id="search"
25+
class="peer h-full w-full outline-none text-sm text-gray-700 pr-2"
26+
type="text"
27+
:placeholder="placeholder"
28+
@input="onChange"
29+
/>
30+
</div>
31+
</div>
32+
</template>
33+
34+
<script>
35+
import { debounce } from '~/utils/debounce'
36+
export default {
37+
name: 'SearchInput',
38+
props: {
39+
placeholder: { type: String, default: 'Search something..' },
40+
},
41+
created() {
42+
this.onChange = debounce((event) => {
43+
const searchTerm = event.target.value
44+
this.$emit('on-search', searchTerm)
45+
}, 400)
46+
},
47+
}
48+
</script>
49+
50+
<style lang="postcss" scoped>
51+
* {
52+
font-family: 'IBM Sans';
53+
color: #1a1f21;
54+
}
55+
</style>

utils/debounce.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function debounce(fn, wait) {
2+
let timer
3+
return function (...args) {
4+
if (timer) {
5+
clearTimeout(timer) // clear any pre-existing timer
6+
}
7+
const context = this // get the current context
8+
timer = setTimeout(() => {
9+
fn.apply(context, args) // call the function if time expires
10+
}, wait)
11+
}
12+
}

0 commit comments

Comments
 (0)