@patternfly/react-core 4.175.17 → 4.179.0
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 +52 -0
- package/dist/esm/components/Card/Card.d.ts +5 -7
- package/dist/esm/components/Card/Card.d.ts.map +1 -1
- package/dist/esm/components/Card/Card.js +14 -2
- package/dist/esm/components/Card/Card.js.map +1 -1
- package/dist/esm/components/Dropdown/DropdownToggle.d.ts +3 -1
- package/dist/esm/components/Dropdown/DropdownToggle.d.ts.map +1 -1
- package/dist/esm/components/Dropdown/DropdownToggle.js +4 -4
- package/dist/esm/components/Dropdown/DropdownToggle.js.map +1 -1
- package/dist/esm/components/Dropdown/Toggle.d.ts +3 -1
- package/dist/esm/components/Dropdown/Toggle.d.ts.map +1 -1
- package/dist/esm/components/Dropdown/Toggle.js +8 -3
- package/dist/esm/components/Dropdown/Toggle.js.map +1 -1
- package/dist/esm/components/MenuToggle/MenuToggle.d.ts +1 -1
- package/dist/esm/components/MenuToggle/MenuToggle.d.ts.map +1 -1
- package/dist/esm/components/MenuToggle/MenuToggle.js +2 -1
- package/dist/esm/components/MenuToggle/MenuToggle.js.map +1 -1
- package/dist/esm/components/Popover/Popover.d.ts.map +1 -1
- package/dist/esm/components/Popover/Popover.js +2 -22
- package/dist/esm/components/Popover/Popover.js.map +1 -1
- package/dist/esm/components/Select/Select.d.ts +2 -0
- package/dist/esm/components/Select/Select.d.ts.map +1 -1
- package/dist/esm/components/Select/Select.js +7 -9
- package/dist/esm/components/Select/Select.js.map +1 -1
- package/dist/js/components/Card/Card.d.ts +5 -7
- package/dist/js/components/Card/Card.d.ts.map +1 -1
- package/dist/js/components/Card/Card.js +14 -2
- package/dist/js/components/Card/Card.js.map +1 -1
- package/dist/js/components/Dropdown/DropdownToggle.d.ts +3 -1
- package/dist/js/components/Dropdown/DropdownToggle.d.ts.map +1 -1
- package/dist/js/components/Dropdown/DropdownToggle.js +4 -4
- package/dist/js/components/Dropdown/DropdownToggle.js.map +1 -1
- package/dist/js/components/Dropdown/Toggle.d.ts +3 -1
- package/dist/js/components/Dropdown/Toggle.d.ts.map +1 -1
- package/dist/js/components/Dropdown/Toggle.js +8 -3
- package/dist/js/components/Dropdown/Toggle.js.map +1 -1
- package/dist/js/components/MenuToggle/MenuToggle.d.ts +1 -1
- package/dist/js/components/MenuToggle/MenuToggle.d.ts.map +1 -1
- package/dist/js/components/MenuToggle/MenuToggle.js +2 -1
- package/dist/js/components/MenuToggle/MenuToggle.js.map +1 -1
- package/dist/js/components/Popover/Popover.d.ts.map +1 -1
- package/dist/js/components/Popover/Popover.js +2 -22
- package/dist/js/components/Popover/Popover.js.map +1 -1
- package/dist/js/components/Select/Select.d.ts +2 -0
- package/dist/js/components/Select/Select.d.ts.map +1 -1
- package/dist/js/components/Select/Select.js +7 -9
- package/dist/js/components/Select/Select.js.map +1 -1
- package/dist/umd/react-core.js +37 -41
- package/dist/umd/react-core.min.js +2 -2
- package/package.json +5 -5
- package/src/components/Alert/examples/Alert.md +201 -382
- package/src/components/Alert/examples/AlertAsyncLiveRegion.tsx +61 -0
- package/src/components/Alert/examples/AlertDynamicLiveRegion.tsx +61 -0
- package/src/components/ApplicationLauncher/__tests__/__snapshots__/ApplicationLauncher.test.tsx.snap +6 -0
- package/src/components/Card/Card.tsx +23 -17
- package/src/components/Card/__tests__/Card.test.tsx +8 -14
- package/src/components/Card/__tests__/__snapshots__/Card.test.tsx.snap +2 -12
- package/src/components/Card/examples/Card.md +230 -264
- package/src/components/DragDrop/examples/DragDrop.md +2 -0
- package/src/components/Dropdown/DropdownToggle.tsx +6 -1
- package/src/components/Dropdown/Toggle.tsx +11 -1
- package/src/components/Dropdown/__tests__/DropdownToggle.test.tsx +51 -0
- package/src/components/Dropdown/__tests__/__snapshots__/Dropdown.test.tsx.snap +8 -0
- package/src/components/Dropdown/__tests__/__snapshots__/DropdownToggle.test.tsx.snap +346 -4
- package/src/components/Dropdown/__tests__/__snapshots__/Toggle.test.tsx.snap +1 -0
- package/src/components/Dropdown/examples/Dropdown.md +160 -91
- package/src/components/MenuToggle/MenuToggle.tsx +5 -2
- package/src/components/MenuToggle/__tests__/MenuToggle.test.tsx +5 -0
- package/src/components/MenuToggle/__tests__/__snapshots__/MenuToggle.test.tsx.snap +59 -0
- package/src/components/MenuToggle/examples/MenuToggle.md +60 -67
- package/src/components/OptionsMenu/__tests__/__snapshots__/OptionsMenu.test.tsx.snap +7 -0
- package/src/components/Pagination/__tests__/__snapshots__/Pagination.test.tsx.snap +15 -0
- package/src/components/Popover/Popover.tsx +1 -19
- package/src/components/Popover/__tests__/Generated/__snapshots__/Popover.test.tsx.snap +0 -1
- package/src/components/Popover/__tests__/__snapshots__/Popover.test.tsx.snap +0 -4
- package/src/components/Select/Select.tsx +9 -1
- package/src/components/Select/__tests__/__snapshots__/Select.test.tsx.snap +25 -0
- package/src/components/Select/examples/Select.md +75 -9
- package/src/demos/Card/Card.md +47 -23
- package/src/demos/CardDemos.md +65 -91
- package/src/demos/Tabs/NestedTabsDemo.js +151 -0
- package/src/demos/Tabs.md +4 -0
- package/src/demos/TextInputGroupDemo.md +8 -246
- package/src/demos/examples/TextInputGroup/AttributeValueFiltering.js +250 -0
- package/src/demos/examples/TextInputGroup/AutoCompleteSearch.js +230 -0
|
@@ -24,255 +24,17 @@ Additionally, attributes can be selected by typing the full (case sensitive) nam
|
|
|
24
24
|
|
|
25
25
|
Attributes can be deselected (returning you to attribute selection mode) by hitting `escape`, or by hitting `backspace` when the only text in the text input is the attribute.
|
|
26
26
|
|
|
27
|
-
```js
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
TextInputGroup,
|
|
31
|
-
TextInputGroupMain,
|
|
32
|
-
TextInputGroupUtilities,
|
|
33
|
-
Button,
|
|
34
|
-
Menu,
|
|
35
|
-
MenuContent,
|
|
36
|
-
MenuList,
|
|
37
|
-
MenuItem,
|
|
38
|
-
Popper,
|
|
39
|
-
Chip,
|
|
40
|
-
ChipGroup,
|
|
41
|
-
Divider
|
|
42
|
-
} from '@patternfly/react-core';
|
|
43
|
-
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
|
|
44
|
-
import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon';
|
|
45
|
-
|
|
46
|
-
export const KeyValueFiltering = () => {
|
|
47
|
-
const [inputValue, setInputValue] = React.useState('');
|
|
48
|
-
const [selectedKey, setSelectedKey] = React.useState('');
|
|
49
|
-
const [menuIsOpen, setMenuIsOpen] = React.useState(false);
|
|
50
|
-
const [currentChips, setCurrentChips] = React.useState([]);
|
|
51
|
-
|
|
52
|
-
/** key and value data to be shown in the menu */
|
|
53
|
-
const data = {
|
|
54
|
-
Cluster: ['acmeqe-managed-1', 'local-cluster'],
|
|
55
|
-
Kind: ['Template', 'ReplicationController', 'ReplicaSet', 'Deployment'],
|
|
56
|
-
Label: ['release', 'environment', 'partition'],
|
|
57
|
-
Name: ['backup-1', 'backup-2', 'production-1', 'production-2', 'testing'],
|
|
58
|
-
Namespace: ['default', 'public'],
|
|
59
|
-
Status: ['running', 'idle', 'stopped']
|
|
60
|
-
};
|
|
61
|
-
const keyNames = ['Cluster', 'Kind', 'Label', 'Name', 'Namespace', 'Status'];
|
|
62
|
-
const [menuItemsText, setMenuItemsText] = React.useState(keyNames);
|
|
63
|
-
const [menuItems, setMenuItems] = React.useState([]);
|
|
64
|
-
|
|
65
|
-
/** refs used to detect when clicks occur inside vs outside of the textInputGroup and menu popper */
|
|
66
|
-
const menuRef = React.useRef();
|
|
67
|
-
const textInputGroupRef = React.useRef();
|
|
68
|
-
|
|
69
|
-
/** callback for updating the inputValue state in this component so that the input can be controlled */
|
|
70
|
-
const handleInputChange = (value, _event) => {
|
|
71
|
-
setInputValue(value);
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
/** callback for removing a chip from the chip selections */
|
|
75
|
-
const deleteChip = chipToDelete => {
|
|
76
|
-
const newChips = currentChips.filter(chip => !Object.is(chip, chipToDelete));
|
|
77
|
-
setCurrentChips(newChips);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/** reset state hooks associated with key selection */
|
|
81
|
-
const clearSelectedKey = () => {
|
|
82
|
-
setInputValue('');
|
|
83
|
-
setSelectedKey('');
|
|
84
|
-
setMenuItemsText(keyNames);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/** callback for clearing all selected chips, the text input, and any selected keys */
|
|
88
|
-
const clearChipsAndInput = () => {
|
|
89
|
-
setCurrentChips([]);
|
|
90
|
-
clearSelectedKey();
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
React.useEffect(() => {
|
|
94
|
-
/** in the menu only show items that include the text in the input */
|
|
95
|
-
const filteredMenuItems = menuItemsText
|
|
96
|
-
.filter(
|
|
97
|
-
item =>
|
|
98
|
-
!inputValue ||
|
|
99
|
-
item.toLowerCase().includes(
|
|
100
|
-
inputValue
|
|
101
|
-
.toString()
|
|
102
|
-
.slice(selectedKey.length && selectedKey.length + 2)
|
|
103
|
-
.toLowerCase()
|
|
104
|
-
)
|
|
105
|
-
)
|
|
106
|
-
.map((currentValue, index) => (
|
|
107
|
-
<MenuItem key={currentValue} itemId={index}>
|
|
108
|
-
{currentValue}
|
|
109
|
-
</MenuItem>
|
|
110
|
-
));
|
|
111
|
-
|
|
112
|
-
/** in the menu show a disabled "no result" when all menu items are filtered out */
|
|
113
|
-
if (filteredMenuItems.length === 0) {
|
|
114
|
-
const noResultItem = (
|
|
115
|
-
<MenuItem isDisabled key="no result">
|
|
116
|
-
No results found
|
|
117
|
-
</MenuItem>
|
|
118
|
-
);
|
|
119
|
-
setMenuItems([noResultItem]);
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/** determine the menu heading text based on key selection; or lack thereof */
|
|
124
|
-
const headingItem = (
|
|
125
|
-
<MenuItem isDisabled key="heading">
|
|
126
|
-
{selectedKey.length ? `${selectedKey} values` : 'Attributes'}
|
|
127
|
-
</MenuItem>
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
const divider = <Divider key="divider" />;
|
|
131
|
-
|
|
132
|
-
setMenuItems([headingItem, divider, ...filteredMenuItems]);
|
|
133
|
-
}, [inputValue]);
|
|
134
|
-
|
|
135
|
-
/** add selected key/value pair as a chip in the chip group */
|
|
136
|
-
const selectValue = selectedValue => {
|
|
137
|
-
setCurrentChips([...currentChips, `${selectedKey}: ${selectedValue}`]);
|
|
138
|
-
clearSelectedKey();
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
/** update the input to show the selected key and the menu to show the values associated with that specific key */
|
|
142
|
-
const selectKey = selectedText => {
|
|
143
|
-
setInputValue(`${selectedText}: `);
|
|
144
|
-
setSelectedKey(selectedText);
|
|
145
|
-
setMenuItemsText(data[selectedText]);
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const handleEnter = () => {
|
|
149
|
-
/** do nothing if the menu contains no real results */
|
|
150
|
-
if (menuItems.length === 1) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** perform the appropriate action based on key selection state */
|
|
155
|
-
if (selectedKey.length) {
|
|
156
|
-
selectValue(menuItems[2].props.children);
|
|
157
|
-
} else {
|
|
158
|
-
selectKey(menuItems[2].props.children);
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
/** allow the user to backspace at the selected key name to drop the currently selected key */
|
|
163
|
-
const handleBackspace = () => {
|
|
164
|
-
if (selectedKey.length && inputValue === `${selectedKey}: `) {
|
|
165
|
-
clearSelectedKey();
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/** allow the user to select a key by simply typing it and entering a colon, exact (case sensitive) matches only */
|
|
170
|
-
const handleColon = () => {
|
|
171
|
-
if (!selectedKey.length && keyNames.includes(inputValue)) {
|
|
172
|
-
selectKey(inputValue);
|
|
173
|
-
event.preventDefault();
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
/** allow the user to focus on the menu and navigate using the arrow keys */
|
|
178
|
-
const handleArrowKey = () => {
|
|
179
|
-
if (menuRef.current) {
|
|
180
|
-
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
181
|
-
firstElement && firstElement.focus();
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
/** enable keyboard only usage */
|
|
186
|
-
const handleTextInputKeyDown = event => {
|
|
187
|
-
switch (event.key) {
|
|
188
|
-
case 'Enter':
|
|
189
|
-
handleEnter();
|
|
190
|
-
break;
|
|
191
|
-
case 'Escape':
|
|
192
|
-
clearSelectedKey();
|
|
193
|
-
break;
|
|
194
|
-
case 'Backspace':
|
|
195
|
-
handleBackspace();
|
|
196
|
-
break;
|
|
197
|
-
case ':':
|
|
198
|
-
handleColon();
|
|
199
|
-
break;
|
|
200
|
-
case 'ArrowUp':
|
|
201
|
-
case 'ArrowDown':
|
|
202
|
-
handleArrowKey();
|
|
203
|
-
break;
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
/** perform the proper key or value selection when a menu item is selected */
|
|
208
|
-
const onSelect = (event, _itemId) => {
|
|
209
|
-
const selectedText = event.target.innerText;
|
|
210
|
-
|
|
211
|
-
if (selectedKey.length) {
|
|
212
|
-
selectValue(selectedText);
|
|
213
|
-
} else {
|
|
214
|
-
selectKey(selectedText);
|
|
215
|
-
}
|
|
216
|
-
event.stopPropagation();
|
|
217
|
-
textInputGroupRef.current.querySelector('input').focus();
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
/** close the menu when a click occurs outside of the menu or text input group */
|
|
221
|
-
const handleClick = event => {
|
|
222
|
-
if (
|
|
223
|
-
menuRef.current &&
|
|
224
|
-
!menuRef.current.contains(event.target) &&
|
|
225
|
-
!textInputGroupRef.current.contains(event.target)
|
|
226
|
-
) {
|
|
227
|
-
setMenuIsOpen(false);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
27
|
+
```js file="./examples/TextInputGroup/AttributeValueFiltering.js"
|
|
28
|
+
```
|
|
29
|
+
### Auto-complete search
|
|
230
30
|
|
|
231
|
-
|
|
232
|
-
const showSearchIcon = !currentChips.length;
|
|
31
|
+
This demo showcases a search input with suggestions, which filters possible selections based on the text you've entered. Unlike the attribute-value filtering demo, it allows creation of new chip items when the text entered is not available in the list of suggestions.
|
|
233
32
|
|
|
234
|
-
|
|
235
|
-
const showClearButton = inputValue || !!currentChips.length;
|
|
33
|
+
The current text in the input can be converted to a chip at any time by hitting `enter`. Auto-complete suggestions can be chosen by clicking the corresponding entry in the menu, or by navigating to an entry using the up/down arrow keys and selecting it with `enter`.
|
|
236
34
|
|
|
237
|
-
|
|
238
|
-
<div ref={textInputGroupRef}>
|
|
239
|
-
<TextInputGroup>
|
|
240
|
-
<TextInputGroupMain
|
|
241
|
-
icon={showSearchIcon && <SearchIcon />}
|
|
242
|
-
value={inputValue}
|
|
243
|
-
onChange={handleInputChange}
|
|
244
|
-
onFocus={() => setMenuIsOpen(true)}
|
|
245
|
-
onKeyDown={handleTextInputKeyDown}
|
|
246
|
-
>
|
|
247
|
-
<ChipGroup>
|
|
248
|
-
{currentChips.map(currentChip => (
|
|
249
|
-
<Chip key={currentChip} onClick={() => deleteChip(currentChip)}>
|
|
250
|
-
{currentChip}
|
|
251
|
-
</Chip>
|
|
252
|
-
))}
|
|
253
|
-
</ChipGroup>
|
|
254
|
-
</TextInputGroupMain>
|
|
255
|
-
<TextInputGroupUtilities>
|
|
256
|
-
{showClearButton && (
|
|
257
|
-
<Button variant="plain" onClick={clearChipsAndInput} aria-label="Clear button and input">
|
|
258
|
-
<TimesIcon />
|
|
259
|
-
</Button>
|
|
260
|
-
)}
|
|
261
|
-
</TextInputGroupUtilities>
|
|
262
|
-
</TextInputGroup>
|
|
263
|
-
</div>
|
|
264
|
-
);
|
|
35
|
+
Hitting `escape` while focused on the input or menu will close the menu, and the menu will reopen when text is entered.
|
|
265
36
|
|
|
266
|
-
|
|
267
|
-
<div ref={menuRef}>
|
|
268
|
-
<Menu onSelect={onSelect}>
|
|
269
|
-
<MenuContent>
|
|
270
|
-
<MenuList>{menuItems}</MenuList>
|
|
271
|
-
</MenuContent>
|
|
272
|
-
</Menu>
|
|
273
|
-
</div>
|
|
274
|
-
);
|
|
37
|
+
When only one item remains in the suggestion list, tab can be used to auto-complete the typing of that item.
|
|
275
38
|
|
|
276
|
-
|
|
277
|
-
};
|
|
39
|
+
```js file="./examples/TextInputGroup/AutoCompleteSearch.js"
|
|
278
40
|
```
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
TextInputGroup,
|
|
4
|
+
TextInputGroupMain,
|
|
5
|
+
TextInputGroupUtilities,
|
|
6
|
+
Button,
|
|
7
|
+
Menu,
|
|
8
|
+
MenuContent,
|
|
9
|
+
MenuList,
|
|
10
|
+
MenuItem,
|
|
11
|
+
Popper,
|
|
12
|
+
Chip,
|
|
13
|
+
ChipGroup,
|
|
14
|
+
Divider
|
|
15
|
+
} from '@patternfly/react-core';
|
|
16
|
+
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
|
|
17
|
+
import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon';
|
|
18
|
+
|
|
19
|
+
export const AttributeValueFiltering = () => {
|
|
20
|
+
const [inputValue, setInputValue] = React.useState('');
|
|
21
|
+
const [selectedKey, setSelectedKey] = React.useState('');
|
|
22
|
+
const [menuIsOpen, setMenuIsOpen] = React.useState(false);
|
|
23
|
+
const [currentChips, setCurrentChips] = React.useState([]);
|
|
24
|
+
|
|
25
|
+
/** key and value data to be shown in the menu */
|
|
26
|
+
const data = {
|
|
27
|
+
Cluster: ['acmeqe-managed-1', 'local-cluster'],
|
|
28
|
+
Kind: ['Template', 'ReplicationController', 'ReplicaSet', 'Deployment'],
|
|
29
|
+
Label: ['release', 'environment', 'partition'],
|
|
30
|
+
Name: ['backup-1', 'backup-2', 'production-1', 'production-2', 'testing'],
|
|
31
|
+
Namespace: ['default', 'public'],
|
|
32
|
+
Status: ['running', 'idle', 'stopped']
|
|
33
|
+
};
|
|
34
|
+
const keyNames = ['Cluster', 'Kind', 'Label', 'Name', 'Namespace', 'Status'];
|
|
35
|
+
const [menuItemsText, setMenuItemsText] = React.useState(keyNames);
|
|
36
|
+
const [menuItems, setMenuItems] = React.useState([]);
|
|
37
|
+
|
|
38
|
+
/** refs used to detect when clicks occur inside vs outside of the textInputGroup and menu popper */
|
|
39
|
+
const menuRef = React.useRef();
|
|
40
|
+
const textInputGroupRef = React.useRef();
|
|
41
|
+
|
|
42
|
+
/** callback for updating the inputValue state in this component so that the input can be controlled */
|
|
43
|
+
const handleInputChange = (value, _event) => {
|
|
44
|
+
setInputValue(value);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** callback for removing a chip from the chip selections */
|
|
48
|
+
const deleteChip = chipToDelete => {
|
|
49
|
+
const newChips = currentChips.filter(chip => !Object.is(chip, chipToDelete));
|
|
50
|
+
setCurrentChips(newChips);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** reset state hooks associated with key selection */
|
|
54
|
+
const clearSelectedKey = () => {
|
|
55
|
+
setInputValue('');
|
|
56
|
+
setSelectedKey('');
|
|
57
|
+
setMenuItemsText(keyNames);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** callback for clearing all selected chips, the text input, and any selected keys */
|
|
61
|
+
const clearChipsAndInput = () => {
|
|
62
|
+
setCurrentChips([]);
|
|
63
|
+
clearSelectedKey();
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
React.useEffect(() => {
|
|
67
|
+
/** in the menu only show items that include the text in the input */
|
|
68
|
+
const filteredMenuItems = menuItemsText
|
|
69
|
+
.filter(
|
|
70
|
+
item =>
|
|
71
|
+
!inputValue ||
|
|
72
|
+
item.toLowerCase().includes(
|
|
73
|
+
inputValue
|
|
74
|
+
.toString()
|
|
75
|
+
.slice(selectedKey.length && selectedKey.length + 2)
|
|
76
|
+
.toLowerCase()
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
.map((currentValue, index) => (
|
|
80
|
+
<MenuItem key={currentValue} itemId={index}>
|
|
81
|
+
{currentValue}
|
|
82
|
+
</MenuItem>
|
|
83
|
+
));
|
|
84
|
+
|
|
85
|
+
/** in the menu show a disabled "no result" when all menu items are filtered out */
|
|
86
|
+
if (filteredMenuItems.length === 0) {
|
|
87
|
+
const noResultItem = (
|
|
88
|
+
<MenuItem isDisabled key="no result">
|
|
89
|
+
No results found
|
|
90
|
+
</MenuItem>
|
|
91
|
+
);
|
|
92
|
+
setMenuItems([noResultItem]);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** determine the menu heading text based on key selection; or lack thereof */
|
|
97
|
+
const headingItem = (
|
|
98
|
+
<MenuItem isDisabled key="heading">
|
|
99
|
+
{selectedKey.length ? `${selectedKey} values` : 'Attributes'}
|
|
100
|
+
</MenuItem>
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const divider = <Divider key="divider" />;
|
|
104
|
+
|
|
105
|
+
setMenuItems([headingItem, divider, ...filteredMenuItems]);
|
|
106
|
+
}, [inputValue]);
|
|
107
|
+
|
|
108
|
+
/** add selected key/value pair as a chip in the chip group */
|
|
109
|
+
const selectValue = selectedValue => {
|
|
110
|
+
setCurrentChips([...currentChips, `${selectedKey}: ${selectedValue}`]);
|
|
111
|
+
clearSelectedKey();
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/** update the input to show the selected key and the menu to show the values associated with that specific key */
|
|
115
|
+
const selectKey = selectedText => {
|
|
116
|
+
setInputValue(`${selectedText}: `);
|
|
117
|
+
setSelectedKey(selectedText);
|
|
118
|
+
setMenuItemsText(data[selectedText]);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const handleEnter = () => {
|
|
122
|
+
/** do nothing if the menu contains no real results */
|
|
123
|
+
if (menuItems.length === 1) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** perform the appropriate action based on key selection state */
|
|
128
|
+
if (selectedKey.length) {
|
|
129
|
+
selectValue(menuItems[2].props.children);
|
|
130
|
+
} else {
|
|
131
|
+
selectKey(menuItems[2].props.children);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/** allow the user to backspace at the selected key name to drop the currently selected key */
|
|
136
|
+
const handleBackspace = () => {
|
|
137
|
+
if (selectedKey.length && inputValue === `${selectedKey}: `) {
|
|
138
|
+
clearSelectedKey();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** allow the user to select a key by simply typing it and entering a colon, exact (case sensitive) matches only */
|
|
143
|
+
const handleColon = () => {
|
|
144
|
+
if (!selectedKey.length && keyNames.includes(inputValue)) {
|
|
145
|
+
selectKey(inputValue);
|
|
146
|
+
event.preventDefault();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/** allow the user to focus on the menu and navigate using the arrow keys */
|
|
151
|
+
const handleArrowKey = () => {
|
|
152
|
+
if (menuRef.current) {
|
|
153
|
+
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
154
|
+
firstElement && firstElement.focus();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/** enable keyboard only usage */
|
|
159
|
+
const handleTextInputKeyDown = event => {
|
|
160
|
+
switch (event.key) {
|
|
161
|
+
case 'Enter':
|
|
162
|
+
handleEnter();
|
|
163
|
+
break;
|
|
164
|
+
case 'Escape':
|
|
165
|
+
clearSelectedKey();
|
|
166
|
+
break;
|
|
167
|
+
case 'Backspace':
|
|
168
|
+
handleBackspace();
|
|
169
|
+
break;
|
|
170
|
+
case ':':
|
|
171
|
+
handleColon();
|
|
172
|
+
break;
|
|
173
|
+
case 'ArrowUp':
|
|
174
|
+
case 'ArrowDown':
|
|
175
|
+
handleArrowKey();
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/** perform the proper key or value selection when a menu item is selected */
|
|
181
|
+
const onSelect = (event, _itemId) => {
|
|
182
|
+
const selectedText = event.target.innerText;
|
|
183
|
+
|
|
184
|
+
if (selectedKey.length) {
|
|
185
|
+
selectValue(selectedText);
|
|
186
|
+
} else {
|
|
187
|
+
selectKey(selectedText);
|
|
188
|
+
}
|
|
189
|
+
event.stopPropagation();
|
|
190
|
+
textInputGroupRef.current.querySelector('input').focus();
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/** close the menu when a click occurs outside of the menu or text input group */
|
|
194
|
+
const handleClick = event => {
|
|
195
|
+
if (
|
|
196
|
+
menuRef.current &&
|
|
197
|
+
!menuRef.current.contains(event.target) &&
|
|
198
|
+
!textInputGroupRef.current.contains(event.target)
|
|
199
|
+
) {
|
|
200
|
+
setMenuIsOpen(false);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/** only show the search icon when no chips are selected */
|
|
205
|
+
const showSearchIcon = !currentChips.length;
|
|
206
|
+
|
|
207
|
+
/** only show the clear button when there is something that can be cleared */
|
|
208
|
+
const showClearButton = inputValue || !!currentChips.length;
|
|
209
|
+
|
|
210
|
+
const inputGroup = (
|
|
211
|
+
<div ref={textInputGroupRef}>
|
|
212
|
+
<TextInputGroup>
|
|
213
|
+
<TextInputGroupMain
|
|
214
|
+
icon={showSearchIcon && <SearchIcon />}
|
|
215
|
+
value={inputValue}
|
|
216
|
+
onChange={handleInputChange}
|
|
217
|
+
onFocus={() => setMenuIsOpen(true)}
|
|
218
|
+
onKeyDown={handleTextInputKeyDown}
|
|
219
|
+
>
|
|
220
|
+
<ChipGroup>
|
|
221
|
+
{currentChips.map(currentChip => (
|
|
222
|
+
<Chip key={currentChip} onClick={() => deleteChip(currentChip)}>
|
|
223
|
+
{currentChip}
|
|
224
|
+
</Chip>
|
|
225
|
+
))}
|
|
226
|
+
</ChipGroup>
|
|
227
|
+
</TextInputGroupMain>
|
|
228
|
+
<TextInputGroupUtilities>
|
|
229
|
+
{showClearButton && (
|
|
230
|
+
<Button variant="plain" onClick={clearChipsAndInput} aria-label="Clear button and input">
|
|
231
|
+
<TimesIcon />
|
|
232
|
+
</Button>
|
|
233
|
+
)}
|
|
234
|
+
</TextInputGroupUtilities>
|
|
235
|
+
</TextInputGroup>
|
|
236
|
+
</div>
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
const menu = (
|
|
240
|
+
<div ref={menuRef}>
|
|
241
|
+
<Menu onSelect={onSelect}>
|
|
242
|
+
<MenuContent>
|
|
243
|
+
<MenuList>{menuItems}</MenuList>
|
|
244
|
+
</MenuContent>
|
|
245
|
+
</Menu>
|
|
246
|
+
</div>
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
return <Popper trigger={inputGroup} popper={menu} isVisible={menuIsOpen} onDocumentClick={handleClick} />;
|
|
250
|
+
};
|