diff --git a/themes/fukasawa/components/SearchInput.js b/themes/fukasawa/components/SearchInput.js index 6290415c..a74ece78 100644 --- a/themes/fukasawa/components/SearchInput.js +++ b/themes/fukasawa/components/SearchInput.js @@ -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
updateSearchKey(e.target.value)} - defaultValue={searchKey} + defaultValue={keyword || ''} />
{ handleSearch(searchKey) }}> + onClick={handleSearch}>
- {(searchKey && searchKey.length && + {(keyword && keyword.length &&
diff --git a/themes/fukasawa/index.js b/themes/fukasawa/index.js index 90df9453..341e9e31 100644 --- a/themes/fukasawa/index.js +++ b/themes/fukasawa/index.js @@ -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 + return } /** diff --git a/themes/gitbook/components/BlogPostCard.js b/themes/gitbook/components/BlogPostCard.js index b481aec4..bb47fcc2 100644 --- a/themes/gitbook/components/BlogPostCard.js +++ b/themes/gitbook/components/BlogPostCard.js @@ -10,7 +10,7 @@ const BlogPostCard = ({ post, className }) => {
- {post.title} + {post.title}
diff --git a/themes/next/components/SearchInput.js b/themes/next/components/SearchInput.js index 21106e74..ff763836 100644 --- a/themes/next/components/SearchInput.js +++ b/themes/next/components/SearchInput.js @@ -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 || ''} />