【发布时间】:2020-02-11 07:19:55
【问题描述】:
我有以下菜单:
因此,理想情况下,您应该单击这 3 个点,这会在其旁边打开另一个框,我决定使用弹出框(https://material-ui.com/components/popover/).. 问题是当您单击 3 个点时没有任何反应。我认为这是因为 onClick 函数返回了一个功能性弹出框组件,但没有显示出来。我在功能组件中放入了调试器和警报,根本没有命中。
这就是那三个点
<IconButton
aria-describedby="simple-popover"
variant="contained"
onClick={e => this.moreClick(e, props.children)}
>
<More />
</IconButton>
这是moreClick功能
moreClick = (e, officeAccount) => {
return (
<AccountFavoritesPopover element={e} officeAccount={officeAccount} />
);
};
这是整个弹出窗口
import React from "react";
import Popover from "@material-ui/core/Popover";
export default function AccountFavoritesPopover(element, officeAccount) {
const anchorEl = element;
const open = Boolean(anchorEl);
const id = open ? "simple-popover" : undefined;
return (
<div>
<Popover
id={id}
open={open}
anchorEl={anchorEl}
//onClose={alert}
anchorOrigin={{
vertical: "top",
horizontal: "right"
}}
transformOrigin={{
vertical: "top",
horizontal: "left"
}}
>
<div>{officeAccount}</div>
</Popover>
</div>
);
}
弹出框必须在主文件中吗?目前这 3 个点在主文件中,而 AccountFavoritesPopover 是一个完全独立的文件。
我试图将“AccountFavoritesPopover”代码放在主文件中,但我无法在主文件中使用 useState,因为它是一个类。此外,我无法将其转换为实际状态并使用 setState,因为一旦 setState 启动,菜单就会消失...
编辑: 下面是菜单
<Creatable
id="Select"
menuIsOpen={this.state.Focused}
components={{
Option: this.Option
}}
/>
下面是菜单里面的选项
<div style={{ position: "relative" }}>
<components.Option {...props} />
<div id="MoreBox">
<IconButton
aria-describedby="simple-popover"
variant="contained"
onClick={e => this.moreClick(e, props.children)}
>
<More />
</IconButton>
</div>
</div>
【问题讨论】:
标签: reactjs components material-ui popover react-component