| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { Button, Dropdown, DropdownList, DropdownItem, MenuToggle, MenuToggleElement } from '@patternfly/react-core'; |
| 3 |
import { Modal as ModalDeprecated, ModalVariant as ModalVariantDeprecated } from '@patternfly/react-core/deprecated'; |
| 4 |
|
| 5 |
export const ModalWithDropdown: React.FunctionComponent = () => { |
| 6 |
const [isModalOpen, setIsModalOpen] = useState(false); |
| 7 |
const [isDropdownOpen, setIsDropdownOpen] = useState(false); |
| 8 |
|
| 9 |
const handleModalToggle = (_event: KeyboardEvent | React.MouseEvent) => { |
| 10 |
setIsModalOpen(!isModalOpen); |
| 11 |
setIsDropdownOpen(false); |
| 12 |
}; |
| 13 |
|
| 14 |
const handleDropdownToggle = () => { |
| 15 |
setIsDropdownOpen(!isDropdownOpen); |
| 16 |
}; |
| 17 |
|
| 18 |
const onSelect = () => { |
| 19 |
setIsDropdownOpen(!isDropdownOpen); |
| 20 |
onFocus(); |
| 21 |
}; |
| 22 |
|
| 23 |
const onFocus = () => { |
| 24 |
const element = document.getElementById('modal-dropdown-toggle'); |
| 25 |
(element as HTMLElement).focus(); |
| 26 |
}; |
| 27 |
|
| 28 |
const onEscapePress = (event: KeyboardEvent) => { |
| 29 |
if (isDropdownOpen) { |
| 30 |
setIsDropdownOpen(!isDropdownOpen); |
| 31 |
onFocus(); |
| 32 |
} else { |
| 33 |
handleModalToggle(event); |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
return ( |
| 38 |
<Fragment> |
| 39 |
<Button variant="primary" onClick={handleModalToggle}> |
| 40 |
Show dropdown modal |
| 41 |
</Button> |
| 42 |
<ModalDeprecated |
| 43 |
title="Dropdown modal" |
| 44 |
variant={ModalVariantDeprecated.small} |
| 45 |
isOpen={isModalOpen} |
| 46 |
onClose={handleModalToggle} |
| 47 |
actions={[ |
| 48 |
<Button key="confirm" variant="primary" onClick={handleModalToggle}> |
| 49 |
Confirm |
| 50 |
</Button>, |
| 51 |
<Button key="cancel" variant="link" onClick={handleModalToggle}> |
| 52 |
Cancel |
| 53 |
</Button> |
| 54 |
]} |
| 55 |
onEscapePress={onEscapePress} |
| 56 |
> |
| 57 |
<div> |
| 58 |
Set the dropdown <strong>menuAppendTo</strong> prop to <em>parent</em> in order to allow the dropdown menu |
| 59 |
break out of the modal container. You'll also want to handle closing of the modal yourself, by listening to |
| 60 |
the <strong>onEscapePress</strong> callback on the Modal component, so you can close the Dropdown first if |
| 61 |
it's open without closing the entire modal. |
| 62 |
</div> |
| 63 |
<br /> |
| 64 |
<div> |
| 65 |
<Dropdown |
| 66 |
isOpen={isDropdownOpen} |
| 67 |
onSelect={onSelect} |
| 68 |
onOpenChange={(isOpen: boolean) => setIsDropdownOpen(isOpen)} |
| 69 |
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 70 |
<MenuToggle ref={toggleRef} onClick={handleDropdownToggle} isExpanded={isDropdownOpen}> |
| 71 |
Dropdown |
| 72 |
</MenuToggle> |
| 73 |
)} |
| 74 |
> |
| 75 |
<DropdownList> |
| 76 |
<DropdownItem value={0} key="action"> |
| 77 |
Action |
| 78 |
</DropdownItem> |
| 79 |
<DropdownItem |
| 80 |
value={1} |
| 81 |
key="link" |
| 82 |
to="#default-link2" |
| 83 |
// Prevent the default onClick functionality for example purposes |
| 84 |
onClick={(ev: any) => ev.preventDefault()} |
| 85 |
> |
| 86 |
Link |
| 87 |
</DropdownItem> |
| 88 |
<DropdownItem value={2} isDisabled key="disabled action"> |
| 89 |
Disabled Action |
| 90 |
</DropdownItem> |
| 91 |
<DropdownItem value={3} isDisabled key="disabled link" to="#default-link4"> |
| 92 |
Disabled Link |
| 93 |
</DropdownItem> |
| 94 |
</DropdownList> |
| 95 |
</Dropdown> |
| 96 |
</div> |
| 97 |
</ModalDeprecated> |
| 98 |
</Fragment> |
| 99 |
); |
| 100 |
}; |