diff --git a/components/SearchInput.js b/components/SearchInput.js
index e01b6a6a..0a64363c 100644
--- a/components/SearchInput.js
+++ b/components/SearchInput.js
@@ -1,14 +1,15 @@
import { useRouter } from 'next/router'
import { useGlobal } from '@/lib/global'
-import { useState } from 'react'
+import { useRef, useState } from 'react'
const SearchInput = ({ currentTag, currentSearch }) => {
const { locale } = useGlobal()
const [searchKey, setSearchKey] = useState(currentSearch)
const router = useRouter()
- const handleSearch = () => {
- if (searchKey && searchKey !== '') {
- router.push({ pathname: '/search', query: { s: searchKey } }).then(r => {
+ const searchInputRef = useRef()
+ const handleSearch = (key) => {
+ if (key && key !== '') {
+ router.push({ pathname: '/search', query: { s: key } }).then(r => {
})
} else {
router.push({ pathname: '/' }).then(r => {
@@ -17,22 +18,34 @@ const SearchInput = ({ currentTag, currentSearch }) => {
}
const handleKeyUp = (e) => {
if (e.keyCode === 13) {
- handleSearch()
+ handleSearch(searchInputRef.current.value)
}
}
+ const cleanSearch = () => {
+ searchInputRef.current.value = ''
+ setSearchKey('')
+ }
+
+ const updateSearchKey = (val) => {
+ setSearchKey(val)
+ }
return
setSearchKey(e.target.value)}
+ onChange={e => updateSearchKey(e.target.value)}
defaultValue={currentSearch}
/>
-
-
+ { (searchKey && searchKey.length &&
)}
+
+
{ handleSearch(searchKey) }}>
+
}