Skip to content

Commit 3444a3c

Browse files
committed
fix(select): use value accessor provider
1 parent 966ff8f commit 3444a3c

File tree

3 files changed

+35
-67
lines changed

3 files changed

+35
-67
lines changed

ionic/components/checkbox/checkbox.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, Optional, Input, HostListener, Provider, forwardRef, Injector} from 'angular2/core';
1+
import {Component, Optional, Input, HostListener, Provider, forwardRef} from 'angular2/core';
22
import {NgControl, NG_VALUE_ACCESSOR} from 'angular2/common';
33

44
import {Form} from '../../util/form';
@@ -74,8 +74,7 @@ export class Checkbox {
7474

7575
constructor(
7676
private _form: Form,
77-
@Optional() private _item: Item,
78-
private _injector: Injector
77+
@Optional() private _item: Item
7978
) {
8079
_form.register(this);
8180

ionic/components/select/select.ts

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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';
33

44
import {Alert} from '../alert/alert';
55
import {Form} from '../../util/form';
@@ -8,6 +8,10 @@ import {merge, isTrueProperty, isBlank} from '../../util/util';
88
import {NavController} from '../nav/nav-controller';
99
import {Option} from '../option/option';
1010

11+
const SELECT_VALUE_ACCESSOR = new Provider(
12+
NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => Select), multi: true});
13+
14+
1115
/**
1216
* @name Select
1317
* @description
@@ -110,7 +114,8 @@ import {Option} from '../option/option';
110114
'</button>',
111115
host: {
112116
'[class.select-disabled]': '_disabled'
113-
}
117+
},
118+
providers: [SELECT_VALUE_ACCESSOR]
114119
})
115120
export class Select {
116121
private _disabled: any = false;
@@ -120,6 +125,7 @@ export class Select {
120125
private _values: Array<string> = [];
121126
private _texts: Array<string> = [];
122127
private _text: string = '';
128+
private _fn: Function;
123129

124130
/**
125131
* @private
@@ -128,13 +134,13 @@ export class Select {
128134

129135
/**
130136
* @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`
132138
*/
133139
@Input() cancelText: string = 'Cancel';
134140

135141
/**
136142
* @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`
138144
*/
139145
@Input() okText: string = 'OK';
140146

@@ -164,15 +170,10 @@ export class Select {
164170
private _elementRef: ElementRef,
165171
private _renderer: Renderer,
166172
@Optional() private _item: Item,
167-
@Optional() private _nav: NavController,
168-
@Optional() ngControl: NgControl
173+
@Optional() private _nav: NavController
169174
) {
170175
this._form.register(this);
171176

172-
if (ngControl) {
173-
ngControl.valueAccessor = this;
174-
}
175-
176177
if (_item) {
177178
this.id = 'sel-' + _item.registerInput('select');
178179
this._labelId = 'lbl-' + _item.id;
@@ -238,7 +239,6 @@ export class Select {
238239
alert.addButton({
239240
text: this.okText,
240241
handler: selectedValues => {
241-
this.value = selectedValues;
242242
this.onChange(selectedValues);
243243
this.change.emit(selectedValues);
244244
}
@@ -252,30 +252,15 @@ export class Select {
252252
* @input {boolean} Whether or not the select component can accept multipl selections
253253
*/
254254
@Input()
255-
get multiple() {
255+
get multiple(): any {
256256
return this._multi;
257257
}
258258

259-
set multiple(val) {
259+
set multiple(val: any) {
260260
this._multi = isTrueProperty(val);
261261
}
262262

263263

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-
279264
/**
280265
* @private
281266
*/
@@ -296,13 +281,13 @@ export class Select {
296281
this._values = val.toArray().filter(o => o.checked).map(o => o.value);
297282
}
298283

299-
this.updateOptions();
284+
this._updOpts();
300285
}
301286

302287
/**
303288
* @private
304289
*/
305-
private updateOptions() {
290+
private _updOpts() {
306291
this._texts = [];
307292

308293
if (this._options) {
@@ -318,20 +303,6 @@ export class Select {
318303
this._text = this._texts.join(', ');
319304
}
320305

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-
335306
/**
336307
* @input {boolean} Whether or not the select component is disabled or not
337308
*/
@@ -347,40 +318,39 @@ export class Select {
347318

348319
/**
349320
* @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
353321
*/
354-
writeValue(val) {
355-
this.value = val;
322+
writeValue(val: any) {
323+
this._values = (Array.isArray(val) ? val : isBlank(val) ? [] : [val]);
324+
this._updOpts();
356325
}
357326

358327
/**
359328
* @private
360329
*/
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+
}
362339

363340
/**
364341
* @private
365342
*/
366-
onTouched(val) {}
343+
registerOnTouched(fn) { this.onTouched = fn; }
367344

368345
/**
369346
* @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.
374347
*/
375-
registerOnChange(fn) { this.onChange = fn; }
348+
onChange(_) {}
376349

377350
/**
378351
* @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.
382352
*/
383-
registerOnTouched(fn) { this.onTouched = fn; }
353+
onTouched() {}
384354

385355
/**
386356
* @private

ionic/components/toggle/toggle.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ElementRef, Renderer, Input, Optional, Provider, Injector, forwardRef} from 'angular2/core';
1+
import {Component, ElementRef, Renderer, Input, Optional, Provider, forwardRef} from 'angular2/core';
22
import {NgControl, ControlValueAccessor, NG_VALUE_ACCESSOR} from 'angular2/common';
33

44
import {Form} from '../../util/form';
@@ -93,8 +93,7 @@ export class Toggle implements ControlValueAccessor {
9393
private _form: Form,
9494
private _elementRef: ElementRef,
9595
private _renderer: Renderer,
96-
@Optional() private _item: Item,
97-
private _injector: Injector
96+
@Optional() private _item: Item
9897
) {
9998
this._form.register(this);
10099

0 commit comments

Comments
 (0)