diff --git a/packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx b/packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx index 27c26cce648..e1d338a4a4f 100644 --- a/packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx +++ b/packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx @@ -70,7 +70,7 @@ export interface ExpandableSectionProps extends React.HTMLProps interface ExpandableSectionState { isExpanded: boolean; hasToggle: boolean; - previousWidth: number; + previousWidth?: number; } const setLineClamp = (lines: number, element: HTMLDivElement) => { @@ -87,7 +87,7 @@ class ExpandableSection extends React.Component { - if (this.expandableContentRef.current) { - const { offsetWidth } = this.expandableContentRef.current; - if (this.state.previousWidth !== offsetWidth) { - this.setState({ previousWidth: offsetWidth }); - this.checkToggleVisibility(); - } + if (!this.expandableContentRef.current) { + return; + } + const { offsetWidth } = this.expandableContentRef.current; + if (this.state.previousWidth !== offsetWidth) { + this.setState({ previousWidth: offsetWidth }); + this.checkToggleVisibility(); } }; handleResize = debounce(this.resize, 250); @@ -221,7 +227,7 @@ class ExpandableSection extends React.Component { - this.setState({ isExpanded: isOpen }, () => onToggleProp(event, this.state.isExpanded)); + this.setState({ isExpanded: isOpen }, () => onToggleProp?.(event, this.state.isExpanded)); }; } @@ -239,7 +245,7 @@ class ExpandableSection extends React.Component onToggle(event, !propOrStateIsExpanded)} + onClick={(event) => onToggle?.(event, !propOrStateIsExpanded)} > {variant !== ExpandableSectionVariant.truncate && (