From 85e07535aefce5b81055ea98069bd2adf10262e7 Mon Sep 17 00:00:00 2001 From: Montse Ortega Date: Wed, 14 Aug 2024 13:58:47 +0200 Subject: [PATCH] Create CustomToolbarFilter to avoid problems with cluster's list (#2650) --- .../clusters/ClustersListToolbar.tsx | 6 +- .../clusters/CustomToolbarFilter.tsx | 131 ++++++++++++++++++ 2 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 libs/ui-lib/lib/ocm/components/clusters/CustomToolbarFilter.tsx diff --git a/libs/ui-lib/lib/ocm/components/clusters/ClustersListToolbar.tsx b/libs/ui-lib/lib/ocm/components/clusters/ClustersListToolbar.tsx index f6f1f1c784..be845f14d1 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, @@ -29,6 +28,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 @@ -127,7 +127,7 @@ const ClustersListToolbar: React.FC = ({ - = ({ /> ))} - + history.push(`${routeBasePath}/clusters/~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..2459a6aadd --- /dev/null +++ b/libs/ui-lib/lib/ocm/components/clusters/CustomToolbarFilter.tsx @@ -0,0 +1,131 @@ +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 };