Skip to content

Commit

Permalink
fix(datepicker): update minDate and maxDate dynamically (#3507)
Browse files Browse the repository at this point in the history
Propagate minDate and maxDate changes dynamically from the input datepicker to the datepicker

Fixes: #3506
  • Loading branch information
arvindm95 authored and maxokorokov committed Jan 27, 2020
1 parent 1bd5975 commit 4f61496
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/datepicker/datepicker-input.spec.ts
Expand Up @@ -639,6 +639,62 @@ describe('NgbInputDatepicker', () => {
expect(dp.maxDate).toEqual({year: 2016, month: 9, day: 13});
});

it('should dynamically propagate the "minDate" option', () => {
const fixture = createTestCmpt(`<input ngbDatepicker [minDate]="minDate">`);
const dpInput = fixture.debugElement.query(By.directive(NgbInputDatepicker)).injector.get(NgbInputDatepicker);
fixture.componentRef.instance.minDate = {year: 2018, 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: 2018, month: 12, day: 31});

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', () => {
const fixture = createTestCmpt(`<input ngbDatepicker [maxDate]="maxDate">`);
const dpInput = fixture.debugElement.query(By.directive(NgbInputDatepicker)).injector.get(NgbInputDatepicker);
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.maxDate).toEqual({year: 2019, month: 12, day: 31});

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', () => {
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});

});

it('should propagate the "outsideDays" option', () => {
const fixture = createTestCmpt(`<input ngbDatepicker outsideDays="collapsed">`);
const dpInput = fixture.debugElement.query(By.directive(NgbInputDatepicker)).injector.get(NgbInputDatepicker);
Expand Down Expand Up @@ -1079,6 +1135,8 @@ class TestNativeComponent {
class TestComponent {
container;
date: NgbDateStruct;
minDate: NgbDateStruct;
maxDate: NgbDateStruct;
isDisabled;

onNavigate() {}
Expand Down
10 changes: 10 additions & 0 deletions src/datepicker/datepicker-input.ts
Expand Up @@ -432,6 +432,16 @@ export class NgbInputDatepicker implements OnChanges,
ngOnChanges(changes: SimpleChanges) {
if (changes['minDate'] || changes['maxDate']) {
this._validatorChange();

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

Expand Down

0 comments on commit 4f61496

Please sign in to comment.