【发布时间】:2018-10-23 21:10:48
【问题描述】:
我正在尝试在一个页面上使用多个弹出框,但唯一要打开的弹出框是数组中的最后一个,无论您将鼠标悬停在哪个触发元素上。 这是使用 Material-UI v1.0.0-beta.46。
class MultiplePopover extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
open: false,
anchorEl: null,
};
this.handlePopoverOpen = this.handlePopoverOpen.bind(this);
this.handlePopoverClose = this.handlePopoverClose.bind(this);
}
handlePopoverOpen(event) {
this.setState({
anchorEl: event.target,
});
}
handlePopoverClose() {
this.setState({
anchorEl: null,
});
}
render() {
const { classes } = this.props;
const { anchorEl } = this.state;
const open = !!anchorEl;
const multi = [
{
_id: 0,
name: 'name1',
hoverText: 'text1',
linkUrl: '#',
},
{
_id: 1,
name: 'name2',
hoverText: 'text2',
linkUrl: '#',
},
{
_id: 2,
name: 'name3',
hoverText: 'text3',
linkUrl: '#',
},
]
return (
<div className="wrapper">
<ul>
{multi.map(m => (
<li
key={m._id}
>
<Typography
onMouseEnter={this.handlePopoverOpen}
onMouseLeave={this.handlePopoverClose}
>
{m.name}
</Typography>
<Popover
className={classes.popover}
classes={{
paper: classes.paper,
}}
open={open}
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
transformOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
>
<Typography>
<a
href="{m.linkUrl}"
target=" /blank"
>
{m.hoverText}
</a>
</Typography>
</Popover>
</li>
))}
</ul>
</div>
);
}
}
我已尝试按照这篇帖子 Popover doesn't work if you have many of them on one page. How to manage them? 的答案进行操作,但无法正常工作。
知道如何让每个弹出框分别打开吗?
你可以在这里看到一个活生生的例子:https://codesandbox.io/s/1r1zjmj163
【问题讨论】:
标签: reactjs react-redux material-ui