From a3044633d16e1b3472a072f647c725f971a844cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Haro?= Date: Sun, 6 Aug 2017 00:52:44 -0300 Subject: [PATCH] feat(datepicker) New input pickerInitialDate - New input (optional) that sets the intial date to display (null = today) - Updated demo page Partially addresses #165 --- .../modules/datepicker/datepicker.page.ts | 5 +++++ .../directives/datepicker.directive.ts | 21 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/demo/src/app/pages/modules/datepicker/datepicker.page.ts b/demo/src/app/pages/modules/datepicker/datepicker.page.ts index 909d43636..a9d7a6421 100644 --- a/demo/src/app/pages/modules/datepicker/datepicker.page.ts +++ b/demo/src/app/pages/modules/datepicker/datepicker.page.ts @@ -94,6 +94,11 @@ export class DatepickerPage { "date, time, month & year.", defaultValue: "datetime" }, + { + name: "pickerInitialDate", + type: "Date", + description: "Sets the intial date to display (null = today)." + }, { name: "pickerMaxDate", type: "Date", diff --git a/src/modules/datepicker/directives/datepicker.directive.ts b/src/modules/datepicker/directives/datepicker.directive.ts index 448f5e47f..1bb47d494 100644 --- a/src/modules/datepicker/directives/datepicker.directive.ts +++ b/src/modules/datepicker/directives/datepicker.directive.ts @@ -1,7 +1,6 @@ - import { Directive, ElementRef, Renderer2, EventEmitter, Output, Input, - HostListener, OnChanges, SimpleChanges + HostListener, OnChanges, SimpleChanges, OnInit } from "@angular/core"; import { AbstractControl, ValidationErrors } from "@angular/forms"; import { @@ -12,6 +11,7 @@ import { IDatepickerLocaleValues, RecursivePartial, SuiLocalizationService } fro import { SuiPopupComponentController, PopupAfterOpen, PopupConfig, PopupTrigger } from "../../popup"; import { SuiDatepicker, DatepickerMode } from "../components/datepicker"; import { CalendarConfig, YearConfig, MonthConfig, DatetimeConfig, TimeConfig, DateConfig } from "../classes/calendar-config"; +import { isValid } from "date-fns"; @Directive({ selector: "[suiDatepicker]", @@ -19,7 +19,7 @@ import { CalendarConfig, YearConfig, MonthConfig, DatetimeConfig, TimeConfig, Da }) export class SuiDatepickerDirective extends SuiPopupComponentController - implements ICustomValueAccessorHost, ICustomValidatorHost, OnChanges, PopupAfterOpen { + implements ICustomValueAccessorHost, ICustomValidatorHost, OnChanges, PopupAfterOpen, OnInit { private _selectedDate?:Date; @@ -63,6 +63,9 @@ export class SuiDatepickerDirective this.writeValue(this.selectedDate); } + @Input("pickerInitialDate") + public initialDate?:Date; + @Input("pickerMaxDate") public maxDate?:Date; @@ -148,6 +151,18 @@ export class SuiDatepickerDirective } } + public ngOnInit():void { + if (this.initialDate !== undefined) { + // Need to explicitly check for null + // tslint:disable-next-line:no-null-keyword + if(this.initialDate !== null) { + this._selectedDate = isValid(this.initialDate) ? new Date(this.initialDate) : undefined; + } else { + this._selectedDate = new Date(); + } + } + } + public ngOnChanges({ maxDate, minDate, mode }:SimpleChanges):void { if (maxDate || minDate || mode) { this.onValidatorChange.emit();