我有一个包含一些详细信息列表的组件。组件中的每个列表都有一个详细信息按钮。当用户点击按钮时。它将显示列表的附加信息。在这里,我将 Material-Ui 与 react 一起使用,并为此目的导入了 Collapse 组件。因此,正如您在下面看到我的代码,当我单击一个列表中的详细信息按钮时,它将打开所有列表的详细信息。我只想打开列表,我点击。请检查我下面的代码
提前致谢。
import React, { Component } from 'react';
import Collapse from '@material-ui/core/Collapse';
import Button from '@material-ui/core/Button';
class Collapsed extends Component {
constructor(props){
super(props);
this.state = {
expanded: {},
details: [
{
id: 0,
name: 'Tony Stark',
role: 'Iron Man'
},
{
id: 1,
name: 'Steve Rogers',
role: 'Captain America',
},
{
id: 2,
name: 'Thor',
role: 'God of Thunder'
}
]
}
}
handleExpandClick = (id) => {
const expended = this.state.expended;
expended[id] = expended.hasOwnProperty(id) ? !expended[id] : true
this.setState({ expended });
}
render() {
const { details, expanded } = this.state;
return (
<div>
{details.map((detail)=>(
<div key={detail.id}>
{detail.name}
<Button
variant="contained"
disableRipple
onClick={() => this.handleExpandClick(detail.id)}
aria-expanded={expanded}
aria-label="Show more"
>
Details
</Button>
<Collapse in={expanded[detail.id]} timeout="auto" unmountOnExit>
{detail.role}
</Collapse>
</div>
))}
</div>
)
}
}
export default Collapsed