Skip to content
This repository has been archived by the owner on Dec 21, 2021. It is now read-only.

Commit

Permalink
Merge pull request #123 from Ultimaker/fix-context-menu
Browse files Browse the repository at this point in the history
only add onCloseMenuHandler to child components that need it
  • Loading branch information
Alan authored Dec 21, 2018
2 parents a814844 + 54e5a7a commit 0be46cc
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/components/drop_down_menu_base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ export class DropDownMenuBase extends React.Component<DropDownMenuBaseProps, {}>
e.stopPropagation();
}

/**
* Determines whether a child component is in the list of components
* that require the onCloseMenuHandler prop to be added
* @param child - the child component
*/
static _isHandlerRequired(child: any): boolean {
const componentList = {
DropDownMenuItem: true,
ContextMenuItem: true,
};

if (child.type && child.type.displayName && componentList[child.type.displayName]) {
return true;
}

return false;
}

constructor(props: DropDownMenuBaseProps) {
super(props);
this._menuRef = React.createRef();
Expand Down Expand Up @@ -78,6 +96,10 @@ export class DropDownMenuBase extends React.Component<DropDownMenuBaseProps, {}>
{ 'drop-down-menu-base--visible': showMenu },
);

const onCloseMenuHandlerProp = {
onCloseMenuHandler: () => this._onToggleMenuHandler(false),
};

return (
// eslint-disable-next-line max-len
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
Expand All @@ -99,9 +121,11 @@ export class DropDownMenuBase extends React.Component<DropDownMenuBaseProps, {}>
>
<ul className="drop-down-menu-base__menu-list">
{React.Children.map(children, (child: JSX.Element) => (
React.cloneElement(child, {
onCloseMenuHandler: () => this._onToggleMenuHandler(false),
})
React.cloneElement(
child,
DropDownMenuBase._isHandlerRequired(child)
&& onCloseMenuHandlerProp,
)
))}
</ul>
</Collapse>
Expand Down

0 comments on commit 0be46cc

Please sign in to comment.