Skip to content

Commit 7246b0d

Browse files
committed
fix(Collapse): fix height issue. (#974)
1 parent 5dbe22f commit 7246b0d

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"homepage": "https://uiwjs.github.io",
55
"private": true,
66
"scripts": {
7-
"lib": "lerna exec --scope @uiw/react-list -- tsbb build \"src/*.{ts,tsx}\" --use-babel --cjs cjs",
8-
"lib:css": "lerna exec --scope @uiw/react-list -- compile-less -d src -o esm",
9-
"lib:css:dist": "lerna exec --scope @uiw/react-list -- compile-less -d src -o lib --combine=dist.css",
10-
"lib:css:watch": "lerna exec --scope @uiw/react-list -- compile-less -d src -o esm --watch",
11-
"lib:watch": "lerna exec --scope @uiw/react-list -- tsbb watch \"src/*.{ts,tsx}\" --use-babel --cjs cjs & npm run lib:css:watch",
12-
"lib:bootstrap": "lerna bootstrap --hoist --scope @uiw/react-list",
7+
"lib": "lerna exec --scope @uiw/react-collapse -- tsbb build \"src/*.{ts,tsx}\" --use-babel --cjs cjs",
8+
"lib:css": "lerna exec --scope @uiw/react-collapse -- compile-less -d src -o esm",
9+
"lib:css:dist": "lerna exec --scope @uiw/react-collapse -- compile-less -d src -o lib --combine=dist.css",
10+
"lib:css:watch": "lerna exec --scope @uiw/react-collapse -- compile-less -d src -o esm --watch",
11+
"lib:watch": "lerna exec --scope @uiw/react-collapse -- tsbb watch \"src/*.{ts,tsx}\" --use-babel --cjs cjs & npm run lib:css:watch",
12+
"lib:bootstrap": "lerna bootstrap --hoist --scope @uiw/react-collapse",
1313
"lib:build": "npm run lib && npm run lib:css && npm run lib:css:dist",
1414
"//>>>>>>>>>>>>": "<<<<<<<<<<",
1515
"watch:other:lib": "lerna exec --parallel --scope @uiw/* --ignore @uiw/doc -- tsbb watch \"src/*.{ts,tsx}\" --use-babel --cjs cjs",

packages/react-collapse/src/Panel.tsx

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { PropsWithChildren } from 'react';
22
import { CSSTransition } from 'react-transition-group';
33
import { TransitionStatus } from 'react-transition-group/Transition';
44
import { IProps, HTMLDivProps } from '@uiw/utils';
@@ -52,25 +52,64 @@ export default function Panel(props: CollapsePanelProps) {
5252
instance.style.height = '1px';
5353
}
5454
if (status === 'entered' || status === 'entering') {
55-
instance.style.height = `${instance.scrollHeight}px`;
55+
// instance.style.height = `${instance.scrollHeight}px`;
56+
instance.style.height = `${getElementHeight(instance)}px`;
5657
}
5758
}
5859
return (
5960
<div className={cls} {...resetProps}>
6061
<div className={`${prefixCls}-header`} onClick={onItemClick}>
6162
{showArrow && iconRender}
6263
<span className={`${prefixCls}-title`}>{header}</span>
63-
{extra && <div className={`${prefixCls}-extra`}>{extra}</div>}
64+
<Extra prefixCls={prefixCls}>{extra}</Extra>
6465
</div>
6566
<CSSTransition in={isActive} unmountOnExit={false} timeout={300} classNames={`${prefixCls}-panel`}>
66-
{(status: TransitionStatus) =>
67-
React.cloneElement(<div>{children}</div>, {
67+
{(status: TransitionStatus) => {
68+
return React.cloneElement(<div>{children}</div>, {
6869
className: `${prefixCls}-panel`,
6970
style: childStyle(children as React.ReactElement),
7071
ref: (e: any) => getInstance(status, e),
71-
})
72-
}
72+
});
73+
}}
7374
</CSSTransition>
7475
</div>
7576
);
7677
}
78+
79+
function Extra({ children, prefixCls }: PropsWithChildren<{ prefixCls: string }>) {
80+
if (!children) return null;
81+
return <div className={`${prefixCls}-extra`}>{children}</div>;
82+
}
83+
84+
function getElementHeight(elm: HTMLDivElement) {
85+
const childNodes = elm.children;
86+
let totalHeight = 0;
87+
const beforeElmStyle = getComputedStyle(elm, '::before');
88+
const afterElmStyle = getComputedStyle(elm, '::after');
89+
const beforeHeight = parseInt(beforeElmStyle.height) || 0;
90+
const afterHeight = parseInt(afterElmStyle.height) || 0;
91+
totalHeight += beforeHeight + afterHeight;
92+
if (childNodes.length === 0) {
93+
return totalHeight;
94+
}
95+
for (let i = 0; i < childNodes.length; i++) {
96+
const childNode = childNodes[i] as HTMLDivElement;
97+
const computedStyle = getComputedStyle(childNode);
98+
const height =
99+
childNode.offsetHeight +
100+
parseInt(computedStyle.marginTop) +
101+
parseInt(computedStyle.marginBottom) +
102+
parseInt(computedStyle.borderTopWidth) +
103+
parseInt(computedStyle.borderBottomWidth) +
104+
parseInt(computedStyle.paddingTop) +
105+
parseInt(computedStyle.paddingBottom);
106+
totalHeight += height;
107+
108+
const beforeStyle = getComputedStyle(childNode, '::before');
109+
const afterStyle = getComputedStyle(childNode, '::after');
110+
const beforeHeight = parseInt(beforeStyle.height) || 0;
111+
const afterHeight = parseInt(afterStyle.height) || 0;
112+
totalHeight += beforeHeight + afterHeight;
113+
}
114+
return totalHeight;
115+
}

0 commit comments

Comments
 (0)