|
| 1 | +import { |
| 2 | + Dropdown, |
| 3 | + MenuToggle, |
| 4 | + MenuToggleCheckbox, |
| 5 | + DropdownItem, |
| 6 | + DropdownList, |
| 7 | + Divider, |
| 8 | + MenuToggleElement |
| 9 | +} from '@patternfly/react-core'; |
| 10 | +import { useRef, useState } from 'react'; |
| 11 | + |
| 12 | +export const DropdownSplitButtonText: React.FunctionComponent = () => { |
| 13 | + const [isOpen, setIsOpen] = useState(false); |
| 14 | + const toggleRef = useRef<MenuToggleElement>(null); |
| 15 | + |
| 16 | + const onFocus = () => { |
| 17 | + if (!toggleRef.current) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const toggleButton = toggleRef.current.querySelector('button[aria-expanded]'); |
| 22 | + toggleButton?.focus(); |
| 23 | + }; |
| 24 | + |
| 25 | + const onSelect = () => { |
| 26 | + setIsOpen(false); |
| 27 | + onFocus(); |
| 28 | + }; |
| 29 | + |
| 30 | + const onToggleClick = () => { |
| 31 | + setIsOpen(!isOpen); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <Dropdown |
| 36 | + onSelect={onSelect} |
| 37 | + onOpenChange={(isOpen: boolean) => setIsOpen(isOpen)} |
| 38 | + toggle={(toggleRefCallback: React.Ref<MenuToggleElement>) => ( |
| 39 | + <MenuToggle |
| 40 | + ref={(node) => { |
| 41 | + // Handle both callback ref and useRef |
| 42 | + if (typeof toggleRefCallback === 'function') { |
| 43 | + toggleRefCallback(node); |
| 44 | + } else if (toggleRefCallback) { |
| 45 | + (toggleRefCallback as React.MutableRefObject<MenuToggleElement | null>).current = node; |
| 46 | + } |
| 47 | + (toggleRef as React.MutableRefObject<MenuToggleElement | null>).current = node; |
| 48 | + }} |
| 49 | + splitButtonItems={[ |
| 50 | + <MenuToggleCheckbox id="split-button-checkbox-example" key="split-checkbox" aria-label="Select all" /> |
| 51 | + ]} |
| 52 | + aria-label="Dropdown with checkbox split button" |
| 53 | + onClick={onToggleClick} |
| 54 | + isExpanded={isOpen} |
| 55 | + /> |
| 56 | + )} |
| 57 | + isOpen={isOpen} |
| 58 | + > |
| 59 | + <DropdownList> |
| 60 | + <DropdownItem value={0} key="action"> |
| 61 | + Action |
| 62 | + </DropdownItem> |
| 63 | + <DropdownItem |
| 64 | + value={1} |
| 65 | + key="link" |
| 66 | + to="#default-link2" |
| 67 | + // Prevent the default onClick functionality for example purposes |
| 68 | + onClick={(ev: any) => ev.preventDefault()} |
| 69 | + > |
| 70 | + Link |
| 71 | + </DropdownItem> |
| 72 | + <DropdownItem value={2} isDisabled key="disabled action"> |
| 73 | + Disabled Action |
| 74 | + </DropdownItem> |
| 75 | + <DropdownItem value={3} isDisabled key="disabled link" to="#default-link4"> |
| 76 | + Disabled Link |
| 77 | + </DropdownItem> |
| 78 | + <DropdownItem |
| 79 | + value={4} |
| 80 | + isAriaDisabled |
| 81 | + key="aria-disabled link" |
| 82 | + to="#default-link5" |
| 83 | + tooltipProps={{ content: 'aria-disabled link', position: 'top' }} |
| 84 | + > |
| 85 | + Aria-disabled Link |
| 86 | + </DropdownItem> |
| 87 | + <Divider component="li" key="separator" /> |
| 88 | + <DropdownItem value={5} key="separated action"> |
| 89 | + Separated Action |
| 90 | + </DropdownItem> |
| 91 | + <DropdownItem value={6} key="separated link" to="#default-link6" onClick={(ev) => ev.preventDefault()}> |
| 92 | + Separated Link |
| 93 | + </DropdownItem> |
| 94 | + </DropdownList> |
| 95 | + </Dropdown> |
| 96 | + ); |
| 97 | +}; |
0 commit comments