diff --git a/resources/js/components/Search/SearchBar.tsx b/resources/js/components/Search/SearchBar.tsx index a9a41c7..609e1bd 100644 --- a/resources/js/components/Search/SearchBar.tsx +++ b/resources/js/components/Search/SearchBar.tsx @@ -31,7 +31,7 @@ function Main ({responsive = true} : Props) { useClickOutside(form, () => setShowResults(false)) - const {index, navigate} = useKeyboardNavigate({ + const {index, navigate, resetIndex} = useKeyboardNavigate({ length: results && results.items.length + 1, onSelect: (index) => { window.location.href = results!.items[index]?.url ?? '/search?q=' + query; @@ -44,6 +44,11 @@ function Main ({responsive = true} : Props) { } } + const handleChange = (e: ChangeEvent) => { + setQuery(e.currentTarget.value) + resetIndex() + } + return ( <>
@@ -51,7 +56,7 @@ function Main ({responsive = true} : Props) {
setShowResults(true)} - onChange={(e: ChangeEvent) => setQuery(e.currentTarget.value)} + onChange={handleChange} className="form-control rounded-5 rounded-end radius-end-0 border border-secondary" type="search" placeholder={t("Search")} diff --git a/resources/js/components/Search/SearchModel.tsx b/resources/js/components/Search/SearchModel.tsx index fe376b0..047663c 100644 --- a/resources/js/components/Search/SearchModel.tsx +++ b/resources/js/components/Search/SearchModel.tsx @@ -2,6 +2,7 @@ import {useState, useRef} from 'preact/hooks'; import {useSearchQuery, useClickOutside, useKeyboardNavigate} from "@/hooks"; import {QueryClient, QueryClientProvider} from "@tanstack/react-query"; import {searchModel} from "@/api/clipzone"; +import {ChangeEvent} from "react"; type Props = { endpoint: string, @@ -35,11 +36,16 @@ function Main ({endpoint, name, label = null, value = null} : Props) { setShowResults(false) } - const {index, navigate} = useKeyboardNavigate({ + const {index, navigate, resetIndex} = useKeyboardNavigate({ length: results?.data.length, onSelect: select }); + const handleChange = (e: ChangeEvent) => { + setQuery(e.currentTarget.value) + resetIndex() + } + return (
@@ -47,7 +53,7 @@ function Main ({endpoint, name, label = null, value = null} : Props) { setShowResults(true)} ref={input} - onChange={(e) => setQuery(e.target.value)} + onChange={handleChange} type="search" className="form-control" id={name} diff --git a/resources/js/hooks/useKeyboardNavigate.ts b/resources/js/hooks/useKeyboardNavigate.ts index 2ded393..43fedbf 100644 --- a/resources/js/hooks/useKeyboardNavigate.ts +++ b/resources/js/hooks/useKeyboardNavigate.ts @@ -10,10 +10,15 @@ export function useKeyboardNavigate (options: Options) { const {length, onSelect} = options; const [index, setIndex] = useState(null); + + const resetIndex = () => setIndex(null); + const navigate = (e: KeyboardEvent) => { if (['ArrowDown', 'ArrowUp', 'Enter'].includes(e.key)) { + e.preventDefault(); + if (length === undefined || length === 0) { setIndex(null); return @@ -44,5 +49,5 @@ export function useKeyboardNavigate (options: Options) { } } - return {index, navigate} + return {index, navigate, resetIndex} }