11import clsx from 'clsx' ;
2- import React , { useEffect , useRef , useState } from 'react' ;
2+ import React , { useCallback , useEffect , useRef , useState } from 'react' ;
33import type { CollapseProps } from './types' ;
44
55const MDBCollapse : React . FC < CollapseProps > = ( {
@@ -28,6 +28,18 @@ const MDBCollapse: React.FC<CollapseProps> = ({
2828 ) ;
2929 const refCollapse = useRef < HTMLElement > ( null ) ;
3030
31+ const handleResize = useCallback ( ( ) => {
32+ if ( showCollapse || statement ) {
33+ setCollapseHeight ( undefined ) ;
34+ }
35+ } , [ showCollapse , statement ] ) ;
36+
37+ useEffect ( ( ) => {
38+ if ( collapseHeight === undefined && ( showCollapse || statement ) ) {
39+ setCollapseHeight ( refCollapse ?. current ?. scrollHeight ) ;
40+ }
41+ } , [ collapseHeight , showCollapse , statement ] ) ;
42+
3143 useEffect ( ( ) => {
3244 if ( typeof show === 'string' ) {
3345 setShowCollapseString ( show ) ;
@@ -47,7 +59,7 @@ const MDBCollapse: React.FC<CollapseProps> = ({
4759 return ( ) => {
4860 clearTimeout ( timer ) ;
4961 } ;
50- } , [ collapseHeight , show , showCollapse , id , showCollapseString , statement ] ) ;
62+ } , [ show , showCollapse , id , showCollapseString , statement ] ) ;
5163
5264 useEffect ( ( ) => {
5365 if ( showCollapse || statement ) {
@@ -57,6 +69,14 @@ const MDBCollapse: React.FC<CollapseProps> = ({
5769 }
5870 } , [ showCollapse , statement ] ) ;
5971
72+ useEffect ( ( ) => {
73+ window . addEventListener ( 'resize' , handleResize ) ;
74+
75+ return ( ) => {
76+ window . removeEventListener ( 'resize' , handleResize ) ;
77+ } ;
78+ } , [ handleResize ] ) ;
79+
6080 return (
6181 < Tag style = { { height : collapseHeight , ...style } } id = { id } className = { classes } { ...props } ref = { refCollapse } >
6282 { children }
0 commit comments