From faea604cdf0d53d506c765226a13cc29f7fd15c5 Mon Sep 17 00:00:00 2001 From: "junda.mao" Date: Mon, 22 Nov 2021 20:12:06 +0800 Subject: [PATCH] feat: add event listener option --- examples/auto-close.js | 77 ++++++++++++++++++++++++++++++++++++++++++ src/index.tsx | 6 ++++ 2 files changed, 83 insertions(+) create mode 100644 examples/auto-close.js diff --git a/examples/auto-close.js b/examples/auto-close.js new file mode 100644 index 00000000..ac2b6aab --- /dev/null +++ b/examples/auto-close.js @@ -0,0 +1,77 @@ +/* eslint no-console:0 */ + +import React from 'react'; +import Trigger from '../src'; +import '../assets/index.less'; + +const builtinPlacements = { + left: { + points: ['cr', 'cl'], + }, + right: { + points: ['cl', 'cr'], + }, + top: { + points: ['bc', 'tc'], + }, + bottom: { + points: ['tc', 'bc'], + }, + topLeft: { + points: ['bl', 'tl'], + }, + topRight: { + points: ['br', 'tr'], + }, + bottomRight: { + points: ['tr', 'br'], + }, + bottomLeft: { + points: ['tl', 'bl'], + }, +}; + +const popupBorderStyle = { + border: '1px solid red', + padding: 10, + background: 'rgba(255, 0, 0, 0.1)', +}; + +class Test extends React.Component { + render() { + return ( +
+
e.stopPropagation()}> + popup content
+ } + //effect on react 17 + triggerEventListenerOption={{ + capture: true + }} + > + Click Me1 + +
+
e.stopPropagation()}> + popup content2
+ } + > + Click Me2 + + + + ); + } +} + +export default Test; diff --git a/src/index.tsx b/src/index.tsx index bd60336f..96320203 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -111,6 +111,7 @@ export interface TriggerProps { /** @private Bump fixed position at bottom in mobile. * This is internal usage currently, do not use in your prod */ mobile?: MobileConfig; + triggerEventListenerOption?: object } interface TriggerState { @@ -150,6 +151,7 @@ export function generateTrigger( showAction: [], hideAction: [], autoDestroy: false, + triggerEventListenerOption: {} }; popupRef = React.createRef(); @@ -207,6 +209,7 @@ export function generateTrigger( componentDidUpdate() { const { props } = this; const { state } = this; + const { triggerEventListenerOption = {} } = props // We must listen to `mousedown` or `touchstart`, edge case: // https://github.com/ant-design/ant-design/issues/5804 @@ -223,6 +226,7 @@ export function generateTrigger( currentDocument, 'mousedown', this.onDocumentClick, + triggerEventListenerOption ); } // always hide on mobile @@ -233,6 +237,7 @@ export function generateTrigger( currentDocument, 'touchstart', this.onDocumentClick, + triggerEventListenerOption ); } // close popup when trigger type contains 'onContextMenu' and document is scrolling. @@ -243,6 +248,7 @@ export function generateTrigger( currentDocument, 'scroll', this.onContextMenuClose, + triggerEventListenerOption ); } // close popup when trigger type contains 'onContextMenu' and window is blur.