fix(datepicker): update validation when switching from null to error input (#14423)

* fix(datepicker): update validation when switching from null to error
input

* add test
This commit is contained in:
mmalerba
2019-01-25 09:08:57 -08:00
committed by Andrew Seguin
parent 5a18ea61e5
commit 5aefe60186
2 changed files with 34 additions and 1 deletions
+2
View File
@@ -329,6 +329,8 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
} else {
this._validatorOnChange();
}
}
+32 -1
View File
@@ -11,7 +11,7 @@ import {
} from '@angular/cdk/testing';
import {Component, FactoryProvider, Type, ValueProvider, ViewChild} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
import {MAT_DATE_LOCALE, MatNativeDateModule, NativeDateModule} from '@angular/material/core';
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
import {DEC, JAN, JUL, JUN, SEP} from '@angular/material/testing';
@@ -1189,6 +1189,36 @@ describe('MatDatepicker', () => {
expect(fixture.debugElement.query(By.css('input')).nativeElement.classList)
.not.toContain('ng-invalid');
}));
it('should update validity when switching between null and invalid', fakeAsync(() => {
const inputEl = fixture.debugElement.query(By.css('input')).nativeElement;
inputEl.value = '';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.model.valid).toBe(true);
inputEl.value = 'abcdefg';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.model.valid).toBe(false);
inputEl.value = '';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(testComponent.model.valid).toBe(true);
}));
});
describe('datepicker with filter and validation', () => {
@@ -1805,6 +1835,7 @@ class FormFieldDatepicker {
})
class DatepickerWithMinAndMaxValidation {
@ViewChild('d') datepicker: MatDatepicker<Date>;
@ViewChild(NgModel) model: NgModel;
date: Date | null;
minDate = new Date(2010, JAN, 1);
maxDate = new Date(2020, JAN, 1);