封装useGlobal组件,存放全局变量;
新增search页面,修复搜索页无法分页的bug
This commit is contained in:
tangly1024
2021-11-04 13:28:08 +08:00
parent 8f9d3c45e7
commit 4b53a3612c
21 changed files with 214 additions and 169 deletions

View File

@@ -1,19 +1,17 @@
import React, { useState } from 'react'
import { useLocale } from '@/lib/locale'
import { useRouter } from 'next/router'
import { useGlobal } from '@/lib/global'
import { useState } from 'react'
const SearchInput = ({ currentTag }) => {
const locale = useLocale()
const SearchInput = ({ currentTag, currentSearch }) => {
const { locale } = useGlobal()
const [searchKey, setSearchKey] = useState(currentSearch)
const router = useRouter()
const [searchValue, setSearchValue] = useState('')
const handleSearch = () => {
if (searchValue && searchValue !== '') {
router.push({ pathname: '/', query: { s: searchValue } }).then(r => {
router.reload()
if (searchKey && searchKey !== '') {
router.push({ pathname: '/search', query: { s: searchKey } }).then(r => {
})
} else {
router.push({ pathname: '/' }).then(r => {
router.reload()
})
}
}
@@ -29,8 +27,8 @@ const SearchInput = ({ currentTag }) => {
placeholder={currentTag ? `${locale.SEARCH.TAGS} #${currentTag}` : `${locale.SEARCH.ARTICLES}`}
className={'pl-2 w-full transition duration-200 leading-10 border-gray-300 bg-white text-black dark:bg-gray-900 dark:text-white'}
onKeyUp={handleKeyUp}
onChange={e => setSearchValue(e.target.value)}
defaultValue={router.query.s ?? ''}
onChange={e => setSearchKey(e.target.value)}
defaultValue={currentSearch}
/>
<div className='py-3 px-5 bg-gray-50 flex border-l dark:border-gray-700 dark:bg-gray-500 justify-center align-middle cursor-pointer'
onClick={handleSearch}>