Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datepicker): reflect changes on popover when minDate, maxDate val… #3507

Merged
merged 2 commits into from Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -964,6 +1020,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 @@ -410,6 +410,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);
arvindm95 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down