Skip to content

Commit

Permalink
fix: add custom formatting to datetime picker (#918)
Browse files Browse the repository at this point in the history
* add custom formatting to datetime picker

* Change time delimiters
  • Loading branch information
jmarkowski authored and mikerodonnell89 committed Jun 17, 2019
1 parent 8c66f73 commit 8425549
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
Expand Up @@ -39,3 +39,12 @@ <h2>Null Validity</h2>
<fd-date-time-picker-allow-null-example></fd-date-time-picker-allow-null-example>
</component-example>
<code-example [code]="datetimePickerAllowNullTs" [language]="'typescript'"></code-example>

<separator></separator>
<h2>Formatting</h2>
<description>Providing a custom format for the dates is possible through providing a service.</description>
<component-example [name]="'ex5'">
<fd-datetime-format-example></fd-datetime-format-example>
</component-example>
<code-example [code]="datetimeFormatHtml" [language]="'HTML'"></code-example>
<code-example [code]="datetimeFormatTs" [language]="'typescript'"></code-example>
Expand Up @@ -11,6 +11,9 @@ import * as dateTimeProgTs from '!raw-loader!./examples/datetime-program-example

import * as dateTimePickerAllowNullTs from '!raw-loader!./examples/datetime-allow-null-example/datetime-allow-null-example.component.ts';

import * as dateTimeFormatHtml from '!raw-loader!./examples/datetime-format-example/datetime-format-example.component.html';
import * as dateTimeFormatTs from '!raw-loader!./examples/datetime-format-example/datetime-format-example.component.ts';


@Component({
selector: 'app-datetime-picker-docs',
Expand All @@ -28,6 +31,9 @@ export class DatetimePickerDocsComponent {
datetimeProgramHtml = dateTimeProgHtml;
datetimeProgramTs = dateTimeProgTs;

datetimeFormatHtml = dateTimeFormatHtml;
datetimeFormatTs = dateTimeFormatTs;

datetimePickerAllowNullTs = dateTimePickerAllowNullTs;

}
@@ -0,0 +1,3 @@
<fd-datetime-picker [(ngModel)]="this.date"></fd-datetime-picker>
<br /> <br />
Selected: {{date ? date.toLocaleString() : 'null'}}
@@ -0,0 +1,37 @@
import { Component, Injectable } from '@angular/core';
import { DateTimeFormatParser } from '../../../../../../../library/src/lib/datetime-picker/format/datetime-parser';

@Injectable()
export class DateTimeFormatExample extends DateTimeFormatParser {

public parse(value: string): Date {
value = value.replace(/[\/hms ]/g, '-');
const values: number[] = value.split('-').map(Number);
return new Date(Number(values[1] - 1), values[0], values[2], values[3], values[4], values[5]);
}

public format(date: Date): string {
const getAtLeastTwoDigits = function(num: number): string { return num < 10 ? '0' + num : String(num) };
return date.getFullYear() + '-' +
getAtLeastTwoDigits(date.getMonth() + 1) + '-' +
date.getDate() + ' ' +
getAtLeastTwoDigits(date.getHours()) + 'h' +
getAtLeastTwoDigits(date.getMinutes()) + 'm' +
getAtLeastTwoDigits(date.getSeconds()) + 's'
}
}


@Component({
selector: 'fd-datetime-format-example',
templateUrl: './datetime-format-example.component.html',
providers: [
{
provide: DateTimeFormatParser,
useClass: DateTimeFormatExample
}
]
})
export class DatetimeFormatExampleComponent {
date = new Date();
}
2 changes: 2 additions & 0 deletions docs/app/documentation/documentation.module.ts
Expand Up @@ -320,6 +320,7 @@ import { DatetimePickerAllowNullExampleComponent } from './component-docs/dateti
import { DatePickerAllowNullExampleComponent } from './component-docs/date-picker/examples/date-picker-allow-null-example.component';
import { TimeFormExampleComponent } from './component-docs/time/examples/time-form-example.component';
import { TableResponsiveExampleComponent } from './component-docs/table/examples/table-responsive-example.component';
import { DatetimeFormatExampleComponent } from './component-docs/datetime-picker/examples/datetime-format-example/datetime-format-example.component';



Expand Down Expand Up @@ -410,6 +411,7 @@ export function highlightJsFactory() {
DatetimeExampleComponent,
DatetimeNonMeridianExampleComponent,
DatetimeProgramExampleComponent,
DatetimeFormatExampleComponent,
DatetimePickerAllowNullExampleComponent,
DropdownContextualMenuExampleComponent,
DropdownDefaultExampleComponent,
Expand Down
13 changes: 8 additions & 5 deletions library/src/lib/datetime-picker/datetime-picker.component.ts
Expand Up @@ -16,6 +16,7 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Subject, Subscription } from 'rxjs';
import { TimeObject } from '../time/time-object';
import { TimeComponent } from '../time/time.component';
import { DateTimeFormatParser } from './format/datetime-parser';

/**
* The datetime picker component is an opinionated composition of the fd-popover,
Expand Down Expand Up @@ -204,7 +205,7 @@ export class DatetimePickerComponent implements OnInit, OnDestroy, ControlValueA
const previous = this.date.getTime();
this.selectedDay = d.selectedDay;
this.date = d.selectedDay.date;
this.inputFieldDate = this.date.toLocaleString();
this.inputFieldDate = this.dateTimeAdapter.format(this.date);
this.time = {hour: this.date.getHours(), minute: this.date.getMinutes(), second: this.date.getSeconds()};
if (this.date.getTime() !== previous) {
this.calendarChange.emit(this.date);
Expand Down Expand Up @@ -236,7 +237,7 @@ export class DatetimePickerComponent implements OnInit, OnDestroy, ControlValueA
inputValueChange(e): void {
let temp;
if (typeof e === 'string') {
temp = new Date(e.replace(/-/g, '/'));
temp = this.dateTimeAdapter.parse(e);
} else {
temp = new Date(e);
}
Expand All @@ -251,7 +252,7 @@ export class DatetimePickerComponent implements OnInit, OnDestroy, ControlValueA
meridianValid = false;
}

if (meridianValid && temp.toLocaleDateString() !== 'Invalid Date') {
if (meridianValid && temp && temp.toLocaleDateString() !== 'Invalid Date') {
const newValue = {hour: temp.getHours(), minute: temp.getMinutes(), second: temp.getSeconds()};
if (newValue.hour !== this.time.hour || newValue.minute !== this.time.minute || newValue.second !== this.time.second) {
this.time = newValue;
Expand Down Expand Up @@ -301,7 +302,9 @@ export class DatetimePickerComponent implements OnInit, OnDestroy, ControlValueA
}

/** @hidden */
constructor(private elRef: ElementRef) {}
constructor(private elRef: ElementRef,
private dateTimeAdapter: DateTimeFormatParser
) {}

/** @hidden */
registerOnChange(fn: (selected: any) => {void}): void {
Expand Down Expand Up @@ -334,7 +337,7 @@ export class DatetimePickerComponent implements OnInit, OnDestroy, ControlValueA
this.date.setHours(this.time.hour);
this.date.setMinutes(this.time.minute);
this.date.setSeconds(this.time.second);
this.inputFieldDate = this.date.toLocaleString();
this.inputFieldDate = this.dateTimeAdapter.format(this.date);

if (fireEvents) {
this.timeChange.emit(this.date);
Expand Down
50 changes: 50 additions & 0 deletions library/src/lib/datetime-picker/format/datetime-parser.ts
@@ -0,0 +1,50 @@
import { Injectable } from '@angular/core';

export function DATE_TIME_FORMAT_FACTORY() {
return new DateTimeFormatParserDefault();
}

/**
* Abstract class which defines the behaviour of the datetime format and parser.
*/
@Injectable({
providedIn: 'root',
useFactory: DATE_TIME_FORMAT_FACTORY
})
export abstract class DateTimeFormatParser {

/**
* Should take in a string value and return a date object.
* @param value String to concert to a date object.
*/
abstract parse(value: string): Date;

/**
* Should take in a date object and return a string representation.
* @param date String to concert to a date object.
*/
abstract format(date: Date): string;
}

/**
* Default implementation of the DateFormatParser service.
*/
@Injectable()
export class DateTimeFormatParserDefault extends DateTimeFormatParser {

/**
* Takes in a string representation of a date and returns a Date object.
* @param value String to convert to a date.
*/
public parse(value: string): Date {
return new Date(value);
}

/**
* Takes in a date object and returns the string representation.
* @param date Date object to convert to a string.
*/
public format(date: Date): string {
return date.toLocaleString();
}
}

0 comments on commit 8425549

Please sign in to comment.