| 1 |
import { useState } from 'react'; |
| 2 |
import { Menu, MenuContent, MenuGroup, MenuList, MenuItem, MenuItemAction } from '@patternfly/react-core'; |
| 3 |
import RhUiMenuBarsIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-menu-bars-icon'; |
| 4 |
import RhUiClipboardFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-clipboard-fill-icon'; |
| 5 |
import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon'; |
| 6 |
import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-notification-fill-icon'; |
| 7 |
|
| 8 |
export const MenuWithActions: React.FunctionComponent = () => { |
| 9 |
const [selectedItems, setSelectedItems] = useState<number[]>([0, 2, 3]); |
| 10 |
|
| 11 |
const onSelect = (_event: React.MouseEvent<Element, MouseEvent> | undefined, itemId: number | string | undefined) => { |
| 12 |
const item = itemId as number; |
| 13 |
if (selectedItems.indexOf(item) !== -1) { |
| 14 |
setSelectedItems(selectedItems.filter((id) => id !== item)); |
| 15 |
} else { |
| 16 |
setSelectedItems([...selectedItems, item]); |
| 17 |
} |
| 18 |
}; |
| 19 |
|
| 20 |
return ( |
| 21 |
<Menu |
| 22 |
onSelect={onSelect} |
| 23 |
// eslint-disable-next-line no-console |
| 24 |
onActionClick={(_event, itemId, actionId) => console.log(`clicked on ${itemId} - ${actionId}`)} |
| 25 |
activeItemId={0} |
| 26 |
> |
| 27 |
<MenuContent> |
| 28 |
<MenuGroup label="Actions" labelHeadingLevel="h3"> |
| 29 |
<MenuList> |
| 30 |
<MenuItem |
| 31 |
isSelected={selectedItems.indexOf(0) !== -1} |
| 32 |
actions={ |
| 33 |
<MenuItemAction |
| 34 |
icon={<RhUiBranchFillIcon />} |
| 35 |
actionId="code" |
| 36 |
// eslint-disable-next-line no-console |
| 37 |
onClick={() => console.log('clicked on code icon')} |
| 38 |
aria-label="Code" |
| 39 |
/> |
| 40 |
} |
| 41 |
description="This is a description" |
| 42 |
itemId={0} |
| 43 |
> |
| 44 |
Item 1 |
| 45 |
</MenuItem> |
| 46 |
<MenuItem |
| 47 |
isDisabled |
| 48 |
isSelected={selectedItems.indexOf(1) !== -1} |
| 49 |
actions={<MenuItemAction icon={<RhUiNotificationFillIcon />} actionId="alert" aria-label="Alert" />} |
| 50 |
description="This is a description" |
| 51 |
itemId={1} |
| 52 |
> |
| 53 |
Item 2 |
| 54 |
</MenuItem> |
| 55 |
<MenuItem |
| 56 |
isSelected={selectedItems.indexOf(2) !== -1} |
| 57 |
actions={<MenuItemAction icon={<RhUiClipboardFillIcon />} actionId="copy" aria-label="Copy" />} |
| 58 |
itemId={2} |
| 59 |
> |
| 60 |
Item 3 |
| 61 |
</MenuItem> |
| 62 |
<MenuItem |
| 63 |
isSelected={selectedItems.indexOf(3) !== -1} |
| 64 |
actions={<MenuItemAction icon={<RhUiMenuBarsIcon />} actionId="expand" aria-label="Expand" />} |
| 65 |
description="This is a description" |
| 66 |
itemId={3} |
| 67 |
> |
| 68 |
Item 4 |
| 69 |
</MenuItem> |
| 70 |
</MenuList> |
| 71 |
</MenuGroup> |
| 72 |
</MenuContent> |
| 73 |
</Menu> |
| 74 |
); |
| 75 |
}; |