Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add event listener option #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions examples/auto-close.js
Original file line number Diff line number Diff line change
@@ -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 (
<div style={{ margin: 200 }}>
<div onMouseDown={e => e.stopPropagation()}>
<Trigger
popupPlacement="right"
action={['click']}
builtinPlacements={builtinPlacements}
popup={
<div>popup content</div>
}
//effect on react 17
triggerEventListenerOption={{
capture: true
}}
>
<span>Click Me1</span>
</Trigger>
</div>
<div onMouseDown={e => e.stopPropagation()}>
<Trigger
popupPlacement="right"
action={['click']}
builtinPlacements={builtinPlacements}
popup={
<div>popup content2 </div>
}
>
<span>Click Me2</span>
</Trigger>
</div>
</div>
);
}
}

export default Test;
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -150,6 +151,7 @@ export function generateTrigger(
showAction: [],
hideAction: [],
autoDestroy: false,
triggerEventListenerOption: {}
};

popupRef = React.createRef<PopupInnerRef>();
Expand Down Expand Up @@ -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
Expand All @@ -223,6 +226,7 @@ export function generateTrigger(
currentDocument,
'mousedown',
this.onDocumentClick,
triggerEventListenerOption
);
}
// always hide on mobile
Expand All @@ -233,6 +237,7 @@ export function generateTrigger(
currentDocument,
'touchstart',
this.onDocumentClick,
triggerEventListenerOption
);
}
// close popup when trigger type contains 'onContextMenu' and document is scrolling.
Expand All @@ -243,6 +248,7 @@ export function generateTrigger(
currentDocument,
'scroll',
this.onContextMenuClose,
triggerEventListenerOption
);
}
// close popup when trigger type contains 'onContextMenu' and window is blur.
Expand Down