class ServiceCard extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
isServiceFormOpen: false,
};
}
clickServiceCardBtn() {
this.setState({
isServiceFormOpen: true,
});
}
render() {
return (
<div className={cn("service-card", { "--opened": this.state.isServiceFormOpen })}>
<button
className="service-card__btn"
onClick={this.clickServiceCardBtn}
>
Кнопка
</button>
</div>
)}
}
<div className="service-list">
{serviceCards.map((card, index) => {
return (
<React.Fragment key={"service-card" + index}>
<ServiceCard
clickServiceCard={this.clickServiceCard}
extraClass="service-list__card"
/>
</React.Fragment>
);
})}
</div>
this.state = {
openedServiceFormIndex: -1,
};
clickServiceCard(index) {
this.setState(
prevState => {
if (index === prevState.openedServiceFormIndex) {
return {
openedServiceFormIndex: -1,
}
} else {
return {
openedServiceFormIndex: index,
}
}
}
);
}
render() {
<div className="service-list">
{serviceCards.map((card, index) => {
return (
<React.Fragment key={"service-card" + index}>
<ServiceCard
opened={index === this.state.openedServiceCardIndex}
clickServiceCard={() => this.clickServiceCard(index)}
extraClass="service-list__card"
/>
</React.Fragment>
);
})}
</div>
}
render() {
return (
<div className={cn("service-card", { "--opened": this.props.opened})}>
<button
className="service-card__btn"
onClick={this.props.clickServiceCard}
>
Кнопка
</button>
</div>
)}
}