javascript - how i can map and set index 0 to true and the others to false onclick react JS
问题描述
How I can map and set index 0 to true and the others to false onclick react js this map function show the details of all tickets , the idea is when user click button show the details of each ticket , how I can return the buttons according to number of tickets then on click show one single ticket detail so how I can do that any suggestions or solutions please
state = {
result: [],
success: 'success',
show: false,
};
ltenaryHandler = () => {
this.setState({
show: !this.state.show
});
};
render() {
const arabic = this.props.intl.locale === 'ar';
const result = this.state.result.map((results, i) => {
const departureDateApi = results.flight.travelSegment[0].departureDate;
const DepartureDate = moment(departureDateApi).format('DD/MM/YYYY');
const tripSegmentDuration = results.flight.travelSegment[0].arrivalTime;
const newTripSegmentDuration = moment(tripSegmentDuration).format('HH:mm:ss');
const DateOfFlight = results.flight.travelSegment[0].departureTime;
const DateOfFlightMomented = moment(DateOfFlight)
.add(0, 'hours')
.format('LLL');
return ( <
div key = {
results.id
}
className = {
'container'
} >
<
TogglePlus onClick = {
() => this.ltenaryHandler(i)
} > {
this.state.show && ( <
div className = {
'ltenaryHandler'
} >
<
BookingConfirmation / >
<
Header display = {
this.props.display
}
test = {
i + 1
}
/> <
DepartureInfo key = {
results
}
display = {
this.props.display
}
airportDeparture = {
results.flight.travelSegment[0].departureAirport === ' ' ?
`${results.flight.travelSegment[0].departureAirport}` :
`${results.flight.travelSegment[0].departureAirport} `.replace(/ /, ' - ')
}
departure = {
DepartureDate
}
segmentTripDuration = {
newTripSegmentDuration
}
/> <
FlightDetail / >
<
FlightInside alt = {
'Logo Company'
}
flightNumberPass = {
results.flight.travelSegment[0].aircraftName
}
PNR = {
results.flight.travelSegment[0].airlinePnr
}
FlightOperator = {
results.flight.travelSegment[0].airlineName
}
departureCountry = {
results.flight.travelSegment[0].departureAirport
}
ArrivalCountry = {
results.flight.travelSegment[0].arrivalAirport
}
DateOfFlight = {
DateOfFlightMomented
}
airport = {
results.flight.travelSegment[0].departureAirport
}
arrivalAirport = {
results.flight.travelSegment[0].arrivalAirport
}
terminal = {
results.flight.travelSegment[0].departureTerminal
}
arrivalTerminal = {
results.flight.travelSegment[0].arrivalTerminal
}
/> <
FlightAmenities / >
<
Wrapper arabic = {
arabic
}
padding = {
'5px 10px'
}
background = {
'#d3d3d4'
} >
<
Desc > {
`${this.props.intl.formatMessage({
id: 'Transit_Airport',
})} `
} < /Desc> <
/Wrapper> <
FlightDetail / >
<
FlightInside alt = {
'Logo Company'
}
flightNumberPass = {
results.flight.travelSegment[0].aircraftName
}
PNR = {
results.flight.travelSegment[0].airlinePnr
}
FlightOperator = {
results.flight.travelSegment[0].airlineName
}
departureCountry = {
results.flight.travelSegment[0].departureAirport
}
ArrivalCountry = {
results.flight.travelSegment[0].arrivalAirport
}
DateOfFlight = {
DateOfFlightMomented
}
airport = {
results.flight.travelSegment[0].departureAirport
}
arrivalAirport = {
results.flight.travelSegment[0].arrivalAirport
}
terminal = {
results.flight.travelSegment[0].departureTerminal
}
arrivalTerminal = {
results.flight.travelSegment[0].arrivalTerminal
}
/> <
FlightAmenities / >
<
/div>
)
} <
/TogglePlus> <
/div>
);
});
解决方案
React 有各种关于npmjs的综合解决方案,包括我相信你所要求的,称为 Accordion。手风琴是可折叠的单选部分。
有多种 npm 解决方案,包括react-accessible-accordion(默认为手风琴)和rc-collapse(必须设置手风琴)。
或者,不使用节点模块,您的问题已在 stackoverflow 上的另一个问题中得到解答。
推荐阅读
- android - Android 错误:尝试在空对象引用上调用虚拟方法“void androidx.appcompat.app.ActionBar.hide()”
- java - 如何在 Google Kubernetes Engine 集群中启用客户端证书
- html - 如何让社交媒体句柄位于图标的右侧?
- java - 我想隐藏一个回收器视图,另一个是滚动的
- c# - 基于datagridview WPF动态添加标签和文本框
- javascript - 如何有条件地导入 react-devtools?
- cucumber - 黄瓜范围报告 - 出现错误 - java.lang.IllegalArgumentException:
- android - 华为定位工具包 - requestLocationUpdates 不会停止,即使 removeLocationUpdates 被调用
- mysql - 如何在我的 phpmyadmin 中使用 WHERE 和 ORDER BY
- python - 在python中将两个列表元素合并为一个?