diff --git a/src/components/SearchResults/index.tsx b/src/components/SearchResults/index.tsx index b3bc9673ef..c728492225 100644 --- a/src/components/SearchResults/index.tsx +++ b/src/components/SearchResults/index.tsx @@ -1,4 +1,4 @@ -import { useMemo, useRef, useState } from "preact/hooks"; +import { useMemo, useRef, useState, useEffect } from "preact/hooks"; import { Icon } from "../Icon"; type SearchResult = { @@ -23,9 +23,19 @@ const SearchResults = ({ uiTranslations, }: SearchResultProps) => { const inputRef = useRef(null); + const clearButtonRef = useRef(null); + const submitButtonRef = useRef(null); const [currentFilter, setCurrentFilter] = useState(""); const [isInputEdited, setInputEdited] = useState(false); + useEffect(() => { + if (searchTerm && clearButtonRef.current) { + clearButtonRef.current.focus(); + } else if (!searchTerm && inputRef.current) { + inputRef.current.focus(); + } + }, [searchTerm]); + const allUniqueCategoriesForResults = useMemo(() => { const categories = results.map((result) => result.category); return [...new Set(categories)]; @@ -59,46 +69,12 @@ const SearchResults = ({ } }; - const renderFilterByOptions = () => { - if (results.length === 0) { - return null; - } - - return ( -
-

Filter by

-
    - {allUniqueCategoriesForResults.map((category) => ( -
  • - -
  • - ))} -
-
- ); - }; - const clearInput = () => { if (inputRef.current) { inputRef.current.value = ""; } }; + const submitInput = () => { if (inputRef.current) { onSearchChange(inputRef.current.value); @@ -130,6 +106,7 @@ const SearchResults = ({ /> {isInputEdited ? ( + + ))} + + + ); + }; + const renderResults = () => { if (results.length === 0) { return (