Skip to content

Commit 19421f3

Browse files
committedOct 9, 2022
fix(SubMenu): fix ref issue.
1 parent c3e3925 commit 19421f3

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed
 

‎packages/react-menu/src/SubMenu.tsx

+46-40
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,30 @@ export const SubMenu = React.forwardRef(function <Tag extends TagType = 'a'>(
108108
node.style.height = '0px';
109109
refNode.current = node;
110110
setIsOpen(true);
111-
setContextHeight({
112-
height: popupRef.current!.overlayDom.current!.getBoundingClientRect().height,
113-
ele: elementSource.current!,
114-
});
111+
if (popupRef.current && popupRef.current.overlayDom.current) {
112+
setContextHeight({
113+
height: popupRef.current.overlayDom.current.getBoundingClientRect().height,
114+
ele: elementSource.current!,
115+
});
116+
}
115117
}
116118
function onEntering(node: HTMLElement) {
117119
node.style.height = `${node.scrollHeight}px`;
118120
}
119121
function onEntered(node: HTMLElement) {
120122
// node.style.height = 'initial';
121-
node.style.height = popupRef.current!.overlayDom.current!.getBoundingClientRect().height + 'px';
123+
if (popupRef.current && popupRef.current.overlayDom.current) {
124+
node.style.height = popupRef.current.overlayDom.current.getBoundingClientRect().height + 'px';
125+
}
122126
}
123127
function onExiting(node: HTMLElement) {
124128
node.style.height = '0px';
125-
setContextHeight({
126-
height: -popupRef.current!.overlayDom.current!.getBoundingClientRect().height,
127-
ele: elementSource.current!,
128-
});
129+
if (popupRef.current && popupRef.current.overlayDom.current) {
130+
setContextHeight({
131+
height: -popupRef.current!.overlayDom.current!.getBoundingClientRect().height,
132+
ele: elementSource.current!,
133+
});
134+
}
129135
}
130136
function onExit(node: HTMLElement) {
131137
node.style.height = `${node.scrollHeight}px`;
@@ -154,7 +160,9 @@ export const SubMenu = React.forwardRef(function <Tag extends TagType = 'a'>(
154160
menuProps.onClick = onClick;
155161
}
156162
return (
157-
<div
163+
<li
164+
data-menu="subitem"
165+
ref={ref}
158166
onClick={(e) => {
159167
if (collapse) {
160168
e.stopPropagation();
@@ -163,37 +171,35 @@ export const SubMenu = React.forwardRef(function <Tag extends TagType = 'a'>(
163171
elementSource.current = e.target;
164172
}}
165173
>
166-
<li data-menu="subitem" ref={ref}>
167-
<OverlayTrigger
168-
placement="rightTop"
169-
autoAdjustOverflow
174+
<OverlayTrigger
175+
placement="rightTop"
176+
autoAdjustOverflow
177+
disabled={disabled}
178+
isOpen={isOpen}
179+
usePortal={false}
180+
isOutside
181+
{...overlayTriggerProps}
182+
{...overlayProps}
183+
ref={popupRef}
184+
overlay={<Menu {...menuProps} style={!collapse ? { paddingLeft: inlineIndent } : {}} />}
185+
>
186+
<MenuItem
187+
{...other}
188+
ref={null}
170189
disabled={disabled}
171-
isOpen={isOpen}
172-
usePortal={false}
173-
isOutside
174-
{...overlayTriggerProps}
175-
{...overlayProps}
176-
ref={popupRef}
177-
overlay={<Menu {...menuProps} style={!collapse ? { paddingLeft: inlineIndent } : {}} />}
178-
>
179-
<MenuItem
180-
{...other}
181-
ref={null}
182-
disabled={disabled}
183-
isSubMenuItem
184-
addonAfter={<IconView collapse={collapse} prefixCls={prefixCls} isOpen={isOpen} />}
185-
className={[
186-
prefixCls ? `${prefixCls}-title` : null,
187-
!collapse ? `${prefixCls}-collapse-title` : null,
188-
className,
189-
]
190-
.filter(Boolean)
191-
.join(' ')
192-
.trim()}
193-
/>
194-
</OverlayTrigger>
195-
</li>
196-
</div>
190+
isSubMenuItem
191+
addonAfter={<IconView collapse={collapse} prefixCls={prefixCls} isOpen={isOpen} />}
192+
className={[
193+
prefixCls ? `${prefixCls}-title` : null,
194+
!collapse ? `${prefixCls}-collapse-title` : null,
195+
className,
196+
]
197+
.filter(Boolean)
198+
.join(' ')
199+
.trim()}
200+
/>
201+
</OverlayTrigger>
202+
</li>
197203
);
198204
});
199205

0 commit comments

Comments
 (0)
Please sign in to comment.