Skip to content

Commit 6a6d106

Browse files
committed
fix(Toolbar filter): Fixed null exception in Toolbar filter
1 parent 911223a commit 6a6d106

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

packages/react-core/src/components/Toolbar/ToolbarFilter.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,29 @@ class ToolbarFilter extends Component<ToolbarFilterProps, ToolbarFilterState> {
130130
) : null;
131131

132132
if (!_isExpanded && this.state.isMounted) {
133+
const collapsedLabelPortalTarget = labelGroupContentRef?.current?.firstElementChild ?? null;
133134
return (
134135
<Fragment>
135136
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
136-
{labelGroupContentRef?.current?.firstElementChild !== null &&
137-
ReactDOM.createPortal(labelGroup, labelGroupContentRef.current.firstElementChild)}
137+
{collapsedLabelPortalTarget != null && ReactDOM.createPortal(labelGroup, collapsedLabelPortalTarget)}
138138
</Fragment>
139139
);
140140
}
141141

142142
return (
143143
<ToolbarContentContext.Consumer>
144-
{({ labelContainerRef }) => (
145-
<Fragment>
146-
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
147-
{labelContainerRef.current && ReactDOM.createPortal(labelGroup, labelContainerRef.current)}
148-
{expandableLabelContainerRef &&
149-
expandableLabelContainerRef.current &&
150-
ReactDOM.createPortal(labelGroup, expandableLabelContainerRef.current)}
151-
</Fragment>
152-
)}
144+
{({ labelContainerRef }) => {
145+
const labelContainer = labelContainerRef?.current ?? null;
146+
return (
147+
<Fragment>
148+
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
149+
{labelContainer != null && ReactDOM.createPortal(labelGroup, labelContainer)}
150+
{expandableLabelContainerRef &&
151+
expandableLabelContainerRef.current &&
152+
ReactDOM.createPortal(labelGroup, expandableLabelContainerRef.current)}
153+
</Fragment>
154+
);
155+
}}
153156
</ToolbarContentContext.Consumer>
154157
);
155158
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { createRef } from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
5+
import { ToolbarContext } from '../ToolbarUtils';
6+
import { ToolbarFilter } from '../ToolbarFilter';
7+
8+
describe('ToolbarFilter', () => {
9+
it('renders when ToolbarFilter is not under Toolbar or ToolbarContent (default context)', () => {
10+
const deleteLabel = jest.fn();
11+
const deleteLabelGroup = jest.fn();
12+
13+
expect(() =>
14+
render(
15+
<ToolbarFilter
16+
labels={['one']}
17+
deleteLabel={deleteLabel}
18+
deleteLabelGroup={deleteLabelGroup}
19+
categoryName="Status"
20+
>
21+
filter content
22+
</ToolbarFilter>
23+
)
24+
).not.toThrow();
25+
26+
expect(screen.getByText('filter content')).toBeInTheDocument();
27+
});
28+
29+
it('does not throw when labelGroupContentRef.current is still null while collapsed (listed filters)', () => {
30+
const labelGroupContentRef = createRef<HTMLDivElement>();
31+
expect(labelGroupContentRef.current).toBeNull();
32+
33+
expect(() =>
34+
render(
35+
<ToolbarContext.Provider
36+
value={{
37+
isExpanded: false,
38+
toggleIsExpanded: () => {},
39+
labelGroupContentRef,
40+
updateNumberFilters: () => {},
41+
numberOfFilters: 0,
42+
clearAllFilters: () => {}
43+
}}
44+
>
45+
<ToolbarFilter labels={['one']} deleteLabel={jest.fn()} deleteLabelGroup={jest.fn()} categoryName="Status">
46+
filter content
47+
</ToolbarFilter>
48+
</ToolbarContext.Provider>
49+
)
50+
).not.toThrow();
51+
52+
expect(screen.getByText('filter content')).toBeInTheDocument();
53+
});
54+
});

0 commit comments

Comments
 (0)