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

Function onKeyDown

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

Source from the content-addressed store, hash-verified

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