Skip to content

Commit fa57faf

Browse files
committed
feat: Add new trigger type --- 'contextmenu'
1 parent 2dd011d commit fa57faf

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

examples/simple.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Test extends React.Component {
5757
maskClosable: false,
5858
placement: 'right',
5959
trigger: {
60-
hover: 1,
60+
contextMenu: 1,
6161
},
6262
offsetX: undefined,
6363
offsetY: undefined,
@@ -165,6 +165,16 @@ class Test extends React.Component {
165165

166166
trigger:
167167

168+
<label>
169+
<input
170+
value="contextMenu"
171+
checked={!!trigger.contextMenu}
172+
type="checkbox"
173+
onChange={this.onTriggerChange}
174+
/>
175+
contextMenu
176+
</label>
177+
168178
<label>
169179
<input
170180
value="hover"

src/index.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function returnDocument() {
2121
}
2222

2323
const ALL_HANDLERS = ['onClick', 'onMouseDown', 'onTouchStart', 'onMouseEnter',
24-
'onMouseLeave', 'onFocus', 'onBlur'];
24+
'onMouseLeave', 'onFocus', 'onBlur', 'onContextMenu'];
2525

2626
const IS_REACT_16 = !!createPortal;
2727

@@ -169,7 +169,7 @@ const Trigger = createReactClass({
169169
// https://github.com/react-component/trigger/issues/50
170170
if (state.popupVisible) {
171171
let currentDocument;
172-
if (!this.clickOutsideHandler && this.isClickToHide()) {
172+
if (!this.clickOutsideHandler && (this.isClickToHide() || this.isContextMenuToShow())) {
173173
currentDocument = props.getDocument();
174174
this.clickOutsideHandler = addEventListener(currentDocument,
175175
'mousedown', this.onDocumentClick);
@@ -180,6 +180,17 @@ const Trigger = createReactClass({
180180
this.touchOutsideHandler = addEventListener(currentDocument,
181181
'touchstart', this.onDocumentClick);
182182
}
183+
// close popup when trigger type contains 'onContextMenu' and document is scrolling.
184+
if (!this.contextMenuOutsideHandler1 && this.isContextMenuToShow()) {
185+
currentDocument = currentDocument || props.getDocument();
186+
this.contextMenuOutsideHandler1 = addEventListener(currentDocument,
187+
'scroll', this.onContextMenuClose);
188+
}
189+
// close popup when trigger type contains 'onContextMenu' and window is blur.
190+
if (!this.contextMenuOutsideHandler2 && this.isContextMenuToShow()) {
191+
this.contextMenuOutsideHandler2 = addEventListener(window,
192+
'blur', this.onContextMenuClose);
193+
}
183194
return;
184195
}
185196

@@ -245,6 +256,18 @@ const Trigger = createReactClass({
245256
}
246257
},
247258

259+
onContextMenu(e) {
260+
e.preventDefault();
261+
this.fireEvents('onContextMenu', e);
262+
this.setPopupVisible(true);
263+
},
264+
265+
onContextMenuClose() {
266+
if (this.isContextMenuToShow()) {
267+
this.close();
268+
}
269+
},
270+
248271
onClick(event) {
249272
this.fireEvents('onClick', event);
250273
// focus will trigger click
@@ -405,6 +428,16 @@ const Trigger = createReactClass({
405428
this.clickOutsideHandler = null;
406429
}
407430

431+
if (this.contextMenuOutsideHandler1) {
432+
this.contextMenuOutsideHandler1.remove();
433+
this.contextMenuOutsideHandler1 = null;
434+
}
435+
436+
if (this.contextMenuOutsideHandler2) {
437+
this.contextMenuOutsideHandler2.remove();
438+
this.contextMenuOutsideHandler2 = null;
439+
}
440+
408441
if (this.touchOutsideHandler) {
409442
this.touchOutsideHandler.remove();
410443
this.touchOutsideHandler = null;
@@ -425,6 +458,11 @@ const Trigger = createReactClass({
425458
return action.indexOf('click') !== -1 || showAction.indexOf('click') !== -1;
426459
},
427460

461+
isContextMenuToShow() {
462+
const { action, showAction } = this.props;
463+
return action.indexOf('contextMenu') !== -1 || showAction.indexOf('contextMenu') !== -1;
464+
},
465+
428466
isClickToHide() {
429467
const { action, hideAction } = this.props;
430468
return action.indexOf('click') !== -1 || hideAction.indexOf('click') !== -1;
@@ -482,6 +520,13 @@ const Trigger = createReactClass({
482520
const children = props.children;
483521
const child = React.Children.only(children);
484522
const newChildProps = { key: 'trigger' };
523+
524+
if (this.isContextMenuToShow()) {
525+
newChildProps.onContextMenu = this.onContextMenu;
526+
} else {
527+
newChildProps.onContextMenu = this.createTwoChains('onContextMenu');
528+
}
529+
485530
if (this.isClickToHide() || this.isClickToShow()) {
486531
newChildProps.onClick = this.onClick;
487532
newChildProps.onMouseDown = this.onMouseDown;

0 commit comments

Comments
 (0)