Browse Source

enable search via tags

master
azri 2 months ago
parent
commit
f1b909f8ab
1 changed files with 17 additions and 5 deletions
  1. 17
    5
      src/components/SearchProduct/SearchProduct.jsx

+ 17
- 5
src/components/SearchProduct/SearchProduct.jsx View File

@@ -12,20 +12,32 @@ import {
12 12
 } from "@mui/material";
13 13
 import { useSelector } from "react-redux";
14 14
 
15
+function doesSubstringExistInArray(array, substring) {
16
+    
17
+    if(array?.length == 0 || !array) return
18
+
19
+    // Convert substring to lowercase for case-insensitive matching
20
+    const lowerCaseSubstring = substring.toLowerCase();
21
+    // Check each element in the array
22
+    return array.some(element => element.toLowerCase().includes(lowerCaseSubstring));
23
+}
24
+
15 25
 const SearchProduct = ({ onClose }) => {
16 26
   const products = useSelector((state) => state.products.products.data);
17 27
   const [searchedProducts, setSearchedProducts] = useState([]);
18 28
   const [searchQuery, setSearchQuery] = useState("");
19 29
 
20 30
   const filterProduct = function () {
31
+
32
+    if(searchQuery?.length < 2) return
33
+
21 34
     let filteredProducts = products.filter((product) => {
35
+
22 36
       let title = product?.title?.toLowerCase();
23 37
       let search = searchQuery.toLowerCase();
24
-      return title?.includes(search);
38
+      return title?.includes(search) || doesSubstringExistInArray(product?.tags, search);
25 39
     });
26 40
 
27
-    // MAX show result
28
-    filteredProducts = filteredProducts?.slice(0, 5);
29 41
     setSearchedProducts(filteredProducts);
30 42
   };
31 43
 
@@ -61,7 +73,7 @@ const SearchProduct = ({ onClose }) => {
61 73
           <Grid size={12}>
62 74
             <TextField
63 75
               fullWidth
64
-              placeholder="Search for a product..."
76
+              placeholder="Search Products Name or Tags..."
65 77
               variant="outlined"
66 78
               value={searchQuery}
67 79
               onChange={(e) => {
@@ -92,7 +104,7 @@ const SearchProduct = ({ onClose }) => {
92 104
       </Box>
93 105
 
94 106
       {/* Product List */}
95
-      <Grid container sx={{ width: "100%" }} columns={12}>
107
+      <Grid container sx={{ width: "100%", overflowY:"scroll" }} columns={12}>
96 108
         {searchedProducts.length > 0 ? (
97 109
           searchedProducts.map((product) => {
98 110
 

Loading…
Cancel
Save