Skip to content

Commit 85e0753

Browse files
authored
Create CustomToolbarFilter to avoid problems with cluster's list (#2650)
1 parent 87b152a commit 85e0753

File tree

2 files changed

+134
-3
lines changed

2 files changed

+134
-3
lines changed

Diff for: libs/ui-lib/lib/ocm/components/clusters/ClustersListToolbar.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Toolbar,
55
ToolbarItem,
66
ToolbarContent,
7-
ToolbarFilter,
87
InputGroup,
98
TextInput,
109
ToolbarProps,
@@ -29,6 +28,7 @@ import { useTranslation } from '../../../common/hooks/use-translation-wrapper';
2928
import { Cluster } from '@openshift-assisted/types/assisted-installer-service';
3029
import { useDispatchDay1, useSelectorDay1 } from '../../store';
3130
import { selectClustersUIState } from '../../store/slices/clusters/selectors';
31+
import { CustomToolbarFilter } from './CustomToolbarFilter';
3232

3333
export type ClusterFiltersType = {
3434
[key: string]: string[]; // value from clusterStatusLabels
@@ -127,7 +127,7 @@ const ClustersListToolbar: React.FC<ClustersListToolbarProps> = ({
127127
</InputGroupItem>
128128
</InputGroup>
129129
</ToolbarItem>
130-
<ToolbarFilter
130+
<CustomToolbarFilter
131131
chips={filters.status}
132132
deleteChip={onDeleteChip}
133133
deleteChipGroup={onDeleteChipGroup}
@@ -151,7 +151,7 @@ const ClustersListToolbar: React.FC<ClustersListToolbarProps> = ({
151151
/>
152152
))}
153153
</Select>
154-
</ToolbarFilter>
154+
</CustomToolbarFilter>
155155
<ToolbarButton
156156
variant={ButtonVariant.primary}
157157
onClick={() => history.push(`${routeBasePath}/clusters/~new`)}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import {
2+
Chip,
3+
ChipGroup,
4+
PickOptional,
5+
ToolbarChip,
6+
ToolbarContentContext,
7+
ToolbarContext,
8+
ToolbarFilterProps,
9+
ToolbarItem,
10+
} from '@patternfly/react-core';
11+
import * as React from 'react';
12+
import * as ReactDOM from 'react-dom';
13+
14+
interface ToolbarFilterState {
15+
isMounted: boolean;
16+
}
17+
18+
class CustomToolbarFilter extends React.Component<ToolbarFilterProps, ToolbarFilterState> {
19+
static displayName = 'CustomToolbarFilter';
20+
static contextType = ToolbarContext;
21+
context!: React.ContextType<typeof ToolbarContext>;
22+
static defaultProps: PickOptional<ToolbarFilterProps> = {
23+
chips: [] as (string | ToolbarChip)[],
24+
showToolbarItem: true,
25+
};
26+
27+
constructor(props: ToolbarFilterProps) {
28+
super(props);
29+
this.state = {
30+
isMounted: false,
31+
};
32+
}
33+
34+
componentDidMount() {
35+
const { categoryName, chips } = this.props;
36+
this.context.updateNumberFilters(
37+
typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
38+
? categoryName.key
39+
: categoryName.toString(),
40+
chips ? chips.length : 0,
41+
);
42+
this.setState({ isMounted: true });
43+
}
44+
45+
componentDidUpdate() {
46+
const { categoryName, chips } = this.props;
47+
this.context.updateNumberFilters(
48+
typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
49+
? categoryName.key
50+
: categoryName.toString(),
51+
chips ? chips.length : 0,
52+
);
53+
}
54+
55+
render() {
56+
const {
57+
children,
58+
chips,
59+
deleteChipGroup,
60+
deleteChip,
61+
chipGroupExpandedText,
62+
chipGroupCollapsedText,
63+
categoryName,
64+
showToolbarItem,
65+
isExpanded,
66+
expandableChipContainerRef,
67+
...props
68+
} = this.props;
69+
const { isExpanded: managedIsExpanded, chipGroupContentRef } = this.context;
70+
const _isExpanded = isExpanded !== undefined ? isExpanded : managedIsExpanded;
71+
const categoryKey =
72+
typeof categoryName !== 'string' && categoryName.hasOwnProperty('key')
73+
? categoryName.key
74+
: categoryName.toString();
75+
76+
const chipGroup =
77+
chips !== undefined && chips.length ? (
78+
<ToolbarItem variant="chip-group">
79+
<ChipGroup
80+
key={categoryKey}
81+
categoryName={typeof categoryName === 'string' ? categoryName : categoryName.name}
82+
isClosable={deleteChipGroup !== undefined}
83+
onClick={() => deleteChipGroup && deleteChipGroup(categoryName)}
84+
collapsedText={chipGroupCollapsedText}
85+
expandedText={chipGroupExpandedText}
86+
>
87+
{chips.map((chip) =>
88+
typeof chip === 'string' ? (
89+
<Chip key={chip} onClick={() => deleteChip && deleteChip(categoryKey, chip)}>
90+
{chip}
91+
</Chip>
92+
) : (
93+
<Chip key={chip.key} onClick={() => deleteChip && deleteChip(categoryKey, chip)}>
94+
{chip.node}
95+
</Chip>
96+
),
97+
)}
98+
</ChipGroup>
99+
</ToolbarItem>
100+
) : null;
101+
102+
if (!_isExpanded && this.state.isMounted) {
103+
return (
104+
<React.Fragment>
105+
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
106+
{chipGroupContentRef &&
107+
chipGroupContentRef.current !== null &&
108+
chipGroupContentRef.current.firstElementChild !== null &&
109+
ReactDOM.createPortal(chipGroup, chipGroupContentRef.current.firstElementChild)}
110+
</React.Fragment>
111+
);
112+
}
113+
114+
return (
115+
<ToolbarContentContext.Consumer>
116+
{({ chipContainerRef }) => (
117+
<React.Fragment>
118+
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
119+
{chipContainerRef.current &&
120+
ReactDOM.createPortal(chipGroup, chipContainerRef.current as Element)}
121+
{expandableChipContainerRef &&
122+
expandableChipContainerRef.current &&
123+
ReactDOM.createPortal(chipGroup, expandableChipContainerRef.current)}
124+
</React.Fragment>
125+
)}
126+
</ToolbarContentContext.Consumer>
127+
);
128+
}
129+
}
130+
131+
export { CustomToolbarFilter };

0 commit comments

Comments
 (0)