0.0.1 • Published 2 years ago
react-event-intercept v0.0.1
react-event-intercept
这是一个基于react开发的dom事件拦截器。
作用是屏蔽dom子节点触发父节点的dom事件。
安装
npm i react-event-intercept
yarn add react-event-intercept
使用方法/示例
tsx
import { useRef } from 'react'
import EventIntercept from 'react-event-intercept'
import './App.less'
function App() {
const box1Ref = useRef(null)
const box2Ref = useRef(null)
const testClick = (e: any) => {
alert("触发点击事件")
}
return (
<div className="root">
<div className="box1" ref={box1Ref}>
会触发点击事件的区域
<div className="box2" ref={box2Ref}>
不会触发的区域
<div></div>
</div>
<div className="box3">
会触发点击事件的区域
</div>
</div>
<EventIntercept bindingEvents={[['click', testClick]]} bindingRef={box1Ref} ignoreRef={[box2Ref]}></EventIntercept>
</div>
)
}
export default App
less
.root {
width: 100%;
height: 100%;
background-color: #fff;
color: #fff;
}
.box1 {
width: 800px;
height: 800px;
background-color: brown;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.box2 {
width: 500px;
height: 500px;
background-color: coral;
}
.box3 {
width: 800px;
height: 100px;
background-color: blueviolet;
}
API
key | describe | type |
---|---|---|
bindingEvents | Event name and calling function. Multiple can be bound and summarized in an array. example: ['eventName1','eventFun1,'eventName2','eventFun2] | Array<[string,function]> |
bindingRef | DOM node of binding event | MutableRefObject<any> |
ignoreRef | Node of the event to intercept | Array<MutableRefObject<any>> |