Skip to content

Commit

Permalink
fix(datepicker): consider empty string inputs as valid
Browse files Browse the repository at this point in the history
Fixes #1588

Closes #1637
  • Loading branch information
pkozlowski-opensource committed Jun 30, 2017
1 parent b0e597e commit 95d1668
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/datepicker/datepicker-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ describe('NgbInputDatepicker', () => {
tick();
expect(form.control.invalid).toBeTruthy();
}));

it('should consider empty strings as valid', fakeAsync(() => {
const fixture = createTestCmpt(`<form><input ngbDatepicker [(ngModel)]="date" name="dp"></form>`);
const inputDebugEl = fixture.debugElement.query(By.css('input'));
const form = fixture.debugElement.query(By.directive(NgForm)).injector.get(NgForm);

inputDebugEl.triggerEventHandler('change', {target: {value: '2016-09-10'}});
fixture.detectChanges();
tick();
expect(form.control.valid).toBeTruthy();

inputDebugEl.triggerEventHandler('change', {target: {value: ''}});
fixture.detectChanges();
tick();
expect(form.control.valid).toBeTruthy();
}));
});

});
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 @@ -186,7 +186,7 @@ export class NgbInputDatepicker implements OnChanges,

manualDateChange(value: string) {
this._model = this._service.toValidDate(this._parserFormatter.parse(value), null);
this._onChange(this._model ? {year: this._model.year, month: this._model.month, day: this._model.day} : value);
this._onChange(this._model ? this._model.toStruct() : (value === '' ? null : value));
this._writeModelValue(this._model);
}

Expand Down
2 changes: 2 additions & 0 deletions src/datepicker/ngb-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ export class NgbDate {
}
}

toStruct() { return {year: this.year, month: this.month, day: this.day}; }

toString() { return `${this.year}-${this.month}-${this.day}`; }
}

0 comments on commit 95d1668

Please sign in to comment.