Skip to content

Commit abe1cae

Browse files
authored
feat(Collapse): optimize panel animation (#611)
* feat(Collapse): optimize panel animation * test: update snapshots
1 parent 975f52e commit abe1cae

File tree

5 files changed

+281
-140
lines changed

5 files changed

+281
-140
lines changed

src/collapse/CollapsePanel.tsx

+5-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, memo, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
1+
import React, { forwardRef, memo, useCallback, useContext, useEffect, useMemo, useRef } from 'react';
22
import cls from 'classnames';
33
import { ChevronDownIcon, ChevronUpIcon } from 'tdesign-icons-react';
44
import type { StyledProps } from '../common';
@@ -42,27 +42,7 @@ const CollapsePanel = forwardRef<HTMLDivElement, CollapsePanelProps>((originProp
4242
[parent, value, disabled],
4343
);
4444

45-
const bodyRef = useRef<HTMLDivElement>(null);
4645
const headRef = useRef<HTMLDivElement>(null);
47-
const [wrapperHeight, setWrapperHeight] = useState('');
48-
49-
useEffect(() => {
50-
const updatePanelState = () => {
51-
if (!headRef.current || !bodyRef.current) {
52-
return;
53-
}
54-
const headHeight = headRef.current.getBoundingClientRect().height;
55-
if (!isActive) {
56-
setWrapperHeight(`${headHeight}px`);
57-
return;
58-
}
59-
const bodyHeight = bodyRef.current.getBoundingClientRect().height;
60-
const height = headHeight + bodyHeight;
61-
setWrapperHeight(`${height}px`);
62-
};
63-
64-
updatePanelState();
65-
}, [isActive]);
6646

6747
useEffect(() => {
6848
if (parent?.defaultExpandAll) {
@@ -89,11 +69,7 @@ const CollapsePanel = forwardRef<HTMLDivElement, CollapsePanelProps>((originProp
8969
// 当前面板处理折叠状态时,是否销毁面板内容
9070
if (props.destroyOnCollapse && !isActive) return null;
9171

92-
return (
93-
<div ref={bodyRef} className={`${collapsePanelClass}__content`}>
94-
{panelContent}
95-
</div>
96-
);
72+
return <div className={`${collapsePanelClass}__content`}>{panelContent}</div>;
9773
};
9874

9975
return (
@@ -107,7 +83,6 @@ const CollapsePanel = forwardRef<HTMLDivElement, CollapsePanelProps>((originProp
10783
[className]: className,
10884
})}
10985
style={{
110-
height: wrapperHeight,
11186
...style,
11287
}}
11388
>
@@ -122,7 +97,9 @@ const CollapsePanel = forwardRef<HTMLDivElement, CollapsePanelProps>((originProp
12297
rightIcon={renderRightIcon()}
12398
/>
12499
</div>
125-
<PanelContent />
100+
<div className={`${collapsePanelClass}__body`} style={{ gridTemplateRows: isActive ? '1fr' : '0fr' }}>
101+
<div className={`${collapsePanelClass}__inner`}>{PanelContent()}</div>
102+
</div>
126103
</div>
127104
);
128105
});

src/collapse/style/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// index.js
21
import '../../_common/style/mobile/components/collapse/v2/_index.less';

src/collapse/style/index.less

-11
This file was deleted.

0 commit comments

Comments
 (0)