Skip to content

[release-4.19] OCPBUGS-56149: Refactor Kebab+ActionsMenu to use PF #15051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release-4.19
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ receivers:
.within(() => {
cy.get('[data-test-id="kebab-button"]').click();
});
cy.get('[data-test-action="Delete Receiver"]').should('be.disabled');
cy.get('[data-test-action="Delete Receiver"] button').should('be.disabled');
alertmanager.reset();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import { ActionsMenu } from '@console/internal/components/utils';
import { ActionsMenu } from '@console/internal/components/utils/actions-menu';
import { PageHeading } from '@console/shared/src/components/heading/PageHeading';
import { TaskKind } from '../../../../types';
import PipelineResourceRef from '../../../shared/common/PipelineResourceRef';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import * as React from 'react';
import { Edge, isNode, Node } from '@patternfly/react-topology';
import { useTranslation } from 'react-i18next';
import { ActionsMenu, SimpleTabNav } from '@console/internal/components/utils';
import { SimpleTabNav } from '@console/internal/components/utils';
import { ActionsMenu } from '@console/internal/components/utils/actions-menu';
import { PageHeading } from '@console/shared/src/components/heading/PageHeading';
import { edgeActions } from '../../actions/edgeActions';
import { TYPE_TRAFFIC_CONNECTOR } from '../../const';
Expand Down
10 changes: 5 additions & 5 deletions frontend/public/components/utils/__tests__/kebab.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ describe('KebabItem', () => {
const nothingOption = { ...mockOption, href: undefined };
const trackOnClick = jest.fn();
const renderItem = render(<KebabItem onClick={trackOnClick} option={nothingOption} />);
fireEvent.click(renderItem.getByRole('button'));
fireEvent.click(renderItem.getByRole('menuitem'));
expect(trackOnClick).toHaveBeenCalledTimes(0);
});
it('should enable when option has href', () => {
const hrefOption = { ...mockOption };
const trackOnClick = jest.fn();
const renderItem = render(<KebabItem onClick={trackOnClick} option={hrefOption} />);
fireEvent.click(renderItem.getByRole('button'));
fireEvent.click(renderItem.getByRole('menuitem'));
expect(trackOnClick).toHaveBeenCalledTimes(1);
});
it('should enable when option has a callback', () => {
const callbackOption = { ...mockOption, href: undefined, callback: () => {} };
const trackOnClick = jest.fn();
const renderItem = render(<KebabItem onClick={trackOnClick} option={callbackOption} />);
fireEvent.click(renderItem.getByRole('button'));
fireEvent.click(renderItem.getByRole('menuitem'));
expect(trackOnClick).toHaveBeenCalledTimes(1);
});
});
Expand All @@ -55,7 +55,7 @@ describe('KebabItemAccessReview_', () => {
impersonate={mockImpersonate}
/>,
);
fireEvent.click(renderItem.getByRole('button'));
fireEvent.click(renderItem.getByRole('menuitem'));
expect(trackOnClick).toHaveBeenCalledTimes(0);
});
it('should enable option when option.accessReview present and allowed', () => {
Expand All @@ -69,7 +69,7 @@ describe('KebabItemAccessReview_', () => {
impersonate={mockImpersonate}
/>,
);
fireEvent.click(renderItem.getByRole('button'));
fireEvent.click(renderItem.getByRole('menuitem'));
expect(trackOnClick).toHaveBeenCalledTimes(1);
});
});
21 changes: 0 additions & 21 deletions frontend/public/components/utils/_kebab.scss

This file was deleted.

98 changes: 98 additions & 0 deletions frontend/public/components/utils/actions-menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
ImpersonateKind,
impersonateStateToProps,
useSafetyFirst,
} from '@console/dynamic-plugin-sdk';
import { Dropdown, MenuToggle, MenuToggleElement } from '@patternfly/react-core';
import { some } from 'lodash-es';
import { useTranslation } from 'react-i18next';
import { connect } from 'react-redux';
import { useNavigate } from 'react-router-dom-v5-compat';

import { ReactNode, RefObject, useEffect, useState } from 'react';
import { KebabItems, KebabOption } from './kebab';
import { checkAccess } from './rbac';

type ActionsMenuProps = {
actions: KebabOption[];
title?: ReactNode;
};

const ActionsMenuDropdown = (props) => {
const { t } = useTranslation();
const navigate = useNavigate();
const [active, setActive] = useState(!!props.active);

const onClick = (event, option) => {
event.preventDefault();

if (option.callback) {
option.callback();
}

if (option.href) {
navigate(option.href);
}

setActive(false);
};

return (
<Dropdown
isOpen={active}
onSelect={() => setActive(false)}
onOpenChange={setActive}
shouldFocusToggleOnSelect
popperProps={{
enableFlip: true,
position: 'right',
}}
toggle={(toggleRef: RefObject<MenuToggleElement>) => (
<MenuToggle
ref={toggleRef}
onClick={() => setActive(!active)}
isExpanded={active}
data-test-id="actions-menu-button"
>
{props.title || t('public~Actions')}
</MenuToggle>
)}
>
<KebabItems options={props.actions} onClick={onClick} />
</Dropdown>
);
};

