diff --git a/src/components/search-bar/search-bar.jsx b/src/components/search-bar/search-bar.jsx new file mode 100644 index 0000000..fcfd737 --- /dev/null +++ b/src/components/search-bar/search-bar.jsx @@ -0,0 +1,67 @@ +import { useMemo, useRef, useState } from "react"; +import { BiSearchAlt } from "react-icons/Bi"; + +function SearchBar() { + const [items, setItems] = useState([]); + const [query, setQuery] = useState(""); + const inputRef = useRef(); + + const filteredItems = useMemo(() => { + return items.filter((item) => { + return item.toLowerCase().includes(query.toLowerCase()); + }); + }, [items, query]); + + function onSubmit(e) { + e.preventDefault(); + + const value = inputRef.current.value; + if (value === "") return; + setItems((prev) => { + return [...prev, value]; + }); + + inputRef.current.value = ""; + } + + return ( + <> +
+
+
+ + setQuery(e.target.value)} + type='search' + style={{ padding: "6px 28px", width: "100%" }} + /> +
+ + + +
+ +
+
+ New Item:{" "} + +
+

Items:

+ {filteredItems.map((item, index) => ( +
{item}
+ ))} +
+ + ); +} + +export default SearchBar; diff --git a/src/layout/Layout.jsx b/src/layout/Layout.jsx index 0cc1e4a..fb9fa79 100644 --- a/src/layout/Layout.jsx +++ b/src/layout/Layout.jsx @@ -1,5 +1,6 @@ import Footer from "@/components/footer/Footer"; import Navbar from "@/components/navbar/Navbar"; +import SearchBar from "@/components/search-bar/search-bar"; export default function Layout({ children }) { return ( @@ -7,6 +8,7 @@ export default function Layout({ children }) { {children}