Skip to content

Commit eb70c6d

Browse files
authored
fix(collapse): improve the timing to detect panel content whether empty (#184)
1 parent 7a72bb2 commit eb70c6d

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

src/components/collapse/index.tsx

+40-23
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ export function Collapse(props: ICollapseProps) {
5252
...restProps
5353
} = props;
5454

55+
// assets data must have id
56+
const filterData = data.filter((panel) => panel.id) as DataBaseProps[];
57+
if (filterData.length < data.length) {
58+
Logger.warn(new SyntaxError('collapse data must have id'));
59+
}
60+
61+
// to save position temporarily, empty array when rerender
62+
const _cachePosition: number[][] = [];
63+
let _cacheWrapperHeight = React.useRef(0);
64+
5565
const handleResize = React.useCallback(() => {
5666
// just want to trigger rerender
5767
setActivePanelKeys((keys) => keys.concat());
@@ -62,6 +72,33 @@ export function Collapse(props: ICollapseProps) {
6272
return () => window.removeEventListener('resize', handleResize);
6373
}, []);
6474

75+
React.useLayoutEffect(() => {
76+
filterData.forEach((panel) => {
77+
const isActive = activePanelKeys.includes(panel.id);
78+
let isEmpty = true;
79+
if (isActive) {
80+
const contentDom = document.querySelector(
81+
`.${collapseContentClassName}[data-content='${panel.id}']`
82+
);
83+
isEmpty = !contentDom?.hasChildNodes();
84+
}
85+
panel._isEmpty = isEmpty;
86+
const [height, top] = calcPosition(
87+
activePanelKeys,
88+
panel,
89+
filterData
90+
);
91+
_cachePosition.push([height, top]);
92+
const dom = document.querySelector<HTMLElement>(
93+
`.${collapseItemClassName}[data-content='${panel.id}']`
94+
);
95+
if (dom) {
96+
dom.style.height = `${height}px`;
97+
dom.style.top = `${top}px`;
98+
}
99+
});
100+
}, [activePanelKeys]);
101+
65102
const handleChangeCallback = (key: React.Key) => {
66103
const currentKeys = activePanelKeys.concat();
67104
if (currentKeys.includes(key)) {
@@ -92,14 +129,6 @@ export function Collapse(props: ICollapseProps) {
92129
return null;
93130
};
94131

95-
const filterData = data.filter((panel) => panel.id) as DataBaseProps[];
96-
if (filterData.length < data.length) {
97-
Logger.warn(new SyntaxError('collapse data must have id'));
98-
}
99-
100-
// to save position temporarily, empty array when rerender
101-
const _cachePosition: number[][] = [];
102-
let _cacheWrapperHeight = React.useRef(0);
103132
/**
104133
* Calculate the position of the panel in view
105134
* @param keys Current active keys
@@ -181,27 +210,14 @@ export function Collapse(props: ICollapseProps) {
181210
{filterData
182211
.filter((p) => !p.hidden)
183212
.map((panel) => {
184-
const content = renderPanels(panel, panel.renderPanel);
185-
// mark current panel empty and content
186-
panel._content = content;
187-
panel._isEmpty = !content;
188213
const isActive = activePanelKeys.includes(panel.id);
189-
const [height, top] = calcPosition(
190-
activePanelKeys,
191-
panel,
192-
filterData
193-
);
194-
_cachePosition.push([height, top]);
195214
return (
196215
<div
197216
className={classNames(
198217
collapseItemClassName,
199218
isActive && collapseActiveClassName
200219
)}
201-
style={{
202-
height,
203-
top,
204-
}}
220+
data-content={panel.id}
205221
key={panel.id}
206222
>
207223
<div
@@ -235,9 +251,10 @@ export function Collapse(props: ICollapseProps) {
235251
</div>
236252
<div
237253
className={collapseContentClassName}
254+
data-content={panel.id}
238255
tabIndex={0}
239256
>
240-
{content}
257+
{renderPanels(panel, panel.renderPanel)}
241258
</div>
242259
</div>
243260
);

0 commit comments

Comments
 (0)