From 8ab22de574c25586206ca27ea02bbca51035d014 Mon Sep 17 00:00:00 2001 From: Josh Heyer Date: Wed, 4 Oct 2023 21:45:41 +0000 Subject: [PATCH] tweak to finally(?) fix this behavior which has annoyed me forever (and probably never affected anyone else) --- src/components/search/index.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/components/search/index.js b/src/components/search/index.js index d96401f4aeb..d4e5f77c0fe 100644 --- a/src/components/search/index.js +++ b/src/components/search/index.js @@ -41,20 +41,30 @@ const useClickOutside = (ref, handler, events) => { }; const Search = ({ searchProduct, onSearchProductChange }) => { - const { clear, refine, query } = useSearchBox(); + const { clear, refine } = useSearchBox(); const searchBarRef = useRef(null); const [focus, setFocus] = useState(false); const inputRef = useRef(null); const searchContentRef = useRef(null); const [arrowIndex, setArrowIndex] = useState(0); - const [inputValue, setInputValue] = useState(query); + const [inputValue, setInputValue] = useState(""); const context = searchProduct ? " " + (products[searchProduct]?.name || searchProduct) : ""; + // allow typing ahead of search loading useEffect(() => { - setInputValue(query); - }, [query]); + setInputValue(inputRef.current?.value || ""); + setFocus( + inputRef.current && inputRef.current.id === document.activeElement?.id, + ); + if (inputRef.current?.value) refine(inputRef.current.value); + }, []); + + const onClear = useCallback(() => { + setInputValue(""); + clear(); + }, [clear]); const onInputChange = useCallback( (e) => { @@ -190,7 +200,7 @@ const Search = ({ searchProduct, onSearchProductChange }) => { ref={inputRef} />