Skip to content

Commit

Permalink
feat(datepicker): infer startDate from model value
Browse files Browse the repository at this point in the history
Closes #843

Closes #844
  • Loading branch information
pkozlowski-opensource committed Oct 6, 2016
1 parent 91c6054 commit 4fc52c1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/datepicker/datepicker-input.spec.ts
Expand Up @@ -9,6 +9,7 @@ import {NgbDatepickerModule} from './datepicker.module';
import {NgbInputDatepicker} from './datepicker-input';
import {NgbDatepicker} from './datepicker';
import {NgbDateStruct} from './ngb-date-struct';
import {NgbDate} from './ngb-date';

const createTestCmpt = (html: string) =>
createGenericTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
Expand Down Expand Up @@ -212,6 +213,19 @@ describe('NgbInputDatepicker', () => {
const dp = fixture.debugElement.query(By.css('ngb-datepicker')).injector.get(NgbDatepicker);
expect(dp.startDate).toEqual({year: 2016, month: 9, day: 13});
});

it('should propagate model as "startDate" option when "startDate" not provided', fakeAsync(() => {
const fixture = createTestCmpt(`<input ngbDatepicker [ngModel]="{year: 2016, month: 9, day: 13}">`);
const dpInput = fixture.debugElement.query(By.directive(NgbInputDatepicker)).injector.get(NgbInputDatepicker);

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

const dp = fixture.debugElement.query(By.css('ngb-datepicker')).injector.get(NgbDatepicker);
expect(dp.startDate).toEqual(NgbDate.from({year: 2016, month: 9, day: 13}));
}));
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/datepicker/datepicker-input.ts
Expand Up @@ -187,12 +187,13 @@ export class NgbInputDatepicker implements ControlValueAccessor {

private _applyDatepickerInputs(datepickerInstance: NgbDatepicker): void {
['dayTemplate', 'firstDayOfWeek', 'markDisabled', 'minDate', 'maxDate', 'showNavigation', 'showWeekdays',
'showWeekNumbers', 'startDate']
'showWeekNumbers']
.forEach((optionName: string) => {
if (this[optionName] !== undefined) {
datepickerInstance[optionName] = this[optionName];
}
});
datepickerInstance.startDate = this.startDate || this._model;
}

private _applyPopupStyling(nativeElement: any) {
Expand Down

0 comments on commit 4fc52c1

Please sign in to comment.