export const ActionsMenu: React.FCC<ActionsMenuProps> = connect(impersonateStateToProps)(
({
actions,
impersonate,
title = undefined,
}: ActionsMenuProps & { impersonate?: ImpersonateKind }) => {
const [isVisible, setVisible] = useSafetyFirst(false);

// Check if any actions are visible when actions have access reviews.
useEffect(() => {
if (!actions.length) {
setVisible(false);
return;
}
const promises = actions.reduce((acc, action) => {
if (action.accessReview) {
acc.push(checkAccess(action.accessReview));
}
return acc;
}, []);

// Only need to resolve if all actions require access review
if (promises.length !== actions.length) {
setVisible(true);
return;
}
Promise.all(promises)
.then((results) => setVisible(some(results, 'status.allowed')))
.catch(() => setVisible(true));
}, [actions, impersonate, setVisible]);
return isVisible ? <ActionsMenuDropdown actions={actions} title={title} /> : null;
},
);
154 changes: 2 additions & 152 deletions frontend/public/components/utils/dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import * as _ from 'lodash-es';
import * as React from 'react';
import classNames from 'classnames';
import * as PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { useTranslation, withTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom-v5-compat';
import { Divider, Popper, Title } from '@patternfly/react-core';
import { impersonateStateToProps, useSafetyFirst } from '@console/dynamic-plugin-sdk';
import { useUserSettingsCompatibility } from '@console/shared';
import { Divider, Popper, Title } from '@patternfly/react-core';
import { CaretDownIcon } from '@patternfly/react-icons/dist/esm/icons/caret-down-icon';
import { CheckIcon } from '@patternfly/react-icons/dist/esm/icons/check-icon';
import { StarIcon } from '@patternfly/react-icons/dist/esm/icons/star-icon';

import { checkAccess } from './rbac';
import { KebabItems } from './kebab';
import { withTranslation } from 'react-i18next';

class DropdownMixin extends React.PureComponent {
constructor(props) {
Expand Down Expand Up @@ -678,147 +672,3 @@ Dropdown.propTypes = {
required: PropTypes.bool,
dataTest: PropTypes.string,
};

const ActionsMenuDropdown = (props) => {
const { t } = useTranslation();
const navigate = useNavigate();
const [active, setActive] = React.useState(!!props.active);

const dropdownElement = React.useRef();

const show = () => {
setActive(true);
};

const hide = (e) => {
e?.stopPropagation();
setActive(false);
};

const listener = React.useCallback(
(event) => {
if (!active) {
return;
}

const { current } = dropdownElement;
if (!current) {
return;
}

if (event.target === current || current.contains(event.target)) {
return;
}

hide(event);
},
[active, dropdownElement],
);

React.useEffect(() => {
if (active) {
window.addEventListener('click', listener);
} else {
window.removeEventListener('click', listener);
}
return () => {
window.removeEventListener('click', listener);
};
}, [active, listener]);

const toggle = (e) => {
e.preventDefault();

if (props.disabled) {
return;
}

if (active) {
hide(e);
} else {
show(e);
}
};

const onClick = (event, option) => {
event.preventDefault();

if (option.callback) {
option.callback();
}

if (option.href) {
navigate(option.href);
}

hide();
};

return (
<div ref={dropdownElement}>
<button
type="button"
aria-haspopup="true"
aria-label={t('public~Actions')}
aria-expanded={active}
className={classNames({
'pf-v6-c-menu-toggle': true,
'pf-m-expanded': active,
})}
onClick={toggle}
data-test-id="actions-menu-button"
>
<span className="pf-v6-c-menu__toggle-text">{props.title || t('public~Actions')}</span>
<CaretDownIcon />
</button>
{active && (
<div className="co-actions-menu dropdown-menu pf-v6-c-menu">
<KebabItems options={props.actions} onClick={onClick} />
</div>
)}
</div>
);
};

const ActionsMenu_ = ({ actions, impersonate, title = undefined }) => {
const [isVisible, setVisible] = useSafetyFirst(false);

// Check if any actions are visible when actions have access reviews.
React.useEffect(() => {
if (!actions.length) {
setVisible(false);
return;
}
const promises = actions.reduce((acc, action) => {
if (action.accessReview) {
acc.push(checkAccess(action.accessReview));
}
return acc;
}, []);

// Only need to resolve if all actions require access review
if (promises.length !== actions.length) {
setVisible(true);
return;
}
Promise.all(promises)
.then((results) => setVisible(_.some(results, 'status.allowed')))
.catch(() => setVisible(true));
}, [actions, impersonate, setVisible]);
return isVisible ? <ActionsMenuDropdown actions={actions} title={title} /> : null;
};

export const ActionsMenu = connect(impersonateStateToProps)(ActionsMenu_);

ActionsMenu.propTypes = {
actions: PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.node,
labelKey: PropTypes.string,
href: PropTypes.string,
callback: PropTypes.func,
accessReview: PropTypes.object,
}),
).isRequired,
title: PropTypes.node,
};
3 changes: 2 additions & 1 deletion frontend/public/components/utils/headings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import * as React from 'react';
import { useTranslation } from 'react-i18next';

import { PageHeading, PageHeadingProps } from '@console/shared/src/components/heading/PageHeading';
import { ActionsMenu } from '@console/internal/components/utils/actions-menu';
import { connectToModel } from '../../kinds';
import { K8sKind, K8sResourceKind, K8sResourceKindReference } from '../../module/k8s';
import { ActionsMenu, FirehoseResult, KebabOption, ResourceIcon } from './index';
import { FirehoseResult, KebabOption, ResourceIcon } from './index';
import { ManagedByOperatorLink } from './managed-by';

export const ResourceItemDeleting = () => {
Expand Down
1 change: 0 additions & 1 deletion frontend/public/components/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export * from './dom-utils';
export * from './owner-references';
export { default } from './operator-backed-owner-references';
export * from './field-level-help';
export * from './single-typeahead-dropdown';
export * from './details-item';
export * from './types';
export * from './release-notes-link';
Expand Down
Loading