Skip to content

Commit 5776f76

Browse files
committed
fix(base-input): inputUpdated() is triggered when initialized
1 parent c7beb8c commit 5776f76

File tree

5 files changed

+53
-49
lines changed

5 files changed

+53
-49
lines changed

src/components/datetime/datetime.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterContentInit, Component, ElementRef, EventEmitter, forwardRef, HostListener, Input, OnDestroy, Optional, Output, Renderer, ViewEncapsulation } from '@angular/core';
1+
import { AfterViewInit, Component, ElementRef, EventEmitter, forwardRef, HostListener, Input, OnDestroy, Optional, Output, Renderer, ViewEncapsulation } from '@angular/core';
22
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
33

44
import { Config } from '../../config/config';
@@ -273,7 +273,7 @@ export const DATETIME_VALUE_ACCESSOR: any = {
273273
providers: [DATETIME_VALUE_ACCESSOR],
274274
encapsulation: ViewEncapsulation.None,
275275
})
276-
export class DateTime extends BaseInput<DateTimeData> implements AfterContentInit, ControlValueAccessor, OnDestroy {
276+
export class DateTime extends BaseInput<DateTimeData> implements AfterViewInit, ControlValueAccessor, OnDestroy {
277277

278278
_text: string = '';
279279
_min: DateTimeData;
@@ -431,16 +431,15 @@ export class DateTime extends BaseInput<DateTimeData> implements AfterContentIni
431431
/**
432432
* @hidden
433433
*/
434-
ngAfterContentInit() {
434+
ngAfterViewInit() {
435435
// first see if locale names were provided in the inputs
436436
// then check to see if they're in the config
437437
// if neither were provided then it will use default English names
438438
['monthNames', 'monthShortNames', 'dayNames', 'dayShortNames'].forEach(type => {
439439
(<any>this)._locale[type] = convertToArrayOfStrings(isPresent((<any>this)[type]) ? (<any>this)[type] : this._config.get(type), type);
440440
});
441441

442-
// update how the datetime value is displayed as formatted text
443-
this.updateText();
442+
this._initialize();
444443
}
445444

446445
/**

src/components/datetime/test/datetime.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,23 @@ describe('DateTime', () => {
144144
describe('writeValue', () => {
145145

146146
it('should updateText with default MMM D, YYYY when no displayFormat or pickerFormat', zoned(() => {
147-
datetime.ngAfterContentInit();
147+
datetime.ngAfterViewInit();
148148
datetime.writeValue('1994-12-15T13:47:20.789Z');
149149

150150
expect(datetime._text).toEqual('Dec 15, 1994');
151151
}));
152152

153153
it('should updateText with pickerFormat when no displayFormat', zoned(() => {
154154
datetime.pickerFormat = 'YYYY';
155-
datetime.ngAfterContentInit();
155+
datetime.ngAfterViewInit();
156156
datetime.writeValue('1994-12-15T13:47:20.789Z');
157157

158158
expect(datetime._text).toEqual('1994');
159159
}));
160160

161161
it('should updateText with displayFormat when no pickerFormat', zoned(() => {
162162
datetime.displayFormat = 'YYYY';
163-
datetime.ngAfterContentInit();
163+
datetime.ngAfterViewInit();
164164
datetime.writeValue('1994-12-15T13:47:20.789Z');
165165

166166
expect(datetime._text).toEqual('1994');
@@ -172,7 +172,7 @@ describe('DateTime', () => {
172172

173173
it('should generate with default MMM D, YYYY when no displayFormat or pickerFormat', zoned(() => {
174174
datetime.monthShortNames = customLocale.monthShortNames;
175-
datetime.ngAfterContentInit();
175+
datetime.ngAfterViewInit();
176176
datetime.setValue('1994-12-15T13:47:20.789Z');
177177

178178
datetime.generate();
@@ -186,7 +186,7 @@ describe('DateTime', () => {
186186

187187
it('should generate with only displayFormat', zoned(() => {
188188
datetime.monthShortNames = customLocale.monthShortNames;
189-
datetime.ngAfterContentInit();
189+
datetime.ngAfterViewInit();
190190
datetime.displayFormat = 'YYYY';
191191
datetime.setValue('1994-12-15T13:47:20.789Z');
192192

@@ -199,7 +199,7 @@ describe('DateTime', () => {
199199

200200
it('should generate with only pickerFormat', zoned(() => {
201201
datetime.monthShortNames = customLocale.monthShortNames;
202-
datetime.ngAfterContentInit();
202+
datetime.ngAfterViewInit();
203203
datetime.pickerFormat = 'YYYY';
204204
datetime.setValue('1994-12-15T13:47:20.789Z');
205205

@@ -212,7 +212,7 @@ describe('DateTime', () => {
212212

213213
it('should generate with custom locale short month names from input property', zoned(() => {
214214
datetime.monthShortNames = customLocale.monthShortNames;
215-
datetime.ngAfterContentInit();
215+
datetime.ngAfterViewInit();
216216
datetime.pickerFormat = 'MMM YYYY';
217217
datetime.setValue('1994-12-15T13:47:20.789Z');
218218

@@ -227,7 +227,7 @@ describe('DateTime', () => {
227227

228228
it('should generate with custom locale full month names from input property', zoned(() => {
229229
datetime.monthNames = customLocale.monthNames;
230-
datetime.ngAfterContentInit();
230+
datetime.ngAfterViewInit();
231231
datetime.pickerFormat = 'MMMM YYYY';
232232
datetime.setValue('1994-12-15T13:47:20.789Z');
233233

src/components/select/select.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,6 @@ export class Select extends BaseInput<string[]> implements AfterViewInit, OnDest
211211
super(config, elementRef, renderer, 'select', [], form, item, null);
212212
}
213213

214-
215-
ngAfterContentInit() {
216-
this._inputUpdated();
217-
}
218-
219214
@HostListener('click', ['$event'])
220215
_click(ev: UIEvent) {
221216
if (ev.detail === 0) {
@@ -405,14 +400,14 @@ export class Select extends BaseInput<string[]> implements AfterViewInit, OnDest
405400
set options(val: QueryList<Option>) {
406401
this._options = val;
407402

408-
if (this._value.length === 0) {
403+
if (this._value.length === 0) {
409404
// there are no values set at this point
410405
// so check to see who should be selected
411406
// we use writeValue() because we don't want to update ngModel
412407
this.writeValue(val.filter(o => o.selected).map(o => o.value));
408+
} else {
409+
this._inputUpdated();
413410
}
414-
415-
this._inputUpdated();
416411
}
417412

418413
_inputNormalize(val: any): string[] {

src/util/base-input.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ export class BaseInput<T> extends Ion implements CommonInput<T> {
147147
console.debug('BaseInput: value changed:', normalized, this);
148148
this._value = normalized;
149149
this._inputCheckHasValue(normalized);
150-
this._inputUpdated();
150+
if (this._init) {
151+
this._inputUpdated();
152+
}
151153
return true;
152154
}
153155

@@ -186,6 +188,9 @@ export class BaseInput<T> extends Ion implements CommonInput<T> {
186188
return;
187189
}
188190
this._init = true;
191+
if (isPresent(this._value)) {
192+
this._inputUpdated();
193+
}
189194
}
190195

191196
/**
@@ -195,6 +200,7 @@ export class BaseInput<T> extends Ion implements CommonInput<T> {
195200
if (this._isFocus) {
196201
return;
197202
}
203+
assert(this._init, 'component was not initialized');
198204
assert(NgZone.isInAngularZone(), '_fireFocus: should be zoned');
199205

200206
this._isFocus = true;
@@ -209,6 +215,7 @@ export class BaseInput<T> extends Ion implements CommonInput<T> {
209215
if (!this._isFocus) {
210216
return;
211217
}
218+
assert(this._init, 'component was not initialized');
212219
assert(NgZone.isInAngularZone(), '_fireBlur: should be zoned');
213220

214221
this._isFocus = false;
@@ -283,5 +290,7 @@ export class BaseInput<T> extends Ion implements CommonInput<T> {
283290
/**
284291
* @hidden
285292
*/
286-
_inputUpdated() {}
293+
_inputUpdated() {
294+
assert(this._init, 'component should be initialized');
295+
}
287296
}

src/util/input-tester.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,33 @@ function testState<T>(input: BaseInput<T>, config: TestConfig, isInit: boolean)
7474
assertEqual(input.isFocus(), false, 'should not be focus');
7575
assertEqual(input.value, config.defaultValue, 'default value is wrong');
7676

77-
let blurCount = 0;
78-
let focusCount = 0;
79-
const subBlur = input.ionBlur.subscribe((ev: any) => {
80-
assertEqual(ev, input, 'ionBlur argument is wrong');
81-
blurCount++;
82-
});
83-
const subFocus = input.ionFocus.subscribe((ev: any) => {
84-
assertEqual(ev, input, 'ionFocus argument is wrong');
85-
focusCount++;
86-
});
87-
input._fireFocus();
88-
assertEqual(input._isFocus, true, 'should be focus');
89-
assertEqual(input.isFocus(), true, 'should be focus');
90-
input._fireFocus();
91-
92-
input._fireBlur();
93-
assertEqual(input._isFocus, false, 'should be not focus');
94-
assertEqual(input.isFocus(), false, 'should be not focus');
95-
input._fireBlur(); // it should not crash
96-
97-
assertEqual(focusCount, 1, 'ionFocus was not called correctly');
98-
assertEqual(blurCount, 1, 'ionBlur was not called correctly');
99-
100-
subBlur.unsubscribe();
101-
subFocus.unsubscribe();
77+
if (isInit) {
78+
let blurCount = 0;
79+
let focusCount = 0;
80+
const subBlur = input.ionBlur.subscribe((ev: any) => {
81+
assertEqual(ev, input, 'ionBlur argument is wrong');
82+
blurCount++;
83+
});
84+
const subFocus = input.ionFocus.subscribe((ev: any) => {
85+
assertEqual(ev, input, 'ionFocus argument is wrong');
86+
focusCount++;
87+
});
88+
input._fireFocus();
89+
assertEqual(input._isFocus, true, 'should be focus');
90+
assertEqual(input.isFocus(), true, 'should be focus');
91+
input._fireFocus();
92+
93+
input._fireBlur();
94+
assertEqual(input._isFocus, false, 'should be not focus');
95+
assertEqual(input.isFocus(), false, 'should be not focus');
96+
input._fireBlur(); // it should not crash
97+
98+
assertEqual(focusCount, 1, 'ionFocus was not called correctly');
99+
assertEqual(blurCount, 1, 'ionBlur was not called correctly');
100+
101+
subBlur.unsubscribe();
102+
subFocus.unsubscribe();
103+
}
102104
}
103105

104106
function testWriteValue<T>(input: BaseInput<T>, config: TestConfig, isInit: boolean) {
@@ -150,7 +152,6 @@ function testWriteValue<T>(input: BaseInput<T>, config: TestConfig, isInit: bool
150152

151153
OnTouchedCalled = OnChangeCalled = ionChangeCalled = 0;
152154

153-
console.log(test[0], input.value);
154155
// Set same value (it should not redispatch)
155156
input.value = test[0];
156157
assertEqual(ionChangeCalled, 0, 'loop: ionChange should not be called');

0 commit comments

Comments
 (0)