@@ -36,7 +36,7 @@ import {
36
36
import { BooleanInput , coerceArray , coerceBooleanProperty } from '@angular/cdk/coercion' ;
37
37
import { SelectionModel } from '@angular/cdk/collections' ;
38
38
import { defer , merge , Observable , Subject } from 'rxjs' ;
39
- import { filter , map , startWith , switchMap , take , takeUntil } from 'rxjs/operators' ;
39
+ import { filter , map , startWith , switchMap , takeUntil } from 'rxjs/operators' ;
40
40
import {
41
41
AbstractControl ,
42
42
ControlValueAccessor ,
@@ -82,9 +82,9 @@ class ListboxSelectionModel<T> extends SelectionModel<T> {
82
82
// The super class is always in multi-selection mode, so we need to override the behavior if
83
83
// this selection model actually belongs to a single-selection listbox.
84
84
if ( this . multiple ) {
85
- super . select ( ...values ) ;
85
+ return super . select ( ...values ) ;
86
86
} else {
87
- super . setSelection ( ...values ) ;
87
+ return super . setSelection ( ...values ) ;
88
88
}
89
89
}
90
90
}
@@ -545,7 +545,7 @@ export class CdkListbox<T = unknown>
545
545
if ( this . _invalid ) {
546
546
this . selectionModel . clear ( false ) ;
547
547
}
548
- this . selectionModel . select ( ...this . options . toArray ( ) . map ( option => option . value ) ) ;
548
+ this . selectionModel . select ( ...this . options . map ( option => option . value ) ) ;
549
549
}
550
550
}
551
551
@@ -635,15 +635,9 @@ export class CdkListbox<T = unknown>
635
635
protected triggerOption ( option : CdkOption < T > | null ) {
636
636
if ( option && ! option . disabled ) {
637
637
this . _lastTriggered = option ;
638
- let changed = false ;
639
- this . selectionModel . changed
640
- . pipe ( take ( 1 ) , takeUntil ( this . destroyed ) )
641
- . subscribe ( ( ) => ( changed = true ) ) ;
642
- if ( this . multiple ) {
643
- this . toggle ( option ) ;
644
- } else {
645
- this . select ( option ) ;
646
- }
638
+ const changed = this . multiple
639
+ ? this . selectionModel . toggle ( option . value )
640
+ : this . selectionModel . select ( option . value ) ;
647
641
if ( changed ) {
648
642
this . _onChange ( this . value ) ;
649
643
this . valueChange . next ( {
@@ -669,10 +663,12 @@ export class CdkListbox<T = unknown>
669
663
}
670
664
this . _lastTriggered = trigger ;
671
665
const isEqual = this . compareWith ?? Object . is ;
672
- const options = this . options . toArray ( ) ;
673
- const updateValues = options
674
- . slice ( Math . max ( 0 , Math . min ( from , to ) ) , Math . min ( options . length , Math . max ( from , to ) + 1 ) )
675
- . map ( option => option . value ) ;
666
+ const updateValues = this . options
667
+ . map ( option => option . value )
668
+ . slice (
669
+ Math . max ( 0 , Math . min ( from , to ) ) ,
670
+ Math . min ( this . options . length , Math . max ( from , to ) + 1 ) ,
671
+ ) ;
676
672
const selected = [ ...this . value ] ;
677
673
for ( const updateValue of updateValues ) {
678
674
const selectedIndex = selected . findIndex ( selectedValue =>
@@ -684,11 +680,7 @@ export class CdkListbox<T = unknown>
684
680
selected . splice ( selectedIndex , 1 ) ;
685
681
}
686
682
}
687
- let changed = false ;
688
- this . selectionModel . changed
689
- . pipe ( take ( 1 ) , takeUntil ( this . destroyed ) )
690
- . subscribe ( ( ) => ( changed = true ) ) ;
691
- this . selectionModel . setSelection ( ...selected ) ;
683
+ let changed = this . selectionModel . setSelection ( ...selected ) ;
692
684
if ( changed ) {
693
685
this . _onChange ( this . value ) ;
694
686
this . valueChange . next ( {
@@ -989,6 +981,7 @@ export class CdkListbox<T = unknown>
989
981
return values . filter ( value => ! validValues . some ( validValue => isEqual ( value , validValue ) ) ) ;
990
982
}
991
983
984
+ /** Get the index of the last triggered option. */
992
985
private _getLastTriggeredIndex ( ) {
993
986
const index = this . options . toArray ( ) . indexOf ( this . _lastTriggered ! ) ;
994
987
return index === - 1 ? null : index ;
0 commit comments