@patternfly/react-core 4.192.3 → 4.192.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/dist/esm/components/DatePicker/DatePicker.d.ts +2 -0
- package/dist/esm/components/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/esm/components/DatePicker/DatePicker.js +7 -3
- package/dist/esm/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/esm/components/Masthead/Masthead.d.ts.map +1 -1
- package/dist/esm/components/Masthead/Masthead.js +6 -2
- package/dist/esm/components/Masthead/Masthead.js.map +1 -1
- package/dist/esm/components/Menu/MenuContent.js +1 -1
- package/dist/esm/components/Menu/MenuContent.js.map +1 -1
- package/dist/esm/components/Nav/Nav.d.ts +4 -2
- package/dist/esm/components/Nav/Nav.d.ts.map +1 -1
- package/dist/esm/components/Nav/Nav.js +2 -1
- package/dist/esm/components/Nav/Nav.js.map +1 -1
- package/dist/esm/components/Nav/NavList.d.ts +2 -8
- package/dist/esm/components/Nav/NavList.d.ts.map +1 -1
- package/dist/esm/components/Nav/NavList.js +4 -7
- package/dist/esm/components/Nav/NavList.js.map +1 -1
- package/dist/esm/components/Page/Page.d.ts +14 -0
- package/dist/esm/components/Page/Page.d.ts.map +1 -1
- package/dist/esm/components/Page/Page.js +31 -16
- package/dist/esm/components/Page/Page.js.map +1 -1
- package/dist/esm/components/Page/PageHeaderToolsGroup.d.ts.map +1 -1
- package/dist/esm/components/Page/PageHeaderToolsGroup.js +3 -1
- package/dist/esm/components/Page/PageHeaderToolsGroup.js.map +1 -1
- package/dist/esm/components/Page/PageHeaderToolsItem.d.ts.map +1 -1
- package/dist/esm/components/Page/PageHeaderToolsItem.js +7 -1
- package/dist/esm/components/Page/PageHeaderToolsItem.js.map +1 -1
- package/dist/esm/components/Page/PageSidebar.d.ts +1 -0
- package/dist/esm/components/Page/PageSidebar.d.ts.map +1 -1
- package/dist/esm/components/Page/PageSidebar.js +3 -2
- package/dist/esm/components/Page/PageSidebar.js.map +1 -1
- package/dist/esm/components/Toolbar/Toolbar.d.ts.map +1 -1
- package/dist/esm/components/Toolbar/Toolbar.js +3 -2
- package/dist/esm/components/Toolbar/Toolbar.js.map +1 -1
- package/dist/esm/components/Toolbar/ToolbarContent.d.ts.map +1 -1
- package/dist/esm/components/Toolbar/ToolbarContent.js +3 -2
- package/dist/esm/components/Toolbar/ToolbarContent.js.map +1 -1
- package/dist/esm/components/Toolbar/ToolbarGroup.d.ts.map +1 -1
- package/dist/esm/components/Toolbar/ToolbarGroup.js +2 -1
- package/dist/esm/components/Toolbar/ToolbarGroup.js.map +1 -1
- package/dist/esm/components/Toolbar/ToolbarItem.d.ts.map +1 -1
- package/dist/esm/components/Toolbar/ToolbarItem.js +3 -2
- package/dist/esm/components/Toolbar/ToolbarItem.js.map +1 -1
- package/dist/esm/components/Toolbar/ToolbarToggleGroup.d.ts.map +1 -1
- package/dist/esm/components/Toolbar/ToolbarToggleGroup.js +8 -5
- package/dist/esm/components/Toolbar/ToolbarToggleGroup.js.map +1 -1
- package/dist/esm/helpers/util.d.ts +16 -8
- package/dist/esm/helpers/util.d.ts.map +1 -1
- package/dist/esm/helpers/util.js +53 -7
- package/dist/esm/helpers/util.js.map +1 -1
- package/dist/js/components/DatePicker/DatePicker.d.ts +2 -0
- package/dist/js/components/DatePicker/DatePicker.d.ts.map +1 -1
- package/dist/js/components/DatePicker/DatePicker.js +7 -3
- package/dist/js/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/js/components/Masthead/Masthead.d.ts.map +1 -1
- package/dist/js/components/Masthead/Masthead.js +6 -2
- package/dist/js/components/Masthead/Masthead.js.map +1 -1
- package/dist/js/components/Menu/MenuContent.js +1 -1
- package/dist/js/components/Menu/MenuContent.js.map +1 -1
- package/dist/js/components/Nav/Nav.d.ts +4 -2
- package/dist/js/components/Nav/Nav.d.ts.map +1 -1
- package/dist/js/components/Nav/Nav.js +3 -2
- package/dist/js/components/Nav/Nav.js.map +1 -1
- package/dist/js/components/Nav/NavList.d.ts +2 -8
- package/dist/js/components/Nav/NavList.d.ts.map +1 -1
- package/dist/js/components/Nav/NavList.js +4 -7
- package/dist/js/components/Nav/NavList.js.map +1 -1
- package/dist/js/components/Page/Page.d.ts +14 -0
- package/dist/js/components/Page/Page.d.ts.map +1 -1
- package/dist/js/components/Page/Page.js +34 -19
- package/dist/js/components/Page/Page.js.map +1 -1
- package/dist/js/components/Page/PageHeaderToolsGroup.d.ts.map +1 -1
- package/dist/js/components/Page/PageHeaderToolsGroup.js +3 -1
- package/dist/js/components/Page/PageHeaderToolsGroup.js.map +1 -1
- package/dist/js/components/Page/PageHeaderToolsItem.d.ts.map +1 -1
- package/dist/js/components/Page/PageHeaderToolsItem.js +6 -1
- package/dist/js/components/Page/PageHeaderToolsItem.js.map +1 -1
- package/dist/js/components/Page/PageSidebar.d.ts +1 -0
- package/dist/js/components/Page/PageSidebar.d.ts.map +1 -1
- package/dist/js/components/Page/PageSidebar.js +4 -3
- package/dist/js/components/Page/PageSidebar.js.map +1 -1
- package/dist/js/components/Toolbar/Toolbar.d.ts.map +1 -1
- package/dist/js/components/Toolbar/Toolbar.js +3 -2
- package/dist/js/components/Toolbar/Toolbar.js.map +1 -1
- package/dist/js/components/Toolbar/ToolbarContent.d.ts.map +1 -1
- package/dist/js/components/Toolbar/ToolbarContent.js +3 -2
- package/dist/js/components/Toolbar/ToolbarContent.js.map +1 -1
- package/dist/js/components/Toolbar/ToolbarGroup.d.ts.map +1 -1
- package/dist/js/components/Toolbar/ToolbarGroup.js +2 -1
- package/dist/js/components/Toolbar/ToolbarGroup.js.map +1 -1
- package/dist/js/components/Toolbar/ToolbarItem.d.ts.map +1 -1
- package/dist/js/components/Toolbar/ToolbarItem.js +3 -2
- package/dist/js/components/Toolbar/ToolbarItem.js.map +1 -1
- package/dist/js/components/Toolbar/ToolbarToggleGroup.d.ts.map +1 -1
- package/dist/js/components/Toolbar/ToolbarToggleGroup.js +7 -4
- package/dist/js/components/Toolbar/ToolbarToggleGroup.js.map +1 -1
- package/dist/js/helpers/util.d.ts +16 -8
- package/dist/js/helpers/util.d.ts.map +1 -1
- package/dist/js/helpers/util.js +55 -8
- package/dist/js/helpers/util.js.map +1 -1
- package/dist/umd/react-core.js +428 -351
- package/dist/umd/react-core.min.js +3 -3
- package/package.json +5 -5
- package/src/components/BackToTop/examples/BackToTop.md +0 -1
- package/src/components/DatePicker/DatePicker.tsx +10 -2
- package/src/components/DatePicker/examples/DatePicker.md +4 -1
- package/src/components/JumpLinks/examples/JumpLinks.md +0 -1
- package/src/components/Masthead/Masthead.tsx +23 -16
- package/src/components/Masthead/__tests__/__snapshots__/Masthead.test.tsx.snap +11 -11
- package/src/components/Menu/MenuContent.tsx +2 -2
- package/src/components/Menu/examples/Menu.md +7 -7
- package/src/components/Nav/Nav.tsx +4 -2
- package/src/components/Nav/NavList.tsx +4 -7
- package/src/components/Page/Page.tsx +49 -16
- package/src/components/Page/PageHeaderToolsGroup.tsx +16 -5
- package/src/components/Page/PageHeaderToolsItem.tsx +20 -14
- package/src/components/Page/PageSidebar.tsx +3 -3
- package/src/components/Page/__tests__/Generated/__snapshots__/Page.test.tsx.snap +2 -0
- package/src/components/Page/__tests__/__snapshots__/Page.test.tsx.snap +18 -9
- package/src/components/Toolbar/Toolbar.tsx +44 -39
- package/src/components/Toolbar/ToolbarContent.tsx +45 -40
- package/src/components/Toolbar/ToolbarGroup.tsx +19 -14
- package/src/components/Toolbar/ToolbarItem.tsx +30 -19
- package/src/components/Toolbar/ToolbarToggleGroup.tsx +60 -53
- package/src/components/Toolbar/__tests__/Generated/__snapshots__/Toolbar.test.tsx.snap +3 -43
- package/src/components/Toolbar/__tests__/Generated/__snapshots__/ToolbarContent.test.tsx.snap +3 -7
- package/src/components/Toolbar/__tests__/Generated/__snapshots__/ToolbarItem.test.tsx.snap +3 -8
- package/src/demos/ComposableMenu/ComposableMenu.md +88 -0
- package/src/demos/ComposableMenu/examples/ComposableActionsMenu.tsx +122 -0
- package/src/demos/ComposableMenu/examples/ComposableApplicationLauncher.tsx +282 -0
- package/src/demos/ComposableMenu/examples/ComposableContextSelector.tsx +202 -0
- package/src/demos/ComposableMenu/examples/ComposableDrilldownMenu.tsx +246 -0
- package/src/demos/ComposableMenu/examples/ComposableDropdwnVariants.tsx +235 -0
- package/src/demos/ComposableMenu/examples/ComposableFlyout.tsx +103 -0
- package/src/demos/ComposableMenu/examples/ComposableOptionsMenuVariants.tsx +94 -0
- package/src/demos/ComposableMenu/examples/ComposableSimpleDropdown.tsx +70 -0
- package/src/demos/ComposableMenu/examples/ComposableSimpleSelect.tsx +74 -0
- package/src/demos/ComposableMenu/examples/ComposableTreeViewMenu.tsx +240 -0
- package/src/demos/ComposableMenu/examples/avatarImg.svg +18 -0
- package/src/demos/ComposableMenu/examples/pf-logo-small.svg +23 -0
- package/src/demos/JumpLinks.md +0 -1
- package/src/demos/Page/Page.md +92 -43
- package/src/helpers/util.ts +61 -12
- package/src/demos/ComposableMenu.md +0 -1443
- package/src/demos/composable-menu.css +0 -3
|
@@ -1,1443 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
id: Composable menu
|
|
3
|
-
section: demos
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
import { Link } from '@reach/router';
|
|
7
|
-
|
|
8
|
-
import './composable-menu.css';
|
|
9
|
-
import CogIcon from '@patternfly/react-icons/dist/esm/icons/cog-icon';
|
|
10
|
-
import EllipsisVIcon from '@patternfly/react-icons/dist/esm/icons/ellipsis-v-icon';
|
|
11
|
-
import TableIcon from '@patternfly/react-icons/dist/esm/icons/table-icon';
|
|
12
|
-
import StorageDomainIcon from '@patternfly/react-icons/dist/esm/icons/storage-domain-icon';
|
|
13
|
-
import CodeBranchIcon from '@patternfly/react-icons/dist/esm/icons/code-branch-icon';
|
|
14
|
-
import LayerGroupIcon from '@patternfly/react-icons/dist/esm/icons/layer-group-icon';
|
|
15
|
-
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
|
|
16
|
-
import BarsIcon from '@patternfly/react-icons/dist/esm/icons/bars-icon';
|
|
17
|
-
import ClipboardIcon from '@patternfly/react-icons/dist/esm/icons/clipboard-icon';
|
|
18
|
-
import BellIcon from '@patternfly/react-icons/dist/esm/icons/bell-icon';
|
|
19
|
-
import ThIcon from '@patternfly/react-icons/dist/esm/icons/th-icon';
|
|
20
|
-
import pfIcon from './examples/pf-logo-small.svg';
|
|
21
|
-
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
|
|
22
|
-
|
|
23
|
-
## Demos
|
|
24
|
-
|
|
25
|
-
Composable menus currently require consumer keyboard handling and use of our undocumented [popper.js](https://popper.js.org/) wrapper component called Popper. We understand this is inconvientent boilerplate and these examples will be updated to use [Dropdown](/components/dropdown) in a future release.
|
|
26
|
-
|
|
27
|
-
### Basic menu with toggle
|
|
28
|
-
|
|
29
|
-
```js
|
|
30
|
-
import React from 'react';
|
|
31
|
-
import { MenuToggle, Menu, MenuContent, MenuList, MenuItem, Popper } from '@patternfly/react-core';
|
|
32
|
-
|
|
33
|
-
BasicComposableMenu = () => {
|
|
34
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
35
|
-
const toggleRef = React.useRef();
|
|
36
|
-
const menuRef = React.useRef();
|
|
37
|
-
|
|
38
|
-
const handleMenuKeys = event => {
|
|
39
|
-
if (!isOpen) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (menuRef.current.contains(event.target) || toggleRef.current.contains(event.target)) {
|
|
43
|
-
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
44
|
-
setIsOpen(!isOpen);
|
|
45
|
-
toggleRef.current.focus();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const handleClickOutside = event => {
|
|
51
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
52
|
-
setIsOpen(false);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
React.useEffect(() => {
|
|
57
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
58
|
-
window.addEventListener('click', handleClickOutside);
|
|
59
|
-
return () => {
|
|
60
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
61
|
-
window.removeEventListener('click', handleClickOutside);
|
|
62
|
-
};
|
|
63
|
-
}, [isOpen, menuRef]);
|
|
64
|
-
|
|
65
|
-
const onToggleClick = ev => {
|
|
66
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
67
|
-
setTimeout(() => {
|
|
68
|
-
if (menuRef.current) {
|
|
69
|
-
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
70
|
-
firstElement && firstElement.focus();
|
|
71
|
-
}
|
|
72
|
-
}, 0);
|
|
73
|
-
setIsOpen(!isOpen);
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const toggle = (
|
|
77
|
-
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
|
|
78
|
-
{isOpen ? 'Expanded' : 'Collapsed'}
|
|
79
|
-
</MenuToggle>
|
|
80
|
-
);
|
|
81
|
-
const menu = (
|
|
82
|
-
<Menu ref={menuRef} onSelect={(_ev, itemId) => console.log('selected', itemId)}>
|
|
83
|
-
<MenuContent>
|
|
84
|
-
<MenuList>
|
|
85
|
-
<MenuItem itemId={0}>Action</MenuItem>
|
|
86
|
-
<MenuItem itemId={1} to="#default-link2" onClick={ev => ev.preventDefault()}>
|
|
87
|
-
Link
|
|
88
|
-
</MenuItem>
|
|
89
|
-
<MenuItem isDisabled>Disabled Action</MenuItem>
|
|
90
|
-
<MenuItem isDisabled to="#default-link4">
|
|
91
|
-
Disabled Link
|
|
92
|
-
</MenuItem>
|
|
93
|
-
</MenuList>
|
|
94
|
-
</MenuContent>
|
|
95
|
-
</Menu>
|
|
96
|
-
);
|
|
97
|
-
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
|
|
98
|
-
};
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### Menu with actions
|
|
102
|
-
|
|
103
|
-
```js
|
|
104
|
-
import React from 'react';
|
|
105
|
-
import {
|
|
106
|
-
MenuToggle,
|
|
107
|
-
Menu,
|
|
108
|
-
MenuList,
|
|
109
|
-
MenuItem,
|
|
110
|
-
MenuGroup,
|
|
111
|
-
MenuItemAction,
|
|
112
|
-
Popper
|
|
113
|
-
} from '@patternfly/react-core';
|
|
114
|
-
import BarsIcon from '@patternfly/react-icons/dist/esm/icons/bars-icon';
|
|
115
|
-
import ClipboardIcon from '@patternfly/react-icons/dist/esm/icons/clipboard-icon';
|
|
116
|
-
import CodeBranchIcon from '@patternfly/react-icons/dist/esm/icons/code-branch-icon';
|
|
117
|
-
import BellIcon from '@patternfly/react-icons/dist/esm/icons/bell-icon';
|
|
118
|
-
|
|
119
|
-
ActionComposableMenu = () => {
|
|
120
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
121
|
-
const [selectedItems, setSelectedItems] = React.useState([]);
|
|
122
|
-
const toggleRef = React.useRef();
|
|
123
|
-
const menuRef = React.useRef();
|
|
124
|
-
|
|
125
|
-
const handleMenuKeys = event => {
|
|
126
|
-
if (isOpen && menuRef.current.contains(event.target)) {
|
|
127
|
-
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
128
|
-
setIsOpen(!isOpen);
|
|
129
|
-
toggleRef.current.focus();
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const handleClickOutside = event => {
|
|
135
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
136
|
-
setIsOpen(false);
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
React.useEffect(() => {
|
|
141
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
142
|
-
window.addEventListener('click', handleClickOutside);
|
|
143
|
-
return () => {
|
|
144
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
145
|
-
window.removeEventListener('click', handleClickOutside);
|
|
146
|
-
};
|
|
147
|
-
}, [isOpen, menuRef]);
|
|
148
|
-
|
|
149
|
-
const onSelect = (event, itemId) => {
|
|
150
|
-
if (selectedItems.includes(itemId)) {
|
|
151
|
-
setSelectedItems(selectedItems.filter(id => id !== itemId));
|
|
152
|
-
} else {
|
|
153
|
-
setSelectedItems([...selectedItems, itemId]);
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const onToggleClick = ev => {
|
|
158
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
159
|
-
setTimeout(() => {
|
|
160
|
-
if (menuRef.current) {
|
|
161
|
-
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
162
|
-
firstElement && firstElement.focus();
|
|
163
|
-
}
|
|
164
|
-
}, 0);
|
|
165
|
-
setIsOpen(!isOpen);
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
const toggle = (
|
|
169
|
-
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
|
|
170
|
-
{isOpen ? 'Expanded' : 'Collapsed'}
|
|
171
|
-
</MenuToggle>
|
|
172
|
-
);
|
|
173
|
-
const menu = (
|
|
174
|
-
<Menu
|
|
175
|
-
ref={menuRef}
|
|
176
|
-
onActionClick={(event, itemId, actionId) => console.log(`clicked on ${itemId} - ${actionId}`)}
|
|
177
|
-
onSelect={onSelect}
|
|
178
|
-
>
|
|
179
|
-
<MenuGroup label="Actions">
|
|
180
|
-
<MenuList>
|
|
181
|
-
<MenuItem
|
|
182
|
-
isSelected={selectedItems.includes(0)}
|
|
183
|
-
actions={
|
|
184
|
-
<MenuItemAction
|
|
185
|
-
icon={<CodeBranchIcon aria-hidden />}
|
|
186
|
-
actionId="code"
|
|
187
|
-
onClick={() => console.log('clicked on code icon')}
|
|
188
|
-
aria-label="Code"
|
|
189
|
-
/>
|
|
190
|
-
}
|
|
191
|
-
description="This is a description"
|
|
192
|
-
itemId={0}
|
|
193
|
-
>
|
|
194
|
-
Item 1
|
|
195
|
-
</MenuItem>
|
|
196
|
-
<MenuItem
|
|
197
|
-
isDisabled
|
|
198
|
-
isSelected={selectedItems.includes(1)}
|
|
199
|
-
actions={<MenuItemAction icon={<BellIcon aria-hidden />} actionId="alert" aria-label="Alert" />}
|
|
200
|
-
description="This is a description"
|
|
201
|
-
itemId={1}
|
|
202
|
-
>
|
|
203
|
-
Item 2
|
|
204
|
-
</MenuItem>
|
|
205
|
-
<MenuItem
|
|
206
|
-
isSelected={selectedItems.includes(2)}
|
|
207
|
-
actions={<MenuItemAction icon={<ClipboardIcon aria-hidden />} actionId="copy" aria-label="Copy" />}
|
|
208
|
-
itemId={2}
|
|
209
|
-
>
|
|
210
|
-
Item 3
|
|
211
|
-
</MenuItem>
|
|
212
|
-
<MenuItem
|
|
213
|
-
isSelected={selectedItems.includes(3)}
|
|
214
|
-
actions={<MenuItemAction icon={<BarsIcon aria-hidden />} actionId="expand" aria-label="Expand" />}
|
|
215
|
-
description="This is a description"
|
|
216
|
-
itemId={3}
|
|
217
|
-
>
|
|
218
|
-
Item 4
|
|
219
|
-
</MenuItem>
|
|
220
|
-
</MenuList>
|
|
221
|
-
</MenuGroup>
|
|
222
|
-
</Menu>
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
|
|
226
|
-
};
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
### Select menu
|
|
230
|
-
|
|
231
|
-
```js
|
|
232
|
-
import React from 'react';
|
|
233
|
-
import { MenuToggle, Menu, MenuContent, MenuList, MenuItem, Popper } from '@patternfly/react-core';
|
|
234
|
-
import TableIcon from '@patternfly/react-icons/dist/esm/icons/table-icon';
|
|
235
|
-
|
|
236
|
-
SelectComposableMenu = () => {
|
|
237
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
238
|
-
const [selected, setSelected] = React.useState('Select a value');
|
|
239
|
-
const toggleRef = React.useRef();
|
|
240
|
-
const menuRef = React.useRef();
|
|
241
|
-
|
|
242
|
-
const handleMenuKeys = event => {
|
|
243
|
-
if (isOpen && menuRef.current.contains(event.target)) {
|
|
244
|
-
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
245
|
-
setIsOpen(!isOpen);
|
|
246
|
-
toggleRef.current.focus();
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
const handleClickOutside = event => {
|
|
252
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
253
|
-
setIsOpen(false);
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
React.useEffect(() => {
|
|
258
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
259
|
-
window.addEventListener('click', handleClickOutside);
|
|
260
|
-
return () => {
|
|
261
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
262
|
-
window.removeEventListener('click', handleClickOutside);
|
|
263
|
-
};
|
|
264
|
-
}, [isOpen, menuRef]);
|
|
265
|
-
|
|
266
|
-
const onToggleClick = ev => {
|
|
267
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
268
|
-
setTimeout(() => {
|
|
269
|
-
if (menuRef.current) {
|
|
270
|
-
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
271
|
-
firstElement && firstElement.focus();
|
|
272
|
-
}
|
|
273
|
-
}, 0);
|
|
274
|
-
setIsOpen(!isOpen);
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
const toggle = (
|
|
278
|
-
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
|
|
279
|
-
{selected}
|
|
280
|
-
</MenuToggle>
|
|
281
|
-
);
|
|
282
|
-
const menu = (
|
|
283
|
-
<Menu ref={menuRef} id="select-menu" onSelect={(_ev, itemId) => setSelected(itemId)} selected={selected}>
|
|
284
|
-
<MenuContent>
|
|
285
|
-
<MenuList>
|
|
286
|
-
<MenuItem itemId="Option 1">Option 1</MenuItem>
|
|
287
|
-
<MenuItem itemId="Option 2">Option 2</MenuItem>
|
|
288
|
-
<MenuItem itemId="Option 3" icon={<TableIcon aria-hidden />}>
|
|
289
|
-
Option 3
|
|
290
|
-
</MenuItem>
|
|
291
|
-
</MenuList>
|
|
292
|
-
</MenuContent>
|
|
293
|
-
</Menu>
|
|
294
|
-
);
|
|
295
|
-
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
|
|
296
|
-
};
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
### Drilldown menu with toggle
|
|
300
|
-
|
|
301
|
-
```js isBeta
|
|
302
|
-
import React from 'react';
|
|
303
|
-
import {
|
|
304
|
-
MenuToggle,
|
|
305
|
-
Menu,
|
|
306
|
-
MenuContent,
|
|
307
|
-
MenuList,
|
|
308
|
-
MenuItem,
|
|
309
|
-
DrilldownMenu,
|
|
310
|
-
Divider,
|
|
311
|
-
Popper
|
|
312
|
-
} from '@patternfly/react-core';
|
|
313
|
-
import StorageDomainIcon from '@patternfly/react-icons/dist/esm/icons/storage-domain-icon';
|
|
314
|
-
import CodeBranchIcon from '@patternfly/react-icons/dist/esm/icons/code-branch-icon';
|
|
315
|
-
import LayerGroupIcon from '@patternfly/react-icons/dist/esm/icons/layer-group-icon';
|
|
316
|
-
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
|
|
317
|
-
|
|
318
|
-
DrilldownComposableMenu = () => {
|
|
319
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
320
|
-
const [activeMenu, setActiveMenu] = React.useState('rootMenu');
|
|
321
|
-
const [menuDrilledIn, setMenuDrilledIn] = React.useState([]);
|
|
322
|
-
const [drilldownPath, setDrilldownPath] = React.useState([]);
|
|
323
|
-
const [menuHeights, setMenuHeights] = React.useState({});
|
|
324
|
-
const toggleRef = React.useRef();
|
|
325
|
-
const menuRef = React.useRef();
|
|
326
|
-
|
|
327
|
-
const handleMenuKeys = event => {
|
|
328
|
-
if (isOpen && menuRef.current.contains(event.target)) {
|
|
329
|
-
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
330
|
-
setIsOpen(!isOpen);
|
|
331
|
-
toggleRef.current.focus();
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
const handleClickOutside = event => {
|
|
337
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
338
|
-
setIsOpen(false);
|
|
339
|
-
}
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
React.useEffect(() => {
|
|
343
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
344
|
-
window.addEventListener('click', handleClickOutside);
|
|
345
|
-
return () => {
|
|
346
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
347
|
-
window.removeEventListener('click', handleClickOutside);
|
|
348
|
-
};
|
|
349
|
-
}, [isOpen, menuRef]);
|
|
350
|
-
|
|
351
|
-
const onToggleClick = ev => {
|
|
352
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
353
|
-
setTimeout(() => {
|
|
354
|
-
if (menuRef.current) {
|
|
355
|
-
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
356
|
-
firstElement && firstElement.focus();
|
|
357
|
-
}
|
|
358
|
-
}, 0);
|
|
359
|
-
setIsOpen(!isOpen);
|
|
360
|
-
setMenuDrilledIn([]);
|
|
361
|
-
setDrilldownPath([]);
|
|
362
|
-
setActiveMenu('rootMenu');
|
|
363
|
-
};
|
|
364
|
-
|
|
365
|
-
const drillIn = (fromMenuId, toMenuId, pathId) => {
|
|
366
|
-
setMenuDrilledIn([...menuDrilledIn, fromMenuId]);
|
|
367
|
-
setDrilldownPath([...drilldownPath, pathId]);
|
|
368
|
-
setActiveMenu(toMenuId);
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
const drillOut = toMenuId => {
|
|
372
|
-
setMenuDrilledIn(menuDrilledIn.slice(0, menuDrilledIn.length - 1));
|
|
373
|
-
setDrilldownPath(drilldownPath.slice(0, drilldownPath.length - 1));
|
|
374
|
-
setActiveMenu(toMenuId);
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
const setHeight = (menuId, height) => {
|
|
378
|
-
if (!menuHeights[menuId]) {
|
|
379
|
-
setMenuHeights({
|
|
380
|
-
...menuHeights,
|
|
381
|
-
[menuId]: height
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
const toggle = (
|
|
387
|
-
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
|
|
388
|
-
{isOpen ? 'Expanded' : 'Collapsed'}
|
|
389
|
-
</MenuToggle>
|
|
390
|
-
);
|
|
391
|
-
const menu = (
|
|
392
|
-
<Menu
|
|
393
|
-
id="rootMenu"
|
|
394
|
-
containsDrilldown
|
|
395
|
-
drilldownItemPath={drilldownPath}
|
|
396
|
-
drilledInMenus={menuDrilledIn}
|
|
397
|
-
activeMenu={activeMenu}
|
|
398
|
-
onDrillIn={drillIn}
|
|
399
|
-
onDrillOut={drillOut}
|
|
400
|
-
onGetMenuHeight={setHeight}
|
|
401
|
-
ref={menuRef}
|
|
402
|
-
>
|
|
403
|
-
<MenuContent menuHeight={`${menuHeights[activeMenu]}px`}>
|
|
404
|
-
<MenuList>
|
|
405
|
-
<MenuItem
|
|
406
|
-
itemId="group:start_rollout"
|
|
407
|
-
direction="down"
|
|
408
|
-
drilldownMenu={
|
|
409
|
-
<DrilldownMenu id="drilldownMenuStart">
|
|
410
|
-
<MenuItem itemId="group:start_rollout" direction="up">
|
|
411
|
-
Start rollout
|
|
412
|
-
</MenuItem>
|
|
413
|
-
<Divider component="li" />
|
|
414
|
-
<MenuItem
|
|
415
|
-
itemId="group:app_grouping"
|
|
416
|
-
description="Groups A-C"
|
|
417
|
-
direction="down"
|
|
418
|
-
drilldownMenu={
|
|
419
|
-
<DrilldownMenu id="drilldownMenuStartGrouping">
|
|
420
|
-
<MenuItem itemId="group:app_grouping" direction="up">
|
|
421
|
-
Application Grouping
|
|
422
|
-
</MenuItem>
|
|
423
|
-
<Divider component="li" />
|
|
424
|
-
<MenuItem itemId="group_a">Group A</MenuItem>
|
|
425
|
-
<MenuItem itemId="group_b">Group B</MenuItem>
|
|
426
|
-
<MenuItem itemId="group_c">Group C</MenuItem>
|
|
427
|
-
</DrilldownMenu>
|
|
428
|
-
}
|
|
429
|
-
>
|
|
430
|
-
Application Grouping
|
|
431
|
-
</MenuItem>
|
|
432
|
-
<MenuItem itemId="count">Count</MenuItem>
|
|
433
|
-
<MenuItem
|
|
434
|
-
itemId="group:labels"
|
|
435
|
-
direction="down"
|
|
436
|
-
drilldownMenu={
|
|
437
|
-
<DrilldownMenu id="drilldownMenuStartLabels">
|
|
438
|
-
<MenuItem itemId="group:labels" direction="up">
|
|
439
|
-
Labels
|
|
440
|
-
</MenuItem>
|
|
441
|
-
<Divider component="li" />
|
|
442
|
-
<MenuItem itemId="label_1">Label 1</MenuItem>
|
|
443
|
-
<MenuItem itemId="label_2">Label 2</MenuItem>
|
|
444
|
-
<MenuItem itemId="label_3">Label 3</MenuItem>
|
|
445
|
-
</DrilldownMenu>
|
|
446
|
-
}
|
|
447
|
-
>
|
|
448
|
-
Labels
|
|
449
|
-
</MenuItem>
|
|
450
|
-
<MenuItem itemId="annotations">Annotations</MenuItem>
|
|
451
|
-
</DrilldownMenu>
|
|
452
|
-
}
|
|
453
|
-
>
|
|
454
|
-
Start rollout
|
|
455
|
-
</MenuItem>
|
|
456
|
-
<MenuItem
|
|
457
|
-
itemId="group:pause_rollout"
|
|
458
|
-
direction="down"
|
|
459
|
-
drilldownMenu={
|
|
460
|
-
<DrilldownMenu id="drilldownMenuPause">
|
|
461
|
-
<MenuItem itemId="group:pause_rollout" direction="up">
|
|
462
|
-
Pause rollouts
|
|
463
|
-
</MenuItem>
|
|
464
|
-
<Divider component="li" />
|
|
465
|
-
<MenuItem
|
|
466
|
-
itemId="group:app_grouping"
|
|
467
|
-
description="Groups A-C"
|
|
468
|
-
direction="down"
|
|
469
|
-
drilldownMenu={
|
|
470
|
-
<DrilldownMenu id="drilldownMenuGrouping">
|
|
471
|
-
<MenuItem itemId="group:app_grouping" direction="up">
|
|
472
|
-
Application Grouping
|
|
473
|
-
</MenuItem>
|
|
474
|
-
<Divider component="li" />
|
|
475
|
-
<MenuItem itemId="group_a">Group A</MenuItem>
|
|
476
|
-
<MenuItem itemId="group_b">Group B</MenuItem>
|
|
477
|
-
<MenuItem itemId="group_c">Group C</MenuItem>
|
|
478
|
-
</DrilldownMenu>
|
|
479
|
-
}
|
|
480
|
-
>
|
|
481
|
-
Application Grouping
|
|
482
|
-
</MenuItem>
|
|
483
|
-
<MenuItem itemId="count">Count</MenuItem>
|
|
484
|
-
<MenuItem
|
|
485
|
-
itemId="group:labels"
|
|
486
|
-
direction="down"
|
|
487
|
-
drilldownMenu={
|
|
488
|
-
<DrilldownMenu id="drilldownMenuLabels">
|
|
489
|
-
<MenuItem itemId="group:labels" direction="up">
|
|
490
|
-
Labels
|
|
491
|
-
</MenuItem>
|
|
492
|
-
<Divider component="li" />
|
|
493
|
-
<MenuItem itemId="label_1">Label 1</MenuItem>
|
|
494
|
-
<MenuItem itemId="label_2">Label 2</MenuItem>
|
|
495
|
-
<MenuItem itemId="label_3">Label 3</MenuItem>
|
|
496
|
-
</DrilldownMenu>
|
|
497
|
-
}
|
|
498
|
-
>
|
|
499
|
-
Labels
|
|
500
|
-
</MenuItem>
|
|
501
|
-
<MenuItem itemId="annotations">Annotations</MenuItem>
|
|
502
|
-
</DrilldownMenu>
|
|
503
|
-
}
|
|
504
|
-
>
|
|
505
|
-
Pause rollouts
|
|
506
|
-
</MenuItem>
|
|
507
|
-
<MenuItem
|
|
508
|
-
itemId="group:storage"
|
|
509
|
-
icon={<StorageDomainIcon aria-hidden />}
|
|
510
|
-
direction="down"
|
|
511
|
-
drilldownMenu={
|
|
512
|
-
<DrilldownMenu id="drilldownMenuStorage">
|
|
513
|
-
<MenuItem itemId="group:storage" icon={<StorageDomainIcon aria-hidden />} direction="up">
|
|
514
|
-
Add storage
|
|
515
|
-
</MenuItem>
|
|
516
|
-
<Divider component="li" />
|
|
517
|
-
<MenuItem icon={<CodeBranchIcon aria-hidden />} itemId="git">
|
|
518
|
-
From Git
|
|
519
|
-
</MenuItem>
|
|
520
|
-
<MenuItem icon={<LayerGroupIcon aria-hidden />} itemId="container">
|
|
521
|
-
Container Image
|
|
522
|
-
</MenuItem>
|
|
523
|
-
<MenuItem icon={<CubeIcon aria-hidden />} itemId="docker">
|
|
524
|
-
Docker File
|
|
525
|
-
</MenuItem>
|
|
526
|
-
</DrilldownMenu>
|
|
527
|
-
}
|
|
528
|
-
>
|
|
529
|
-
Add storage
|
|
530
|
-
</MenuItem>
|
|
531
|
-
<MenuItem itemId="edit">Edit</MenuItem>
|
|
532
|
-
<MenuItem itemId="delete_deployment">Delete deployment config</MenuItem>
|
|
533
|
-
</MenuList>
|
|
534
|
-
</MenuContent>
|
|
535
|
-
</Menu>
|
|
536
|
-
);
|
|
537
|
-
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
|
|
538
|
-
};
|
|
539
|
-
```
|
|
540
|
-
|
|
541
|
-
### Filter tree menu
|
|
542
|
-
|
|
543
|
-
```js
|
|
544
|
-
import React from 'react';
|
|
545
|
-
import { MenuToggle, Menu, MenuContent, MenuGroup, MenuList, Popper, TreeView } from '@patternfly/react-core';
|
|
546
|
-
|
|
547
|
-
FilterTreeComposableMenu = () => {
|
|
548
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
549
|
-
const [checkedItems, setCheckedItems] = React.useState([]);
|
|
550
|
-
const toggleRef = React.useRef();
|
|
551
|
-
const menuRef = React.useRef();
|
|
552
|
-
|
|
553
|
-
const statusOptions = [
|
|
554
|
-
{
|
|
555
|
-
name: 'Ready',
|
|
556
|
-
id: 'ready',
|
|
557
|
-
checkProps: { checked: false },
|
|
558
|
-
customBadgeContent: 1,
|
|
559
|
-
children: [
|
|
560
|
-
{
|
|
561
|
-
name: 'Updated',
|
|
562
|
-
id: 'updated',
|
|
563
|
-
checkProps: { checked: false },
|
|
564
|
-
customBadgeContent: 0
|
|
565
|
-
},
|
|
566
|
-
{
|
|
567
|
-
name: 'Waiting to update',
|
|
568
|
-
id: 'waiting',
|
|
569
|
-
checkProps: { checked: false },
|
|
570
|
-
customBadgeContent: 0
|
|
571
|
-
},
|
|
572
|
-
{
|
|
573
|
-
name: 'Conditions degraded',
|
|
574
|
-
id: 'degraded',
|
|
575
|
-
checkProps: { checked: false },
|
|
576
|
-
customBadgeContent: 1
|
|
577
|
-
},
|
|
578
|
-
{
|
|
579
|
-
name: 'Approval required',
|
|
580
|
-
id: 'approval',
|
|
581
|
-
checkProps: { checked: false },
|
|
582
|
-
customBadgeContent: 0
|
|
583
|
-
}
|
|
584
|
-
]
|
|
585
|
-
},
|
|
586
|
-
{
|
|
587
|
-
name: 'Not ready',
|
|
588
|
-
id: 'nr',
|
|
589
|
-
checkProps: { checked: false },
|
|
590
|
-
customBadgeContent: 1,
|
|
591
|
-
children: [
|
|
592
|
-
{
|
|
593
|
-
name: 'Conditions degraded',
|
|
594
|
-
id: 'nr-degraded',
|
|
595
|
-
checkProps: { checked: false },
|
|
596
|
-
customBadgeContent: 1
|
|
597
|
-
}
|
|
598
|
-
]
|
|
599
|
-
},
|
|
600
|
-
{
|
|
601
|
-
name: 'Updating',
|
|
602
|
-
id: 'updating',
|
|
603
|
-
checkProps: { checked: false },
|
|
604
|
-
customBadgeContent: 0
|
|
605
|
-
}
|
|
606
|
-
];
|
|
607
|
-
|
|
608
|
-
const roleOptions = [
|
|
609
|
-
{
|
|
610
|
-
name: 'Server',
|
|
611
|
-
id: 'server',
|
|
612
|
-
checkProps: { checked: false },
|
|
613
|
-
customBadgeContent: 2
|
|
614
|
-
},
|
|
615
|
-
{
|
|
616
|
-
name: 'Worker',
|
|
617
|
-
id: 'worker',
|
|
618
|
-
checkProps: { checked: false },
|
|
619
|
-
customBadgeContent: 0
|
|
620
|
-
}
|
|
621
|
-
];
|
|
622
|
-
// Helper functions
|
|
623
|
-
const isChecked = dataItem => checkedItems.some(item => item.id === dataItem.id);
|
|
624
|
-
const areAllDescendantsChecked = dataItem =>
|
|
625
|
-
dataItem.children ? dataItem.children.every(child => areAllDescendantsChecked(child)) : isChecked(dataItem);
|
|
626
|
-
const areSomeDescendantsChecked = dataItem =>
|
|
627
|
-
dataItem.children ? dataItem.children.some(child => areSomeDescendantsChecked(child)) : isChecked(dataItem);
|
|
628
|
-
const flattenTree = tree => {
|
|
629
|
-
var result = [];
|
|
630
|
-
tree.forEach(item => {
|
|
631
|
-
result.push(item);
|
|
632
|
-
if (item.children) {
|
|
633
|
-
result = result.concat(flattenTree(item.children));
|
|
634
|
-
}
|
|
635
|
-
});
|
|
636
|
-
return result;
|
|
637
|
-
};
|
|
638
|
-
|
|
639
|
-
mapTree = item => {
|
|
640
|
-
const hasCheck = areAllDescendantsChecked(item);
|
|
641
|
-
// Reset checked properties to be updated
|
|
642
|
-
item.checkProps.checked = false;
|
|
643
|
-
|
|
644
|
-
if (hasCheck) {
|
|
645
|
-
item.checkProps.checked = true;
|
|
646
|
-
} else {
|
|
647
|
-
const hasPartialCheck = areSomeDescendantsChecked(item);
|
|
648
|
-
if (hasPartialCheck) {
|
|
649
|
-
item.checkProps.checked = null;
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
if (item.children) {
|
|
654
|
-
return {
|
|
655
|
-
...item,
|
|
656
|
-
children: item.children.map(mapTree)
|
|
657
|
-
};
|
|
658
|
-
}
|
|
659
|
-
return item;
|
|
660
|
-
};
|
|
661
|
-
|
|
662
|
-
filterItems = (item, checkedItem) => {
|
|
663
|
-
if (item.id === checkedItem.id) {
|
|
664
|
-
return true;
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
if (item.children) {
|
|
668
|
-
return (
|
|
669
|
-
(item.children = item.children
|
|
670
|
-
.map(opt => Object.assign({}, opt))
|
|
671
|
-
.filter(child => filterItems(child, checkedItem))).length > 0
|
|
672
|
-
);
|
|
673
|
-
}
|
|
674
|
-
};
|
|
675
|
-
|
|
676
|
-
const onCheck = (evt, treeViewItem, tree) => {
|
|
677
|
-
const checked = evt.target.checked;
|
|
678
|
-
|
|
679
|
-
let options = [];
|
|
680
|
-
switch (tree) {
|
|
681
|
-
case 'status':
|
|
682
|
-
options = statusOptions;
|
|
683
|
-
break;
|
|
684
|
-
case 'role':
|
|
685
|
-
options = roleOptions;
|
|
686
|
-
break;
|
|
687
|
-
default:
|
|
688
|
-
break;
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
const checkedItemTree = options.map(opt => Object.assign({}, opt)).filter(item => filterItems(item, treeViewItem));
|
|
692
|
-
const flatCheckedItems = flattenTree(checkedItemTree);
|
|
693
|
-
|
|
694
|
-
setCheckedItems(
|
|
695
|
-
prevState =>
|
|
696
|
-
checked
|
|
697
|
-
? prevState.checkedItems.concat(
|
|
698
|
-
flatCheckedItems.filter(item => !prevState.checkedItems.some(i => i.id === item.id))
|
|
699
|
-
)
|
|
700
|
-
: prevState.checkedItems.filter(item => !flatCheckedItems.some(i => i.id === item.id)),
|
|
701
|
-
() => {
|
|
702
|
-
console.log('Checked items: ', checkedItems);
|
|
703
|
-
}
|
|
704
|
-
);
|
|
705
|
-
};
|
|
706
|
-
|
|
707
|
-
const handleMenuKeys = event => {
|
|
708
|
-
if (!isOpen) {
|
|
709
|
-
return;
|
|
710
|
-
}
|
|
711
|
-
if (menuRef.current.contains(event.target) || toggleRef.current.contains(event.target)) {
|
|
712
|
-
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
713
|
-
setIsOpen(!isOpen);
|
|
714
|
-
toggleRef.current.focus();
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
};
|
|
718
|
-
|
|
719
|
-
const handleClickOutside = event => {
|
|
720
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
721
|
-
setIsOpen(false);
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
|
|
725
|
-
React.useEffect(() => {
|
|
726
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
727
|
-
window.addEventListener('click', handleClickOutside);
|
|
728
|
-
return () => {
|
|
729
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
730
|
-
window.removeEventListener('click', handleClickOutside);
|
|
731
|
-
};
|
|
732
|
-
}, [isOpen, menuRef]);
|
|
733
|
-
|
|
734
|
-
const onToggleClick = ev => {
|
|
735
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
736
|
-
setTimeout(() => {
|
|
737
|
-
if (menuRef.current) {
|
|
738
|
-
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
739
|
-
firstElement && firstElement.focus();
|
|
740
|
-
}
|
|
741
|
-
}, 0);
|
|
742
|
-
setIsOpen(!isOpen);
|
|
743
|
-
};
|
|
744
|
-
|
|
745
|
-
const toggle = (
|
|
746
|
-
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
|
|
747
|
-
{isOpen ? 'Expanded' : 'Collapsed'}
|
|
748
|
-
</MenuToggle>
|
|
749
|
-
);
|
|
750
|
-
const statusMapped = statusOptions.map(mapTree);
|
|
751
|
-
const roleMapped = roleOptions.map(mapTree);
|
|
752
|
-
const menu = (
|
|
753
|
-
<Menu ref={menuRef} onSelect={(_ev, itemId) => console.log('selected', itemId)}>
|
|
754
|
-
<MenuContent>
|
|
755
|
-
<MenuList>
|
|
756
|
-
<MenuGroup label="Status">
|
|
757
|
-
<TreeView
|
|
758
|
-
data={statusMapped}
|
|
759
|
-
hasBadges
|
|
760
|
-
hasChecks
|
|
761
|
-
onCheck={(event, item, parentItem) => onCheck(event, item, 'status')}
|
|
762
|
-
/>
|
|
763
|
-
</MenuGroup>
|
|
764
|
-
<MenuGroup label="Role">
|
|
765
|
-
<TreeView
|
|
766
|
-
data={roleMapped}
|
|
767
|
-
hasBadges
|
|
768
|
-
hasChecks
|
|
769
|
-
onCheck={(event, item, parentItem) => onCheck(event, item, 'role')}
|
|
770
|
-
/>
|
|
771
|
-
</MenuGroup>
|
|
772
|
-
</MenuList>
|
|
773
|
-
</MenuContent>
|
|
774
|
-
</Menu>
|
|
775
|
-
);
|
|
776
|
-
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
|
|
777
|
-
};
|
|
778
|
-
```
|
|
779
|
-
|
|
780
|
-
### Flyout
|
|
781
|
-
|
|
782
|
-
The flyout will automatically position to the left or top if it would otherwise go outside the window. The menu must be placed in a container outside the main content like Popper, [Popover](/components/popover) or [Tooltip](/components/tooltip) since it may go over the side nav.
|
|
783
|
-
|
|
784
|
-
```js isBeta
|
|
785
|
-
import React from 'react';
|
|
786
|
-
import { MenuToggle, Menu, MenuContent, MenuList, MenuItem, Popper } from '@patternfly/react-core';
|
|
787
|
-
|
|
788
|
-
MenuWithFlyout = () => {
|
|
789
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
790
|
-
const menuRef = React.useRef();
|
|
791
|
-
|
|
792
|
-
const handleMenuKeys = event => {
|
|
793
|
-
if (!isOpen) {
|
|
794
|
-
return;
|
|
795
|
-
}
|
|
796
|
-
if (menuRef.current.contains(event.target) || toggleRef.current.contains(event.target)) {
|
|
797
|
-
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
798
|
-
setIsOpen(!isOpen);
|
|
799
|
-
toggleRef.current.focus();
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
};
|
|
803
|
-
|
|
804
|
-
const handleClickOutside = event => {
|
|
805
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
806
|
-
setIsOpen(false);
|
|
807
|
-
}
|
|
808
|
-
};
|
|
809
|
-
|
|
810
|
-
React.useEffect(() => {
|
|
811
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
812
|
-
window.addEventListener('click', handleClickOutside);
|
|
813
|
-
return () => {
|
|
814
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
815
|
-
window.removeEventListener('click', handleClickOutside);
|
|
816
|
-
};
|
|
817
|
-
}, [isOpen, menuRef]);
|
|
818
|
-
|
|
819
|
-
const onSelect = itemId => console.log('selected', itemId);
|
|
820
|
-
|
|
821
|
-
const numFlyouts = 15;
|
|
822
|
-
const FlyoutMenu = ({ depth, children }) => (
|
|
823
|
-
<Menu key={depth} containsFlyout id={`menu-${depth}`} onSelect={onSelect}>
|
|
824
|
-
<MenuContent>
|
|
825
|
-
<MenuList>
|
|
826
|
-
<MenuItem flyoutMenu={children} itemId={`next-menu-${depth}`}>
|
|
827
|
-
Next menu
|
|
828
|
-
</MenuItem>
|
|
829
|
-
{[...Array(numFlyouts - depth).keys()].map(j => (
|
|
830
|
-
<MenuItem key={`${depth}-${j}`} itemId={`${depth}-${j}`}>
|
|
831
|
-
Menu {depth} item {j}
|
|
832
|
-
</MenuItem>
|
|
833
|
-
))}
|
|
834
|
-
<MenuItem flyoutMenu={children} itemId={`next-menu-2-${depth}`}>
|
|
835
|
-
Next menu
|
|
836
|
-
</MenuItem>
|
|
837
|
-
</MenuList>
|
|
838
|
-
</MenuContent>
|
|
839
|
-
</Menu>
|
|
840
|
-
);
|
|
841
|
-
let curFlyout = <FlyoutMenu depth={1} />;
|
|
842
|
-
for (let i = 2; i < numFlyouts - 1; i++) {
|
|
843
|
-
curFlyout = <FlyoutMenu depth={i}>{curFlyout}</FlyoutMenu>;
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
const onToggleClick = ev => {
|
|
847
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
848
|
-
setTimeout(() => {
|
|
849
|
-
if (menuRef.current) {
|
|
850
|
-
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
851
|
-
firstElement && firstElement.focus();
|
|
852
|
-
}
|
|
853
|
-
}, 0);
|
|
854
|
-
setIsOpen(!isOpen);
|
|
855
|
-
};
|
|
856
|
-
|
|
857
|
-
const toggle = (
|
|
858
|
-
<MenuToggle onClick={onToggleClick} isExpanded={isOpen}>
|
|
859
|
-
{isOpen ? 'Expanded' : 'Collapsed'}
|
|
860
|
-
</MenuToggle>
|
|
861
|
-
);
|
|
862
|
-
|
|
863
|
-
const menu = (
|
|
864
|
-
<Menu ref={menuRef} containsFlyout onSelect={onSelect}>
|
|
865
|
-
<MenuContent>
|
|
866
|
-
<MenuList>
|
|
867
|
-
<MenuItem itemId="start">Start rollout</MenuItem>
|
|
868
|
-
<MenuItem itemId="pause">Pause rollouts</MenuItem>
|
|
869
|
-
<MenuItem itemId="storage">Add storage</MenuItem>
|
|
870
|
-
<MenuItem description="Description" flyoutMenu={curFlyout} itemId="next-menu-root">
|
|
871
|
-
Edit
|
|
872
|
-
</MenuItem>
|
|
873
|
-
<MenuItem itemId="delete">Delete deployment config</MenuItem>
|
|
874
|
-
</MenuList>
|
|
875
|
-
</MenuContent>
|
|
876
|
-
</Menu>
|
|
877
|
-
);
|
|
878
|
-
|
|
879
|
-
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
|
|
880
|
-
};
|
|
881
|
-
```
|
|
882
|
-
|
|
883
|
-
### Application launcher using menu components
|
|
884
|
-
|
|
885
|
-
```js
|
|
886
|
-
import React from 'react';
|
|
887
|
-
import {
|
|
888
|
-
MenuToggle,
|
|
889
|
-
Menu,
|
|
890
|
-
MenuContent,
|
|
891
|
-
MenuList,
|
|
892
|
-
MenuItem,
|
|
893
|
-
MenuGroup,
|
|
894
|
-
MenuInput,
|
|
895
|
-
Popper,
|
|
896
|
-
Tooltip,
|
|
897
|
-
Divider,
|
|
898
|
-
TextInput
|
|
899
|
-
} from '@patternfly/react-core';
|
|
900
|
-
import { Link } from '@reach/router';
|
|
901
|
-
import ThIcon from '@patternfly/react-icons/dist/js/icons/th-icon';
|
|
902
|
-
import pfIcon from './examples/pf-logo-small.svg';
|
|
903
|
-
|
|
904
|
-
MenuAppLauncher = () => {
|
|
905
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
906
|
-
const [refFullOptions, setRefFullOptions] = React.useState();
|
|
907
|
-
const [favorites, setFavorites] = React.useState([]);
|
|
908
|
-
const [filteredIds, setFilteredIds] = React.useState(['*']);
|
|
909
|
-
const toggleRef = React.useRef();
|
|
910
|
-
const menuRef = React.useRef();
|
|
911
|
-
const containerRef = React.useRef();
|
|
912
|
-
|
|
913
|
-
const handleMenuKeys = event => {
|
|
914
|
-
if (!isOpen) {
|
|
915
|
-
return;
|
|
916
|
-
}
|
|
917
|
-
if (menuRef.current.contains(event.target) || toggleRef.current.contains(event.target)) {
|
|
918
|
-
if (event.key === 'Escape') {
|
|
919
|
-
setIsOpen(!isOpen);
|
|
920
|
-
toggleRef.current.focus();
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
};
|
|
924
|
-
|
|
925
|
-
const handleClickOutside = event => {
|
|
926
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
927
|
-
setIsOpen(false);
|
|
928
|
-
}
|
|
929
|
-
};
|
|
930
|
-
|
|
931
|
-
const onToggleClick = ev => {
|
|
932
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
933
|
-
setTimeout(() => {
|
|
934
|
-
if (menuRef.current) {
|
|
935
|
-
const firstElement = menuRef.current.querySelector('li > button,input:not(:disabled)');
|
|
936
|
-
firstElement && firstElement.focus();
|
|
937
|
-
setRefFullOptions(Array.from(menuRef.current.querySelectorAll('li:not(li[role=separator])')));
|
|
938
|
-
}
|
|
939
|
-
}, 0);
|
|
940
|
-
setIsOpen(!isOpen);
|
|
941
|
-
};
|
|
942
|
-
|
|
943
|
-
React.useEffect(() => {
|
|
944
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
945
|
-
window.addEventListener('click', handleClickOutside);
|
|
946
|
-
|
|
947
|
-
return () => {
|
|
948
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
949
|
-
window.removeEventListener('click', handleClickOutside);
|
|
950
|
-
};
|
|
951
|
-
}, [isOpen, menuRef]);
|
|
952
|
-
|
|
953
|
-
const toggle = (
|
|
954
|
-
<MenuToggle
|
|
955
|
-
aria-label="Toggle"
|
|
956
|
-
ref={toggleRef}
|
|
957
|
-
variant="plain"
|
|
958
|
-
onClick={onToggleClick}
|
|
959
|
-
isExpanded={isOpen}
|
|
960
|
-
style={{ width: 'auto' }}
|
|
961
|
-
>
|
|
962
|
-
<ThIcon />
|
|
963
|
-
</MenuToggle>
|
|
964
|
-
);
|
|
965
|
-
|
|
966
|
-
const menuItems = [
|
|
967
|
-
<MenuGroup key="group1" label="Group 1">
|
|
968
|
-
<MenuList>
|
|
969
|
-
<MenuItem itemId="0" id="0" isFavorited={favorites.includes('0')}>
|
|
970
|
-
Application 1
|
|
971
|
-
</MenuItem>
|
|
972
|
-
<MenuItem
|
|
973
|
-
itemId="1"
|
|
974
|
-
id="1"
|
|
975
|
-
isFavorited={favorites.includes('1')}
|
|
976
|
-
to="#default-link2"
|
|
977
|
-
onClick={ev => ev.preventDefault()}
|
|
978
|
-
>
|
|
979
|
-
Application 2
|
|
980
|
-
</MenuItem>
|
|
981
|
-
</MenuList>
|
|
982
|
-
</MenuGroup>,
|
|
983
|
-
<Divider key="group1-divider" />,
|
|
984
|
-
<MenuGroup key="group2" label="Group 2">
|
|
985
|
-
<MenuList>
|
|
986
|
-
<MenuItem
|
|
987
|
-
itemId="2"
|
|
988
|
-
id="2"
|
|
989
|
-
isFavorited={favorites.includes('2')}
|
|
990
|
-
component={props => <Link {...props} to="#router-link" />}
|
|
991
|
-
>
|
|
992
|
-
@reach/router Link
|
|
993
|
-
</MenuItem>
|
|
994
|
-
<MenuItem
|
|
995
|
-
itemId="3"
|
|
996
|
-
id="3"
|
|
997
|
-
isFavorited={favorites.includes('3')}
|
|
998
|
-
isExternalLink
|
|
999
|
-
icon={<img src={pfIcon} />}
|
|
1000
|
-
component={props => <Link {...props} to="#router-link2" />}
|
|
1001
|
-
>
|
|
1002
|
-
@reach/router Link with icon
|
|
1003
|
-
</MenuItem>
|
|
1004
|
-
</MenuList>
|
|
1005
|
-
</MenuGroup>,
|
|
1006
|
-
<Divider key="group2-divider" />,
|
|
1007
|
-
<MenuList key="other-items">
|
|
1008
|
-
<MenuItem key="tooltip-app" isFavorited={favorites.includes('4')} itemId="4" id="4">
|
|
1009
|
-
<Tooltip content={<div>Launch Application 3</div>} position="right">
|
|
1010
|
-
<span>Application 3 with tooltip</span>
|
|
1011
|
-
</Tooltip>
|
|
1012
|
-
</MenuItem>
|
|
1013
|
-
<MenuItem key="disabled-app" itemId="5" id="5" isDisabled>
|
|
1014
|
-
Unavailable Application
|
|
1015
|
-
</MenuItem>
|
|
1016
|
-
</MenuList>
|
|
1017
|
-
];
|
|
1018
|
-
|
|
1019
|
-
const createFavorites = favIds => {
|
|
1020
|
-
let favorites = [];
|
|
1021
|
-
|
|
1022
|
-
menuItems.forEach(item => {
|
|
1023
|
-
if (item.type === MenuList) {
|
|
1024
|
-
item.props.children.filter(child => {
|
|
1025
|
-
if (favIds.includes(child.props.itemId)) {
|
|
1026
|
-
favorites.push(child);
|
|
1027
|
-
}
|
|
1028
|
-
});
|
|
1029
|
-
} else if (item.type === MenuGroup) {
|
|
1030
|
-
item.props.children.props.children.filter(child => {
|
|
1031
|
-
if (favIds.includes(child.props.itemId)) {
|
|
1032
|
-
favorites.push(child);
|
|
1033
|
-
}
|
|
1034
|
-
});
|
|
1035
|
-
} else {
|
|
1036
|
-
if (favIds.includes(item.props.itemId)) {
|
|
1037
|
-
favorites.push(item);
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
});
|
|
1041
|
-
|
|
1042
|
-
return favorites;
|
|
1043
|
-
};
|
|
1044
|
-
|
|
1045
|
-
const filterItems = (items, filteredIds) => {
|
|
1046
|
-
if (filteredIds.length === 1 && filteredIds[0] === '*') {
|
|
1047
|
-
return items;
|
|
1048
|
-
}
|
|
1049
|
-
let keepDivider = false;
|
|
1050
|
-
let filteredCopy = items
|
|
1051
|
-
.map(group => {
|
|
1052
|
-
if (group.type === MenuGroup) {
|
|
1053
|
-
let filteredGroup = React.cloneElement(group, {
|
|
1054
|
-
children: React.cloneElement(group.props.children, {
|
|
1055
|
-
children: group.props.children.props.children.filter(child => {
|
|
1056
|
-
if (filteredIds.includes(child.props.itemId)) {
|
|
1057
|
-
return child;
|
|
1058
|
-
}
|
|
1059
|
-
})
|
|
1060
|
-
})
|
|
1061
|
-
});
|
|
1062
|
-
const filteredList = filteredGroup.props.children;
|
|
1063
|
-
if (filteredList.props.children.length > 0) {
|
|
1064
|
-
keepDivider = true;
|
|
1065
|
-
return filteredGroup;
|
|
1066
|
-
} else {
|
|
1067
|
-
keepDivider = false;
|
|
1068
|
-
}
|
|
1069
|
-
} else if (group.type === MenuList) {
|
|
1070
|
-
let filteredGroup = React.cloneElement(group, {
|
|
1071
|
-
children: group.props.children.filter(child => {
|
|
1072
|
-
if (filteredIds.includes(child.props.itemId)) {
|
|
1073
|
-
return child;
|
|
1074
|
-
}
|
|
1075
|
-
})
|
|
1076
|
-
});
|
|
1077
|
-
if (filteredGroup.props.children.length > 0) {
|
|
1078
|
-
keepDivider = true;
|
|
1079
|
-
return filteredGroup;
|
|
1080
|
-
} else {
|
|
1081
|
-
keepDivider = false;
|
|
1082
|
-
}
|
|
1083
|
-
} else {
|
|
1084
|
-
if ((keepDivider && group.type === Divider) || filteredIds.includes(group.props.itemId)) {
|
|
1085
|
-
return group;
|
|
1086
|
-
}
|
|
1087
|
-
}
|
|
1088
|
-
})
|
|
1089
|
-
.filter(newGroup => newGroup);
|
|
1090
|
-
|
|
1091
|
-
if (filteredCopy.length > 0) {
|
|
1092
|
-
let lastGroup = filteredCopy.pop();
|
|
1093
|
-
if (lastGroup.type !== Divider) {
|
|
1094
|
-
filteredCopy.push(lastGroup);
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
return filteredCopy;
|
|
1099
|
-
};
|
|
1100
|
-
|
|
1101
|
-
const onTextChange = textValue => {
|
|
1102
|
-
if (textValue === '') {
|
|
1103
|
-
setFilteredIds(['*']);
|
|
1104
|
-
return;
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
const filteredIds = refFullOptions
|
|
1108
|
-
.filter(item => item.innerText.toLowerCase().includes(textValue.toString().toLowerCase()))
|
|
1109
|
-
.map(item => item.id);
|
|
1110
|
-
setFilteredIds(filteredIds);
|
|
1111
|
-
};
|
|
1112
|
-
|
|
1113
|
-
const onFavorite = (event, itemId, actionId) => {
|
|
1114
|
-
event.stopPropagation();
|
|
1115
|
-
if (actionId === 'fav') {
|
|
1116
|
-
const isFavorite = favorites.includes(itemId);
|
|
1117
|
-
if (isFavorite) {
|
|
1118
|
-
setFavorites(favorites.filter(fav => fav !== itemId));
|
|
1119
|
-
} else {
|
|
1120
|
-
setFavorites([...favorites, itemId]);
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
};
|
|
1124
|
-
|
|
1125
|
-
const filteredFavorites = filterItems(createFavorites(favorites), filteredIds);
|
|
1126
|
-
const filteredItems = filterItems(menuItems, filteredIds);
|
|
1127
|
-
if (filteredItems.length === 0) {
|
|
1128
|
-
filteredItems.push(<MenuItem key="no-items">No results found</MenuItem>);
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
const menu = (
|
|
1132
|
-
<Menu ref={menuRef} onActionClick={onFavorite} onSelect={(_ev, itemId) => console.log('selected', itemId)}>
|
|
1133
|
-
<MenuInput>
|
|
1134
|
-
<TextInput
|
|
1135
|
-
aria-label="Filter menu items"
|
|
1136
|
-
iconVariant="search"
|
|
1137
|
-
type="search"
|
|
1138
|
-
onChange={value => onTextChange(value)}
|
|
1139
|
-
/>
|
|
1140
|
-
</MenuInput>
|
|
1141
|
-
<Divider />
|
|
1142
|
-
<MenuContent>
|
|
1143
|
-
{filteredFavorites.length > 0 && (
|
|
1144
|
-
<React.Fragment>
|
|
1145
|
-
<MenuGroup key="favorites-group" label="Favorites">
|
|
1146
|
-
<MenuList>{filteredFavorites}</MenuList>
|
|
1147
|
-
</MenuGroup>
|
|
1148
|
-
<Divider key="favorites-divider" />
|
|
1149
|
-
</React.Fragment>
|
|
1150
|
-
)}
|
|
1151
|
-
{filteredItems}
|
|
1152
|
-
</MenuContent>
|
|
1153
|
-
</Menu>
|
|
1154
|
-
);
|
|
1155
|
-
return (
|
|
1156
|
-
<div ref={containerRef}>
|
|
1157
|
-
<Popper
|
|
1158
|
-
trigger={toggle}
|
|
1159
|
-
popper={menu}
|
|
1160
|
-
isVisible={isOpen}
|
|
1161
|
-
popperMatchesTriggerWidth={false}
|
|
1162
|
-
appendTo={containerRef.current}
|
|
1163
|
-
/>
|
|
1164
|
-
</div>
|
|
1165
|
-
);
|
|
1166
|
-
};
|
|
1167
|
-
```
|
|
1168
|
-
|
|
1169
|
-
### Context selector using menu components
|
|
1170
|
-
|
|
1171
|
-
```js
|
|
1172
|
-
import React from 'react';
|
|
1173
|
-
import {
|
|
1174
|
-
MenuToggle,
|
|
1175
|
-
Menu,
|
|
1176
|
-
MenuContent,
|
|
1177
|
-
MenuFooter,
|
|
1178
|
-
MenuList,
|
|
1179
|
-
MenuItem,
|
|
1180
|
-
MenuInput,
|
|
1181
|
-
Popper,
|
|
1182
|
-
Divider,
|
|
1183
|
-
TextInput,
|
|
1184
|
-
InputGroup,
|
|
1185
|
-
Button
|
|
1186
|
-
} from '@patternfly/react-core';
|
|
1187
|
-
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
|
|
1188
|
-
|
|
1189
|
-
MenuContextSelector = () => {
|
|
1190
|
-
const items = [
|
|
1191
|
-
{
|
|
1192
|
-
text: 'Action'
|
|
1193
|
-
},
|
|
1194
|
-
{
|
|
1195
|
-
text: 'Link',
|
|
1196
|
-
href: '#'
|
|
1197
|
-
},
|
|
1198
|
-
{
|
|
1199
|
-
text: 'Disabled action',
|
|
1200
|
-
isDisabled: true
|
|
1201
|
-
},
|
|
1202
|
-
{
|
|
1203
|
-
text: 'Disabled link',
|
|
1204
|
-
href: '#',
|
|
1205
|
-
isDisabled: true
|
|
1206
|
-
},
|
|
1207
|
-
'My project',
|
|
1208
|
-
'OpenShift cluster',
|
|
1209
|
-
'Production Ansible',
|
|
1210
|
-
'AWS',
|
|
1211
|
-
'Azure',
|
|
1212
|
-
'My project 2',
|
|
1213
|
-
'OpenShift cluster ',
|
|
1214
|
-
'Production Ansible 2 ',
|
|
1215
|
-
'AWS 2',
|
|
1216
|
-
'Azure 2'
|
|
1217
|
-
];
|
|
1218
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
1219
|
-
const [selected, setSelected] = React.useState(items[0].text || items[0]);
|
|
1220
|
-
const [filteredItems, setFilteredItems] = React.useState(items);
|
|
1221
|
-
const [searchInputValue, setSearchInputValue] = React.useState('');
|
|
1222
|
-
const toggleRef = React.useRef();
|
|
1223
|
-
const menuRef = React.useRef();
|
|
1224
|
-
const menuFooterBtnRef = React.useRef();
|
|
1225
|
-
|
|
1226
|
-
const handleMenuKeys = event => {
|
|
1227
|
-
if (!isOpen) {
|
|
1228
|
-
return;
|
|
1229
|
-
}
|
|
1230
|
-
if (menuFooterBtnRef.current.contains(event.target)) {
|
|
1231
|
-
if (event.key === 'Tab') {
|
|
1232
|
-
if (event.shiftKey) {
|
|
1233
|
-
return;
|
|
1234
|
-
}
|
|
1235
|
-
setIsOpen(!isOpen);
|
|
1236
|
-
toggleRef.current.focus();
|
|
1237
|
-
}
|
|
1238
|
-
}
|
|
1239
|
-
if (menuRef.current.contains(event.target)) {
|
|
1240
|
-
if (event.key === 'Escape') {
|
|
1241
|
-
setIsOpen(!isOpen);
|
|
1242
|
-
toggleRef.current.focus();
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
};
|
|
1246
|
-
|
|
1247
|
-
const handleClickOutside = event => {
|
|
1248
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
1249
|
-
setIsOpen(false);
|
|
1250
|
-
}
|
|
1251
|
-
};
|
|
1252
|
-
|
|
1253
|
-
React.useEffect(() => {
|
|
1254
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
1255
|
-
window.addEventListener('click', handleClickOutside);
|
|
1256
|
-
|
|
1257
|
-
return () => {
|
|
1258
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
1259
|
-
window.removeEventListener('click', handleClickOutside);
|
|
1260
|
-
};
|
|
1261
|
-
}, [isOpen, menuRef]);
|
|
1262
|
-
|
|
1263
|
-
const onToggleClick = ev => {
|
|
1264
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
1265
|
-
setTimeout(() => {
|
|
1266
|
-
if (menuRef.current) {
|
|
1267
|
-
const firstElement = menuRef.current.querySelector('li > button,input:not(:disabled)');
|
|
1268
|
-
firstElement && firstElement.focus();
|
|
1269
|
-
}
|
|
1270
|
-
}, 0);
|
|
1271
|
-
setIsOpen(!isOpen);
|
|
1272
|
-
};
|
|
1273
|
-
|
|
1274
|
-
const toggle = (
|
|
1275
|
-
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
|
|
1276
|
-
{selected}
|
|
1277
|
-
</MenuToggle>
|
|
1278
|
-
);
|
|
1279
|
-
|
|
1280
|
-
const onSelect = (event, itemId) => {
|
|
1281
|
-
setSelected(itemId);
|
|
1282
|
-
setIsOpen(!isOpen);
|
|
1283
|
-
};
|
|
1284
|
-
|
|
1285
|
-
const onSearchInputChange = value => {
|
|
1286
|
-
setSearchInputValue(value);
|
|
1287
|
-
};
|
|
1288
|
-
|
|
1289
|
-
const onSearchButtonClick = event => {
|
|
1290
|
-
const filtered =
|
|
1291
|
-
searchInputValue === ''
|
|
1292
|
-
? items
|
|
1293
|
-
: items.filter(str => str.toLowerCase().indexOf(searchInputValue.toLowerCase()) !== -1);
|
|
1294
|
-
|
|
1295
|
-
setFilteredItems(filtered || []);
|
|
1296
|
-
setIsOpen(true); // Keep menu open after search executed
|
|
1297
|
-
};
|
|
1298
|
-
|
|
1299
|
-
const onEnterPressed = event => {
|
|
1300
|
-
if (event.key === 'Enter') {
|
|
1301
|
-
onSearchButtonClick(event);
|
|
1302
|
-
}
|
|
1303
|
-
};
|
|
1304
|
-
|
|
1305
|
-
const menu = (
|
|
1306
|
-
<Menu ref={menuRef} id="context-selector" onSelect={onSelect}>
|
|
1307
|
-
<MenuInput>
|
|
1308
|
-
<InputGroup>
|
|
1309
|
-
<TextInput
|
|
1310
|
-
value={searchInputValue}
|
|
1311
|
-
type="search"
|
|
1312
|
-
placeholder="Search"
|
|
1313
|
-
onChange={onSearchInputChange}
|
|
1314
|
-
onKeyPress={onEnterPressed}
|
|
1315
|
-
aria-labelledby="pf-context-selector-search-button-id-1"
|
|
1316
|
-
/>
|
|
1317
|
-
<Button
|
|
1318
|
-
variant={ButtonVariant.control}
|
|
1319
|
-
aria-label="Search menu items"
|
|
1320
|
-
id="pf-context-selector-search-button-id-1"
|
|
1321
|
-
onClick={onSearchButtonClick}
|
|
1322
|
-
>
|
|
1323
|
-
<SearchIcon aria-hidden="true" />
|
|
1324
|
-
</Button>
|
|
1325
|
-
</InputGroup>
|
|
1326
|
-
</MenuInput>
|
|
1327
|
-
<Divider />
|
|
1328
|
-
<MenuContent maxMenuHeight="200px">
|
|
1329
|
-
<MenuList>
|
|
1330
|
-
{filteredItems.map((item, index) => {
|
|
1331
|
-
const [ itemText, isDisabled, href ] = typeof item === 'string'
|
|
1332
|
-
? [ item, null, null ]
|
|
1333
|
-
: [ item.text, item.isDisabled || null, item.href || null ];
|
|
1334
|
-
return (
|
|
1335
|
-
<MenuItem itemId={itemText} key={index} isDisabled={isDisabled} to={href}>
|
|
1336
|
-
{itemText}
|
|
1337
|
-
</MenuItem>
|
|
1338
|
-
);
|
|
1339
|
-
})}
|
|
1340
|
-
</MenuList>
|
|
1341
|
-
</MenuContent>
|
|
1342
|
-
<MenuFooter>
|
|
1343
|
-
<Button ref={menuFooterBtnRef} variant="link" isInline>
|
|
1344
|
-
Action
|
|
1345
|
-
</Button>
|
|
1346
|
-
</MenuFooter>
|
|
1347
|
-
</Menu>
|
|
1348
|
-
);
|
|
1349
|
-
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
|
|
1350
|
-
};
|
|
1351
|
-
```
|
|
1352
|
-
|
|
1353
|
-
### Options menu using menu components
|
|
1354
|
-
|
|
1355
|
-
```js
|
|
1356
|
-
import React from 'react';
|
|
1357
|
-
import {
|
|
1358
|
-
MenuToggle,
|
|
1359
|
-
Menu,
|
|
1360
|
-
MenuContent,
|
|
1361
|
-
MenuList,
|
|
1362
|
-
MenuItem,
|
|
1363
|
-
MenuGroup,
|
|
1364
|
-
Popper,
|
|
1365
|
-
Divider
|
|
1366
|
-
} from '@patternfly/react-core';
|
|
1367
|
-
|
|
1368
|
-
MenuOptionsMenu = () => {
|
|
1369
|
-
|
|
1370
|
-
const [isOpen, setIsOpen] = React.useState(false);
|
|
1371
|
-
const [selected, setSelected] = React.useState(undefined);
|
|
1372
|
-
const toggleRef = React.useRef();
|
|
1373
|
-
const menuRef = React.useRef();
|
|
1374
|
-
|
|
1375
|
-
const handleMenuKeys = event => {
|
|
1376
|
-
if (isOpen && menuRef.current.contains(event.target)) {
|
|
1377
|
-
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
1378
|
-
setIsOpen(!isOpen);
|
|
1379
|
-
toggleRef.current.focus();
|
|
1380
|
-
}
|
|
1381
|
-
}
|
|
1382
|
-
};
|
|
1383
|
-
|
|
1384
|
-
const handleClickOutside = event => {
|
|
1385
|
-
if (isOpen && !menuRef.current.contains(event.target)) {
|
|
1386
|
-
setIsOpen(false);
|
|
1387
|
-
}
|
|
1388
|
-
};
|
|
1389
|
-
|
|
1390
|
-
React.useEffect(() => {
|
|
1391
|
-
window.addEventListener('keydown', handleMenuKeys);
|
|
1392
|
-
window.addEventListener('click', handleClickOutside);
|
|
1393
|
-
|
|
1394
|
-
return () => {
|
|
1395
|
-
window.removeEventListener('keydown', handleMenuKeys);
|
|
1396
|
-
window.removeEventListener('click', handleClickOutside);
|
|
1397
|
-
};
|
|
1398
|
-
}, [isOpen, menuRef]);
|
|
1399
|
-
|
|
1400
|
-
const onToggleClick = ev => {
|
|
1401
|
-
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
1402
|
-
setTimeout(() => {
|
|
1403
|
-
if (menuRef.current) {
|
|
1404
|
-
const firstElement = menuRef.current.querySelector('li > button,input:not(:disabled)');
|
|
1405
|
-
firstElement && firstElement.focus();
|
|
1406
|
-
}
|
|
1407
|
-
}, 0);
|
|
1408
|
-
setIsOpen(!isOpen);
|
|
1409
|
-
};
|
|
1410
|
-
|
|
1411
|
-
const toggle = (
|
|
1412
|
-
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
|
|
1413
|
-
Options menu
|
|
1414
|
-
</MenuToggle>
|
|
1415
|
-
);
|
|
1416
|
-
|
|
1417
|
-
const menu = (
|
|
1418
|
-
<Menu ref={menuRef} id="options-menu" selected={selected} onSelect={(_ev, itemId) => setSelected(itemId)}>
|
|
1419
|
-
<MenuContent>
|
|
1420
|
-
<MenuList>
|
|
1421
|
-
<MenuItem itemId={0}>Option 1</MenuItem>
|
|
1422
|
-
<MenuItem itemId={1} isDisabled>Disabled Option</MenuItem>
|
|
1423
|
-
<Divider key="group1-divider" />
|
|
1424
|
-
<MenuGroup label="Group 1">
|
|
1425
|
-
<MenuList>
|
|
1426
|
-
<MenuItem itemId={2}>Option 1</MenuItem>
|
|
1427
|
-
<MenuItem itemId={3}>Option 2</MenuItem>
|
|
1428
|
-
</MenuList>
|
|
1429
|
-
</MenuGroup>
|
|
1430
|
-
<Divider key="group2-divider" />
|
|
1431
|
-
<MenuGroup label="Group 2">
|
|
1432
|
-
<MenuList>
|
|
1433
|
-
<MenuItem itemId={4}>Option 1</MenuItem>
|
|
1434
|
-
<MenuItem itemId={5}>Option 2</MenuItem>
|
|
1435
|
-
</MenuList>
|
|
1436
|
-
</MenuGroup>
|
|
1437
|
-
</MenuList>
|
|
1438
|
-
</MenuContent>
|
|
1439
|
-
</Menu>
|
|
1440
|
-
);
|
|
1441
|
-
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
|
|
1442
|
-
};
|
|
1443
|
-
```
|