Skip to content

Commit

Permalink
feat(datepicker) Added initial date support for the datepicker (#216)
Browse files Browse the repository at this point in the history
* feat(datepicker) New input pickerInitialDate

- New input (optional) that sets the intial date to display (null = today)
- Updated demo page

Partially addresses #165

* Now pickerInitialDate only sets CalendarService.currentDate property

* feat(datepicker): Initial date support
- Code formatting
  • Loading branch information
harogaston authored and edcarroll committed Aug 13, 2017
1 parent d16d60e commit 35f98b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 6 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,12 @@ 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 when no date is selected.",
defaultValue: "new Date()"
},
{
name: "pickerMaxDate",
type: "Date",
Expand Down
5 changes: 4 additions & 1 deletion src/modules/datepicker/directives/datepicker.directive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
Directive, ElementRef, Renderer2, EventEmitter, Output, Input,
HostListener, OnChanges, SimpleChanges
Expand Down Expand Up @@ -63,6 +62,9 @@ export class SuiDatepickerDirective
this.writeValue(this.selectedDate);
}

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

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

Expand Down Expand Up @@ -131,6 +133,7 @@ export class SuiDatepickerDirective
if (this.componentInstance) {
this.componentInstance.service.config = this.config;
this.componentInstance.service.localeValues = this.localeValues;
this.componentInstance.service.currentDate = this.initialDate || new Date();
this.componentInstance.service.selectedDate = this.selectedDate;
this.componentInstance.service.maxDate = this.maxDate;
this.componentInstance.service.minDate = this.minDate;
Expand Down
10 changes: 6 additions & 4 deletions src/modules/datepicker/services/calendar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export class CalendarService {
constructor(config:CalendarConfig, public localeValues:IDatepickerLocaleValues) {
this.config = config;

this.currentDate = new Date();

this.firstDayOfWeek = this.localeValues.firstDayOfWeek;

this.onDateChange = new EventEmitter<Date>();
Expand All @@ -101,15 +103,15 @@ export class CalendarService {
this.currentView = this.config.mappings.finalView;

if (!this._selectedDate) {
let today = new Date().getTime();
let current = this.currentDate.getTime();
if (this._minDate) {
today = Math.max(today, this._minDate.getTime());
current = Math.max(current, this._minDate.getTime());
}
if (this._maxDate) {
today = Math.min(today, this._maxDate.getTime());
current = Math.min(current, this._maxDate.getTime());
}

this.currentDate = new Date(today);
this.currentDate = new Date(current);
this.config.updateBounds(this.currentDate);

this.currentView = this.config.mappings.initialView;
Expand Down

0 comments on commit 35f98b6

Please sign in to comment.