| 1 |
import { useEffect, useRef, useState } from 'react'; |
| 2 |
import { |
| 3 |
TextInputGroup, |
| 4 |
TextInputGroupMain, |
| 5 |
TextInputGroupUtilities, |
| 6 |
Button, |
| 7 |
Menu, |
| 8 |
MenuContent, |
| 9 |
MenuList, |
| 10 |
MenuItem, |
| 11 |
Popper, |
| 12 |
Divider, |
| 13 |
Label, |
| 14 |
LabelGroup |
| 15 |
} from '@patternfly/react-core'; |
| 16 |
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon'; |
| 17 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 18 |
|
| 19 |
export const AutoCompleteSearch: React.FunctionComponent = () => { |
| 20 |
const [inputValue, setInputValue] = useState(''); |
| 21 |
const [menuIsOpen, setMenuIsOpen] = useState(false); |
| 22 |
const [currentChips, setCurrentChips] = useState<string[]>([]); |
| 23 |
const [hint, setHint] = useState(''); |
| 24 |
|
| 25 |
/** auto-completing suggestion text items to be shown in the menu */ |
| 26 |
const suggestionItems = ['Cluster', 'Kind', 'Label', 'Name', 'Namespace', 'Status']; |
| 27 |
const [menuItems, setMenuItems] = useState<React.ReactElement<any>[]>([]); |
| 28 |
|
| 29 |
/** refs used to detect when clicks occur inside vs outside of the textInputGroup and menu popper */ |
| 30 |
const menuRef = useRef<HTMLDivElement>(undefined); |
| 31 |
const textInputGroupRef = useRef<HTMLDivElement>(undefined); |
| 32 |
|
| 33 |
/** callback for updating the inputValue state in this component so that the input can be controlled */ |
| 34 |
const handleInputChange = (_event: React.FormEvent<HTMLInputElement>, value: string) => { |
| 35 |
setInputValue(value); |
| 36 |
}; |
| 37 |
|
| 38 |
/** callback for removing a chip from the chip selections */ |
| 39 |
const deleteChip = (chipToDelete: string) => { |
| 40 |
const newChips = currentChips.filter((chip) => !Object.is(chip, chipToDelete)); |
| 41 |
setCurrentChips(newChips); |
| 42 |
}; |
| 43 |
|
| 44 |
/** callback for clearing all selected chips and the text input */ |
| 45 |
const clearChipsAndInput = () => { |
| 46 |
setCurrentChips([]); |
| 47 |
setInputValue(''); |
| 48 |
}; |
| 49 |
|
| 50 |
useEffect(() => { |
| 51 |
/** in the menu only show items that include the text in the input */ |
| 52 |
const filteredMenuItems = suggestionItems |
| 53 |
.filter((item) => !inputValue || item.toLowerCase().includes(inputValue.toString().toLowerCase())) |
| 54 |
.map((currentValue, index) => ( |
| 55 |
<MenuItem key={currentValue} itemId={index}> |
| 56 |
{currentValue} |
| 57 |
</MenuItem> |
| 58 |
)); |
| 59 |
|
| 60 |
/** in the menu show a disabled "no result" when all menu items are filtered out */ |
| 61 |
if (filteredMenuItems.length === 0) { |
| 62 |
const noResultItem = ( |
| 63 |
<MenuItem isDisabled key="no result"> |
| 64 |
No results found |
| 65 |
</MenuItem> |
| 66 |
); |
| 67 |
setMenuItems([noResultItem]); |
| 68 |
setHint(''); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
/** The hint is set whenever there is only one autocomplete option left. */ |
| 73 |
if (filteredMenuItems.length === 1) { |
| 74 |
const hint = filteredMenuItems[0].props.children; |
| 75 |
if (hint.toLowerCase().indexOf(inputValue.toLowerCase())) { |
| 76 |
// the match was found in a place other than the start, so typeahead wouldn't work right |
| 77 |
setHint(''); |
| 78 |
} else { |
| 79 |
// use the input for the first part, otherwise case difference could make things look wrong |
| 80 |
setHint(inputValue + hint.substr(inputValue.length)); |
| 81 |
} |
| 82 |
} else { |
| 83 |
setHint(''); |
| 84 |
} |
| 85 |
|
| 86 |
/** add a heading to the menu */ |
| 87 |
const headingItem = ( |
| 88 |
<MenuItem isDisabled key="heading"> |
| 89 |
Suggestions |
| 90 |
</MenuItem> |
| 91 |
); |
| 92 |
|
| 93 |
const divider = <Divider key="divider" />; |
| 94 |
|
| 95 |
setMenuItems([headingItem, divider, ...filteredMenuItems]); |
| 96 |
}, [inputValue]); |
| 97 |
|
| 98 |
/** add the given string as a chip in the chip group and clear the input */ |
| 99 |
const addChip = (newChipText: string) => { |
| 100 |
setCurrentChips([...currentChips, `${newChipText}`]); |
| 101 |
setInputValue(''); |
| 102 |
}; |
| 103 |
|
| 104 |
/** add the current input value as a chip */ |
| 105 |
const handleEnter = () => { |
| 106 |
if (inputValue.length) { |
| 107 |
addChip(inputValue); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
const handleTab = () => { |
| 112 |
if (menuItems.length === 3) { |
| 113 |
setInputValue(menuItems[2].props.children); |
| 114 |
} |
| 115 |
setMenuIsOpen(false); |
| 116 |
}; |
| 117 |
|
| 118 |
/** close the menu when escape is hit */ |
| 119 |
const handleEscape = () => { |
| 120 |
setMenuIsOpen(false); |
| 121 |
}; |
| 122 |
|
| 123 |
/** allow the user to focus on the menu and navigate using the arrow keys */ |
| 124 |
const handleArrowKey = () => { |
| 125 |
if (menuRef.current) { |
| 126 |
const firstElement = menuRef.current.querySelector<HTMLButtonElement>('li > button:not(:disabled)'); |
| 127 |
firstElement && firstElement.focus(); |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
/** reopen the menu if it's closed and any un-designated keys are hit */ |
| 132 |
const handleDefault = () => { |
| 133 |
if (!menuIsOpen) { |
| 134 |
setMenuIsOpen(true); |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
/** enable keyboard only usage while focused on the text input */ |
| 139 |
const handleTextInputKeyDown = (event: React.KeyboardEvent) => { |
| 140 |
switch (event.key) { |
| 141 |
case 'Enter': |
| 142 |
handleEnter(); |
| 143 |
break; |
| 144 |
case 'Escape': |
| 145 |
handleEscape(); |
| 146 |
break; |
| 147 |
case 'Tab': |
| 148 |
handleTab(); |
| 149 |
break; |
| 150 |
case 'ArrowUp': |
| 151 |
case 'ArrowDown': |
| 152 |
handleArrowKey(); |
| 153 |
break; |
| 154 |
default: |
| 155 |
handleDefault(); |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
/** apply focus to the text input */ |
| 160 |
const focusTextInput = () => { |
| 161 |
textInputGroupRef.current.querySelector('input').focus(); |
| 162 |
}; |
| 163 |
|
| 164 |
/** add the text of the selected item as a new chip */ |
| 165 |
const onSelect = (event: React.MouseEvent<Element, MouseEvent>, _itemId: string | number) => { |
| 166 |
const selectedText = (event.target as HTMLElement).innerText; |
| 167 |
addChip(selectedText); |
| 168 |
event.stopPropagation(); |
| 169 |
focusTextInput(); |
| 170 |
}; |
| 171 |
|
| 172 |
/** close the menu when a click occurs outside of the menu or text input group */ |
| 173 |
const handleClick = (event: MouseEvent) => { |
| 174 |
if ( |
| 175 |
menuRef.current && |
| 176 |
!menuRef.current.contains(event.target as HTMLElement) && |
| 177 |
!textInputGroupRef.current.contains(event.target as HTMLElement) |
| 178 |
) { |
| 179 |
setMenuIsOpen(false); |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
/** enable keyboard only usage while focused on the menu */ |
| 184 |
const handleMenuKeyDown = (event: React.KeyboardEvent) => { |
| 185 |
switch (event.key) { |
| 186 |
case 'Tab': |
| 187 |
case 'Escape': |
| 188 |
event.preventDefault(); |
| 189 |
focusTextInput(); |
| 190 |
setMenuIsOpen(false); |
| 191 |
break; |
| 192 |
case 'Enter': |
| 193 |
case ' ': |
| 194 |
setTimeout(() => setMenuIsOpen(false), 0); |
| 195 |
break; |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
/** show the search icon only when there are no chips to prevent the chips from being displayed behind the icon */ |
| 200 |
const showSearchIcon = !currentChips.length; |
| 201 |
|
| 202 |
/** only show the clear button when there is something that can be cleared */ |
| 203 |
const showClearButton = !!inputValue || !!currentChips.length; |
| 204 |
|
| 205 |
/** render the utilities component only when a component it contains is being rendered */ |
| 206 |
const showUtilities = showClearButton; |
| 207 |
|
| 208 |
const inputGroup = ( |
| 209 |
<div ref={textInputGroupRef}> |
| 210 |
<TextInputGroup> |
| 211 |
<TextInputGroupMain |
| 212 |
icon={showSearchIcon && <SearchIcon />} |
| 213 |
value={inputValue} |
| 214 |
hint={hint} |
| 215 |
onChange={handleInputChange} |
| 216 |
onFocus={() => setMenuIsOpen(true)} |
| 217 |
onKeyDown={handleTextInputKeyDown} |
| 218 |
placeholder="search" |
| 219 |
aria-label="Search input" |
| 220 |
> |
| 221 |
<LabelGroup> |
| 222 |
{currentChips.map((currentChip) => ( |
| 223 |
<Label key={currentChip} variant="outline" onClose={() => deleteChip(currentChip)}> |
| 224 |
{currentChip} |
| 225 |
</Label> |
| 226 |
))} |
| 227 |
</LabelGroup> |
| 228 |
</TextInputGroupMain> |
| 229 |
{showUtilities && ( |
| 230 |
<TextInputGroupUtilities> |
| 231 |
{showClearButton && ( |
| 232 |
<Button |
| 233 |
variant="plain" |
| 234 |
onClick={clearChipsAndInput} |
| 235 |
aria-label="Clear button for chips and input" |
| 236 |
icon={<RhMicronsCloseIcon />} |
| 237 |
/> |
| 238 |
)} |
| 239 |
</TextInputGroupUtilities> |
| 240 |
)} |
| 241 |
</TextInputGroup> |
| 242 |
</div> |
| 243 |
); |
| 244 |
|
| 245 |
const menu = ( |
| 246 |
<div ref={menuRef}> |
| 247 |
<Menu onSelect={onSelect} onKeyDown={handleMenuKeyDown}> |
| 248 |
<MenuContent> |
| 249 |
<MenuList>{menuItems}</MenuList> |
| 250 |
</MenuContent> |
| 251 |
</Menu> |
| 252 |
</div> |
| 253 |
); |
| 254 |
|
| 255 |
return ( |
| 256 |
<Popper |
| 257 |
trigger={inputGroup} |
| 258 |
triggerRef={textInputGroupRef} |
| 259 |
popper={menu} |
| 260 |
popperRef={menuRef} |
| 261 |
appendTo={() => textInputGroupRef.current} |
| 262 |
isVisible={menuIsOpen} |
| 263 |
onDocumentClick={handleClick} |
| 264 |
/> |
| 265 |
); |
| 266 |
}; |