我正在尝试模拟一种类似于在模式弹出窗口打开时单击叠加层的行为。在 sidenav 组件外部单击时,我想关闭当前处于某个flyout模式的所有元素。
我有一个多层嵌套导航菜单,存储在它自己的组件中,Sidebar. 我有以下代码处理clicks发生在Sidebar组件外部的代码:
class Sidebar extends React.Component {
...
handleClick = (e) => {
if (this.node.contains(e.target)) {
return;
}
console.log('outside');
};
componentDidMount() {
window.addEventListener('mousedown', this.handleClick, false);
}
componentWillUnmount() {
window.removeEventListener('mousedown', this.handleClick, false);
}
render() {
return (
<div
ref={node => this.node = node}
className="sidebar"
data-color={this.props.bgColor}
data-active-color={this.props.activeColor}
>
{renderSideBar()}
</div>
);
}
...
}
这部分工作正常 - 但是当弹出菜单在单击父菜单选项时显示时,我希望它关闭当前打开的任何弹出菜单。
-|
|
- Menu Item 1
|
|-option 1 (currently open)
|-option 2
- Menu Item 2
|
|-option 1 (closed)
|-option 2 (closed, clicked to expand - this is when it should close [Menu Item 1/Option 1]
<li>在映射包含菜单结构的数据对象时,使用标签生成菜单项。
有没有一种方法可以基本上选择所有具有 'collapse' / aria-expanded="true" 类的注册对象并将其删除?类似于如何jQuery选择 dom 元素并操作它们。
我知道这不是 React 工作的前提,它只是我想要模拟的行为的一个例子。
隔江千里
相关分类