From 1ca4bda4ea83a542b099c950a1f96eccaef93c61 Mon Sep 17 00:00:00 2001 From: Gabriel Cerdio Date: Thu, 7 Nov 2024 18:07:36 -0600 Subject: [PATCH] Add Search bar --- src/App.jsx | 5 ++- src/components/searchbar.jsx | 84 ++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/components/searchbar.jsx diff --git a/src/App.jsx b/src/App.jsx index d0b91ae..c5b9ecc 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -4,6 +4,7 @@ import { useRef } from 'react'; import * as THREE from 'three'; import WineButtons from './components/WineButtons'; import vinos from './vinos.jsx'; +import SearchBar from './components/searchbar.jsx'; // Componente para las luces rotativas function RotatingLights() { @@ -91,6 +92,8 @@ function App() { Types of wines + +
-

© 2024 Types of Wines. Todos los derechos reservados.

+

© 2024 Types of Wines by Cardelli. All rights reserved.

); diff --git a/src/components/searchbar.jsx b/src/components/searchbar.jsx new file mode 100644 index 0000000..66fc28d --- /dev/null +++ b/src/components/searchbar.jsx @@ -0,0 +1,84 @@ +import React, { useState } from 'react'; +import { vinos } from '../vinos'; + +function SearchBar() { + const [searchTerm, setSearchTerm] = useState(''); + const [searchResults, setSearchResults] = useState([]); + + const searchWines = (term) => { + const results = []; + + // Función recursiva para buscar en el objeto de vinos + const searchInObject = (obj) => { + if (Array.isArray(obj)) { + obj.forEach(wine => { + if (wine.toLowerCase().includes(term.toLowerCase())) { + results.push(wine); + } + }); + } else if (typeof obj === 'object') { + Object.values(obj).forEach(value => searchInObject(value)); + } + }; + + searchInObject(vinos); + return results; + }; + + const handleSearch = (e) => { + const term = e.target.value; + setSearchTerm(term); + + if (term.length > 2) { + const results = searchWines(term); + setSearchResults(results); + } else { + setSearchResults([]); + } + }; + + return ( +
+
+ + +
+ + {/* Resultados de la búsqueda */} + {searchResults.length > 0 && ( +
+
    + {searchResults.map((wine, index) => ( +
  • + {wine} +
  • + ))} +
+
+ )} +
+ ); +} + +export default SearchBar;