我正在尝试在一个页面上使用多个弹出框,但是唯一要打开的弹出框是数组中的最后一个,无论您悬停在哪个触发元素上。 这是使用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?的回答,但我无法让它发挥作用。
知道如何让每个popover单独打开吗?
您可以在此处查看实时示例:https://codesandbox.io/s/1r1zjmj163
答案 0 :(得分:3)
你做错了。
首先。 open
声明只有一些元素存在于状态中,但它没有声明打开了哪一个。我已将新的openedPopoverId
带到状态(默认为null)。因此,在这种情况下,您必须签入Popover
组件
open={this.state.openedPopoverId === m._id}
二。您必须在mouseEnter事件上传递此值,以便:
<Typography
onMouseEnter={this.handlePopoverOpen}
变为
onMouseEnter={(e) => this.handlePopoverOpen(e, m._id)}
第三。更改事件处理程序:
handlePopoverOpen(event, popoverId) {
this.setState({
openedPopoverId: popoverId,
anchorEl: event.target,
});
}
handlePopoverClose() {
this.setState({
openedPopoverId: null,
anchorEl: null,
});
}
最终代码(已测试,有效):
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Typography from 'material-ui/Typography';
import Popover from 'material-ui/Popover';
const styles = ({
paper: {
padding: '20px',
width: '14vw',
},
popover: {
pointerEvents: 'none',
},
});
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, popoverId) {
this.setState({
openedPopoverId: popoverId,
anchorEl: event.target,
});
}
handlePopoverClose() {
this.setState({
openedPopoverId: null,
anchorEl: null,
});
}
render() {
const { classes } = this.props;
const { anchorEl, openedPopoverId } = this.state;
const multi = [
{
_id: 0,
name: 'name1',
hoverText: 'text1',
linkUrl: '#',
},
{
_id: 1,
name: 'name2',
hoverText: 'text2',
linkUrl: '#',
},
{
_id: 2,
name: 'name3',
hoverText: 'text3',
linkUrl: '#',
},
]
console.log(openedPopoverId)
return (
<div className="wrapper">
<ul>
{multi.map(m => (
<li
key={m._id}
>
<Typography
onMouseEnter={(e) => this.handlePopoverOpen(e, m._id)}
onMouseLeave={this.handlePopoverClose}
>
{m.name}
</Typography>
<Popover
className={classes.popover}
classes={{
paper: classes.paper,
}}
open={openedPopoverId === m._id}
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>
);
}
}
MultiplePopover.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(MultiplePopover);
答案 1 :(得分:0)
@asiniy 为什么要评论css&#39; pointerEvents&#39;导致弹出窗口代码中断? 如果popover内有链接,这将导致问题。 应该css控制事件??
popover: {
//pointerEvents: 'none',
},