Skip to content

Commit bcaf426

Browse files
committed
refactor(material/select): use afterNextRender to wait for options
1 parent 63a764d commit bcaf426

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/material/select/select.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
import {
1010
ActiveDescendantKeyManager,
11-
LiveAnnouncer,
1211
addAriaReferencedId,
12+
LiveAnnouncer,
1313
removeAriaReferencedId,
1414
} from '@angular/cdk/a11y';
1515
import {Directionality} from '@angular/cdk/bidi';
@@ -34,7 +34,10 @@ import {
3434
import {ViewportRuler} from '@angular/cdk/scrolling';
3535
import {
3636
AfterContentInit,
37+
afterNextRender,
38+
AfterRenderPhase,
3739
Attribute,
40+
booleanAttribute,
3841
ChangeDetectionStrategy,
3942
ChangeDetectorRef,
4043
Component,
@@ -45,9 +48,12 @@ import {
4548
ElementRef,
4649
EventEmitter,
4750
Inject,
51+
inject,
4852
InjectionToken,
53+
Injector,
4954
Input,
5055
NgZone,
56+
numberAttribute,
5157
OnChanges,
5258
OnDestroy,
5359
OnInit,
@@ -58,9 +64,6 @@ import {
5864
SimpleChanges,
5965
ViewChild,
6066
ViewEncapsulation,
61-
booleanAttribute,
62-
inject,
63-
numberAttribute,
6467
} from '@angular/core';
6568
import {
6669
AbstractControl,
@@ -71,17 +74,17 @@ import {
7174
Validators,
7275
} from '@angular/forms';
7376
import {
77+
_countGroupLabelsBeforeOption,
7478
_ErrorStateTracker,
79+
_getOptionScrollPosition,
7580
ErrorStateMatcher,
81+
MAT_OPTGROUP,
82+
MAT_OPTION_PARENT_COMPONENT,
7683
MatOptgroup,
7784
MatOption,
7885
MatOptionSelectionChange,
79-
MAT_OPTGROUP,
80-
MAT_OPTION_PARENT_COMPONENT,
81-
_countGroupLabelsBeforeOption,
82-
_getOptionScrollPosition,
8386
} from '@angular/material/core';
84-
import {MatFormField, MatFormFieldControl, MAT_FORM_FIELD} from '@angular/material/form-field';
87+
import {MAT_FORM_FIELD, MatFormField, MatFormFieldControl} from '@angular/material/form-field';
8588
import {defer, merge, Observable, Subject} from 'rxjs';
8689
import {
8790
distinctUntilChanged,
@@ -553,10 +556,18 @@ export class MatSelect
553556
);
554557
}
555558

556-
return this._ngZone.onStable.pipe(
557-
take(1),
558-
switchMap(() => this.optionSelectionChanges),
559-
);
559+
return new Observable(subscriber => {
560+
afterNextRender(
561+
() => {
562+
subscriber.next();
563+
subscriber.complete();
564+
},
565+
{
566+
injector: this._injector,
567+
phase: AfterRenderPhase.Read,
568+
},
569+
);
570+
}).pipe(switchMap(() => this.optionSelectionChanges));
560571
}) as Observable<MatOptionSelectionChange>;
561572

562573
/** Event emitted when the select panel has been toggled. */
@@ -584,10 +595,16 @@ export class MatSelect
584595
*/
585596
@Output() readonly valueChange: EventEmitter<any> = new EventEmitter<any>();
586597

598+
private _injector = inject(Injector);
599+
587600
constructor(
588601
protected _viewportRuler: ViewportRuler,
589602
protected _changeDetectorRef: ChangeDetectorRef,
590-
protected _ngZone: NgZone,
603+
/**
604+
* @deprecated Unused param, will be removed.
605+
* @breaking-change 19.0.0
606+
*/
607+
_unusedNgZone: NgZone,
591608
defaultErrorStateMatcher: ErrorStateMatcher,
592609
readonly _elementRef: ElementRef,
593610
@Optional() private _dir: Directionality,

tools/public_api_guard/material/select.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export { MatPrefix }
8686

8787
// @public (undocumented)
8888
export class MatSelect implements AfterContentInit, OnChanges, OnDestroy, OnInit, DoCheck, ControlValueAccessor, MatFormFieldControl<any> {
89-
constructor(_viewportRuler: ViewportRuler, _changeDetectorRef: ChangeDetectorRef, _ngZone: NgZone, defaultErrorStateMatcher: ErrorStateMatcher, _elementRef: ElementRef, _dir: Directionality, parentForm: NgForm, parentFormGroup: FormGroupDirective, _parentFormField: MatFormField, ngControl: NgControl, tabIndex: string, scrollStrategyFactory: any, _liveAnnouncer: LiveAnnouncer, _defaultOptions?: MatSelectConfig | undefined);
89+
constructor(_viewportRuler: ViewportRuler, _changeDetectorRef: ChangeDetectorRef,
90+
_unusedNgZone: NgZone, defaultErrorStateMatcher: ErrorStateMatcher, _elementRef: ElementRef, _dir: Directionality, parentForm: NgForm, parentFormGroup: FormGroupDirective, _parentFormField: MatFormField, ngControl: NgControl, tabIndex: string, scrollStrategyFactory: any, _liveAnnouncer: LiveAnnouncer, _defaultOptions?: MatSelectConfig | undefined);
9091
ariaLabel: string;
9192
ariaLabelledby: string;
9293
protected _canOpen(): boolean;
@@ -153,8 +154,6 @@ export class MatSelect implements AfterContentInit, OnChanges, OnDestroy, OnInit
153154
ngOnDestroy(): void;
154155
// (undocumented)
155156
ngOnInit(): void;
156-
// (undocumented)
157-
protected _ngZone: NgZone;
158157
_onAttached(): void;
159158
_onBlur(): void;
160159
_onChange: (value: any) => void;

0 commit comments

Comments
 (0)