@@ -52,6 +52,16 @@ export function Collapse(props: ICollapseProps) {
52
52
...restProps
53
53
} = props ;
54
54
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
+
55
65
const handleResize = React . useCallback ( ( ) => {
56
66
// just want to trigger rerender
57
67
setActivePanelKeys ( ( keys ) => keys . concat ( ) ) ;
@@ -62,6 +72,33 @@ export function Collapse(props: ICollapseProps) {
62
72
return ( ) => window . removeEventListener ( 'resize' , handleResize ) ;
63
73
} , [ ] ) ;
64
74
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
+
65
102
const handleChangeCallback = ( key : React . Key ) => {
66
103
const currentKeys = activePanelKeys . concat ( ) ;
67
104
if ( currentKeys . includes ( key ) ) {
@@ -92,14 +129,6 @@ export function Collapse(props: ICollapseProps) {
92
129
return null ;
93
130
} ;
94
131
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 ) ;
103
132
/**
104
133
* Calculate the position of the panel in view
105
134
* @param keys Current active keys
@@ -181,27 +210,14 @@ export function Collapse(props: ICollapseProps) {
181
210
{ filterData
182
211
. filter ( ( p ) => ! p . hidden )
183
212
. map ( ( panel ) => {
184
- const content = renderPanels ( panel , panel . renderPanel ) ;
185
- // mark current panel empty and content
186
- panel . _content = content ;
187
- panel . _isEmpty = ! content ;
188
213
const isActive = activePanelKeys . includes ( panel . id ) ;
189
- const [ height , top ] = calcPosition (
190
- activePanelKeys ,
191
- panel ,
192
- filterData
193
- ) ;
194
- _cachePosition . push ( [ height , top ] ) ;
195
214
return (
196
215
< div
197
216
className = { classNames (
198
217
collapseItemClassName ,
199
218
isActive && collapseActiveClassName
200
219
) }
201
- style = { {
202
- height,
203
- top,
204
- } }
220
+ data-content = { panel . id }
205
221
key = { panel . id }
206
222
>
207
223
< div
@@ -235,9 +251,10 @@ export function Collapse(props: ICollapseProps) {
235
251
</ div >
236
252
< div
237
253
className = { collapseContentClassName }
254
+ data-content = { panel . id }
238
255
tabIndex = { 0 }
239
256
>
240
- { content }
257
+ { renderPanels ( panel , panel . renderPanel ) }
241
258
</ div >
242
259
</ div >
243
260
) ;
0 commit comments