Skip to content

Commit

Permalink
feat(datepicker) New input pickerInitialDate
Browse files Browse the repository at this point in the history
- New input (optional) that sets the intial date to display (null = today)
- Updated demo page

Partially addresses edcarroll#165
  • Loading branch information
harogaston committed Aug 6, 2017
1 parent e06b386 commit a304463
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions demo/src/app/pages/modules/datepicker/datepicker.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export class DatepickerPage {
"<code>date</code>, <code>time</code>, <code>month</code> & <code>year</code>.",
defaultValue: "datetime"
},
{
name: "pickerInitialDate",
type: "Date",
description: "Sets the intial date to display (null = today)."
},
{
name: "pickerMaxDate",
type: "Date",
Expand Down
21 changes: 18 additions & 3 deletions src/modules/datepicker/directives/datepicker.directive.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,14 +11,15 @@ 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]",
providers: [customValidatorFactory(SuiDatepickerDirective)]
})
export class SuiDatepickerDirective
extends SuiPopupComponentController<SuiDatepicker>
implements ICustomValueAccessorHost<Date>, ICustomValidatorHost, OnChanges, PopupAfterOpen {
implements ICustomValueAccessorHost<Date>, ICustomValidatorHost, OnChanges, PopupAfterOpen, OnInit {

private _selectedDate?:Date;

Expand Down Expand Up @@ -63,6 +63,9 @@ export class SuiDatepickerDirective
this.writeValue(this.selectedDate);
}

@Input("pickerInitialDate")
public initialDate?:Date;

@Input("pickerMaxDate")
public maxDate?:Date;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a304463

Please sign in to comment.