From 358e00c88ae9b6dffdbf74cfc5b6abf445d86b81 Mon Sep 17 00:00:00 2001 From: Alan Date: Fri, 21 Dec 2018 12:02:37 +0100 Subject: [PATCH 1/2] only add onCloseMenuHandler to child components that need it --- src/components/drop_down_menu_base.tsx | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/components/drop_down_menu_base.tsx b/src/components/drop_down_menu_base.tsx index 4bb53f68..e8ffb6ac 100755 --- a/src/components/drop_down_menu_base.tsx +++ b/src/components/drop_down_menu_base.tsx @@ -31,6 +31,19 @@ export class DropDownMenuBase extends React.Component e.stopPropagation(); } + 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(); @@ -78,6 +91,10 @@ export class DropDownMenuBase extends React.Component { '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 @@ -99,9 +116,11 @@ export class DropDownMenuBase extends React.Component >
    {React.Children.map(children, (child: JSX.Element) => ( - React.cloneElement(child, { - onCloseMenuHandler: () => this._onToggleMenuHandler(false), - }) + React.cloneElement( + child, + DropDownMenuBase._isHandlerRequired(child) + && onCloseMenuHandlerProp, + ) ))}
From f74ce947bd1ac876eead6684389115c2f9804850 Mon Sep 17 00:00:00 2001 From: Alan Date: Fri, 21 Dec 2018 12:08:01 +0100 Subject: [PATCH 2/2] add comments --- src/components/drop_down_menu_base.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/components/drop_down_menu_base.tsx b/src/components/drop_down_menu_base.tsx index e8ffb6ac..52f96fee 100755 --- a/src/components/drop_down_menu_base.tsx +++ b/src/components/drop_down_menu_base.tsx @@ -31,6 +31,11 @@ export class DropDownMenuBase extends React.Component 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,