import { useGlobal } from '@/lib/global' import { useRouter } from 'next/router' import { useImperativeHandle, useRef, useState } from 'react' const SearchInput = (props) => { const { keyword, cRef } = props const [onLoading, setLoadingState] = useState(false) const { locale } = useGlobal() const router = useRouter() const searchInputRef = useRef() useImperativeHandle(cRef, () => { return { focus: () => { searchInputRef?.current?.focus() } } }) /** * 搜索 */ const handleSearch = () => { const key = searchInputRef.current.value if (key && key !== '') { setLoadingState(true) router.push({ pathname: '/search/' + key }).then(r => { setLoadingState(false) }) // location.href = '/search/' + key } else { router.push({ pathname: '/' }).then(r => { }) } } /** * 监听案件 * @param {*} e */ const handleKeyUp = (e) => { if (e.keyCode === 13) { // 回车 handleSearch(searchInputRef.current.value) } else if (e.keyCode === 27) { // ESC cleanSearch() } } /** * 清理索索 */ const cleanSearch = () => { searchInputRef.current.value = '' } return
{(keyword && keyword.length &&
)}
} export default SearchInput