MCPcopy Create free account
hub / github.com/coder/coder / handleInlineKeyDown

Function handleInlineKeyDown

site/src/components/Autocomplete/Autocomplete.tsx:188–245  ·  view source on GitHub ↗
(e: KeyboardEvent<HTMLElement>)

Source from the content-addressed store, hash-verified

186 : undefined;
187
188 const handleInlineKeyDown = (e: KeyboardEvent<HTMLElement>) => {
189 if (disabled) {
190 return;
191 }
192
193 if (e.key === "ArrowDown" || e.key === "ArrowUp") {
194 e.preventDefault();
195 if (!isOpen) {
196 handleOpenChange(true);
197 }
198
199 if (options.length === 0) {
200 updateHighlightedValue(null);
201 return;
202 }
203
204 const currentIndex = options.findIndex(
205 (option) => getOptionValue(option) === highlightedValueRef.current,
206 );
207 const nextIndex =
208 e.key === "ArrowDown"
209 ? (currentIndex + 1) % options.length
210 : (currentIndex <= 0 ? options.length : currentIndex) - 1;
211 const nextOption = options[nextIndex];
212 if (!nextOption) {
213 updateHighlightedValue(null);
214 return;
215 }
216 updateHighlightedValue(getOptionValue(nextOption));
217 return;
218 }
219
220 if (e.key === "Enter") {
221 e.preventDefault();
222 e.stopPropagation();
223 if (!loading && options.length === 0) {
224 onEnterEmpty?.();
225 return;
226 }
227
228 const highlightedOption = options.find(
229 (option) => getOptionValue(option) === highlightedValueRef.current,
230 );
231 if (highlightedOption) {
232 handleSelect(highlightedOption);
233 }
234 return;
235 }
236
237 if (e.key === "Escape") {
238 e.preventDefault();
239 if (onEscapeKeyDown) {
240 e.stopPropagation();
241 onEscapeKeyDown();
242 }
243 handleOpenChange(false);
244 }
245 };

Callers

nothing calls this directly

Calls 2

findMethod · 0.80
handleOpenChangeFunction · 0.50

Tested by

no test coverage detected