11import {
22 Directive , ContentChild , forwardRef , Renderer2 , ElementRef , AfterContentInit ,
3- ContentChildren , QueryList , Input , HostListener , ChangeDetectorRef
3+ ContentChildren , QueryList , Input , HostListener , ChangeDetectorRef , OnDestroy
44} from "@angular/core" ;
55import { Transition , SuiTransition , TransitionController , TransitionDirection } from "../../transition/index" ;
66import { HandledEvent , IAugmentedElement , KeyCode } from "../../../misc/util/index" ;
@@ -59,7 +59,7 @@ export class SuiDropdownMenuItem {
5959@Directive ( {
6060 selector : "[suiDropdownMenu]"
6161} )
62- export class SuiDropdownMenu extends SuiTransition implements AfterContentInit {
62+ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit , OnDestroy {
6363 private _service :DropdownService ;
6464 private _transitionController :TransitionController ;
6565
@@ -129,6 +129,8 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit {
129129 @Input ( )
130130 public menuSelectedItemClass :string ;
131131
132+ private _documentKeyDownListener :( ) => void ;
133+
132134 constructor ( renderer :Renderer2 , public element :ElementRef , changeDetector :ChangeDetectorRef ) {
133135 super ( renderer , element , changeDetector ) ;
134136
@@ -141,6 +143,8 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit {
141143
142144 this . menuAutoSelectFirst = false ;
143145 this . menuSelectedItemClass = "selected" ;
146+
147+ this . _documentKeyDownListener = renderer . listen ( "document" , "keydown" , ( e :KeyboardEvent ) => this . onDocumentKeyDown ( e ) ) ;
144148 }
145149
146150 @HostListener ( "click" , [ "$event" ] )
@@ -158,8 +162,7 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit {
158162 }
159163 }
160164
161- @HostListener ( "document:keydown" , [ "$event" ] )
162- public onDocumentKeydown ( e :KeyboardEvent ) :void {
165+ public onDocumentKeyDown ( e :KeyboardEvent ) :void {
163166 // Only the root dropdown (i.e. not nested dropdowns) is responsible for keeping track of the currently selected item.
164167 if ( this . _service . isOpen && ! this . _service . isNested ) {
165168 // Stop document events like scrolling while open.
@@ -312,4 +315,8 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit {
312315 // We use `_items` rather than `items` in case one or more have become disabled.
313316 this . resetSelection ( ) ;
314317 }
318+
319+ public ngOnDestroy ( ) :void {
320+ this . _documentKeyDownListener ( ) ;
321+ }
315322}
0 commit comments