import React, { Component } from "react";
class Filters extends Component {
state = {
form: [
{ label: "Все", name: "all", value: -1 },
{ label: "Яблоко", name: "apple", value: 0, checked: true },
{ label: "Груша", name: "pear", value: 1, checked: true },
{ label: "Арбуз", name: "watermelon", value: 2, checked: true },
{ label: "Абрикос", name: "apricot", value: 3, checked: true }
]
};
onHandleChange = e => {
const { checked, name } = e.target;
const { form } = this.state;
const index = form.findIndex(item => item.name === name);
const item = form[index];
const newForm = [...form];
newForm[index] = { ...item, checked };
this.setState({ form: newForm });
};
render() {
return (
<div className="filters">
<div className="stops-quantity">
<p className="currency__name">Выбрать</p>
{this.state.form.map(item => (
<div className="check" key={item.name}>
<input
className="stops__checked"
type="checkbox"
name={item.name}
value={item.value}
checked={item.checked}
onChange={this.onHandleChange}
/>
<label className="stops__label" htmlFor="check1">
{item.label}
</label>
</div>
))}
</div>
</div>
);
}
}
export default Filters;