当前位置: 首页 > news >正文

react控制react Popover组件显示隐藏

1.antd版本

"antd": "^3.26.16",

2.完整代码(因为要使用Modal.confirm,不使用e.target.closest查找class不太好控制)

import React, { Component } from "react";
import { Select, Popover, DatePicker, Button, Modal } from "antd";// 在不使用React.createRef的情况下,点击外层气泡外层隐藏,内层保持显示
export default class TestPopover extends Component {constructor(...args) {super(...args);this.state = {isShow: false}this.popoverRef = null; // 用于存储 Popover 内容区域的 DOMthis.triggerRef = null; // 用于存储触发按钮的 DOM}componentWillMount() {// 使用mousedown速度比click快document.addEventListener("mousedown", this.onOutsideClick);}componentWillReceiveProps(nextProps) {}componentWillUnmount() {document.removeEventListener("mousedown", this.onOutsideClick);}// 全局点击处理onOutsideClick = (e) => {// 是否点击了【操作日志】按钮if (this.triggerRef?.contains(e.target)) {return;}// 是否点击了内容if (this.popoverRef?.contains(e.target)){return;}// e.target.closest是查找当前级和父级dom是否存在// antd弹窗(试了只能匹配class才比较好控制)if(e.target.closest(".ant-modal-wrap")){return;}// 其他情况 → 关闭this.setState({ isShow: false });};onDelete = ()=>{Modal.confirm({title: `确认删除吗?`,content: "",// centered: true,zIndex: 1500,okText: "确认",cancelText: "取消",onOk: () => {}})}togglePopover = ()=>{this.setState({isShow:!this.state.isShow})}render() {const content =  (<div style={{ width: "1100px"}} ref={(node) => (this.popoverRef = node)}><div><div><div><Selectstyle={{width:"200px"}}allowClear placeholder={"请选择"}{/*下拉列表默认添加到body,绑定到父组件,避免点击下拉框隐藏当前content*/}getPopupContainer={triggerNode => triggerNode.parentNode}><Select.Option value="all" key="all">全部</Select.Option></Select></div><div><DatePicker{/*下拉列表默认添加到body,绑定到父组件,避免点击下拉框隐藏当前content*/}getCalendarContainer={triggerNode => triggerNode.parentNode}allowClear placeholder={"请选择"}/></div></div></div><div><Button onClick={this.onDelete} type="danger">删除</Button><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div><div><Button>测试</Button></div></div></div>);return (<Popoverplacement="bottomRight"content={content}visible={this.state.isShow}trigger="click"><div  ref={(node) => (this.triggerRef = node)}><Button onClick={this.togglePopover}>切换气泡</Button></div></Popover>)}
}

http://www.xdnf.cn/news/1143433.html

相关文章:

  • 【怜渠客】简单实现手机云控Windows电脑锁屏
  • 力扣面试150(33/150)
  • pytest + requests 接口自动化测试框架
  • UE 一些基础的python
  • AE MDX L6 L12 L18 电源手侧操作使用说明
  • 在git中同时配置gitcode和github访问权限
  • 40.限流规则
  • 【世纪龙科技】汽车故障诊断与排除仿真教学软件让课堂更高效安全
  • openinstall九周年:聚焦安全防御,护航业务持续增长
  • 注册表单案例
  • 低空感知技术
  • 服务端高并发方案设计
  • python学智能算法(二十四)|SVM-最优化几何距离的理解
  • git--gitlab
  • 【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - jieba库分词简介及使用
  • Python函数全解析:从基础到高阶实战
  • 无符号乘法运算的硬件逻辑实现 ————取自《湖科大教书匠》
  • 【面试】面试官:请举例回答Elasticsearch的数据模型有哪些核心概念?
  • VSCode - VSCode 快速跳转标签页
  • setTimeout、setInterval、requestAnimationFrame的使用以及区别
  • 广东省省考备考(第四十九天7.18)——资料分析(知识点回顾与练习)
  • centos7安装MySQL8.4手册
  • 产品需求文档(PRD)格式全解析:从 RP 到 Word 的选择与实践
  • 【服务器与部署 12】数据库生产环境部署实战:MySQL、PostgreSQL、Redis高可用配置全攻略
  • 【世纪龙科技】汽车故障诊断与排除仿真教学软件
  • uni-app 跳转页面传参
  • 图机器学习(13)——图相似性检测
  • 西门子工业软件全球高级副总裁兼大中华区董事总经理梁乃明先生一行到访庭田科技
  • OpenTelemetry学习笔记(四):OpenTelemetry 语义约定,即字段映射(1)
  • Simulink建模-Mux与Demux模块虚拟向量的组装与拆解