diff --git a/components/BlogPostListEmpty.js b/components/BlogPostListEmpty.js index d40f51db..0cca9919 100644 --- a/components/BlogPostListEmpty.js +++ b/components/BlogPostListEmpty.js @@ -15,7 +15,7 @@ const BlogPostListEmpty = () => { }) }, 3000) }) - return
+ return

没有文章了,3秒后返回首页

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) }}> +
}