mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
78 lines
2.5 KiB
JavaScript
78 lines
2.5 KiB
JavaScript
import { useRouter } from 'next/router'
|
|
import { useImperativeHandle, useRef, useState } from 'react'
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
import { faSearch, faSpinner, faTimes } from '@fortawesome/free-solid-svg-icons'
|
|
|
|
const SearchInput = ({ currentTag, currentSearch, cRef, className }) => {
|
|
const [searchKey, setSearchKey] = useState(currentSearch || getSearchKey() || '')
|
|
const [onLoading, setLoadingState] = useState(false)
|
|
const router = useRouter()
|
|
const searchInputRef = useRef()
|
|
useImperativeHandle(cRef, () => {
|
|
return {
|
|
focus: () => {
|
|
searchInputRef?.current?.focus()
|
|
}
|
|
}
|
|
})
|
|
|
|
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 <div className={'flex w-full bg-gray-100 ' + className}>
|
|
<input
|
|
ref={searchInputRef}
|
|
type='text'
|
|
className={'w-full text-sm pl-2 transition focus:shadow-lg font-light leading-10 text-black bg-gray-100 dark:bg-gray-900 dark:text-white'}
|
|
onKeyUp={handleKeyUp}
|
|
onChange={e => updateSearchKey(e.target.value)}
|
|
defaultValue={searchKey}
|
|
/>
|
|
|
|
<div className='-ml-8 cursor-pointer dark:bg-gray-600 dark:hover:bg-gray-800 float-right items-center justify-center py-2'
|
|
onClick={() => { handleSearch(searchKey) }}>
|
|
<FontAwesomeIcon spin={onLoading} icon={onLoading ? faSpinner : faSearch} className='hover:text-black transform duration-200 text-gray-500 cursor-pointer' />
|
|
</div>
|
|
|
|
{(searchKey && searchKey.length &&
|
|
<div className='-ml-12 cursor-pointer dark:bg-gray-600 dark:hover:bg-gray-800 float-right items-center justify-center py-2'>
|
|
<FontAwesomeIcon icon={faTimes} className='hover:text-black transform duration-200 text-gray-400 cursor-pointer' onClick={cleanSearch} />
|
|
</div>
|
|
)}
|
|
</div>
|
|
}
|
|
|
|
function getSearchKey () {
|
|
const router = useRouter()
|
|
if (router.query && router.query.s) {
|
|
return router.query.s
|
|
}
|
|
return null
|
|
}
|
|
|
|
export default SearchInput
|