Skip to content

Commit 688c430

Browse files
authoredMar 18, 2025··
fix(material/select): close panel on detach output event (#30634)
1 parent fb81ab4 commit 688c430

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed
 

‎src/material/select/select.html

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
[cdkConnectedOverlayPositions]="_positions"
4141
[cdkConnectedOverlayWidth]="_overlayWidth"
4242
[cdkConnectedOverlayFlexibleDimensions]="true"
43+
(detach)="close()"
4344
(backdropClick)="close()"
4445
(overlayKeydown)="_handleOverlayKeydown($event)">
4546
<div

‎src/material/select/select.spec.ts

+43-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
TAB,
1616
UP_ARROW,
1717
} from '@angular/cdk/keycodes';
18-
import {OverlayContainer, OverlayModule} from '@angular/cdk/overlay';
18+
import {CloseScrollStrategy, Overlay, OverlayContainer, OverlayModule} from '@angular/cdk/overlay';
1919
import {ScrollDispatcher} from '@angular/cdk/scrolling';
2020
import {
2121
createKeyboardEvent,
@@ -56,15 +56,15 @@ import {
5656
ReactiveFormsModule,
5757
Validators,
5858
} from '@angular/forms';
59-
import {ErrorStateMatcher, MatOption, MatOptionSelectionChange} from '../core';
60-
import {FloatLabelType, MAT_FORM_FIELD_DEFAULT_OPTIONS, MatFormFieldModule} from '../form-field';
61-
import {MAT_SELECT_CONFIG, MatSelectConfig} from '../select';
6259
import {By} from '@angular/platform-browser';
6360
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
6461
import {EMPTY, Observable, Subject, Subscription} from 'rxjs';
6562
import {map} from 'rxjs/operators';
63+
import {ErrorStateMatcher, MatOption, MatOptionSelectionChange} from '../core';
64+
import {FloatLabelType, MAT_FORM_FIELD_DEFAULT_OPTIONS, MatFormFieldModule} from '../form-field';
65+
import {MAT_SELECT_CONFIG, MatSelectConfig} from '../select';
6666
import {MatSelectModule} from './index';
67-
import {MatSelect} from './select';
67+
import {MAT_SELECT_SCROLL_STRATEGY, MatSelect} from './select';
6868
import {
6969
getMatSelectDynamicMultipleError,
7070
getMatSelectNonArrayValueError,
@@ -1782,6 +1782,44 @@ describe('MatSelect', () => {
17821782
.withContext('Expected select element to remain focused.')
17831783
.toBe(true);
17841784
}));
1785+
1786+
it('should close the panel on scroll event when MAT_SELECT_SCROLL_STRATEGY token was defined with CloseScrollStrategy', fakeAsync(() => {
1787+
// Need to recreate the testing module, because the issue we're
1788+
// testing for only the MAT_SELECT_SCROLL_STRATEGY is defined with thw
1789+
// is defined with the CloseScrollStrategy
1790+
1791+
TestBed.resetTestingModule();
1792+
TestBed.configureTestingModule({
1793+
imports: [MatFormFieldModule, MatSelectModule],
1794+
declarations: [BasicSelect],
1795+
providers: [
1796+
{
1797+
provide: MAT_SELECT_SCROLL_STRATEGY,
1798+
useFactory: (overlay: Overlay) => (): CloseScrollStrategy =>
1799+
overlay.scrollStrategies.close(),
1800+
deps: [Overlay],
1801+
},
1802+
{
1803+
provide: ScrollDispatcher,
1804+
useFactory: () => ({
1805+
scrolled: () => scrolledSubject,
1806+
}),
1807+
},
1808+
],
1809+
});
1810+
1811+
fixture = TestBed.createComponent(BasicSelect);
1812+
fixture.detectChanges();
1813+
1814+
const select = fixture.componentInstance.select;
1815+
select.open();
1816+
1817+
scrolledSubject.next();
1818+
fixture.detectChanges();
1819+
flush();
1820+
1821+
expect(select.panelOpen).toBe(false);
1822+
}));
17851823
});
17861824

17871825
describe('selection logic', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.