@@ -206,14 +206,7 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
206206 }
207207 private _valid ?: boolean ;
208208
209- @Input ( )
210- get compareWith ( ) {
211- return this . _compareWith ;
212- }
213- set compareWith ( fn : ( o1 : any , o2 : any ) => boolean ) {
214- this . _compareWith = fn ;
215- }
216- private _compareWith = ( o1 : any , o2 : any ) => o1 === o2 ;
209+ @Input ( ) compareWith : ( o1 : any , o2 : any ) => boolean = ( a1 , a2 ) => a1 === a2 ;
217210
218211 /** Value of the select */
219212 @Input ( )
@@ -390,6 +383,8 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
390383 . then ( ( ) => {
391384 this . _selectBuilder ( ) ;
392385 } ) ;
386+ this . _initializeSelection ( ) ;
387+ this . _subscribeToMenuEvents ( ) ;
393388 }
394389
395390 ngOnDestroy ( ) : void {
@@ -416,7 +411,9 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
416411 }
417412
418413 writeValue ( value : any ) : void {
419- this . setSelectionByValue ( value , false ) ;
414+ if ( this . _initialized ) {
415+ this . setSelectionByValue ( value , false ) ;
416+ }
420417 }
421418
422419 registerOnChange ( fn : ( value : any ) => void ) : void {
@@ -455,23 +452,20 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
455452 }
456453
457454 setSelectedIndex ( index : number ) : void {
458- this . _foundation . setSelectedIndex ( index , this . _menu . closeSurfaceOnSelection ) ;
459- this . setSelectionByValue ( this . _menu . _list ?. getListItemByIndex ( index ) ?. value ) ;
455+ this . setSelectionByValue ( this . _menu . _list ?. getListItemByIndex ( index ) ?. value , true , index ) ;
460456 }
461457
462458 setSelectionByValue ( value : any , isUserInput = true , index ?: number ) : void {
463459 if ( ! this . _value && ! value ) {
464460 return ;
465461 }
466462
467- if ( index ) {
468- // This method was called directly, not called from setSelectedIndex
469- this . _foundation . setSelectedIndex ( index , this . _menu . closeSurfaceOnSelection ) ;
470- value = this . _menu . _list ?. getListItemByIndex ( index ) ?. value ;
463+ if ( ! index ) {
464+ index = this . _menu . _list ?. getListItemIndexByValue ( value ) ;
471465 }
472466
473467 this . _value = value ;
474- this . _foundation ?. setValue ( this . _value ) ;
468+ this . _foundation . setSelectedIndex ( index ?? - 1 , this . _menu . closeSurfaceOnSelection ) ;
475469
476470 if ( isUserInput ) {
477471 this . _onChange ( this . _value ) ;
@@ -482,15 +476,13 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
482476 index : this . getSelectedIndex ( ) ,
483477 value : this . _value
484478 } ) ;
485- this . _changeDetectorRef . markForCheck ( ) ;
486- }
487479
488- async _asyncSetSelectionByValue ( value : any , isUserInput : boolean = true ) : Promise < void > {
489- return this . setSelectionByValue ( value , isUserInput ) ;
480+ this . _foundation . handleChange ( ) ;
481+ this . _changeDetectorRef . markForCheck ( ) ;
490482 }
491483
492484 // Implemented as part of ControlValueAccessor.
493- setDisabledState ( disabled : boolean ) {
485+ setDisabledState ( disabled : boolean ) : void {
494486 this . _disabled = coerceBooleanProperty ( disabled ) ;
495487 this . _foundation ?. setDisabled ( this . _disabled ) ;
496488 this . _changeDetectorRef . markForCheck ( ) ;
@@ -503,11 +495,10 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
503495 /** Initialize Select internal state based on the environment state */
504496 private layout ( ) : void {
505497 if ( this . _initialized ) {
506- this . _selectBuilder ( ) ;
507-
508- if ( this . _outlined ) {
498+ this . _asyncBuildFoundation ( ) . then ( ( ) => {
499+ this . _selectBuilder ( ) ;
509500 this . _foundation . layout ( ) ;
510- }
501+ } ) ;
511502 }
512503 }
513504
@@ -518,7 +509,6 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
518509 const value = this . ngControl ?. value ?? this . _value ;
519510 if ( value ) {
520511 this . setSelectionByValue ( value , false ) ;
521- this . _foundation . layout ( ) ;
522512 }
523513 } ) ;
524514 }
@@ -543,14 +533,13 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
543533 private _selectBuilder ( ) : void {
544534 this . _changeDetectorRef . detectChanges ( ) ;
545535
536+ // initialize after running a detectChanges()
546537 this . _asyncInitFoundation ( )
547538 . then ( ( ) => {
548- // initialize after running a detectChanges()
549539 if ( ! this . outlined ) {
550540 this . _ripple = this . _createRipple ( ) ;
551541 this . _ripple . init ( ) ;
552542 }
553- this . _initializeSelection ( ) ;
554543
555544 this . _menu . wrapFocus = false ;
556545 this . _menu . elementRef . nativeElement . setAttribute ( 'role' , 'listbox' ) ;
@@ -561,13 +550,12 @@ export class MdcSelect extends _MdcSelectMixinBase implements AfterViewInit, DoC
561550 this . _menu . _list . singleSelection = true ;
562551 }
563552 } ) ;
564- this . _subscribeToMenuEvents ( ) ;
565553 }
566554
567555 private _subscribeToMenuEvents ( ) : void {
568556 // When the list items change, re-subscribe
569557 this . _menu . _list ! . items . changes . pipe ( takeUntil ( this . _destroyed ) )
570- . subscribe ( ( ) => this . layout ( ) ) ;
558+ . subscribe ( ( ) => this . _initializeSelection ( ) ) ;
571559
572560 // Subscribe to menu opened event
573561 this . _menu . opened . pipe ( takeUntil ( this . _destroyed ) )
0 commit comments