1
- import { Component , Optional , ElementRef , Renderer , Input , Output , EventEmitter , HostListener , ContentChildren , QueryList } from 'angular2/core' ;
2
- import { NgControl } from 'angular2/common' ;
1
+ import { Component , Optional , ElementRef , Renderer , Input , Output , Provider , forwardRef , EventEmitter , HostListener , ContentChildren , QueryList } from 'angular2/core' ;
2
+ import { NgControl , NG_VALUE_ACCESSOR } from 'angular2/common' ;
3
3
4
4
import { Alert } from '../alert/alert' ;
5
5
import { Form } from '../../util/form' ;
@@ -8,6 +8,10 @@ import {merge, isTrueProperty, isBlank} from '../../util/util';
8
8
import { NavController } from '../nav/nav-controller' ;
9
9
import { Option } from '../option/option' ;
10
10
11
+ const SELECT_VALUE_ACCESSOR = new Provider (
12
+ NG_VALUE_ACCESSOR , { useExisting : forwardRef ( ( ) => Select ) , multi : true } ) ;
13
+
14
+
11
15
/**
12
16
* @name Select
13
17
* @description
@@ -110,7 +114,8 @@ import {Option} from '../option/option';
110
114
'</button>' ,
111
115
host : {
112
116
'[class.select-disabled]' : '_disabled'
113
- }
117
+ } ,
118
+ providers : [ SELECT_VALUE_ACCESSOR ]
114
119
} )
115
120
export class Select {
116
121
private _disabled : any = false ;
@@ -120,6 +125,7 @@ export class Select {
120
125
private _values : Array < string > = [ ] ;
121
126
private _texts : Array < string > = [ ] ;
122
127
private _text : string = '' ;
128
+ private _fn : Function ;
123
129
124
130
/**
125
131
* @private
@@ -128,13 +134,13 @@ export class Select {
128
134
129
135
/**
130
136
* @private
131
- * @input {string} The text of the cancel button. Defatuls to 'cancel'
137
+ * @input {string} The text of the cancel button. Defatuls to `Cancel`
132
138
*/
133
139
@Input ( ) cancelText : string = 'Cancel' ;
134
140
135
141
/**
136
142
* @private
137
- * @input {string} The text of the ok button. Defatuls to 'OK'
143
+ * @input {string} The text of the ok button. Defatuls to `OK`
138
144
*/
139
145
@Input ( ) okText : string = 'OK' ;
140
146
@@ -164,15 +170,10 @@ export class Select {
164
170
private _elementRef : ElementRef ,
165
171
private _renderer : Renderer ,
166
172
@Optional ( ) private _item : Item ,
167
- @Optional ( ) private _nav : NavController ,
168
- @Optional ( ) ngControl : NgControl
173
+ @Optional ( ) private _nav : NavController
169
174
) {
170
175
this . _form . register ( this ) ;
171
176
172
- if ( ngControl ) {
173
- ngControl . valueAccessor = this ;
174
- }
175
-
176
177
if ( _item ) {
177
178
this . id = 'sel-' + _item . registerInput ( 'select' ) ;
178
179
this . _labelId = 'lbl-' + _item . id ;
@@ -238,7 +239,6 @@ export class Select {
238
239
alert . addButton ( {
239
240
text : this . okText ,
240
241
handler : selectedValues => {
241
- this . value = selectedValues ;
242
242
this . onChange ( selectedValues ) ;
243
243
this . change . emit ( selectedValues ) ;
244
244
}
@@ -252,30 +252,15 @@ export class Select {
252
252
* @input {boolean} Whether or not the select component can accept multipl selections
253
253
*/
254
254
@Input ( )
255
- get multiple ( ) {
255
+ get multiple ( ) : any {
256
256
return this . _multi ;
257
257
}
258
258
259
- set multiple ( val ) {
259
+ set multiple ( val : any ) {
260
260
this . _multi = isTrueProperty ( val ) ;
261
261
}
262
262
263
263
264
- /**
265
- * @private
266
- */
267
- @Input ( )
268
- get value ( ) : any {
269
- return ( this . _multi ? this . _values : this . _values . join ( ) ) ;
270
- }
271
-
272
- set value ( val : any ) {
273
- // passed in value could be either an array, undefined or a string
274
- this . _values = ( Array . isArray ( val ) ? val : isBlank ( val ) ? [ ] : [ val ] ) ;
275
- this . updateOptions ( ) ;
276
- }
277
-
278
-
279
264
/**
280
265
* @private
281
266
*/
@@ -296,13 +281,13 @@ export class Select {
296
281
this . _values = val . toArray ( ) . filter ( o => o . checked ) . map ( o => o . value ) ;
297
282
}
298
283
299
- this . updateOptions ( ) ;
284
+ this . _updOpts ( ) ;
300
285
}
301
286
302
287
/**
303
288
* @private
304
289
*/
305
- private updateOptions ( ) {
290
+ private _updOpts ( ) {
306
291
this . _texts = [ ] ;
307
292
308
293
if ( this . _options ) {
@@ -318,20 +303,6 @@ export class Select {
318
303
this . _text = this . _texts . join ( ', ' ) ;
319
304
}
320
305
321
-
322
- /**
323
- * @private
324
- */
325
- ngAfterContentInit ( ) {
326
- // using a setTimeout here to prevent
327
- // "has changed after it was checked" error
328
- // this will be fixed in future ng2 versions
329
- setTimeout ( ( ) => {
330
- this . onChange ( this . _values ) ;
331
- } ) ;
332
- }
333
-
334
-
335
306
/**
336
307
* @input {boolean} Whether or not the select component is disabled or not
337
308
*/
@@ -347,40 +318,39 @@ export class Select {
347
318
348
319
/**
349
320
* @private
350
- * Angular2 Forms API method called by the model (Control) on change to update
351
- * the checked value.
352
- * https://github.com/angular/angular/blob/master/modules/angular2/src/forms/directives/shared.ts#L34
353
321
*/
354
- writeValue ( val ) {
355
- this . value = val ;
322
+ writeValue ( val : any ) {
323
+ this . _values = ( Array . isArray ( val ) ? val : isBlank ( val ) ? [ ] : [ val ] ) ;
324
+ this . _updOpts ( ) ;
356
325
}
357
326
358
327
/**
359
328
* @private
360
329
*/
361
- onChange ( val ) { }
330
+ registerOnChange ( fn : Function ) : void {
331
+ this . _fn = fn ;
332
+ this . onChange = ( val : any ) => {
333
+ console . debug ( 'select, onChange' , val ) ;
334
+ fn ( val ) ;
335
+ this . _values = ( Array . isArray ( val ) ? val : isBlank ( val ) ? [ ] : [ val ] ) ;
336
+ this . _updOpts ( ) ;
337
+ } ;
338
+ }
362
339
363
340
/**
364
341
* @private
365
342
*/
366
- onTouched ( val ) { }
343
+ registerOnTouched ( fn ) { this . onTouched = fn ; }
367
344
368
345
/**
369
346
* @private
370
- * Angular2 Forms API method called by the view (NgControl) to register the
371
- * onChange event handler that updates the model (Control).
372
- * https://github.com/angular/angular/blob/master/modules/angular2/src/forms/directives/shared.ts#L27
373
- * @param {Function } fn the onChange event handler.
374
347
*/
375
- registerOnChange ( fn ) { this . onChange = fn ; }
348
+ onChange ( _ ) { }
376
349
377
350
/**
378
351
* @private
379
- * Angular2 Forms API method called by the the view (NgControl) to register
380
- * the onTouched event handler that marks model (Control) as touched.
381
- * @param {Function } fn onTouched event handler.
382
352
*/
383
- registerOnTouched ( fn ) { this . onTouched = fn ; }
353
+ onTouched ( ) { }
384
354
385
355
/**
386
356
* @private
0 commit comments