| 1 |
import { forwardRef } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Menu/menu'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { MenuContext, MenuItemContext } from './MenuContext'; |
| 5 |
import { Button } from '../Button'; |
| 6 |
export interface MenuItemActionProps extends React.HTMLProps<HTMLDivElement> { |
| 7 |
/** Additional classes added to the action button */ |
| 8 |
className?: string; |
| 9 |
/** The action icon to use */ |
| 10 |
icon?: 'favorites' | React.ReactNode; |
| 11 |
/** Callback on action click, can also specify onActionClick on the Menu instead */ |
| 12 |
onClick?: (event?: any) => void; |
| 13 |
/** Accessibility label */ |
| 14 |
'aria-label': string; |
| 15 |
/** Flag indicating if the item is favorited */ |
| 16 |
isFavorited?: boolean; |
| 17 |
/** Disables action, can also be specified on the MenuItem instead */ |
| 18 |
isDisabled?: boolean; |
| 19 |
/** Identifies the action item in the onActionClick on the Menu */ |
| 20 |
actionId?: any; |
| 21 |
/** @hide Forwarded ref */ |
| 22 |
innerRef?: React.Ref<any>; |
| 23 |
} |
| 24 |
|
| 25 |
const MenuItemActionBase: React.FunctionComponent<MenuItemActionProps> = ({ |
| 26 |
className, |
| 27 |
icon, |
| 28 |
onClick, |
| 29 |
'aria-label': ariaLabel, |
| 30 |
isFavorited = null, |
| 31 |
isDisabled, |
| 32 |
actionId, |
| 33 |
innerRef, |
| 34 |
...props |
| 35 |
}: MenuItemActionProps) => ( |
| 36 |
<MenuContext.Consumer> |
| 37 |
{({ onActionClick }) => ( |
| 38 |
<MenuItemContext.Consumer> |
| 39 |
{({ itemId, isDisabled: isDisabledContext }) => { |
| 40 |
const onClickButton = (event: any) => { |
| 41 |
// event specified on the MenuItemAction |
| 42 |
onClick && onClick(event); |
| 43 |
// event specified on the Menu |
| 44 |
onActionClick && onActionClick(event, itemId, actionId); |
| 45 |
}; |
| 46 |
return ( |
| 47 |
<div |
| 48 |
className={css( |
| 49 |
styles.menuItemAction, |
| 50 |
isFavorited !== null && 'pf-m-favorite', |
| 51 |
isFavorited && styles.modifiers.favorited, |
| 52 |
className |
| 53 |
)} |
| 54 |
{...props} |
| 55 |
> |
| 56 |
<Button |
| 57 |
aria-label={ariaLabel} |
| 58 |
onClick={onClickButton} |
| 59 |
ref={innerRef} |
| 60 |
role="menuitem" |
| 61 |
variant="plain" |
| 62 |
isFavorite={isFavorited !== null} |
| 63 |
isFavorited={isFavorited ?? false} |
| 64 |
tabIndex={-1} |
| 65 |
isDisabled={isDisabled || isDisabledContext} |
| 66 |
icon={isFavorited === null ? icon : undefined} |
| 67 |
/> |
| 68 |
</div> |
| 69 |
); |
| 70 |
}} |
| 71 |
</MenuItemContext.Consumer> |
| 72 |
)} |
| 73 |
</MenuContext.Consumer> |
| 74 |
); |
| 75 |
|
| 76 |
export const MenuItemAction = forwardRef((props: MenuItemActionProps, ref: React.Ref<HTMLButtonElement>) => ( |
| 77 |
<MenuItemActionBase {...props} innerRef={ref} /> |
| 78 |
)); |
| 79 |
MenuItemAction.displayName = 'MenuItemAction'; |