| 351 | |
| 352 | // Handle the Tab key to contain focus within the scope |
| 353 | let onKeyDown = e => { |
| 354 | if ( |
| 355 | e.key !== 'Tab' || |
| 356 | e.altKey || |
| 357 | e.ctrlKey || |
| 358 | e.metaKey || |
| 359 | !shouldContainFocus(scopeRef) || |
| 360 | e.isComposing |
| 361 | ) { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | let focusedElement = getActiveElement(ownerDocument); |
| 366 | let scope = scopeRef.current; |
| 367 | if (!scope || !isElementInScope(focusedElement, scope)) { |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | let scopeRoot = getScopeRoot(scope); |
| 372 | let walker = getFocusableTreeWalker(scopeRoot, {tabbable: true}, scope); |
| 373 | if (!focusedElement) { |
| 374 | return; |
| 375 | } |
| 376 | walker.currentNode = focusedElement; |
| 377 | let nextElement = ( |
| 378 | e.shiftKey ? walker.previousNode() : walker.nextNode() |
| 379 | ) as FocusableElement; |
| 380 | if (!nextElement) { |
| 381 | walker.currentNode = e.shiftKey |
| 382 | ? scope[scope.length - 1].nextElementSibling! |
| 383 | : scope[0].previousElementSibling!; |
| 384 | nextElement = (e.shiftKey ? walker.previousNode() : walker.nextNode()) as FocusableElement; |
| 385 | } |
| 386 | |
| 387 | e.preventDefault(); |
| 388 | if (nextElement) { |
| 389 | focusElement(nextElement, true); |
| 390 | if (nextElement instanceof getOwnerWindow(nextElement).HTMLInputElement) { |
| 391 | nextElement.select(); |
| 392 | } |
| 393 | } |
| 394 | }; |
| 395 | |
| 396 | let onFocus: EventListener = e => { |
| 397 | // If focusing an element in a child scope of the currently active scope, the child becomes active. |
nothing calls this directly
no test coverage detected