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