@patternfly/react-core 4.192.2 → 4.192.6
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/Menu/MenuContent.js +1 -1
- package/dist/esm/components/Menu/MenuContent.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/Menu/MenuContent.js +1 -1
- package/dist/js/components/Menu/MenuContent.js.map +1 -1
- package/dist/umd/react-core.css +910 -910
- package/dist/umd/react-core.js +8 -4
- package/dist/umd/react-core.min.css +72 -72
- package/dist/umd/react-core.min.js +2 -2
- package/index.d.ts +4 -0
- package/package.json +5 -5
- package/src/components/BackToTop/examples/BackToTop.css +3 -3
- package/src/components/BackToTop/examples/BackToTop.md +1 -6
- package/src/components/BackToTop/examples/BackToTopBasic.tsx +4 -0
- package/src/components/Backdrop/examples/Backdrop.md +3 -12
- package/src/components/Backdrop/examples/BackdropBasic.tsx +4 -0
- package/src/components/Backdrop/examples/BackdropSpinner.tsx +10 -0
- package/src/components/BackgroundImage/examples/BackgroundImage.md +2 -21
- package/src/components/BackgroundImage/examples/BackgroundImageBasic.tsx +15 -0
- package/src/components/Badge/examples/Badge.md +3 -24
- package/src/components/Badge/examples/BadgeRead.tsx +19 -0
- package/src/components/Badge/examples/BadgeUnread.tsx +8 -0
- package/src/components/Banner/examples/Banner.md +1 -14
- package/src/components/Banner/examples/BannerBasic.tsx +16 -0
- package/src/components/Brand/examples/Brand.md +3 -5
- package/src/components/Brand/examples/BrandBasic.tsx +5 -0
- package/src/components/Breadcrumb/examples/Breadcrumb.md +4 -105
- package/src/components/Breadcrumb/examples/BreadcrumbBasic.tsx +13 -0
- package/src/components/Breadcrumb/examples/BreadcrumbDropdown.tsx +53 -0
- package/src/components/Breadcrumb/examples/BreadcrumbWithHeading.tsx +12 -0
- package/src/components/Breadcrumb/examples/BreadcrumbWithoutHomeLink.tsx +15 -0
- package/src/components/Button/examples/ButtonAriaDisabled.tsx +33 -37
- package/src/components/Button/examples/ButtonCallToAction.tsx +8 -6
- package/src/components/Button/examples/ButtonDisabled.tsx +33 -37
- package/src/components/Button/examples/ButtonInlineSpanLink.tsx +2 -2
- package/src/components/Button/examples/ButtonLinks.tsx +6 -6
- package/src/components/Button/examples/ButtonProgress.tsx +8 -5
- package/src/components/Button/examples/ButtonSmall.tsx +9 -7
- package/src/components/Button/examples/ButtonTypes.tsx +4 -6
- package/src/components/Button/examples/ButtonVariations.tsx +31 -36
- 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/Menu/MenuContent.tsx +2 -2
- package/src/components/Menu/examples/Menu.md +7 -7
- 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/ComposableMenu.md +0 -1443
- package/src/demos/composable-menu.css +0 -3
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
MenuToggle,
|
|
4
|
+
Menu,
|
|
5
|
+
MenuContent,
|
|
6
|
+
MenuList,
|
|
7
|
+
MenuItem,
|
|
8
|
+
MenuGroup,
|
|
9
|
+
MenuInput,
|
|
10
|
+
Popper,
|
|
11
|
+
Tooltip,
|
|
12
|
+
Divider,
|
|
13
|
+
TextInput
|
|
14
|
+
} from '@patternfly/react-core';
|
|
15
|
+
import { Link } from '@reach/router';
|
|
16
|
+
import ThIcon from '@patternfly/react-icons/dist/js/icons/th-icon';
|
|
17
|
+
import pfIcon from 'pf-logo-small.svg';
|
|
18
|
+
|
|
19
|
+
export const ComposableApplicationLauncher: React.FunctionComponent = () => {
|
|
20
|
+
const [isOpen, setIsOpen] = React.useState<boolean>(false);
|
|
21
|
+
const [refFullOptions, setRefFullOptions] = React.useState<Element[]>();
|
|
22
|
+
const [favorites, setFavorites] = React.useState<string[]>([]);
|
|
23
|
+
const [filteredIds, setFilteredIds] = React.useState<string[]>(['*']);
|
|
24
|
+
const menuRef = React.useRef<HTMLDivElement>();
|
|
25
|
+
const toggleRef = React.useRef<HTMLButtonElement>();
|
|
26
|
+
const containerRef = React.useRef<HTMLDivElement>();
|
|
27
|
+
|
|
28
|
+
const handleMenuKeys = (event: KeyboardEvent) => {
|
|
29
|
+
if (!isOpen) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (menuRef.current.contains(event.target as Node) || toggleRef.current.contains(event.target as Node)) {
|
|
33
|
+
if (event.key === 'Escape') {
|
|
34
|
+
setIsOpen(!isOpen);
|
|
35
|
+
toggleRef.current.focus();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const handleClickOutside = (event: MouseEvent) => {
|
|
41
|
+
if (isOpen && !menuRef.current.contains(event.target as Node)) {
|
|
42
|
+
setIsOpen(false);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const onToggleClick = (ev: React.MouseEvent) => {
|
|
47
|
+
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
if (menuRef.current) {
|
|
50
|
+
const firstElement = menuRef.current.querySelector('li > button,input:not(:disabled)');
|
|
51
|
+
firstElement && (firstElement as HTMLElement).focus();
|
|
52
|
+
setRefFullOptions(Array.from(menuRef.current.querySelectorAll('li:not(li[role=separator])')));
|
|
53
|
+
}
|
|
54
|
+
}, 0);
|
|
55
|
+
setIsOpen(!isOpen);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
React.useEffect(() => {
|
|
59
|
+
window.addEventListener('keydown', handleMenuKeys);
|
|
60
|
+
window.addEventListener('click', handleClickOutside);
|
|
61
|
+
|
|
62
|
+
return () => {
|
|
63
|
+
window.removeEventListener('keydown', handleMenuKeys);
|
|
64
|
+
window.removeEventListener('click', handleClickOutside);
|
|
65
|
+
};
|
|
66
|
+
}, [isOpen, menuRef]);
|
|
67
|
+
|
|
68
|
+
const toggle = (
|
|
69
|
+
<MenuToggle
|
|
70
|
+
aria-label="Toggle"
|
|
71
|
+
ref={toggleRef}
|
|
72
|
+
variant="plain"
|
|
73
|
+
onClick={onToggleClick}
|
|
74
|
+
isExpanded={isOpen}
|
|
75
|
+
style={{ width: 'auto' }}
|
|
76
|
+
>
|
|
77
|
+
<ThIcon />
|
|
78
|
+
</MenuToggle>
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const menuItems = [
|
|
82
|
+
<MenuGroup key="group1" label="Group 1">
|
|
83
|
+
<MenuList>
|
|
84
|
+
<MenuItem itemId="0" id="0" isFavorited={favorites.includes('0')}>
|
|
85
|
+
Application 1
|
|
86
|
+
</MenuItem>
|
|
87
|
+
<MenuItem
|
|
88
|
+
itemId="1"
|
|
89
|
+
id="1"
|
|
90
|
+
isFavorited={favorites.includes('1')}
|
|
91
|
+
to="#default-link2"
|
|
92
|
+
onClick={ev => ev.preventDefault()}
|
|
93
|
+
>
|
|
94
|
+
Application 2
|
|
95
|
+
</MenuItem>
|
|
96
|
+
</MenuList>
|
|
97
|
+
</MenuGroup>,
|
|
98
|
+
<Divider key="group1-divider" />,
|
|
99
|
+
<MenuGroup key="group2" label="Group 2">
|
|
100
|
+
<MenuList>
|
|
101
|
+
<MenuItem
|
|
102
|
+
itemId="2"
|
|
103
|
+
id="2"
|
|
104
|
+
isFavorited={favorites.includes('2')}
|
|
105
|
+
component={props => <Link {...props} to="#router-link" />}
|
|
106
|
+
>
|
|
107
|
+
@reach/router Link
|
|
108
|
+
</MenuItem>
|
|
109
|
+
<MenuItem
|
|
110
|
+
itemId="3"
|
|
111
|
+
id="3"
|
|
112
|
+
isFavorited={favorites.includes('3')}
|
|
113
|
+
isExternalLink
|
|
114
|
+
icon={<img src={pfIcon} />}
|
|
115
|
+
component={props => <Link {...props} to="#router-link2" />}
|
|
116
|
+
>
|
|
117
|
+
@reach/router Link with icon
|
|
118
|
+
</MenuItem>
|
|
119
|
+
</MenuList>
|
|
120
|
+
</MenuGroup>,
|
|
121
|
+
<Divider key="group2-divider" />,
|
|
122
|
+
<MenuList key="other-items">
|
|
123
|
+
<MenuItem key="tooltip-app" isFavorited={favorites.includes('4')} itemId="4" id="4">
|
|
124
|
+
<Tooltip content={<div>Launch Application 3</div>} position="right">
|
|
125
|
+
<span>Application 3 with tooltip</span>
|
|
126
|
+
</Tooltip>
|
|
127
|
+
</MenuItem>
|
|
128
|
+
<MenuItem key="disabled-app" itemId="5" id="5" isDisabled>
|
|
129
|
+
Unavailable Application
|
|
130
|
+
</MenuItem>
|
|
131
|
+
</MenuList>
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
const createFavorites = (favIds: string[]) => {
|
|
135
|
+
const favorites = [];
|
|
136
|
+
|
|
137
|
+
menuItems.forEach(item => {
|
|
138
|
+
if (item.type === MenuList) {
|
|
139
|
+
item.props.children.filter(child => {
|
|
140
|
+
if (favIds.includes(child.props.itemId)) {
|
|
141
|
+
favorites.push(child);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
} else if (item.type === MenuGroup) {
|
|
145
|
+
item.props.children.props.children.filter(child => {
|
|
146
|
+
if (favIds.includes(child.props.itemId)) {
|
|
147
|
+
favorites.push(child);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
} else {
|
|
151
|
+
if (favIds.includes(item.props.itemId)) {
|
|
152
|
+
favorites.push(item);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
return favorites;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const filterItems = (items: any[], filteredIds: string[]) => {
|
|
161
|
+
if (filteredIds.length === 1 && filteredIds[0] === '*') {
|
|
162
|
+
return items;
|
|
163
|
+
}
|
|
164
|
+
let keepDivider = false;
|
|
165
|
+
const filteredCopy = items
|
|
166
|
+
.map(group => {
|
|
167
|
+
if (group.type === MenuGroup) {
|
|
168
|
+
const filteredGroup = React.cloneElement(group, {
|
|
169
|
+
children: React.cloneElement(group.props.children, {
|
|
170
|
+
children: group.props.children.props.children.filter(child => {
|
|
171
|
+
if (filteredIds.includes(child.props.itemId)) {
|
|
172
|
+
return child;
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
})
|
|
176
|
+
});
|
|
177
|
+
const filteredList = filteredGroup.props.children;
|
|
178
|
+
if (filteredList.props.children.length > 0) {
|
|
179
|
+
keepDivider = true;
|
|
180
|
+
return filteredGroup;
|
|
181
|
+
} else {
|
|
182
|
+
keepDivider = false;
|
|
183
|
+
}
|
|
184
|
+
} else if (group.type === MenuList) {
|
|
185
|
+
const filteredGroup = React.cloneElement(group, {
|
|
186
|
+
children: group.props.children.filter(child => {
|
|
187
|
+
if (filteredIds.includes(child.props.itemId)) {
|
|
188
|
+
return child;
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
});
|
|
192
|
+
if (filteredGroup.props.children.length > 0) {
|
|
193
|
+
keepDivider = true;
|
|
194
|
+
return filteredGroup;
|
|
195
|
+
} else {
|
|
196
|
+
keepDivider = false;
|
|
197
|
+
}
|
|
198
|
+
} else {
|
|
199
|
+
if ((keepDivider && group.type === Divider) || filteredIds.includes(group.props.itemId)) {
|
|
200
|
+
return group;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
.filter(newGroup => newGroup);
|
|
205
|
+
|
|
206
|
+
if (filteredCopy.length > 0) {
|
|
207
|
+
const lastGroup = filteredCopy.pop();
|
|
208
|
+
if (lastGroup.type !== Divider) {
|
|
209
|
+
filteredCopy.push(lastGroup);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return filteredCopy;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const onTextChange = (textValue: string) => {
|
|
217
|
+
if (textValue === '') {
|
|
218
|
+
setFilteredIds(['*']);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const filteredIds = refFullOptions
|
|
223
|
+
.filter(item => (item as HTMLElement).innerText.toLowerCase().includes(textValue.toString().toLowerCase()))
|
|
224
|
+
.map(item => item.id);
|
|
225
|
+
setFilteredIds(filteredIds);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const onFavorite = (event: any, itemId: string, actionId: string) => {
|
|
229
|
+
event.stopPropagation();
|
|
230
|
+
if (actionId === 'fav') {
|
|
231
|
+
const isFavorite = favorites.includes(itemId);
|
|
232
|
+
if (isFavorite) {
|
|
233
|
+
setFavorites(favorites.filter(fav => fav !== itemId));
|
|
234
|
+
} else {
|
|
235
|
+
setFavorites([...favorites, itemId]);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const filteredFavorites = filterItems(createFavorites(favorites), filteredIds);
|
|
241
|
+
const filteredItems = filterItems(menuItems, filteredIds);
|
|
242
|
+
if (filteredItems.length === 0) {
|
|
243
|
+
filteredItems.push(<MenuItem key="no-items">No results found</MenuItem>);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const menu = (
|
|
247
|
+
// eslint-disable-next-line no-console
|
|
248
|
+
<Menu ref={menuRef} onActionClick={onFavorite} onSelect={(_ev, itemId) => console.log('selected', itemId)}>
|
|
249
|
+
<MenuInput>
|
|
250
|
+
<TextInput
|
|
251
|
+
aria-label="Filter menu items"
|
|
252
|
+
iconVariant="search"
|
|
253
|
+
type="search"
|
|
254
|
+
onChange={value => onTextChange(value)}
|
|
255
|
+
/>
|
|
256
|
+
</MenuInput>
|
|
257
|
+
<Divider />
|
|
258
|
+
<MenuContent>
|
|
259
|
+
{filteredFavorites.length > 0 && (
|
|
260
|
+
<React.Fragment>
|
|
261
|
+
<MenuGroup key="favorites-group" label="Favorites">
|
|
262
|
+
<MenuList>{filteredFavorites}</MenuList>
|
|
263
|
+
</MenuGroup>
|
|
264
|
+
<Divider key="favorites-divider" />
|
|
265
|
+
</React.Fragment>
|
|
266
|
+
)}
|
|
267
|
+
{filteredItems}
|
|
268
|
+
</MenuContent>
|
|
269
|
+
</Menu>
|
|
270
|
+
);
|
|
271
|
+
return (
|
|
272
|
+
<div ref={containerRef}>
|
|
273
|
+
<Popper
|
|
274
|
+
trigger={toggle}
|
|
275
|
+
popper={menu}
|
|
276
|
+
isVisible={isOpen}
|
|
277
|
+
popperMatchesTriggerWidth={false}
|
|
278
|
+
appendTo={containerRef.current}
|
|
279
|
+
/>
|
|
280
|
+
</div>
|
|
281
|
+
);
|
|
282
|
+
};
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
MenuToggle,
|
|
4
|
+
Menu,
|
|
5
|
+
MenuContent,
|
|
6
|
+
MenuFooter,
|
|
7
|
+
MenuList,
|
|
8
|
+
MenuItem,
|
|
9
|
+
MenuInput,
|
|
10
|
+
Popper,
|
|
11
|
+
Divider,
|
|
12
|
+
TextInput,
|
|
13
|
+
InputGroup,
|
|
14
|
+
Button,
|
|
15
|
+
ButtonVariant
|
|
16
|
+
} from '@patternfly/react-core';
|
|
17
|
+
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
|
|
18
|
+
|
|
19
|
+
interface ItemData {
|
|
20
|
+
text: string;
|
|
21
|
+
href?: string;
|
|
22
|
+
isDisabled?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type ItemArrayType = (ItemData | string)[];
|
|
26
|
+
|
|
27
|
+
export const ComposableContextSelector: React.FunctionComponent = () => {
|
|
28
|
+
const items: ItemArrayType = [
|
|
29
|
+
{
|
|
30
|
+
text: 'Action'
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
text: 'Link',
|
|
34
|
+
href: '#'
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
text: 'Disabled action',
|
|
38
|
+
isDisabled: true
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
text: 'Disabled link',
|
|
42
|
+
href: '#',
|
|
43
|
+
isDisabled: true
|
|
44
|
+
},
|
|
45
|
+
'My project',
|
|
46
|
+
'OpenShift cluster',
|
|
47
|
+
'Production Ansible',
|
|
48
|
+
'AWS',
|
|
49
|
+
'Azure',
|
|
50
|
+
'My project 2',
|
|
51
|
+
'OpenShift cluster ',
|
|
52
|
+
'Production Ansible 2 ',
|
|
53
|
+
'AWS 2',
|
|
54
|
+
'Azure 2'
|
|
55
|
+
];
|
|
56
|
+
const [isOpen, setIsOpen] = React.useState<boolean>(false);
|
|
57
|
+
const [selected, setSelected] = React.useState<ItemData | string>(
|
|
58
|
+
typeof items[0] === 'string' ? items[0] : items[0].text
|
|
59
|
+
);
|
|
60
|
+
const [filteredItems, setFilteredItems] = React.useState<ItemArrayType>(items);
|
|
61
|
+
const [searchInputValue, setSearchInputValue] = React.useState<string>('');
|
|
62
|
+
const menuRef = React.useRef<HTMLDivElement>();
|
|
63
|
+
const toggleRef = React.useRef<HTMLButtonElement>();
|
|
64
|
+
const menuFooterBtnRef = React.useRef<HTMLButtonElement>();
|
|
65
|
+
|
|
66
|
+
const handleMenuKeys = (event: KeyboardEvent) => {
|
|
67
|
+
if (!isOpen) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (menuFooterBtnRef.current.contains(event.target as Node)) {
|
|
71
|
+
if (event.key === 'Tab') {
|
|
72
|
+
if (event.shiftKey) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
setIsOpen(!isOpen);
|
|
76
|
+
toggleRef.current.focus();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (menuRef.current.contains(event.target as Node)) {
|
|
80
|
+
if (event.key === 'Escape') {
|
|
81
|
+
setIsOpen(!isOpen);
|
|
82
|
+
toggleRef.current.focus();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const handleClickOutside = (event: MouseEvent) => {
|
|
88
|
+
if (isOpen && !menuRef.current.contains(event.target as Node)) {
|
|
89
|
+
setIsOpen(false);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
React.useEffect(() => {
|
|
94
|
+
window.addEventListener('keydown', handleMenuKeys);
|
|
95
|
+
window.addEventListener('click', handleClickOutside);
|
|
96
|
+
|
|
97
|
+
return () => {
|
|
98
|
+
window.removeEventListener('keydown', handleMenuKeys);
|
|
99
|
+
window.removeEventListener('click', handleClickOutside);
|
|
100
|
+
};
|
|
101
|
+
}, [isOpen, menuRef]);
|
|
102
|
+
|
|
103
|
+
const onToggleClick = (ev: React.MouseEvent) => {
|
|
104
|
+
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
105
|
+
setTimeout(() => {
|
|
106
|
+
if (menuRef.current) {
|
|
107
|
+
const firstElement = menuRef.current.querySelector('li > button,input:not(:disabled)');
|
|
108
|
+
firstElement && (firstElement as HTMLElement).focus();
|
|
109
|
+
}
|
|
110
|
+
}, 0);
|
|
111
|
+
setIsOpen(!isOpen);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const toggle = (
|
|
115
|
+
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen}>
|
|
116
|
+
{selected}
|
|
117
|
+
</MenuToggle>
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const onSelect = (ev: React.MouseEvent<Element, MouseEvent>, itemId: string) => {
|
|
121
|
+
setSelected(itemId);
|
|
122
|
+
setIsOpen(!isOpen);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const onSearchInputChange = (value: string) => {
|
|
126
|
+
setSearchInputValue(value);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const onSearchButtonClick = () => {
|
|
130
|
+
const filtered =
|
|
131
|
+
searchInputValue === ''
|
|
132
|
+
? items
|
|
133
|
+
: items.filter(item => {
|
|
134
|
+
const str = typeof item === 'string' ? item : item.text;
|
|
135
|
+
return str.toLowerCase().indexOf(searchInputValue.toLowerCase()) !== -1;
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
setFilteredItems(filtered || []);
|
|
139
|
+
setIsOpen(true); // Keep menu open after search executed
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const onEnterPressed = (event: React.KeyboardEvent) => {
|
|
143
|
+
if (event.key === 'Enter') {
|
|
144
|
+
onSearchButtonClick();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const menu = (
|
|
149
|
+
<Menu
|
|
150
|
+
ref={menuRef}
|
|
151
|
+
id="context-selector"
|
|
152
|
+
onSelect={onSelect}
|
|
153
|
+
isScrollable
|
|
154
|
+
style={
|
|
155
|
+
{
|
|
156
|
+
'--pf-c-menu--Width': '300px'
|
|
157
|
+
} as React.CSSProperties
|
|
158
|
+
}
|
|
159
|
+
>
|
|
160
|
+
<MenuInput>
|
|
161
|
+
<InputGroup>
|
|
162
|
+
<TextInput
|
|
163
|
+
value={searchInputValue}
|
|
164
|
+
type="search"
|
|
165
|
+
placeholder="Search"
|
|
166
|
+
onChange={onSearchInputChange}
|
|
167
|
+
onKeyPress={onEnterPressed}
|
|
168
|
+
aria-labelledby="pf-context-selector-search-button-id-1"
|
|
169
|
+
/>
|
|
170
|
+
<Button
|
|
171
|
+
variant={ButtonVariant.control}
|
|
172
|
+
aria-label="Search menu items"
|
|
173
|
+
id="pf-context-selector-search-button-id-1"
|
|
174
|
+
onClick={onSearchButtonClick}
|
|
175
|
+
>
|
|
176
|
+
<SearchIcon aria-hidden="true" />
|
|
177
|
+
</Button>
|
|
178
|
+
</InputGroup>
|
|
179
|
+
</MenuInput>
|
|
180
|
+
<Divider />
|
|
181
|
+
<MenuContent maxMenuHeight="200px">
|
|
182
|
+
<MenuList>
|
|
183
|
+
{filteredItems.map((item, index) => {
|
|
184
|
+
const [itemText, isDisabled, href] =
|
|
185
|
+
typeof item === 'string' ? [item, null, null] : [item.text, item.isDisabled || null, item.href || null];
|
|
186
|
+
return (
|
|
187
|
+
<MenuItem itemId={itemText} key={index} isDisabled={isDisabled} to={href}>
|
|
188
|
+
{itemText}
|
|
189
|
+
</MenuItem>
|
|
190
|
+
);
|
|
191
|
+
})}
|
|
192
|
+
</MenuList>
|
|
193
|
+
</MenuContent>
|
|
194
|
+
<MenuFooter>
|
|
195
|
+
<Button ref={menuFooterBtnRef} variant="link" isInline>
|
|
196
|
+
Action
|
|
197
|
+
</Button>
|
|
198
|
+
</MenuFooter>
|
|
199
|
+
</Menu>
|
|
200
|
+
);
|
|
201
|
+
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
|
|
202
|
+
};
|