Skip to content

Commit

Permalink
tweak to finally(?) fix this behavior which has annoyed me forever
Browse files Browse the repository at this point in the history
(and probably never affected anyone else)
  • Loading branch information
josh-heyer committed Oct 4, 2023
1 parent aa87a32 commit 8ab22de
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/components/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -190,7 +200,7 @@ const Search = ({ searchProduct, onSearchProductChange }) => {
ref={inputRef}
/>
<ClearButton
onClick={clear}
onClick={onClear}
className={`${inputValue.length === 0 && "d-none"}`}
/>
<SlashIndicator query={inputValue} />
Expand Down

1 comment on commit 8ab22de

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.