Skip to content

Instantly share code, notes, and snippets.

@pushkar100
Last active May 1, 2022 22:00
Show Gist options
  • Select an option

  • Save pushkar100/d51e7209be51c8334f1e1e3509b0ba20 to your computer and use it in GitHub Desktop.

Select an option

Save pushkar100/d51e7209be51c8334f1e1e3509b0ba20 to your computer and use it in GitHub Desktop.
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