theme-fix-search

This commit is contained in:
tangly1024
2023-07-09 16:21:43 +08:00
parent 5beab016c2
commit 715ec43ad3
4 changed files with 27 additions and 31 deletions

View File

@@ -1,8 +1,8 @@
import { useRouter } from 'next/router'
import { useImperativeHandle, useRef, useState } from 'react'
const SearchInput = ({ currentTag, currentSearch, cRef }) => {
const [searchKey, setSearchKey] = useState(currentSearch || '')
const SearchInput = (props) => {
const { keyword, cRef } = props
const [onLoading, setLoadingState] = useState(false)
const router = useRouter()
const searchInputRef = useRef()
@@ -13,17 +13,28 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => {
}
}
})
const handleSearch = (key) => {
/**
* 搜索
*/
const handleSearch = () => {
const key = searchInputRef.current.value
if (key && key !== '') {
setLoadingState(true)
router.push({ pathname: '/search', query: { s: key } }).then(r => {
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)
@@ -31,43 +42,29 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => {
cleanSearch()
}
}
/**
* 清理索索
*/
const cleanSearch = () => {
searchInputRef.current.value = ''
setSearchKey('')
}
let lock = false
const updateSearchKey = (val) => {
if (!lock) {
setSearchKey(val)
}
}
function lockSearchInput () {
lock = true
}
function unLockSearchInput () {
lock = false
}
return <div className='flex w-full bg-gray-100'>
<input
ref={searchInputRef}
type='text'
className={'outline-none w-full text-sm pl-2 transition focus:shadow-lg font-light leading-10 text-black bg-gray-100 dark:bg-gray-800 dark:text-white'}
onKeyUp={handleKeyUp}
onCompositionStart={lockSearchInput}
onCompositionUpdate={lockSearchInput}
onCompositionEnd={unLockSearchInput}
onChange={e => updateSearchKey(e.target.value)}
defaultValue={searchKey}
defaultValue={keyword || ''}
/>
<div className='-ml-8 cursor-pointer float-right items-center justify-center py-2'
onClick={() => { handleSearch(searchKey) }}>
onClick={handleSearch}>
<i className={`hover:text-black transform duration-200 text-gray-500 cursor-pointer fas ${onLoading ? 'fa-spinner animate-spin' : 'fa-search'}`} />
</div>
{(searchKey && searchKey.length &&
{(keyword && keyword.length &&
<div className='-ml-12 cursor-pointer dark:bg-gray-600 dark:hover:bg-gray-800 float-right items-center justify-center py-2'>
<i className='hover:text-black transform duration-200 text-gray-400 cursor-pointer fas fa-times' onClick={cleanSearch} />
</div>

View File

@@ -146,7 +146,7 @@ const LayoutSlug = (props) => {
/**
* 搜索页
*/
const LayoutSearch = (props) => {
const LayoutSearch = props => {
const { keyword } = props
const router = useRouter()
useEffect(() => {
@@ -162,7 +162,7 @@ const LayoutSearch = (props) => {
}
}, 300)
}, [router])
return <LayoutPostList />
return <LayoutPostList {...props}/>
}
/**

View File

@@ -10,7 +10,7 @@ const BlogPostCard = ({ post, className }) => {
<div key={post.id} className={`${className} py-1 cursor-pointer px-2 hover:bg-gray-50 rounded-md dark:hover:bg-gray-600 ${currentSelected ? 'bg-green-50 text-green-500' : ''}`}>
<div className="flex flex-col w-full">
<Link href={`${BLOG.SUB_PATH}/${post.slug}`} passHref>
<a>{post.title}</a>
{post.title}
</Link>
</div>
</div>

View File

@@ -4,9 +4,8 @@ import { useImperativeHandle, useRef, useState } from 'react'
let lock = false
const SearchInput = ({ currentTag, currentSearch, cRef }) => {
const SearchInput = ({ currentTag, keyword, cRef }) => {
const { locale } = useGlobal()
// const [searchKey, setSearchKey] = useState(currentSearch || '')
const [onLoading, setLoadingState] = useState(false)
const router = useRouter()
const searchInputRef = useRef()
@@ -77,7 +76,7 @@ const SearchInput = ({ currentTag, currentSearch, cRef }) => {
onCompositionUpdate={lockSearchInput}
onCompositionEnd={unLockSearchInput}
onChange={e => updateSearchKey(e.target.value)}
defaultValue={currentSearch || ''}
defaultValue={keyword || ''}
/>
<div className='-ml-8 cursor-pointer float-right items-center justify-center py-2'