Skip to content

Commit

Permalink
MGMT-18567: Solving errors in clusters list (#2652)
Browse files Browse the repository at this point in the history
* Create CustomToolbarFilter to avoid problems with cluster's list

* Add note to CustomToolbarFilter component

---------

Co-authored-by: Julie Gyselová <[email protected]>
  • Loading branch information
ammont82 and jgyselov authored Aug 22, 2024
1 parent 0adb6a9 commit 5a35b21
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 7 deletions.
10 changes: 3 additions & 7 deletions libs/ui-lib/lib/ocm/components/clusters/ClustersListToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Toolbar,
ToolbarItem,
ToolbarContent,
ToolbarFilter,
InputGroup,
TextInput,
ToolbarProps,
Expand All @@ -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
Expand Down Expand Up @@ -103,10 +103,6 @@ const ClustersListToolbar: React.FC<ClustersListToolbarProps> = ({
);
const { t } = useTranslation();

React.useEffect(() => {
window.addEventListener('resize', () => setStatusExpanded(false));
}, []);

return (
<Toolbar
id="clusters-list-toolbar"
Expand All @@ -131,7 +127,7 @@ const ClustersListToolbar: React.FC<ClustersListToolbarProps> = ({
</InputGroupItem>
</InputGroup>
</ToolbarItem>
<ToolbarFilter
<CustomToolbarFilter
chips={filters.status}
deleteChip={onDeleteChip}
deleteChipGroup={onDeleteChipGroup}
Expand All @@ -155,7 +151,7 @@ const ClustersListToolbar: React.FC<ClustersListToolbarProps> = ({
/>
))}
</Select>
</ToolbarFilter>
</CustomToolbarFilter>
<ToolbarButton
variant={ButtonVariant.primary}
onClick={() => navigate(`~new`)}
Expand Down
134 changes: 134 additions & 0 deletions libs/ui-lib/lib/ocm/components/clusters/CustomToolbarFilter.tsx
Original file line number Diff line number Diff line change
@@ -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<ToolbarFilterProps, ToolbarFilterState> {
static displayName = 'CustomToolbarFilter';
static contextType = ToolbarContext;
context!: React.ContextType<typeof ToolbarContext>;
static defaultProps: PickOptional<ToolbarFilterProps> = {
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 ? (
<ToolbarItem variant="chip-group">
<ChipGroup
key={categoryKey}
categoryName={typeof categoryName === 'string' ? categoryName : categoryName.name}
isClosable={deleteChipGroup !== undefined}
onClick={() => deleteChipGroup && deleteChipGroup(categoryName)}
collapsedText={chipGroupCollapsedText}
expandedText={chipGroupExpandedText}
>
{chips.map((chip) =>
typeof chip === 'string' ? (
<Chip key={chip} onClick={() => deleteChip && deleteChip(categoryKey, chip)}>
{chip}
</Chip>
) : (
<Chip key={chip.key} onClick={() => deleteChip && deleteChip(categoryKey, chip)}>
{chip.node}
</Chip>
),
)}
</ChipGroup>
</ToolbarItem>
) : null;

if (!_isExpanded && this.state.isMounted) {
return (
<React.Fragment>
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
{chipGroupContentRef &&
chipGroupContentRef.current !== null &&
chipGroupContentRef.current.firstElementChild !== null &&
ReactDOM.createPortal(chipGroup, chipGroupContentRef.current.firstElementChild)}
</React.Fragment>
);
}

return (
<ToolbarContentContext.Consumer>
{({ chipContainerRef }) => (
<React.Fragment>
{showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>}
{chipContainerRef.current &&
ReactDOM.createPortal(chipGroup, chipContainerRef.current as Element)}
{expandableChipContainerRef &&
expandableChipContainerRef.current &&
ReactDOM.createPortal(chipGroup, expandableChipContainerRef.current)}
</React.Fragment>
)}
</ToolbarContentContext.Consumer>
);
}
}

export { CustomToolbarFilter };

0 comments on commit 5a35b21

Please sign in to comment.