MCPcopy Create free account
hub / github.com/adobe/react-spectrum / onKeyDown

Function onKeyDown

packages/react-aria/src/autocomplete/useAutocomplete.ts:327–454  ·  view source on GitHub ↗
(e: BaseEvent<ReactKeyboardEvent<any>>)

Source from the content-addressed store, hash-verified

325 let keyDownTarget = useRef<Element | null>(null);
326 // For textfield specific keydown operations
327 let onKeyDown = (e: BaseEvent<ReactKeyboardEvent<any>>) => {
328 keyDownTarget.current = getEventTarget(e) as Element;
329 if (e.nativeEvent.isComposing) {
330 return;
331 }
332
333 let focusedNodeId = queuedActiveDescendant.current;
334 if (
335 focusedNodeId !== null &&
336 getOwnerDocument(inputRef.current).getElementById(focusedNodeId) == null
337 ) {
338 // if the focused id doesn't exist in document, then we need to clear the tracked focused node, otherwise
339 // we will be attempting to fire key events on a non-existing node instead of trying to focus the newly swapped wrapped collection.
340 // This can happen if you are swapping out the Autocomplete wrapped collection component like in the docs search.
341 queuedActiveDescendant.current = null;
342 focusedNodeId = null;
343 }
344
345 switch (e.key) {
346 case 'a':
347 if (isCtrlKeyPressed(e)) {
348 return;
349 }
350 break;
351 case 'Escape':
352 // Early return for Escape here so it doesn't leak the Escape event from the simulated collection event below and
353 // close the dialog prematurely. Ideally that should be up to the discretion of the input element hence the check
354 // for isPropagationStopped
355 if (e.isDefaultPrevented()) {
356 return;
357 }
358 break;
359 case ' ':
360 // Space shouldn't trigger onAction so early return.
361 return;
362 case 'Tab':
363 // Don't propogate Tab down to the collection, otherwise we will try to focus the collection via useSelectableCollection's Tab handler (aka shift tab logic)
364 // We want FocusScope to handle Tab if one exists (aka sub dialog), so special casepropogate
365 if ('continuePropagation' in e) {
366 e.continuePropagation();
367 }
368 return;
369 case 'Home':
370 case 'End':
371 case 'PageDown':
372 case 'PageUp':
373 case 'ArrowUp':
374 case 'ArrowDown':
375 case 'ArrowRight':
376 case 'ArrowLeft': {
377 if ((e.key === 'Home' || e.key === 'End') && focusedNodeId == null && e.shiftKey) {
378 return;
379 }
380
381 // If there is text within the input field, we'll want continue propagating events down
382 // to the wrapped collection if there is a focused node so that a user can continue moving the
383 // virtual focus. However, if the user doesn't have a focus in the collection, just move the text
384 // cursor instead. They can move focus down into the collection via down/up arrow if need be

Callers

nothing calls this directly

Calls 4

getEventTargetFunction · 0.90
getOwnerDocumentFunction · 0.90
isCtrlKeyPressedFunction · 0.90
continuePropagationMethod · 0.65

Tested by

no test coverage detected