Skip to content

Commit 94200fb

Browse files
authoredJun 15, 2022
fix(Menu): 节点的子级节点无法展开 #822 (#852)
1 parent da86bba commit 94200fb

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed
 

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

+2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export interface MenuProps extends IProps, HTMLUlProps {
2020
bordered?: boolean;
2121
}
2222
interface MenuContextType {
23+
// 需要加上或者减去的高度
2324
height: number;
25+
// 事件源dom
2426
ele: EventTarget | null;
2527
}
2628
export const ThemeContext = createContext(

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

+24-21
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,21 @@ export const SubMenu = React.forwardRef(function <Tag extends TagType = 'a'>(
7474
};
7575
const popupRef = React.useRef<OverlayTriggerRef>(null);
7676
const refNode = React.useRef<HTMLElement | null>();
77-
const currentHeight = React.useRef<number>(0);
7877
const elementSource = React.useRef<EventTarget | null>();
7978
const [isOpen, setIsOpen] = useState(!!overlayProps.isOpen);
8079
const { height, setContextHeight, ele } = useContext(ThemeContext);
8180

8281
React.useEffect(() => {
8382
if (refNode.current && refNode.current.style && ele === elementSource.current) {
84-
const currentHeight = refNode.current!.style.height;
85-
if (height + 'px' === currentHeight) return;
86-
refNode.current!.style.height = Number(currentHeight.substr(0, currentHeight.length - 2)) + height + 'px';
83+
const currentHeight = Number(refNode.current!.style.height.substr(0, refNode.current!.style.height.length - 2));
84+
// 设置的高度 < '已有展开的高度',
85+
if (refNode.current!.getBoundingClientRect().height < currentHeight) {
86+
refNode.current!.style.height = currentHeight + 'px';
87+
} else {
88+
refNode.current!.style.height = currentHeight + height + 'px';
89+
}
8790
}
88-
}, [height]);
91+
}, [height, ele]);
8992

9093
useMemo(() => {
9194
if (collapse) setIsOpen(false);
@@ -101,23 +104,12 @@ export const SubMenu = React.forwardRef(function <Tag extends TagType = 'a'>(
101104
}
102105
}
103106
}
104-
function onExit(node: HTMLElement) {
105-
node.style.height = `${node.scrollHeight}px`;
106-
setIsOpen(false);
107-
}
108-
function onExiting(node: HTMLElement) {
109-
node.style.height = '0px';
110-
setContextHeight({
111-
height: -currentHeight.current,
112-
ele: elementSource.current!,
113-
});
114-
}
115107
function onEnter(node: HTMLElement) {
116-
node.style.height = '1px';
108+
node.style.height = '0px';
109+
refNode.current = node;
117110
setIsOpen(true);
118-
currentHeight.current = popupRef.current!.overlayDom.current!.getBoundingClientRect().height;
119111
setContextHeight({
120-
height: currentHeight.current,
112+
height: popupRef.current!.overlayDom.current!.getBoundingClientRect().height,
121113
ele: elementSource.current!,
122114
});
123115
}
@@ -126,9 +118,20 @@ export const SubMenu = React.forwardRef(function <Tag extends TagType = 'a'>(
126118
}
127119
function onEntered(node: HTMLElement) {
128120
// node.style.height = 'initial';
129-
node.style.height = currentHeight.current + 'px';
130-
refNode.current = node;
121+
node.style.height = popupRef.current!.overlayDom.current!.getBoundingClientRect().height + 'px';
122+
}
123+
function onExiting(node: HTMLElement) {
124+
node.style.height = '0px';
125+
setContextHeight({
126+
height: -popupRef.current!.overlayDom.current!.getBoundingClientRect().height,
127+
ele: elementSource.current!,
128+
});
129+
}
130+
function onExit(node: HTMLElement) {
131+
node.style.height = `${node.scrollHeight}px`;
132+
setIsOpen(false);
131133
}
134+
132135
if (!collapse) {
133136
delete menuProps.onClick;
134137
menuProps.bordered = false;

0 commit comments

Comments
 (0)
Please sign in to comment.