Skip to content

Commit

Permalink
fix(datepicker): update model correctly with updateOn: 'blur'
Browse files Browse the repository at this point in the history
  • Loading branch information
maxokorokov committed Jan 23, 2019
1 parent eee0afb commit 8f90512
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/datepicker/datepicker-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,28 @@ describe('NgbInputDatepicker', () => {

expect(inputDebugEl.classes['ng-touched']).toBeTruthy();
}));

it('should update model with updateOnBlur when selecting a date', fakeAsync(() => {
const fixture = createTestCmpt(`
<input ngbDatepicker [startDate]="{year: 2018, month: 3}" [(ngModel)]="date" #d="ngbDatepicker"
[ngModelOptions]="{updateOn: 'blur'}">`);

const inputDebugEl = fixture.debugElement.query(By.css('input'));
const dpInput = fixture.debugElement.query(By.directive(NgbInputDatepicker)).injector.get(NgbInputDatepicker);

// open
dpInput.open();
fixture.detectChanges();
expect(inputDebugEl.classes['ng-touched']).toBeFalsy();
expect(fixture.componentInstance.date).toBeUndefined();

// select date
fixture.nativeElement.querySelectorAll('.ngb-dp-day')[3].click(); // 1 MAR 2018
fixture.detectChanges();
expect(fixture.componentInstance.date).toEqual({year: 2018, month: 3, day: 1});
expect(inputDebugEl.nativeElement.value).toBe('2018-03-01');
expect(inputDebugEl.classes['ng-touched']).toBeTruthy();
}));
});

describe('manual data entry', () => {
Expand Down
1 change: 1 addition & 0 deletions src/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export class NgbInputDatepicker implements OnChanges,
this._cRef.instance.registerOnChange((selectedDate) => {
this.writeValue(selectedDate);
this._onChange(selectedDate);
this._onTouched();
});

this._cRef.changeDetectorRef.detectChanges();
Expand Down

0 comments on commit 8f90512

Please sign in to comment.