diff --git a/libs/ui-lib/lib/ocm/components/clusters/ClustersListToolbar.tsx b/libs/ui-lib/lib/ocm/components/clusters/ClustersListToolbar.tsx index 8bf03e1129..83484ddb37 100644 --- a/libs/ui-lib/lib/ocm/components/clusters/ClustersListToolbar.tsx +++ b/libs/ui-lib/lib/ocm/components/clusters/ClustersListToolbar.tsx @@ -4,7 +4,6 @@ import { Toolbar, ToolbarItem, ToolbarContent, - ToolbarFilter, InputGroup, TextInput, ToolbarProps, @@ -28,6 +27,7 @@ import { useTranslation } from '../../../common/hooks/use-translation-wrapper'; import { Cluster } from '@openshift-assisted/types/assisted-installer-service'; import { useDispatchDay1, useSelectorDay1 } from '../../store'; import { selectClustersUIState } from '../../store/slices/clusters/selectors'; +import { CustomToolbarFilter } from './CustomToolbarFilter'; export type ClusterFiltersType = { [key: string]: string[]; // value from clusterStatusLabels @@ -103,10 +103,6 @@ const ClustersListToolbar: React.FC = ({ ); const { t } = useTranslation(); - React.useEffect(() => { - window.addEventListener('resize', () => setStatusExpanded(false)); - }, []); - return ( = ({ - = ({ /> ))} - + navigate(`~new`)} diff --git a/libs/ui-lib/lib/ocm/components/clusters/CustomToolbarFilter.tsx b/libs/ui-lib/lib/ocm/components/clusters/CustomToolbarFilter.tsx new file mode 100644 index 0000000000..400654addd --- /dev/null +++ b/libs/ui-lib/lib/ocm/components/clusters/CustomToolbarFilter.tsx @@ -0,0 +1,134 @@ +// This component resolves the folowing bug: https://issues.redhat.com/browse/MGMT-18567 +// Once this issues is addressed in PatternFly, this component should be removed. + +import { + Chip, + ChipGroup, + PickOptional, + ToolbarChip, + ToolbarContentContext, + ToolbarContext, + ToolbarFilterProps, + ToolbarItem, +} from '@patternfly/react-core'; +import * as React from 'react'; +import * as ReactDOM from 'react-dom'; + +interface ToolbarFilterState { + isMounted: boolean; +} + +class CustomToolbarFilter extends React.Component { + static displayName = 'CustomToolbarFilter'; + static contextType = ToolbarContext; + context!: React.ContextType; + static defaultProps: PickOptional = { + chips: [] as (string | ToolbarChip)[], + showToolbarItem: true, + }; + + constructor(props: ToolbarFilterProps) { + super(props); + this.state = { + isMounted: false, + }; + } + + componentDidMount() { + const { categoryName, chips } = this.props; + this.context.updateNumberFilters( + typeof categoryName !== 'string' && categoryName.hasOwnProperty('key') + ? categoryName.key + : categoryName.toString(), + chips ? chips.length : 0, + ); + this.setState({ isMounted: true }); + } + + componentDidUpdate() { + const { categoryName, chips } = this.props; + this.context.updateNumberFilters( + typeof categoryName !== 'string' && categoryName.hasOwnProperty('key') + ? categoryName.key + : categoryName.toString(), + chips ? chips.length : 0, + ); + } + + render() { + const { + children, + chips, + deleteChipGroup, + deleteChip, + chipGroupExpandedText, + chipGroupCollapsedText, + categoryName, + showToolbarItem, + isExpanded, + expandableChipContainerRef, + ...props + } = this.props; + const { isExpanded: managedIsExpanded, chipGroupContentRef } = this.context; + const _isExpanded = isExpanded !== undefined ? isExpanded : managedIsExpanded; + const categoryKey = + typeof categoryName !== 'string' && categoryName.hasOwnProperty('key') + ? categoryName.key + : categoryName.toString(); + + const chipGroup = + chips !== undefined && chips.length ? ( + + deleteChipGroup && deleteChipGroup(categoryName)} + collapsedText={chipGroupCollapsedText} + expandedText={chipGroupExpandedText} + > + {chips.map((chip) => + typeof chip === 'string' ? ( + deleteChip && deleteChip(categoryKey, chip)}> + {chip} + + ) : ( + deleteChip && deleteChip(categoryKey, chip)}> + {chip.node} + + ), + )} + + + ) : null; + + if (!_isExpanded && this.state.isMounted) { + return ( + + {showToolbarItem && {children}} + {chipGroupContentRef && + chipGroupContentRef.current !== null && + chipGroupContentRef.current.firstElementChild !== null && + ReactDOM.createPortal(chipGroup, chipGroupContentRef.current.firstElementChild)} + + ); + } + + return ( + + {({ chipContainerRef }) => ( + + {showToolbarItem && {children}} + {chipContainerRef.current && + ReactDOM.createPortal(chipGroup, chipContainerRef.current as Element)} + {expandableChipContainerRef && + expandableChipContainerRef.current && + ReactDOM.createPortal(chipGroup, expandableChipContainerRef.current)} + + )} + + ); + } +} + +export { CustomToolbarFilter };