1- import { Component , Directive , Provider , ViewChild , HostBinding , ElementRef , HostListener , forwardRef } from 'angular2/core' ;
1+ import { Component , Directive , Provider , ViewChild , HostBinding , ElementRef , HostListener , OnInit , forwardRef } from 'angular2/core' ;
22import { ControlValueAccessor , NG_VALUE_ACCESSOR } from 'angular2/common' ;
33import { Search , SearchValueAccessor } from '../search' ;
4+ import { SelectOption } from './select-option.component' ;
45import { DropdownMenu } from '../dropdown' ;
56import { KEYCODE } from '../dropdown/dropdown.service' ;
67
78@Component ( {
89 selector : 'sui-select' ,
910 directives : [ DropdownMenu ] ,
10- inputs : [ 'placeholder' , 'options' , 'optionsField' , 'isSearchable' , 'searchDelay' , 'isDisabled' , 'allowMultiple' ] ,
11+ inputs : [ 'placeholder' , 'options' , 'optionsField' , 'isSearchable' , 'searchDelay' , 'isDisabled' , 'allowMultiple' , 'maxSelected' ] ,
1112 outputs : [ 'selectedOptionChange' ] ,
1213 host : {
1314 '[class.visible]' : 'isOpen' ,
@@ -28,12 +29,13 @@ import {KEYCODE} from '../dropdown/dropdown.service';
2829<!-- Select dropdown menu -->
2930<div class="menu" suiDropdownMenu>
3031 <ng-content></ng-content>
31- <div *ngIf="!results.length" class="message">No Results</div>
32+ <div *ngIf="!results.length && !maxSelectedReached" class="message">No Results</div>
33+ <div *ngIf="!results.length && maxSelectedReached" class="message">Max {{ maxSelected }} selections</div>
3234</div>
3335` ,
34- styles : [ `:host input.search { width: 12em !important; }` ]
36+ styles : [ `:host input.search { width: 12em !important; } .selected-results { display: none; } ` ]
3537} )
36- export class Select extends Search {
38+ export class Select extends Search implements OnInit {
3739 @ViewChild ( DropdownMenu ) protected _menu :DropdownMenu ;
3840
3941 @HostBinding ( 'class.ui' )
@@ -44,14 +46,18 @@ export class Select extends Search {
4446 public isSearchable :boolean = false ;
4547 @HostBinding ( 'class.multiple' )
4648 public allowMultiple :boolean = false ;
47- protected searchDelay :number = 0 ;
49+ public searchDelay :number = 0 ;
4850 @HostBinding ( 'class.loading' )
4951 protected _loading :boolean = false ;
5052 public placeholder :string = "Select one" ;
5153 public selectedOptionHTML :string ;
5254
5355 public selectedOptions :any = [ ] ;
5456 public selectedOptionsHTML :Array < string > = [ ] ;
57+ public maxSelected :number ;
58+ private maxSelectedReached :boolean = false ;
59+
60+ public renderedOptions :Array < SelectOption > = [ ] ;
5561
5662 @HostBinding ( 'class.active' )
5763 public get isOpen ( ) :boolean {
@@ -63,12 +69,17 @@ export class Select extends Search {
6369 }
6470
6571 protected get results ( ) :Array < any > {
72+ this . maxSelectedReached = false ;
6673 var results = this . options ;
6774 if ( this . isSearchable || this . _optionsLookup ) {
6875 results = this . _results ;
6976 }
7077 if ( this . allowMultiple ) {
71- results = results . filter ( r => ( this . selectedOptions || [ ] ) . indexOf ( r ) == - 1 ) ;
78+ results = results . filter ( ( r :any ) => ( this . selectedOptions || [ ] ) . indexOf ( r ) == - 1 ) ;
79+ if ( this . selectedOptions && this . maxSelected == this . selectedOptions . length ) {
80+ this . maxSelectedReached = true ;
81+ results = [ ] ;
82+ }
7283 }
7384 return results ;
7485 }
@@ -100,26 +111,33 @@ export class Select extends Search {
100111 } ) ;
101112 }
102113
103- public selectOption ( option :any , valueHTML :string ) :void {
114+ public ngOnInit ( ) :void {
115+ if ( this . isSearchable ) {
116+ //Initialise initial results
117+ this . search ( ) ;
118+ }
119+ }
120+
121+ public selectOption ( selectOption :SelectOption ) :void {
104122 if ( ! this . allowMultiple ) {
105- super . select ( option ) ;
106- this . selectedOptionHTML = valueHTML ;
123+ super . select ( selectOption . value ) ;
124+ this . selectedOptionHTML = selectOption . HTML ;
107125 }
108126 else {
109127 this . selectedOptions = this . selectedOptions || [ ] ;
110- this . selectedOptions . push ( option ) ;
111- this . selectedOptionsHTML . push ( valueHTML ) ;
128+ this . selectedOptions . push ( selectOption . value ) ;
129+ this . selectedOptionsHTML . push ( selectOption . HTML ) ;
112130
113131 this . selectedOptionChange . emit ( this . selectedOptions ) ;
114- this . onItemSelected . emit ( option ) ;
132+ this . onItemSelected . emit ( selectOption . value ) ;
115133 }
116134 if ( this . isSearchable ) {
117135 this . focusFirstItem ( ) ;
118136 this . focusSearch ( ) ;
119137 }
120138 this . _query = "" ;
121139 if ( this . isSearchable ) {
122- this . search ( ( ) => { } ) ;
140+ this . search ( ) ;
123141 }
124142 }
125143
@@ -135,7 +153,7 @@ export class Select extends Search {
135153 }
136154
137155 //noinspection JSMethodCanBeStatic
138- private selectedOptionClick ( event ) {
156+ private selectedOptionClick ( event : MouseEvent ) {
139157 event . stopPropagation ( ) ;
140158 }
141159
@@ -149,15 +167,24 @@ export class Select extends Search {
149167 setTimeout ( ( ) => {
150168 this . _service . selectedItem = null ;
151169 this . _service . selectNextItem ( ) ;
152- } )
170+ } ) ;
153171 }
154172
155173 public writeValue ( value :any ) {
156174 if ( this . allowMultiple ) {
157- this . selectedOptions = value ;
175+ //This allows all of the possible results to load in first, so we can set the innerHTML correctly without using a template.
176+ setTimeout ( ( ) => {
177+ this . selectedOptions = value ;
178+ ( this . selectedOptions || [ ] ) . forEach ( ( v :any , i :number ) => {
179+ this . selectedOptionsHTML [ i ] = this . renderedOptions . find ( ( rO :SelectOption ) => rO . value == v ) . HTML
180+ } ) ;
181+ } ) ;
158182 return ;
159183 }
160184 this . selectedOption = value ;
185+ if ( value ) {
186+ setTimeout ( ( ) => this . selectedOptionHTML = this . renderedOptions . find ( ( rO :SelectOption ) => rO . value == value ) . HTML ) ;
187+ }
161188 }
162189
163190 @HostListener ( 'click' , [ '$event' ] )
@@ -179,7 +206,7 @@ export class Select extends Search {
179206 return false ;
180207 }
181208
182- public searchKeyDown ( event ) {
209+ public searchKeyDown ( event : KeyboardEvent ) {
183210 if ( event . which == KEYCODE . BACKSPACE && ! this . _query ) {
184211 var selectedOptions = this . selectedOptions || [ ] ;
185212 var lastSelectedOption = selectedOptions [ selectedOptions . length - 1 ] ;
0 commit comments