From 04815c09ef5c4d7c1eb065f58d552f29a824031f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20K=C3=B6pp?= Date: Thu, 14 Nov 2024 12:32:55 +0100 Subject: [PATCH] Add feature SearchBar --- frontend/src/components/ProductView.tsx | 32 ++++++++++++++++++------- frontend/src/components/SearchBar.css | 30 +++++++++++++++++++++++ frontend/src/components/SearchBar.tsx | 30 +++++++++++++++++++++++ 3 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 frontend/src/components/SearchBar.css create mode 100644 frontend/src/components/SearchBar.tsx diff --git a/frontend/src/components/ProductView.tsx b/frontend/src/components/ProductView.tsx index bec7273..deda54c 100644 --- a/frontend/src/components/ProductView.tsx +++ b/frontend/src/components/ProductView.tsx @@ -3,10 +3,12 @@ import ProductCard from "./ProductCard.tsx"; import axios from "axios"; import {useEffect, useState} from "react"; import {Product} from "../Product.ts"; +import SearchBar from "./SearchBar.tsx" export default function ProductView() { - const [products, setProducts] = useState([]) + const [products, setProducts] = useState([]); + const [filteredProducts, setFilteredProducts] = useState([]); function fetchAllProducts() { axios({ @@ -15,10 +17,23 @@ export default function ProductView() { }) .then((response) => { - setProducts(response.data) - }) + setProducts(response.data); + setFilteredProducts(response.data); + }); } + const filterProducts = (query: string) => { + if (query.trim() === "") { + setFilteredProducts(products); + } else { + const lowercasedQuery = query.toLowerCase(); + const filtered = products.filter((product) => + product.name.toLowerCase().includes(lowercasedQuery) + ); + setFilteredProducts(filtered); + } + }; + useEffect(() => { fetchAllProducts(); }, []); @@ -26,13 +41,12 @@ export default function ProductView() { return (

Products

+ {/* Add the SearchBar component */}
- { - products.map((product) => { - return - }) - } + {filteredProducts.map((product) => { + return ; + })}
); -}; \ No newline at end of file +} \ No newline at end of file diff --git a/frontend/src/components/SearchBar.css b/frontend/src/components/SearchBar.css new file mode 100644 index 0000000..4ff849b --- /dev/null +++ b/frontend/src/components/SearchBar.css @@ -0,0 +1,30 @@ +.search-bar { + display: flex; + justify-content: center; + margin: 20px 0; +} + +.search-input { + width: 300px; + padding: 10px; + font-size: 16px; + border: 2px solid mediumpurple; + border-radius: 4px; + outline: none; + background-color: black; + transition: box-shadow 0.3s ease-in-out, background-color 0.3s ease-in-out, text-shadow 0.3s ease-in-out; +} + +.search-input:hover { + border-color: deeppink; + box-shadow: 0 0 10px rgba(0, 123, 255, 0.75); + background-color: rgba(0, 123, 255, 0.1); + text-shadow: 0 0 5px rgba(0, 123, 255, 0.75); +} + +.search-input:focus { + box-shadow: 0 0 15px rgba(0, 123, 255, 1); + border-color: #007bff; + background-color: rgba(0, 123, 255, 0.15); + text-shadow: 0 0 8px rgba(0, 123, 255, 1); +} \ No newline at end of file diff --git a/frontend/src/components/SearchBar.tsx b/frontend/src/components/SearchBar.tsx new file mode 100644 index 0000000..a9a20e6 --- /dev/null +++ b/frontend/src/components/SearchBar.tsx @@ -0,0 +1,30 @@ +import React, { useState } from "react"; +import './SearchBar.css'; + +type SearchBarProps = { + onSearch: (query: string) => void; +}; + +const SearchBar = ({ onSearch }: SearchBarProps) => { + const [query, setQuery] = useState(''); + + const handleChange = (e: React.ChangeEvent) => { + const value = e.target.value; + setQuery(value); + onSearch(value); // Trigger filtering when the user types + }; + + return ( +
+ +
+ ); +}; + +export default SearchBar; \ No newline at end of file