From 128a4d34629edcb6d5c113f80390956fa56889e7 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 14 Mar 2025 23:29:01 +0000 Subject: [PATCH] fix(material/select): close panel on detach output event --- src/material/select/select.html | 1 + src/material/select/select.spec.ts | 48 ++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/material/select/select.html b/src/material/select/select.html index 9a9b147c3c51..6eaecee7f5c3 100644 --- a/src/material/select/select.html +++ b/src/material/select/select.html @@ -40,6 +40,7 @@ [cdkConnectedOverlayPositions]="_positions" [cdkConnectedOverlayWidth]="_overlayWidth" [cdkConnectedOverlayFlexibleDimensions]="true" + (detach)="close()" (backdropClick)="close()" (overlayKeydown)="_handleOverlayKeydown($event)">
{ .withContext('Expected select element to remain focused.') .toBe(true); })); + + it('should close the panel on scroll event when MAT_SELECT_SCROLL_STRATEGY token was defined with CloseScrollStrategy', fakeAsync(() => { + // Need to recreate the testing module, because the issue we're + // testing for only the MAT_SELECT_SCROLL_STRATEGY is defined with thw + // is defined with the CloseScrollStrategy + + TestBed.resetTestingModule(); + TestBed.configureTestingModule({ + imports: [MatFormFieldModule, MatSelectModule], + declarations: [BasicSelect], + providers: [ + { + provide: MAT_SELECT_SCROLL_STRATEGY, + useFactory: (overlay: Overlay) => (): CloseScrollStrategy => + overlay.scrollStrategies.close(), + deps: [Overlay], + }, + { + provide: ScrollDispatcher, + useFactory: () => ({ + scrolled: () => scrolledSubject, + }), + }, + ], + }); + + fixture = TestBed.createComponent(BasicSelect); + fixture.detectChanges(); + + const select = fixture.componentInstance.select; + select.open(); + + scrolledSubject.next(); + fixture.detectChanges(); + flush(); + + expect(select.panelOpen).toBe(false); + })); }); describe('selection logic', () => {