JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="app"></div>
CSS
.filters {
border: 1px solid black;
float: left;
margin: 0;
}
.price-input {
width: 50px;
}
.size {
display: block;
}
.good {
border: 1px solid black;
margin: 10px;
float: left;
text-align: center;
}
.good img {
width: 70px;
}
Babel + JSX
const Block = ({ HeaderTag = 'h2', headerText, className = '', children }) => (
<div className={className}>
<HeaderTag>{headerText}</HeaderTag>
{children}
</div>
);
const CountryFilter = ({ value, onChange, countries }) => (
<Block headerText="Country">
<select onChange={onChange} value={value}>
<option value="">-- Country --</option>
{countries.map(n => <option key={n}>{n}</option>)}
</select>
</Block>
);
const SizeFilter = ({ value, onChange, sizes }) => (
<Block headerText="Size">
{sizes.map(n => (
<label className="size" key={n}>
<input
type="checkbox"
value={n}
checked={value.includes(n)}
onChange={onChange}
/>
{n}
</label>
))}
</Block>
);
const PriceInput = ({ index, ...props }) => (
<input className="price-input" data-index={index} {...props} />
);
const PriceFilter = ({ value, onChange }) => (
<Block headerText="Price">
<PriceInput value={value[0]} onChange={onChange} index="0" />
-
<PriceInput value={value[1]} onChange={onChange} index="1" />
usd
</Block>
);
const GoodsList = ({ goods }) => (
<div>
{goods.map(n => (
<Block className="good" HeaderTag="h3" headerText={n.name} key={n.id}>
<img src={n.image} />
<p>Цена: {n.cost}</p>
<button data-art={n.name}>Купить</button>
</Block>
))}
</div>
);
const App = ({ goods }) => {
const countries = React.useMemo(() => [...new Set(goods.map(n => n.country))], [ goods ]);
const sizes = React.useMemo(() => [...new Set(goods.map(n => n.size))], [ goods ]);
const [ country, setCountry ] = React.useState('');
const [ size, setSize ] = React.useState([]);
const [ price, setPrice ] = React.useState([ '', '' ]);
const onCountryChange = e => setCountry(e.target.value);
const onSizeChange = ({ target: { checked, value } }) => {
setSize((!size.includes(value) && checked)
? [...