Skip to content

Commit

Permalink
fix(datepicker): set min/max dates correctly with custom adapters (#3686
Browse files Browse the repository at this point in the history
)

Fixes #3598
  • Loading branch information
maxokorokov committed Apr 17, 2020
1 parent 145259d commit 644f71f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
24 changes: 21 additions & 3 deletions src/datepicker/datepicker-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,6 @@ describe('NgbInputDatepicker', () => {
fixture.componentRef.instance.minDate = {year: 2019, month: 11, day: 14};
fixture.detectChanges();
expect(dp.minDate).toEqual({year: 2019, month: 11, day: 14});

});

it('should dynamically propagate the "maxDate" option', () => {
Expand All @@ -692,7 +691,6 @@ describe('NgbInputDatepicker', () => {
fixture.componentRef.instance.maxDate = {year: 2019, month: 12, day: 14};
fixture.detectChanges();
expect(dp.maxDate).toEqual({year: 2019, month: 12, day: 14});

});

it('should dynamically propagate the "maxDate" and "minDate" option', () => {
Expand All @@ -714,7 +712,6 @@ describe('NgbInputDatepicker', () => {

expect(dp.minDate).toEqual({year: 2019, month: 10, day: 14});
expect(dp.maxDate).toEqual({year: 2019, month: 12, day: 14});

});

it('should propagate the "outsideDays" option', () => {
Expand Down Expand Up @@ -1133,6 +1130,27 @@ describe('NgbInputDatepicker', () => {
inputDebugEl.triggerEventHandler('input', {target: {value: '2018-01-03'}});
expect(fixture.componentInstance.date).toEqual(new Date(2018, 0, 3));
});

it('should dynamically propagate the "maxDate" and "minDate" option', () => {
const fixture = createTestCmpt(`<input ngbDatepicker [minDate]="minDate" [maxDate]="maxDate">`);
const dpInput = fixture.debugElement.query(By.directive(NgbInputDatepicker)).injector.get(NgbInputDatepicker);
fixture.componentRef.instance.minDate = {year: 2019, month: 11, day: 14};
fixture.componentRef.instance.maxDate = {year: 2019, month: 12, day: 31};

dpInput.open();
fixture.detectChanges();

const dp = fixture.debugElement.query(By.css('ngb-datepicker')).injector.get(NgbDatepicker);
expect(dp.minDate).toEqual({year: 2019, month: 11, day: 14});
expect(dp.maxDate).toEqual({year: 2019, month: 12, day: 31});

fixture.componentRef.instance.minDate = {year: 2019, month: 10, day: 14};
fixture.componentRef.instance.maxDate = {year: 2019, month: 12, day: 14};
fixture.detectChanges();

expect(dp.minDate).toEqual({year: 2019, month: 10, day: 14});
expect(dp.maxDate).toEqual({year: 2019, month: 12, day: 14});
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,10 @@ export class NgbInputDatepicker implements OnChanges,

if (this.isOpen()) {
if (changes['minDate']) {
this._cRef !.instance.minDate = this._dateAdapter.toModel(changes.minDate.currentValue);
this._cRef !.instance.minDate = this.minDate;
}
if (changes['maxDate']) {
this._cRef !.instance.maxDate = this._dateAdapter.toModel(changes.maxDate.currentValue);
this._cRef !.instance.maxDate = this.maxDate;
}
this._cRef !.instance.ngOnChanges(changes);
}
Expand Down

0 comments on commit 644f71f

Please sign in to comment.