Skip to content

feat: Add new trigger type --- 'contextmenu' #72

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

Merged
merged 2 commits into from
Oct 16, 2017
Merged
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
9 changes: 9 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ class Test extends React.Component {
/>
click
</label>
<label>
<input
value="contextMenu"
checked={!!trigger.contextMenu}
type="checkbox"
onChange={this.onTriggerChange}
/>
contextMenu
</label>
&nbsp;&nbsp;&nbsp;&nbsp;
<label>
<input
Expand Down
49 changes: 47 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function returnDocument() {
}

const ALL_HANDLERS = ['onClick', 'onMouseDown', 'onTouchStart', 'onMouseEnter',
'onMouseLeave', 'onFocus', 'onBlur'];
'onMouseLeave', 'onFocus', 'onBlur', 'onContextMenu'];

const IS_REACT_16 = !!createPortal;

Expand Down Expand Up @@ -169,7 +169,7 @@ const Trigger = createReactClass({
// https://github.com/react-component/trigger/issues/50
if (state.popupVisible) {
let currentDocument;
if (!this.clickOutsideHandler && this.isClickToHide()) {
if (!this.clickOutsideHandler && (this.isClickToHide() || this.isContextMenuToShow())) {
currentDocument = props.getDocument();
this.clickOutsideHandler = addEventListener(currentDocument,
'mousedown', this.onDocumentClick);
Expand All @@ -180,6 +180,17 @@ const Trigger = createReactClass({
this.touchOutsideHandler = addEventListener(currentDocument,
'touchstart', this.onDocumentClick);
}
// close popup when trigger type contains 'onContextMenu' and document is scrolling.
if (!this.contextMenuOutsideHandler1 && this.isContextMenuToShow()) {
currentDocument = currentDocument || props.getDocument();
this.contextMenuOutsideHandler1 = addEventListener(currentDocument,
'scroll', this.onContextMenuClose);
}
// close popup when trigger type contains 'onContextMenu' and window is blur.
if (!this.contextMenuOutsideHandler2 && this.isContextMenuToShow()) {
this.contextMenuOutsideHandler2 = addEventListener(window,
'blur', this.onContextMenuClose);
}
return;
}

Expand Down Expand Up @@ -245,6 +256,18 @@ const Trigger = createReactClass({
}
},

onContextMenu(e) {
e.preventDefault();
this.fireEvents('onContextMenu', e);
this.setPopupVisible(true);
},

onContextMenuClose() {
if (this.isContextMenuToShow()) {
this.close();
}
},

onClick(event) {
this.fireEvents('onClick', event);
// focus will trigger click
Expand Down Expand Up @@ -405,6 +428,16 @@ const Trigger = createReactClass({
this.clickOutsideHandler = null;
}

if (this.contextMenuOutsideHandler1) {
this.contextMenuOutsideHandler1.remove();
this.contextMenuOutsideHandler1 = null;
}

if (this.contextMenuOutsideHandler2) {
this.contextMenuOutsideHandler2.remove();
this.contextMenuOutsideHandler2 = null;
}

if (this.touchOutsideHandler) {
this.touchOutsideHandler.remove();
this.touchOutsideHandler = null;
Expand All @@ -425,6 +458,11 @@ const Trigger = createReactClass({
return action.indexOf('click') !== -1 || showAction.indexOf('click') !== -1;
},

isContextMenuToShow() {
const { action, showAction } = this.props;
return action.indexOf('contextMenu') !== -1 || showAction.indexOf('contextMenu') !== -1;
},

isClickToHide() {
const { action, hideAction } = this.props;
return action.indexOf('click') !== -1 || hideAction.indexOf('click') !== -1;
Expand Down Expand Up @@ -482,6 +520,13 @@ const Trigger = createReactClass({
const children = props.children;
const child = React.Children.only(children);
const newChildProps = { key: 'trigger' };

if (this.isContextMenuToShow()) {
newChildProps.onContextMenu = this.onContextMenu;
} else {
newChildProps.onContextMenu = this.createTwoChains('onContextMenu');
}

if (this.isClickToHide() || this.isClickToShow()) {
newChildProps.onClick = this.onClick;
newChildProps.onMouseDown = this.onMouseDown;
Expand Down
76 changes: 49 additions & 27 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,28 @@ describe('rc-trigger', function main() {
}], done);
});

it('contextMenu works', done => {
const trigger = ReactDOM.render((
<Trigger
action={['contextMenu']}
popupAlign={placementAlignMap.left}
popup={<strong>trigger</strong>}
>
<div className="target">contextMenu</div>
</Trigger>
), div);

const target = scryRenderedDOMComponentsWithClass(trigger, 'target')[0];
// can not simulate mouseenter
Simulate.contextMenu(target);
async.series([timeout(200), (next) => {
const popupDomNode = trigger.getPopupDomNode();
expect(popupDomNode).to.be.ok();
expect($(popupDomNode).css('display')).to.be('block');
next();
}], done);
});

it('afterPopupVisibleChange can be triggered', (done) => {
let triggered = 0;
const trigger = ReactDOM.render((
Expand Down Expand Up @@ -601,33 +623,33 @@ describe('rc-trigger', function main() {
const domNode = ReactDOM.findDOMNode(trigger);
Simulate.click(domNode);
async.series([timeout(100),
(next) => {
const popupDomNode = trigger.getPopupDomNode();
expect(popupDomNode).to.be.ok();
expect($(popupDomNode).css('opacity')).not.to.be('1');
next();
},
timeout(500),
(next) => {
const popupDomNode = trigger.getPopupDomNode();
expect(popupDomNode).to.be.ok();
expect($(popupDomNode).css('opacity')).to.be('1');
Simulate.click(domNode);
next();
},
timeout(100),
(next) => {
const popupDomNode = trigger.getPopupDomNode();
expect(popupDomNode).to.be.ok();
expect($(popupDomNode).css('opacity')).not.to.be('1');
next();
},
timeout(500),
(next) => {
const popupDomNode = trigger.getPopupDomNode();
expect($(popupDomNode).css('display')).to.be('none');
next();
}],
(next) => {
const popupDomNode = trigger.getPopupDomNode();
expect(popupDomNode).to.be.ok();
expect($(popupDomNode).css('opacity')).not.to.be('1');
next();
},
timeout(500),
(next) => {
const popupDomNode = trigger.getPopupDomNode();
expect(popupDomNode).to.be.ok();
expect($(popupDomNode).css('opacity')).to.be('1');
Simulate.click(domNode);
next();
},
timeout(100),
(next) => {
const popupDomNode = trigger.getPopupDomNode();
expect(popupDomNode).to.be.ok();
expect($(popupDomNode).css('opacity')).not.to.be('1');
next();
},
timeout(500),
(next) => {
const popupDomNode = trigger.getPopupDomNode();
expect($(popupDomNode).css('display')).to.be('none');
next();
}],
done);
});
});
Expand Down