Last active
May 1, 2022 22:00
-
-
Save pushkar100/d51e7209be51c8334f1e1e3509b0ba20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useState } from "react"; | |
| const ProductSearchFilter = ({ | |
| products = [], | |
| colors = [], | |
| sizes = [], | |
| onSearch = () => {} | |
| }) => { | |
| const [product, setProduct] = useState(null); | |
| const [selectedColors, setSelectedColors] = useState([]); | |
| return ( | |
| <> | |
| <div> | |
| {products.map((product) => ( | |
| <label for={product}> | |
| <input | |
| type="radio" | |
| name="product" | |
| value={product} | |
| onChange={(e) => e.target.checked && setProduct(product)} | |
| /> | |
| {product} | |
| </label> | |
| ))} | |
| </div> | |
| <div> | |
| {colors.map((color) => ( | |
| <label for={color}> | |
| <input | |
| type="checkbox" | |
| name="selectedcolors" | |
| value={color} | |
| onChange={(e) => | |
| e.target.checked | |
| ? setSelectedColors([color, ...selectedColors]) | |
| : setSelectedColors(selectedColors.filter((c) => c !== color)) | |
| } | |
| /> | |
| {color} | |
| </label> | |
| ))} | |
| </div> | |
| <button onClick={() => onSearch(product, selectedColors)}>Search</button> | |
| </> | |
| ); | |
| }; | |
| export default ProductSearchFilter; | |
| /** | |
| * Usage: | |
| <ProductSearchFilter | |
| products={["t-shirt", "shorts", "jeans"]} | |
| colors={["red", "white", "black"]} | |
| onSearch={() => ...hit the api...} | |
| /> | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment