| 1 |
import { useRef, useState } from 'react'; |
| 2 |
import { MenuToggle, MenuItemAction, Select, SelectGroup, SelectList, SelectOption } 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 ActionsMenuDemo: React.FunctionComponent = () => { |
| 9 |
const [isOpen, setIsOpen] = useState<boolean>(false); |
| 10 |
const [selectedItems, setSelectedItems] = useState<number[]>([]); |
| 11 |
const menuRef = useRef<HTMLDivElement>(null); |
| 12 |
|
| 13 |
const onSelect = (event: React.MouseEvent | undefined, value: string | number | undefined) => { |
| 14 |
if (typeof value === 'string' || typeof value === 'undefined') { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
if (selectedItems.includes(value)) { |
| 19 |
setSelectedItems(selectedItems.filter((id) => id !== value)); |
| 20 |
} else { |
| 21 |
setSelectedItems([...selectedItems, value]); |
| 22 |
} |
| 23 |
}; |
| 24 |
|
| 25 |
const onToggleClick = () => { |
| 26 |
setIsOpen(!isOpen); |
| 27 |
}; |
| 28 |
|
| 29 |
return ( |
| 30 |
<Select |
| 31 |
isOpen={isOpen} |
| 32 |
ref={menuRef} |
| 33 |
toggle={(toggleRef) => ( |
| 34 |
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}> |
| 35 |
{isOpen ? 'Expanded' : 'Collapsed'} |
| 36 |
</MenuToggle> |
| 37 |
)} |
| 38 |
// eslint-disable-next-line no-console |
| 39 |
onActionClick={(event, value, actionId) => console.log(`clicked on ${value} - ${actionId}`)} |
| 40 |
onSelect={onSelect} |
| 41 |
onOpenChange={(isOpen) => setIsOpen(isOpen)} |
| 42 |
> |
| 43 |
<SelectGroup label="Actions"> |
| 44 |
<SelectList> |
| 45 |
<SelectOption |
| 46 |
isSelected={selectedItems.includes(0)} |
| 47 |
actions={ |
| 48 |
<MenuItemAction |
| 49 |
icon={<RhUiBranchFillIcon />} |
| 50 |
actionId="code" |
| 51 |
// eslint-disable-next-line no-console |
| 52 |
onClick={() => console.log('clicked on code icon')} |
| 53 |
aria-label="Code" |
| 54 |
/> |
| 55 |
} |
| 56 |
description="This is a description" |
| 57 |
value={0} |
| 58 |
> |
| 59 |
Item 1 |
| 60 |
</SelectOption> |
| 61 |
<SelectOption |
| 62 |
isDisabled |
| 63 |
isSelected={selectedItems.includes(1)} |
| 64 |
actions={<MenuItemAction icon={<RhUiNotificationFillIcon />} actionId="alert" aria-label="Alert" />} |
| 65 |
description="This is a description" |
| 66 |
value={1} |
| 67 |
> |
| 68 |
Item 2 |
| 69 |
</SelectOption> |
| 70 |
<SelectOption |
| 71 |
isSelected={selectedItems.includes(2)} |
| 72 |
actions={<MenuItemAction icon={<RhUiClipboardFillIcon />} actionId="copy" aria-label="Copy" />} |
| 73 |
value={2} |
| 74 |
> |
| 75 |
Item 3 |
| 76 |
</SelectOption> |
| 77 |
<SelectOption |
| 78 |
isSelected={selectedItems.includes(3)} |
| 79 |
actions={<MenuItemAction icon={<RhUiMenuBarsIcon />} actionId="expand" aria-label="Expand" />} |
| 80 |
description="This is a description" |
| 81 |
value={3} |
| 82 |
> |
| 83 |
Item 4 |
| 84 |
</SelectOption> |
| 85 |
</SelectList> |
| 86 |
</SelectGroup> |
| 87 |
</Select> |
| 88 |
); |
| 89 |
}; |