From 19421f375a3eb1dd179cbe3d3b2c8087fd626cc3 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sun, 9 Oct 2022 23:54:02 +0800 Subject: [PATCH] fix(SubMenu): fix ref issue. --- packages/react-menu/src/SubMenu.tsx | 86 +++++++++++++++-------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/packages/react-menu/src/SubMenu.tsx b/packages/react-menu/src/SubMenu.tsx index e24f6df64b..defcbea420 100644 --- a/packages/react-menu/src/SubMenu.tsx +++ b/packages/react-menu/src/SubMenu.tsx @@ -108,24 +108,30 @@ export const SubMenu = React.forwardRef(function ( node.style.height = '0px'; refNode.current = node; setIsOpen(true); - setContextHeight({ - height: popupRef.current!.overlayDom.current!.getBoundingClientRect().height, - ele: elementSource.current!, - }); + if (popupRef.current && popupRef.current.overlayDom.current) { + setContextHeight({ + height: popupRef.current.overlayDom.current.getBoundingClientRect().height, + ele: elementSource.current!, + }); + } } function onEntering(node: HTMLElement) { node.style.height = `${node.scrollHeight}px`; } function onEntered(node: HTMLElement) { // node.style.height = 'initial'; - node.style.height = popupRef.current!.overlayDom.current!.getBoundingClientRect().height + 'px'; + if (popupRef.current && popupRef.current.overlayDom.current) { + node.style.height = popupRef.current.overlayDom.current.getBoundingClientRect().height + 'px'; + } } function onExiting(node: HTMLElement) { node.style.height = '0px'; - setContextHeight({ - height: -popupRef.current!.overlayDom.current!.getBoundingClientRect().height, - ele: elementSource.current!, - }); + if (popupRef.current && popupRef.current.overlayDom.current) { + setContextHeight({ + height: -popupRef.current!.overlayDom.current!.getBoundingClientRect().height, + ele: elementSource.current!, + }); + } } function onExit(node: HTMLElement) { node.style.height = `${node.scrollHeight}px`; @@ -154,7 +160,9 @@ export const SubMenu = React.forwardRef(function ( menuProps.onClick = onClick; } return ( -
{ if (collapse) { e.stopPropagation(); @@ -163,37 +171,35 @@ export const SubMenu = React.forwardRef(function ( elementSource.current = e.target; }} > -
  • - } + > + } - > - } - className={[ - prefixCls ? `${prefixCls}-title` : null, - !collapse ? `${prefixCls}-collapse-title` : null, - className, - ] - .filter(Boolean) - .join(' ') - .trim()} - /> - -
  • -
    + isSubMenuItem + addonAfter={} + className={[ + prefixCls ? `${prefixCls}-title` : null, + !collapse ? `${prefixCls}-collapse-title` : null, + className, + ] + .filter(Boolean) + .join(' ') + .trim()} + /> + + ); });