MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / onKey

Function onKey

src/components/common/SearchDialog.jsx:103–117  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

101
102 useEffect(() => {
103 const onKey = e => {
104 if (!searchValue) return;
105 if (e.key === 'ArrowDown' || (e.key === 'Tab' && !e.shiftKey)) {
106 e.preventDefault();
107 setKeyboardNav(true);
108 setSelectedIndex(p => Math.min(p + 1, results.length - 1));
109 } else if (e.key === 'ArrowUp' || (e.key === 'Tab' && e.shiftKey)) {
110 e.preventDefault();
111 setKeyboardNav(true);
112 setSelectedIndex(p => Math.max(p - 1, 0));
113 } else if (e.key === 'Enter' && selectedIndex >= 0) {
114 e.preventDefault();
115 handleSelect(results[selectedIndex]);
116 }
117 };
118 window.addEventListener('keydown', onKey);
119 return () => window.removeEventListener('keydown', onKey);
120 }, [results, searchValue, selectedIndex, handleSelect]);

Callers

nothing calls this directly

Calls 1

toggleSearchFunction · 0.85

Tested by

no test coverage detected