@@ -5,8 +5,8 @@ import { SuiSelectBase } from "../classes/select-base";
55import { SuiSelectOption } from "./select-option" ;
66
77@Component ( {
8- selector : "sui-multi-select" ,
9- template : `
8+ selector : "sui-multi-select" ,
9+ template : `
1010<!-- Dropdown icon -->
1111<i class="{{ icon }} icon" (click)="onCaretClick($event)"></i>
1212
@@ -51,24 +51,24 @@ import { SuiSelectOption } from "./select-option";
5151 </ng-container>
5252</div>
5353` ,
54- styles : [ `
54+ styles : [ `
5555:host input.search {
5656 width: 12em !important;
5757}
5858` ]
59- } )
59+ } )
6060export class SuiMultiSelect < T , U > extends SuiSelectBase < T , U > implements ICustomValueAccessorHost < U [ ] > {
61- public selectedOptions : T [ ] ;
61+ public selectedOptions :T [ ] ;
6262 @Output ( )
63- public selectedOptionsChange : EventEmitter < U [ ] > ;
63+ public selectedOptionsChange :EventEmitter < U [ ] > ;
6464 @Input ( )
65- public maxSelected : number ;
65+ public maxSelected :number ;
6666 @HostBinding ( "class.multiple" )
67- public readonly hasClasses : boolean ;
67+ public readonly hasClasses :boolean ;
6868 // Stores the values written by ngModel before it can be matched to an option from `options`.
69- private _writtenOptions ?: U [ ] ;
69+ private _writtenOptions ?:U [ ] ;
7070
71- constructor ( element : ElementRef , localizationService : SuiLocalizationService ) {
71+ constructor ( element :ElementRef , localizationService :SuiLocalizationService ) {
7272 super ( element , localizationService ) ;
7373
7474 this . selectedOptions = [ ] ;
@@ -78,13 +78,13 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
7878 this . hasClasses = true ;
7979 }
8080
81- public get filteredOptions ( ) : T [ ] {
81+ public get filteredOptions ( ) :T [ ] {
8282 if ( this . maxSelectedReached ) {
8383 // If we have reached the maximum number of selections, then empty the results completely.
8484 return [ ] ;
8585 }
8686
87- const searchResults : T [ ] = this . searchService . results ;
87+ const searchResults :T [ ] = this . searchService . results ;
8888
8989 if ( ! this . hasLabels ) {
9090 return searchResults ;
@@ -95,53 +95,66 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
9595 }
9696 }
9797
98- public get availableOptions ( ) : T [ ] {
98+ public get availableOptions ( ) :T [ ] {
9999 return this . filteredOptions ;
100100 }
101101
102- private _hasLabels : boolean ;
102+ private _hasLabels :boolean ;
103103
104104 @Input ( )
105- public get hasLabels ( ) : boolean {
105+ public get hasLabels ( ) :boolean {
106106 return this . _hasLabels ;
107107 }
108108
109- public set hasLabels ( hasLabels : boolean ) {
109+ public set hasLabels ( hasLabels :boolean ) {
110110 this . _hasLabels = hasLabels ;
111111 }
112112
113- private _placeholder : string ;
113+ private _showCountText : string ;
114114
115115 @Input ( )
116- public get placeholder ( ) : string {
116+ public get showCountText ( ) :string {
117+ return this . _showCountText ;
118+ }
119+
120+ public set showCountText ( showCountText :string ) {
121+ this . _showCountText = showCountText ;
122+ }
123+
124+
125+ private _placeholder :string ;
126+
127+ @Input ( )
128+ public get placeholder ( ) :string {
117129 return this . _placeholder || this . localeValues . multi . placeholder ;
118130 }
119131
120- public set placeholder ( placeholder : string ) {
132+ public set placeholder ( placeholder :string ) {
121133 this . _placeholder = placeholder ;
122134 }
123135
124- public get maxSelectedReached ( ) : boolean {
136+ public get maxSelectedReached ( ) :boolean {
125137 if ( this . maxSelected == undefined ) {
126138 // If there is no maximum then we can immediately return.
127139 return false ;
128140 }
129141 return this . selectedOptions . length === this . maxSelected ;
130142 }
131143
132- public get maxSelectedMessage ( ) : string {
144+ public get maxSelectedMessage ( ) :string {
133145 return this . _localizationService . interpolate (
134146 this . localeValues . multi . maxSelectedMessage ,
135147 [ [ "max" , this . maxSelected . toString ( ) ] ] ) ;
136148 }
137149
138- public get selectedMessage ( ) : string {
150+ public get selectedMessage ( ) :string {
139151 return this . _localizationService . interpolate (
140152 this . localeValues . multi . selectedMessage ,
141- [ [ "count" , this . selectedOptions . length . toString ( ) ] ] ) ;
153+ [ [ "count" , this . selectedOptions . length . toString ( ) === "0" && this . _showCountText ?
154+ this . _showCountText : this . selectedOptions . length . toString ( ) ] ] ) ;
142155 }
143156
144- public selectOption ( option : T ) : void {
157+ public selectOption ( option :T ) :void {
145158 if ( this . selectedOptions . indexOf ( option ) !== - 1 ) {
146159 this . deselectOption ( option ) ;
147160 return ;
@@ -159,7 +172,7 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
159172 }
160173 }
161174
162- public writeValue ( values : U [ ] ) : void {
175+ public writeValue ( values :U [ ] ) :void {
163176 if ( values instanceof Array ) {
164177 if ( this . searchService . options . length > 0 ) {
165178 // If the options have already been loaded, we can immediately match the ngModel values to options.
@@ -187,7 +200,7 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
187200 }
188201 }
189202
190- public deselectOption ( option : T ) : void {
203+ public deselectOption ( option :T ) :void {
191204 // Update selected options to the previously selected options \ {option}.
192205 this . selectedOptions = this . selectedOptions . filter ( so => so !== option ) ;
193206 this . selectedOptionsChange . emit ( this . selectedOptions . map ( o => this . valueGetter ( o ) ) ) ;
@@ -200,14 +213,14 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
200213 }
201214 }
202215
203- public onQueryInputKeydown ( event : KeyboardEvent ) : void {
216+ public onQueryInputKeydown ( event :KeyboardEvent ) :void {
204217 if ( event . keyCode === KeyCode . Backspace && this . query === "" && this . selectedOptions . length > 0 ) {
205218 // Deselect the rightmost option when the user presses backspace in the search input.
206219 this . deselectOption ( this . selectedOptions [ this . selectedOptions . length - 1 ] ) ;
207220 }
208221 }
209222
210- protected optionsUpdateHook ( ) : void {
223+ protected optionsUpdateHook ( ) :void {
211224 if ( ! this . _writtenOptions && this . selectedOptions . length > 0 ) {
212225 // We need to check the options still exist.
213226 this . writeValue ( this . selectedOptions . map ( o => this . valueGetter ( o ) ) ) ;
@@ -226,7 +239,7 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
226239 }
227240 }
228241
229- protected initialiseRenderedOption ( option : SuiSelectOption < T > ) : void {
242+ protected initialiseRenderedOption ( option :SuiSelectOption < T > ) :void {
230243 super . initialiseRenderedOption ( option ) ;
231244
232245 // Boldens the item so it appears selected in the dropdown.
@@ -236,15 +249,15 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
236249
237250// Value accessor directive for the select to support ngModel.
238251@Directive ( {
239- selector : "sui-multi-select" ,
240- host : {
252+ selector : "sui-multi-select" ,
253+ host : {
241254 "(selectedOptionsChange)" : "onChange($event)" ,
242255 "(touched)" : "onTouched()"
243256 } ,
244- providers : [ customValueAccessorFactory ( SuiMultiSelectValueAccessor ) ]
245- } )
257+ providers : [ customValueAccessorFactory ( SuiMultiSelectValueAccessor ) ]
258+ } )
246259export class SuiMultiSelectValueAccessor < T , U > extends CustomValueAccessor < U [ ] , SuiMultiSelect < T , U > > {
247- constructor ( host : SuiMultiSelect < T , U > ) {
260+ constructor ( host :SuiMultiSelect < T , U > ) {
248261 super ( host ) ;
249262 }
250263}
0 commit comments