Skip to content

Commit

Permalink
fix(datepicker): retain manually entered values even if invalid
Browse files Browse the repository at this point in the history
Fixes #1710

Closes #1725
  • Loading branch information
pkozlowski-opensource committed Aug 3, 2017
1 parent 38b8ffa commit 4bd81c3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/datepicker/datepicker-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,33 @@ describe('NgbInputDatepicker', () => {
}));
});

describe('manual data entry', () => {

it('should reformat value entered by a user when it is valid', fakeAsync(() => {
const fixture = createTestCmpt(`<input ngbDatepicker (ngModelChange)="date">`);
const inputDebugEl = fixture.debugElement.query(By.css('input'));

inputDebugEl.triggerEventHandler('change', {target: {value: '2016-9-1'}});
tick();
fixture.detectChanges();

expect(inputDebugEl.nativeElement.value).toBe('2016-09-01');
}));

it('should retain value entered by a user if it is not valid', fakeAsync(() => {
const fixture = createTestCmpt(`<input ngbDatepicker (ngModelChange)="date">`);
const inputDebugEl = fixture.debugElement.query(By.css('input'));

inputDebugEl.nativeElement.value = '2016-09-aa';
inputDebugEl.triggerEventHandler('change', {target: {value: inputDebugEl.nativeElement.value}});
tick();
fixture.detectChanges();

expect(inputDebugEl.nativeElement.value).toBe('2016-09-aa');
}));

});

describe('validation', () => {

describe('values set from model', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class NgbInputDatepicker implements OnChanges,
manualDateChange(value: string, updateView = false) {
this._model = this._service.toValidDate(this._parserFormatter.parse(value), null);
this._onChange(this._model ? this._model.toStruct() : (value === '' ? null : value));
if (updateView) {
if (updateView && this._model) {
this._writeModelValue(this._model);
}
}
Expand Down

0 comments on commit 4bd81c3

Please sign in to comment.