Skip to content

Commit bd6b854

Browse files
authored
fix: add checks for contentRef to avoid errors in console (patternfly#10870)
1 parent a54217a commit bd6b854

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

packages/react-core/src/components/ExpandableSection/ExpandableSection.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface ExpandableSectionProps extends React.HTMLProps<HTMLDivElement>
7070
interface ExpandableSectionState {
7171
isExpanded: boolean;
7272
hasToggle: boolean;
73-
previousWidth: number;
73+
previousWidth?: number;
7474
}
7575

7676
const setLineClamp = (lines: number, element: HTMLDivElement) => {
@@ -87,7 +87,7 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
8787
super(props);
8888

8989
this.state = {
90-
isExpanded: props.isExpanded,
90+
isExpanded: props.isExpanded || false,
9191
hasToggle: true,
9292
previousWidth: undefined
9393
};
@@ -129,6 +129,9 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
129129
componentDidMount() {
130130
if (this.props.variant === ExpandableSectionVariant.truncate) {
131131
const expandableContent = this.expandableContentRef.current;
132+
if (!expandableContent) {
133+
return;
134+
}
132135
this.setState({ previousWidth: expandableContent.offsetWidth });
133136
this.observer = getResizeObserver(expandableContent, this.handleResize, false);
134137

@@ -143,6 +146,8 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
143146
componentDidUpdate(prevProps: ExpandableSectionProps) {
144147
if (
145148
this.props.variant === ExpandableSectionVariant.truncate &&
149+
this.props.truncateMaxLines &&
150+
this.expandableContentRef.current &&
146151
(prevProps.truncateMaxLines !== this.props.truncateMaxLines || prevProps.children !== this.props.children)
147152
) {
148153
const expandableContent = this.expandableContentRef.current;
@@ -171,12 +176,13 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
171176
};
172177

173178
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();
180186
}
181187
};
182188
handleResize = debounce(this.resize, 250);
@@ -221,7 +227,7 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
221227
if (isExpanded === undefined) {
222228
propOrStateIsExpanded = this.state.isExpanded;
223229
onToggle = (event, isOpen) => {
224-
this.setState({ isExpanded: isOpen }, () => onToggleProp(event, this.state.isExpanded));
230+
this.setState({ isExpanded: isOpen }, () => onToggleProp?.(event, this.state.isExpanded));
225231
};
226232
}
227233

@@ -239,7 +245,7 @@ class ExpandableSection extends React.Component<ExpandableSectionProps, Expandab
239245
aria-expanded={propOrStateIsExpanded}
240246
aria-controls={uniqueContentId}
241247
id={uniqueToggleId}
242-
onClick={(event) => onToggle(event, !propOrStateIsExpanded)}
248+
onClick={(event) => onToggle?.(event, !propOrStateIsExpanded)}
243249
>
244250
{variant !== ExpandableSectionVariant.truncate && (
245251
<span className={css(styles.expandableSectionToggleIcon)}>

0 commit comments

Comments
 (0)