|
1 |
| -import React from 'react'; |
| 1 | +import React, { PropsWithChildren } from 'react'; |
2 | 2 | import { CSSTransition } from 'react-transition-group';
|
3 | 3 | import { TransitionStatus } from 'react-transition-group/Transition';
|
4 | 4 | import { IProps, HTMLDivProps } from '@uiw/utils';
|
@@ -52,25 +52,64 @@ export default function Panel(props: CollapsePanelProps) {
|
52 | 52 | instance.style.height = '1px';
|
53 | 53 | }
|
54 | 54 | 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`; |
56 | 57 | }
|
57 | 58 | }
|
58 | 59 | return (
|
59 | 60 | <div className={cls} {...resetProps}>
|
60 | 61 | <div className={`${prefixCls}-header`} onClick={onItemClick}>
|
61 | 62 | {showArrow && iconRender}
|
62 | 63 | <span className={`${prefixCls}-title`}>{header}</span>
|
63 |
| - {extra && <div className={`${prefixCls}-extra`}>{extra}</div>} |
| 64 | + <Extra prefixCls={prefixCls}>{extra}</Extra> |
64 | 65 | </div>
|
65 | 66 | <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>, { |
68 | 69 | className: `${prefixCls}-panel`,
|
69 | 70 | style: childStyle(children as React.ReactElement),
|
70 | 71 | ref: (e: any) => getInstance(status, e),
|
71 |
| - }) |
72 |
| - } |
| 72 | + }); |
| 73 | + }} |
73 | 74 | </CSSTransition>
|
74 | 75 | </div>
|
75 | 76 | );
|
76 | 77 | }
|
| 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