| 1 |
import { useEffect, useRef, useState } from 'react'; |
| 2 |
import { Menu, MenuContent, MenuItem, MenuList, Popper, SearchInput } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
import { words } from '../words'; |
| 5 |
|
| 6 |
export const SearchInputAutocomplete: React.FunctionComponent = () => { |
| 7 |
const [value, setValue] = useState(''); |
| 8 |
const [hint, setHint] = useState(''); |
| 9 |
const [autocompleteOptions, setAutocompleteOptions] = useState([]); |
| 10 |
|
| 11 |
const [isAutocompleteOpen, setIsAutocompleteOpen] = useState(false); |
| 12 |
|
| 13 |
const searchInputRef = useRef(null); |
| 14 |
const autocompleteRef = useRef(null); |
| 15 |
|
| 16 |
const onClear = () => { |
| 17 |
setValue(''); |
| 18 |
}; |
| 19 |
|
| 20 |
const onChange = (_event, newValue) => { |
| 21 |
if ( |
| 22 |
newValue !== '' && |
| 23 |
searchInputRef && |
| 24 |
searchInputRef.current && |
| 25 |
searchInputRef.current.contains(document.activeElement) |
| 26 |
) { |
| 27 |
setIsAutocompleteOpen(true); |
| 28 |
|
| 29 |
// When the value of the search input changes, build a list of no more than 10 autocomplete options. |
| 30 |
// Options which start with the search input value are listed first, followed by options which contain |
| 31 |
// the search input value. |
| 32 |
let options = words |
| 33 |
.filter((option) => option.startsWith(newValue.toLowerCase())) |
| 34 |
.map((option) => ( |
| 35 |
<MenuItem itemId={option} key={option}> |
| 36 |
{option} |
| 37 |
</MenuItem> |
| 38 |
)); |
| 39 |
if (options.length > 10) { |
| 40 |
options = options.slice(0, 10); |
| 41 |
} else { |
| 42 |
options = [ |
| 43 |
...options, |
| 44 |
...words |
| 45 |
.filter((option) => !option.startsWith(newValue.toLowerCase()) && option.includes(newValue.toLowerCase())) |
| 46 |
.map((option) => ( |
| 47 |
<MenuItem itemId={option} key={option}> |
| 48 |
{option} |
| 49 |
</MenuItem> |
| 50 |
)) |
| 51 |
].slice(0, 10); |
| 52 |
} |
| 53 |
|
| 54 |
// The hint is set whenever there is only one autocomplete option left. |
| 55 |
setHint(options.length === 1 ? options[0].props.itemId : ''); |
| 56 |
// The menu is hidden if there are no options |
| 57 |
setIsAutocompleteOpen(options.length > 0); |
| 58 |
setAutocompleteOptions(options); |
| 59 |
} else { |
| 60 |
setIsAutocompleteOpen(false); |
| 61 |
} |
| 62 |
setValue(newValue); |
| 63 |
}; |
| 64 |
|
| 65 |
// Whenever an autocomplete option is selected, set the search input value, close the menu, and put the browser |
| 66 |
// focus back on the search input |
| 67 |
const onSelect = (e, itemId) => { |
| 68 |
e.stopPropagation(); |
| 69 |
setValue(itemId); |
| 70 |
setIsAutocompleteOpen(false); |
| 71 |
searchInputRef.current.focus(); |
| 72 |
}; |
| 73 |
|
| 74 |
const handleMenuKeys = (event) => { |
| 75 |
// If there is a hint while the browser focus is on the search input, tab or right arrow will 'accept' the hint value |
| 76 |
// and set it as the search input value |
| 77 |
if (hint && (event.key === 'Tab' || event.key === 'ArrowRight') && searchInputRef.current === event.target) { |
| 78 |
setValue(hint); |
| 79 |
setHint(''); |
| 80 |
setIsAutocompleteOpen(false); |
| 81 |
if (event.key === 'ArrowRight') { |
| 82 |
event.preventDefault(); |
| 83 |
} |
| 84 |
// if the autocomplete is open and the browser focus is on the search input, |
| 85 |
} else if (isAutocompleteOpen && searchInputRef.current && searchInputRef.current === event.target) { |
| 86 |
// the escape key closes the autocomplete menu and keeps the focus on the search input. |
| 87 |
if (event.key === 'Escape') { |
| 88 |
setIsAutocompleteOpen(false); |
| 89 |
searchInputRef.current.focus(); |
| 90 |
// the up and down arrow keys move browser focus into the autocomplete menu |
| 91 |
} else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') { |
| 92 |
const firstElement = autocompleteRef.current.querySelector('li > button:not(:disabled)'); |
| 93 |
firstElement && firstElement.focus(); |
| 94 |
event.preventDefault(); // by default, the up and down arrow keys scroll the window |
| 95 |
// the tab, enter, and space keys will close the menu, and the tab key will move browser |
| 96 |
// focus forward one element (by default) |
| 97 |
} else if (event.key === 'Tab' || event.key === 'Enter' || event.key === ' ') { |
| 98 |
setIsAutocompleteOpen(false); |
| 99 |
if (event.key === 'Enter' || event.key === ' ') { |
| 100 |
event.preventDefault(); |
| 101 |
} |
| 102 |
} |
| 103 |
// If the autocomplete is open and the browser focus is in the autocomplete menu |
| 104 |
// hitting tab will close the autocomplete and but browser focus back on the search input. |
| 105 |
} else if (isAutocompleteOpen && autocompleteRef.current.contains(event.target) && event.key === 'Tab') { |
| 106 |
event.preventDefault(); |
| 107 |
setIsAutocompleteOpen(false); |
| 108 |
searchInputRef.current.focus(); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
// The autocomplete menu should close if the user clicks outside the menu. |
| 113 |
const handleClickOutside = (event) => { |
| 114 |
if ( |
| 115 |
isAutocompleteOpen && |
| 116 |
autocompleteRef && |
| 117 |
autocompleteRef.current && |
| 118 |
!autocompleteRef.current.contains(event.target) |
| 119 |
) { |
| 120 |
setIsAutocompleteOpen(false); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
useEffect(() => { |
| 125 |
window.addEventListener('keydown', handleMenuKeys); |
| 126 |
window.addEventListener('click', handleClickOutside); |
| 127 |
return () => { |
| 128 |
window.removeEventListener('keydown', handleMenuKeys); |
| 129 |
window.removeEventListener('click', handleClickOutside); |
| 130 |
}; |
| 131 |
}, [isAutocompleteOpen, hint, searchInputRef.current]); |
| 132 |
|
| 133 |
const searchInput = ( |
| 134 |
<SearchInput |
| 135 |
value={value} |
| 136 |
onChange={onChange} |
| 137 |
onClear={onClear} |
| 138 |
ref={searchInputRef} |
| 139 |
hint={hint} |
| 140 |
id="autocomplete-search" |
| 141 |
aria-label="Search with autocomplete" |
| 142 |
/> |
| 143 |
); |
| 144 |
|
| 145 |
const autocomplete = ( |
| 146 |
<Menu ref={autocompleteRef} onSelect={onSelect}> |
| 147 |
<MenuContent> |
| 148 |
<MenuList>{autocompleteOptions}</MenuList> |
| 149 |
</MenuContent> |
| 150 |
</Menu> |
| 151 |
); |
| 152 |
|
| 153 |
return ( |
| 154 |
<Popper |
| 155 |
trigger={searchInput} |
| 156 |
triggerRef={searchInputRef} |
| 157 |
popper={autocomplete} |
| 158 |
popperRef={autocompleteRef} |
| 159 |
isVisible={isAutocompleteOpen} |
| 160 |
enableFlip={false} |
| 161 |
// append the autocomplete menu to the search input in the DOM for the sake of the keyboard navigation experience |
| 162 |
appendTo={() => document.querySelector('#autocomplete-search')} |
| 163 |
/> |
| 164 |
); |
| 165 |
}; |