@@ -70,7 +70,7 @@ export interface ExpandableSectionProps extends React.HTMLProps<HTMLDivElement>
70
70
interface ExpandableSectionState {
71
71
isExpanded : boolean ;
72
72
hasToggle : boolean ;
73
- previousWidth : number ;
73
+ previousWidth ? : number ;
74
74
}
75
75
76
76
const setLineClamp = ( lines : number , element : HTMLDivElement ) => {
@@ -87,7 +87,7 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
87
87
super ( props ) ;
88
88
89
89
this . state = {
90
- isExpanded : props . isExpanded ,
90
+ isExpanded : props . isExpanded || false ,
91
91
hasToggle : true ,
92
92
previousWidth : undefined
93
93
} ;
@@ -129,6 +129,9 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
129
129
componentDidMount ( ) {
130
130
if ( this . props . variant === ExpandableSectionVariant . truncate ) {
131
131
const expandableContent = this . expandableContentRef . current ;
132
+ if ( ! expandableContent ) {
133
+ return ;
134
+ }
132
135
this . setState ( { previousWidth : expandableContent . offsetWidth } ) ;
133
136
this . observer = getResizeObserver ( expandableContent , this . handleResize , false ) ;
134
137
@@ -143,6 +146,8 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
143
146
componentDidUpdate ( prevProps : ExpandableSectionProps ) {
144
147
if (
145
148
this . props . variant === ExpandableSectionVariant . truncate &&
149
+ this . props . truncateMaxLines &&
150
+ this . expandableContentRef . current &&
146
151
( prevProps . truncateMaxLines !== this . props . truncateMaxLines || prevProps . children !== this . props . children )
147
152
) {
148
153
const expandableContent = this . expandableContentRef . current ;
@@ -171,12 +176,13 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
171
176
} ;
172
177
173
178
resize = ( ) => {
174
- if ( this . expandableContentRef . current ) {
175
- const { offsetWidth } = this . expandableContentRef . current ;
176
- if ( this . state . previousWidth !== offsetWidth ) {
177
- this . setState ( { previousWidth : offsetWidth } ) ;
178
- this . checkToggleVisibility ( ) ;
179
- }
179
+ if ( ! this . expandableContentRef . current ) {
180
+ return ;
181
+ }
182
+ const { offsetWidth } = this . expandableContentRef . current ;
183
+ if ( this . state . previousWidth !== offsetWidth ) {
184
+ this . setState ( { previousWidth : offsetWidth } ) ;
185
+ this . checkToggleVisibility ( ) ;
180
186
}
181
187
} ;
182
188
handleResize = debounce ( this . resize , 250 ) ;
@@ -221,7 +227,7 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
221
227
if ( isExpanded === undefined ) {
222
228
propOrStateIsExpanded = this . state . isExpanded ;
223
229
onToggle = ( event , isOpen ) => {
224
- this . setState ( { isExpanded : isOpen } , ( ) => onToggleProp ( event , this . state . isExpanded ) ) ;
230
+ this . setState ( { isExpanded : isOpen } , ( ) => onToggleProp ?. ( event , this . state . isExpanded ) ) ;
225
231
} ;
226
232
}
227
233
@@ -239,7 +245,7 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
239
245
aria-expanded = { propOrStateIsExpanded }
240
246
aria-controls = { uniqueContentId }
241
247
id = { uniqueToggleId }
242
- onClick = { ( event ) => onToggle ( event , ! propOrStateIsExpanded ) }
248
+ onClick = { ( event ) => onToggle ?. ( event , ! propOrStateIsExpanded ) }
243
249
>
244
250
{ variant !== ExpandableSectionVariant . truncate && (
245
251
< span className = { css ( styles . expandableSectionToggleIcon ) } >
0 commit comments