From 737795038cc5d65dc2a10d014192b805fc516222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20FINDIKLI?= Date: Wed, 15 Nov 2023 17:03:58 +0300 Subject: [PATCH] Fixed #14124 - Dropdown Tab key not closing the overlay --- src/app/components/dropdown/dropdown.ts | 33 +++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/app/components/dropdown/dropdown.ts b/src/app/components/dropdown/dropdown.ts index ed458a3062c..68256af5f32 100755 --- a/src/app/components/dropdown/dropdown.ts +++ b/src/app/components/dropdown/dropdown.ts @@ -200,14 +200,16 @@ export class DropdownItem {
+ > +
@@ -303,6 +305,16 @@ export class DropdownItem {
+
@@ -749,7 +761,9 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV @ViewChild('overlay') overlayViewChild: Nullable; - @ViewChild('firstHiddenFocusableElementOnOverlay') firstHiddenFocusableElementOnOverlay: Nullable; + @ViewChild('firstHiddenFocusableEl') firstHiddenFocusableElementOnOverlay: Nullable; + + @ViewChild('lastHiddenFocusableEl') lastHiddenFocusableElementOnOverlay: Nullable; @ContentChildren(PrimeTemplate) templates: Nullable>; @@ -1589,7 +1603,7 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV onTabKey(event, pressedInInputText = false) { if (!pressedInInputText) { if (this.overlayVisible && this.hasFocusableElements()) { - DomHandler.focus(this.firstHiddenFocusableElementOnOverlay.nativeElement); + DomHandler.focus(event.shiftKey ? this.lastHiddenFocusableElementOnOverlay.nativeElement : this.firstHiddenFocusableElementOnOverlay.nativeElement); event.preventDefault(); } else { if (this.focusedOptionIndex() !== -1) { @@ -1606,8 +1620,17 @@ export class Dropdown implements OnInit, AfterViewInit, AfterContentInit, AfterV DomHandler.focus(focusableEl); } + onLastHiddenFocus(event) { + const focusableEl = + event.relatedTarget === this.focusInputViewChild?.nativeElement + ? DomHandler.getLastFocusableElement(this.overlayViewChild?.overlayViewChild?.nativeElement, ':not([data-p-hidden-focusable="true"])') + : this.focusInputViewChild?.nativeElement; + + DomHandler.focus(focusableEl); + } + hasFocusableElements() { - return DomHandler.getFocusableElements(this.overlayViewChild.overlayViewChild.nativeElement, ':not(.p-hidden-focusable)').length > 0; + return DomHandler.getFocusableElements(this.overlayViewChild.overlayViewChild.nativeElement, ':not([data-p-hidden-focusable="true"])').length > 0; } onBackspaceKey(event: KeyboardEvent, pressedInInputText = false) {