|
| 1 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import { useSearchParams } from "react-router-dom"; |
| 3 | + |
| 4 | +import { useAppContext } from "@contexts/AppContext"; |
| 5 | +import { defaultCategory } from "@utils/consts"; |
| 6 | + |
1 | 7 | import { SearchIcon } from "./Icons"; |
2 | 8 |
|
3 | 9 | const SearchInput = () => { |
| 10 | + const [searchParams, setSearchParams] = useSearchParams(); |
| 11 | + |
| 12 | + const { searchText, setSearchText, setCategory } = useAppContext(); |
| 13 | + |
| 14 | + const inputRef = useRef<HTMLInputElement | null>(null); |
| 15 | + |
| 16 | + const [inputVal, setInputVal] = useState<string>(""); |
| 17 | + |
| 18 | + const handleSearchFieldClick = () => { |
| 19 | + inputRef.current?.focus(); |
| 20 | + }; |
| 21 | + |
| 22 | + const handleSearchKeyPress = (e: KeyboardEvent) => { |
| 23 | + if (e.key === "/") { |
| 24 | + e.preventDefault(); |
| 25 | + inputRef.current?.focus(); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + const clearSearch = useCallback(() => { |
| 30 | + setInputVal(""); |
| 31 | + setCategory(defaultCategory); |
| 32 | + setSearchText(""); |
| 33 | + setSearchParams({}); |
| 34 | + }, [setCategory, setSearchParams, setSearchText]); |
| 35 | + |
| 36 | + const handleEscapePress = useCallback( |
| 37 | + (e: KeyboardEvent) => { |
| 38 | + if (e.key !== "Escape") { |
| 39 | + return; |
| 40 | + } |
| 41 | + // check if the input is focused |
| 42 | + if (document.activeElement !== inputRef.current) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + inputRef.current?.blur(); |
| 47 | + |
| 48 | + clearSearch(); |
| 49 | + }, |
| 50 | + [clearSearch] |
| 51 | + ); |
| 52 | + |
| 53 | + const handleReturnPress = useCallback( |
| 54 | + (e: KeyboardEvent) => { |
| 55 | + if (e.key !== "Enter") { |
| 56 | + return; |
| 57 | + } |
| 58 | + // check if the input is focused |
| 59 | + if (document.activeElement !== inputRef.current) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const formattedVal = inputVal.trim().toLowerCase(); |
| 64 | + |
| 65 | + setCategory(defaultCategory); |
| 66 | + setSearchText(formattedVal); |
| 67 | + if (!formattedVal) { |
| 68 | + setSearchParams({}); |
| 69 | + } else { |
| 70 | + setSearchParams({ search: formattedVal }); |
| 71 | + } |
| 72 | + }, |
| 73 | + [inputVal, setCategory, setSearchParams, setSearchText] |
| 74 | + ); |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + document.addEventListener("keyup", handleSearchKeyPress); |
| 78 | + document.addEventListener("keyup", handleEscapePress); |
| 79 | + document.addEventListener("keyup", handleReturnPress); |
| 80 | + |
| 81 | + return () => { |
| 82 | + document.removeEventListener("keyup", handleSearchKeyPress); |
| 83 | + document.removeEventListener("keyup", handleEscapePress); |
| 84 | + document.removeEventListener("keyup", handleReturnPress); |
| 85 | + }; |
| 86 | + }, [handleEscapePress, handleReturnPress]); |
| 87 | + |
| 88 | + /** |
| 89 | + * Set the input value and search text to the search query from the URL |
| 90 | + */ |
| 91 | + useEffect(() => { |
| 92 | + const search = searchParams.get("search") || ""; |
| 93 | + setInputVal(search); |
| 94 | + setSearchText(search); |
| 95 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 96 | + }, []); |
| 97 | + |
4 | 98 | return ( |
5 | | - <div className="search-field"> |
6 | | - <label htmlFor="search"> |
7 | | - <SearchIcon /> |
8 | | - </label> |
| 99 | + <div className="search-field" onClick={handleSearchFieldClick}> |
| 100 | + <SearchIcon /> |
9 | 101 | <input |
| 102 | + ref={inputRef} |
10 | 103 | type="search" |
11 | 104 | id="search" |
12 | | - placeholder="Search here..." |
13 | 105 | autoComplete="off" |
| 106 | + value={inputVal} |
| 107 | + onChange={(e) => { |
| 108 | + const newValue = e.target.value; |
| 109 | + if (!newValue) { |
| 110 | + clearSearch(); |
| 111 | + return; |
| 112 | + } |
| 113 | + setInputVal(newValue); |
| 114 | + }} |
| 115 | + onBlur={() => { |
| 116 | + // ensure the input value is always in sync with the search text |
| 117 | + if (inputVal !== searchText) { |
| 118 | + setInputVal(searchText); |
| 119 | + } |
| 120 | + }} |
14 | 121 | /> |
| 122 | + {!inputVal && !searchText && ( |
| 123 | + <label htmlFor="search"> |
| 124 | + Type <kbd>/</kbd> to search |
| 125 | + </label> |
| 126 | + )} |
15 | 127 | </div> |
16 | 128 | ); |
17 | 129 | }; |
|
0 commit comments