import { useRouter } from 'next/router' import { useGlobal } from '@/lib/global' import { useRef, useState } from 'react' const SearchInput = ({ currentTag, currentSearch }) => { const { locale } = useGlobal() const [searchKey, setSearchKey] = useState(currentSearch) const [onLoading,setLoadingState] = useState(false) const router = useRouter() const searchInputRef = useRef() const handleSearch = (key) => { if (key && key !== '') { setLoadingState(true) router.push({ pathname: '/search', query: { s: key } }).then(r => { setLoadingState(false) }) } else { router.push({ pathname: '/' }).then(r => { }) } } const handleKeyUp = (e) => { if (e.keyCode === 13) { // 回车 handleSearch(searchInputRef.current.value) } else if (e.keyCode === 27) { // ESC cleanSearch() } } const cleanSearch = () => { searchInputRef.current.value = '' setSearchKey('') } const updateSearchKey = (val) => { setSearchKey(val) } return
updateSearchKey(e.target.value)} defaultValue={currentSearch} /> { (searchKey && searchKey.length && )}
{ handleSearch(searchKey) }}>
} export default SearchInput