Skip to content

Commit ad3367d

Browse files
committed
OCPBUGS-63471: Render a button if there is 1 action
1 parent c039d78 commit ad3367d

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

frontend/public/components/utils/actions-menu.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,31 @@ import {
33
impersonateStateToProps,
44
useSafetyFirst,
55
} from '@console/dynamic-plugin-sdk';
6-
import { Dropdown, MenuToggle, MenuToggleElement } from '@patternfly/react-core';
6+
import { Button, Dropdown, MenuToggle, MenuToggleElement } from '@patternfly/react-core';
77
import { some } from 'lodash-es';
88
import { useTranslation } from 'react-i18next';
99
import { connect } from 'react-redux';
1010
import { useNavigate } from 'react-router-dom-v5-compat';
1111

1212
import { ReactNode, RefObject, useEffect, useState } from 'react';
13-
import { KebabItems, KebabOption } from './kebab';
13+
import { KebabItem, KebabItems, KebabOption } from './kebab';
1414
import { checkAccess } from './rbac';
1515

1616
type ActionsMenuProps = {
1717
actions: KebabOption[];
1818
title?: ReactNode;
1919
};
2020

21-
const ActionsMenuDropdown = (props) => {
21+
type ActionsMenuDropdownProps = {
22+
actions: KebabOption[];
23+
title?: ReactNode;
24+
active?: boolean;
25+
};
26+
27+
const ActionsMenuDropdown: React.FCC<ActionsMenuDropdownProps> = ({ actions, title, active }) => {
2228
const { t } = useTranslation();
2329
const navigate = useNavigate();
24-
const [active, setActive] = useState(!!props.active);
30+
const [isActive, setIsActive] = useState(!!active);
2531

2632
const onClick = (event, option) => {
2733
event.preventDefault();
@@ -34,14 +40,22 @@ const ActionsMenuDropdown = (props) => {
3440
navigate(option.href);
3541
}
3642

37-
setActive(false);
43+
setIsActive(false);
3844
};
3945

46+
if (actions.length === 0) {
47+
return null;
48+
}
49+
50+
if (actions.length === 1) {
51+
return <KebabItem option={actions[0]} onClick={onClick} Component={Button} />;
52+
}
53+
4054
return (
4155
<Dropdown
42-
isOpen={active}
43-
onSelect={() => setActive(false)}
44-
onOpenChange={setActive}
56+
isOpen={isActive}
57+
onSelect={() => setIsActive(false)}
58+
onOpenChange={setIsActive}
4559
shouldFocusToggleOnSelect
4660
popperProps={{
4761
enableFlip: true,
@@ -50,15 +64,15 @@ const ActionsMenuDropdown = (props) => {
5064
toggle={(toggleRef: RefObject<MenuToggleElement>) => (
5165
<MenuToggle
5266
ref={toggleRef}
53-
onClick={() => setActive(!active)}
67+
onClick={() => setIsActive(!active)}
5468
isExpanded={active}
5569
data-test-id="actions-menu-button"
5670
>
57-
{props.title || t('public~Actions')}
71+
{title || t('public~Actions')}
5872
</MenuToggle>
5973
)}
6074
>
61-
<KebabItems options={props.actions} onClick={onClick} />
75+
<KebabItems options={actions} onClick={onClick} />
6276
</Dropdown>
6377
);
6478
};

frontend/public/components/utils/kebab.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,20 @@ const KebabItem_: React.FC<KebabItemProps & { isAllowed: boolean }> = ({
8888
onClick,
8989
autoFocus,
9090
isAllowed,
91+
Component = DropdownItem,
9192
}) => {
9293
const { t } = useTranslation();
9394
const isDisabled = !isAllowed || option.isDisabled || (!option.href && !option.callback);
9495
return (
95-
<DropdownItem
96+
<Component
9697
onClick={(e) => !isDisabled && onClick(e, option)}
9798
autoFocus={autoFocus}
9899
isDisabled={isDisabled}
99100
data-test-action={option.labelKey ? t(option.labelKey, option.labelKind) : option.label}
100101
icon={option.icon}
101102
>
102103
{option.labelKey ? t(option.labelKey, option.labelKind) : option.label}
103-
</DropdownItem>
104+
</Component>
104105
);
105106
};
106107
export const KebabItemAccessReview_ = (
@@ -456,6 +457,12 @@ type KebabItemProps = {
456457
option: KebabOption;
457458
onClick: (event: React.MouseEvent<{}>, option: KebabOption) => void;
458459
autoFocus?: boolean;
460+
Component?: React.ComponentType<
461+
Pick<
462+
React.ComponentProps<typeof DropdownItem>,
463+
'onClick' | 'isDisabled' | 'autoFocus' | 'children' | 'icon'
464+
>
465+
>;
459466
};
460467

461468
export type KebabItemsProps = {

0 commit comments

Comments
 (0)