Skip to content

Commit 22d299e

Browse files
loic-martinez-freelancevkbansal
authored andcommitted
Changes on submenu onclick behavior (#282)
* SubMenu click close the context menu * Updated .d.ts file to add onClick for SubMenu * Updated the API doc to reflect changes on SubMenu * Fixed the import group
1 parent 44a6281 commit 22d299e

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ A component for using submenus within the contextmenu.
7171
| className | String | | | Custom `className` applied to root element of the context-menu. |
7272
| attributes | Object | | | The attributes will be passed directly passed to the root element of `SubMenu`. Use this to customize it like adding custom classes (`className`, `disabledClassName`, `visibleClassName`, `selectedClassName` and `listClassName`), etc. |
7373
| selected | boolean | | `false` | **Internal Prop**: will be set from the surrounded context `ContextMenu` or `SubMenu`. If set to `true` the css class `react-contextmenu-item--selected` will be added to associated element. |
74+
| onClick | Function | | | The function to be called on click of `SubMenu`. The function will receive three parameters. The first is `event` object. The second is the merged data passed using `props.data` and collect from `ContextMenuTrigger`. The third is the element on which right-click occured. |
7475
| onMouseMove | Function | | | **Internal Prop**: will be directly passed to associated element, so the surrounded context `ContextMenu` or `SubMenu` can handle the interactions to pass the correct `selected` state. Also the surrounded context can store the current selected `SubMenu` entry. |
7576
| onMouseOut | Function | | | **Internal Prop**: will be directly passed to associated element, so the surrounded context `ContextMenu` or `SubMenu` can handle the interactions to pass the correct `selected` state. Also the surrounded context can store the current selected `SubMenu` entry. |
77+
| preventCloseOnClick | Boolean | | `false` | Prevent the context-menu to close when clicking on this `SubMenu`. |
7678
| forceOpen | boolean | | `false` | **Internal Prop**: if the user hits enter or the right arrow key on a selected `SubMenu` entry, the surrounded context will pass `true` to this flag. The `SubMenu` stays open until this flag is `false`. |
7779
| forceClose | Function | | | **Internal Prop**: if the user hits the escape key during an open `SubMenu`, this function will be called to indicate the surrounding context to reset the `forceOpen` flag. |
7880
| parentKeyNavigationHandler | Function | | | **Internal Prop**: the `keydown` handler from the surrounding context will be passed to every open `SubMenu`. Then the `Submenu` will unregister the referenced handler and use it's own for key control. When the `SubMenu` hides again the original handler will be restored. |

src/SubMenu.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import cx from 'classnames';
44
import assign from 'object-assign';
55

6+
import { hideMenu } from './actions';
67
import AbstractMenu from './AbstractMenu';
78
import { callIfExists, cssClasses, hasOwnProp, store } from './helpers';
89
import listener from './globalEventListener';
@@ -47,7 +48,7 @@ export default class SubMenu extends AbstractMenu {
4748
}
4849

4950
componentDidMount() {
50-
this.listenId = listener.register(() => {}, this.hideMenu);
51+
this.listenId = listener.register(() => {}, this.hideSubMenu);
5152
}
5253

5354
getSubMenuType() { // eslint-disable-line class-methods-use-this
@@ -151,7 +152,7 @@ export default class SubMenu extends AbstractMenu {
151152
return position;
152153
}
153154

154-
hideMenu = (e) => {
155+
hideSubMenu = (e) => {
155156
// avoid closing submenus of a different menu tree
156157
if (e.detail && e.detail.id && this.menu && e.detail.id !== this.menu.id) {
157158
return;
@@ -175,6 +176,10 @@ export default class SubMenu extends AbstractMenu {
175176
assign({}, this.props.data, store.data),
176177
store.target
177178
);
179+
180+
if (!this.props.onClick || this.props.preventCloseOnClick) return;
181+
182+
hideMenu();
178183
}
179184

180185
handleMouseEnter = () => {

src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ declare module "react-contextmenu" {
3636
disabled?: boolean,
3737
hoverDelay?: number,
3838
rtl?: boolean,
39+
preventCloseOnClick?: boolean,
40+
onClick?: {(event: React.TouchEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>, data: Object, target: HTMLElement): void} | Function,
3941
}
4042

4143
module ReactContextmenu {

0 commit comments

Comments
 (0)