Skip to content

fix(datepicker): not marking as dirty when invalid value is typed in #19730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/material/datepicker/datepicker-input-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,16 @@ export abstract class MatDatepickerInputBase<S, D = ExtractDateTypeFromSelection
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
} else if (lastValueWasValid !== this._lastValueValid) {
this._validatorOnChange();
} else {
// Call the CVA change handler for invalid values
// since this is what marks the control as dirty.
if (value && !this.value) {
this._cvaOnChange(date);
}

if (lastValueWasValid !== this._lastValueValid) {
this._validatorOnChange();
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/material/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,18 @@ describe('MatDatepicker', () => {
expect(inputEl.classList).toContain('ng-dirty');
}));

it('should mark input dirty after invalid value is typed in', () => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;

expect(inputEl.classList).toContain('ng-pristine');

inputEl.value = 'hello there';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();

expect(inputEl.classList).toContain('ng-dirty');
});

it('should not mark dirty after model change', fakeAsync(() => {
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;

Expand Down Expand Up @@ -1504,6 +1516,20 @@ describe('MatDatepicker', () => {
expect(valueDuringChangeEvent).toBe('1/1/2020');
});

it('should not fire dateInput when typing an invalid value', () => {
expect(testComponent.onDateInput).not.toHaveBeenCalled();

inputEl.value = 'a';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
expect(testComponent.onDateInput).not.toHaveBeenCalled();

inputEl.value = 'b';
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
expect(testComponent.onDateInput).not.toHaveBeenCalled();
});

});

describe('with ISO 8601 strings as input', () => {
Expand Down