3.0.2 • Published 3 months ago

@eqproject/eqp-datetimerangepicker v3.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

Table of contents

Required

  • Angular Material installed and imported
  • @angular-material-components/datetime-picker (^15.0.0)
  • @angular-material-components/moment-adapter (v9.0.0)
  • @angular/material-moment-adapter (15.2.9)
  • Moment.js
  • @ngx-translate/core

Getting started

This package is based on angular material and momentjs and allows to create 4 kind of date/time picker: date only, time only, both and a date range.

Notes

By default returns the seletected date converted in UTC time zone.

Step 1: Install eqp-datetimerangepicker:

NPM

npm i --save @eqproject/eqp-datetimerangepicker

If needed dependecies are not installed run this commands:

npm i @angular-material-components/datetime-picker@15.0.0
npm i --save  @angular-material-components/moment-adapter
npm i moment --save
npm i @ngx-translate/core --save
npm i @angular/material-moment-adapter --save
ng add @angular/material

Step 2:

Import EqpDatetimerangepickerModule, NgxMatDatetimePickerModule and NgxMatTimepickerModule :

import { EqpDatetimerangepickerModule } from "@eqproject/eqp-datetimerangepicker";
import { NgxMatDatetimePickerModule, NgxMatTimepickerModule } from "@angular-material-components/datetime-picker";

@NgModule({
  declarations: [AppComponent],
  imports: [EqpDatetimerangepickerModule, NgxMatDatetimePickerModule, NgxMatTimepickerModule],
  bootstrap: [AppComponent]
})
export class AppModule {}

Step 4: Invoke the loadTranslateService method

This step is mandatory as it allows eqp-datetimerangepicker to load an external TranslateService (which must reside in the project) and which will be used to change the language of the default presets in the DATE_RANGE picker.

import { EqpDatetimerangepickerModule, EqpDatetimerangepickerService } from "@eqproject/eqp-datetimerangepicker";
import { TranslateLoader, TranslateModule, TranslateService } from "@ngx-translate/core";
import { TranslateHttpLoader } from "@ngx-translate/http-loader";
import { HttpClient } from "@angular/common/http";
import { LOCALE_ID, NgModule } from "@angular/core";
import { registerLocaleData } from "@angular/common";
import localeIt from "@angular/common/locales/it";
import { MAT_DATE_LOCALE } from "@angular/material/core";
registerLocaleData(localeIt, "it-IT");

@NgModule({
  declarations: [AppComponent],
  imports: [
    EqpDatetimerangepickerModule,
    NgxMatDatetimePickerModule,
    NgxMatTimepickerModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
      },
      isolate: false
    })
  ],
  providers: [
    { provide: MAT_DATE_LOCALE, useValue: "it-IT" },
    { provide: LOCALE_ID, useValue: "it-IT" }
  ],
  bootstrap: [AppComponent]
})

export class AppModule {
  constructor(
    private eqpDateTimeRangePickerservice: EqpDatetimerangepickerService,
    private translateService: TranslateService
  ) {
    this.eqpDateTimeRangePickerservice.loadTranslateService(this.translateService);
  }
}

export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {
  return new TranslateHttpLoader(http);
}

API

Inputs

InputTypeDefaultRequiredDescription
typePickerModeEnumPickerModeEnum.DATEnoDefines the view mode of the picker. DATETIME = 1 DATE = 2 TIME = 3 DATE_RANGE=4
readonlyInputbooleanfalsenoDefines the input area as read only. if true the value can be changed only trougth the calendar.
minDateDate, nullnullnoSets the lowest date that can be inserted.
maxDateDate, nullnullnoSets the highest date that can be inserted.
isRequiredbooleanfalsenoMarks the input as required. Needed only with NgModelInput (with FormControl Validators.Required is enough)
formGroupInputFormGroup, nullnullnoFormGroup in which the eqp-datetimerangepicker is used. If not null then formControlNameInput is required.
formControlNameInputstring, nullnullnoHas effect only if formGroupInput is not null. FormControlName of the control used in the defined formGroupInput. (NOTE: use it without ngModelInput).
formControlNameInputStartstring, nullnullnoFormControlName used for the start date of the DATE_RANGE picker. (NOTE: use it instead formControlName without ngModelInput, in order to work you also need to specify formControlNameInputEnd property).
formControlNameInputEndstring, nullnullnoFormControlName used for the end date of the DATE_RANGE picker. (NOTE: use it instead formControlName without ngModelInput, in order to work you also need to specify formControlNameInputStart property).
ngModelInputDate, string, nullnullnongModel to bind the inputfor all kind of picker. (NOTE: use it instead formGroup and formControl binding)
placeholderstringDATE: "Seleziona una data", DATETIME: "Seleziona una data e un orario", TIME: "Seleziona un orario"noplaceholder viewed in case of DATE, DATETIME or TIME picker.
startPlaceholederstringDATE_RANGE: "Seleziona data inizio"noplaceholder viewed in case of DATE_RANGE picker forn the stat date. (NOTE: use it instead placeholder property, in order to work you also need to specify endPlaceholeder property).
endPlaceholederstringDATE_RANGE: "fine"noplaceholder viewed in case of DATE_RANGE picker forn the end date. (NOTE: use it instead placeholder property, in order to work you also need to specify startPlaceholeder property).
disabledbooleanfalsenoIf true, the picker is readonly and can't be modified.
showSpinnersbooleantruenoIf true, the spinners above and below input are visible
showSecondsbooleantruenoIf true, it is not possible to select seconds
disableMinutebooleanfalsenoIf true, the minute is readonly.
stepSecondnumber1noThe number of seconds to add/substract when clicking second spinners.
stepHournumber1noThe number of hours to add/substract when clicking hour spinners.
stepMinutenumber1noThe number of minutes to add/substract when clicking minute spinners.
colorThemePaletteundefinednoColor palette to use on the datepicker's calendar.
enableMeridianbooleanfalsenoWhether to display 12H or 24H mode.
touchUibooleanfalsenoWhether the calendar UI is in touch mode. In touch mode the calendar opens in a dialog rather than a popup and elements have more padding to allow for bigger touch targets.
showRangePresetbooleantruenoWhether to display the DATE_RANGE picker presets window. if true the customRangePreset is required
customRangePresetArray<{label: string; orderPosition?: number; getRangeFunction?: () => [Date, Date];}>[]noArray of objects to define the range presets of DATE_RANGE picker. if showRangePreset is true, then the customRangePreset must be defined
languagestringitnoit or en to specify the locale language of the application,
timeTypeTimeTypeEnumTimeTypeEnum.STRINGnoThe type of data returned in the case of TIME mode picker.
showButtonsbooleanfalsenoshow the buttons, inside modal window, to cancel and apply the selected data.
cancelButtonTextstringCancellanocancel button text, inside modal window.
applyButtonTextstringApplicanoapply button text, inside modal window.
datepickerFilter(date: Date) => booleannullnoapply function called to filter all selectable days (if the function return true, the day is selectable). NOTE: day 0 is SUNDAY. This input is not used in TIME mode
showTimePopoverbooleantruenoUsing the TIME picker via modal or keyboard input. false: the input can be inserted only by keyboard (using formControl the only output type ll'be string), true: keyboard input will be disabled and can only be entered via modal

customRangePreset definition

The DATE_RANGE preset window has some default preset that needs only the label property to be used. this default preset has poper orderPosition value and getRangeFunction.

PropertyTypeDefaultRequiredDescription
labelstringyestext to show on the preset button
orderPositionnumber, undefinedundefinednoindex to specify the position of the preset. To inset a custom preset between default preset, the ordetPosition must be between them
getRangeFunction(() => [Date, Date]), undefinedundefinednofuncion to be executed from the picker to get the range on click of the preset

Default preset

labelorderPosition
Today100
Last 7 days200
This week300
This month400
This year500
Last week600
Last month700
Last year800

Outputs

OutputEvent ArgumentsRequiredDescription
(ngModelInputChange)EventEmitter<any>noInvoked when the selected value changes and ngModelInput is bound. The output type is the same as the ngModelInput type.
(formControlChange)EventEmitter<any>noInvoked when the selected value changes and formGroupInput is bound.

Model, Interfaces and Enums used

Enums description

EnumTypeDescriptionNotes
PickerModeEnumDefine the view mode of the picker.Has the following values: DATETIME = 1 -> shows a picker to select date and time; DATE = 2 -> shows a date only picker and the returned time of date is set to ("00:00:00"); TIME = 3 -> shows a time only picker which returns the selected time; DATE_RANGE = 4 -> shows a date only picker where can be selected the initial and the end date of a range, and returns the start and the end dates with time ("00:00:00");
TimeTypeEnumUsed to define the type of data returned in the case of TIME modeDATE = 1; STRING = 2
export enum PickerModeEnum {
  DATETIME = 1,
  DATE = 2,
  TIME = 3,
  DATE_RANGE = 4
}
export enum TimeTypeEnum {
  DATE = 1,
  STRING = 2,
}

Examples & Use cases

It is highly recommended not to use ngModelInput together with formControl - formGroup

Using ngModelInput (type = DATE)

<eqp-datetimerangepicker
  [minDate]="minDate"
  [maxDate]="maxDate"
  [type]="PickerModeEnum.DATE"
  [disabled]="disable"
  [(ngModelInput)]="date"
  [isRequired]="isRequired"
  [placeholder]="''"></eqp-datetimerangepicker>
PickerModeEnum = PickerModeEnum;
minDate: Date = new Date(); // minimum date selectable
maxDate: Date = null; // maximum date selectable (ex. last day of this year)
disable: boolean = false; // flag to set the input disabled
date: Date | null = null;

Using ngModelInput (type = TIME)

<eqp-datetimerangepicker
  [type]="PickerModeEnum.TIME"
  [timeType]="PickerModeEnum.DATE"
  [disabled]="disable"
  [(ngModelInput)]="timePickerInput"
  [placeholder]="''"
  [isRequired]="isRequired"
  [showSeconds]="showSeconds"></eqp-datetimerangepicker>
PickerModeEnum = PickerModeEnum;
TimeTypeEnum = TimeTypeEnum;
timePickerInput: Date | string | null = null;
showSeconds: boolean = true; // flag to allow second selection from the picker

Using ngModelInput (type = DATE_TIME)

<eqp-datetimerangepicker
  [minDate]="minDate"
  [maxDate]="maxDate"
  [type]="PickerModeEnum.DATE_TIME"
  [disabled]="disable"
  [(ngModelInput)]="dateTimePickerWithTime"
  [placeholder]="''"
  [isRequired]="isRequired"
  [showSeconds]="showSeconds"></eqp-datetimerangepicker>
PickerModeEnum = PickerModeEnum;
dateTimePickerWithTime: Date | null = null;
showSeconds: boolean = true; // flag to allow second selection from the picker

Using ngModelInput (type = DATE_RANGE)

<eqp-datetimerangepicker
  [customRangePreset]="customRangePreset"
  [minDate]="minDate"
  [maxDate]="maxDate"
  [type]="PickerModeEnum.DATE_RANGE"
  [disabled]="disable"
  [(ngModelInput)]="range"
  [startPlaceholeder]="''"
  [endPlaceholeder]="''"
  [isRequired]="isRequired"
  [showRangePreset]="showRangePreset"
  [language]="presetLanguage"></eqp-datetimerangepicker>
PickerModeEnum = PickerModeEnum;
range: {from: Date | null, to: Date | null} = { from: null, to: null };
showRangePreset: boolean = true // flag to show the preset window
presetLanguage: string = "en"

getTwoDayAgoToNow: (() => [Date, Date]) | undefined = () => [
    new Date(new Date().setDate(new Date().getDate() - 2)),
    new Date()
  ];

customRangePreset = [
  {
    label: "Tow days ago",
    orderPosition: 150, // to show this preset between Today and Last year
    getRangeFunction: this.getTwoDayAgoToNow //function to be executed that rerun [Date, Date]
  }, // custom preset
  {
    label: "Today"
  }, // default presets to be used
  {
    label: "Last year"
  },
  {
    label: "This month"
  },
  {
    label: "Last week"
  },
  {
    label: "This week"
  }
];

Using formControl (type = DATE)

<eqp-datetimerangepicker
  [minDate]="minDate"
  [maxDate]="maxDate"
  [type]="PickerModeEnum.DATE"
  [disabled]="disable"
  [formGroupInput]="formGroup"
  formControlNameInput="dateControl"
  [isRequired]="isRequired"
  [placeholder]="''"></eqp-datetimerangepicker>

Using formControl (type = TIME)

<eqp-datetimerangepicker
  [type]="PickerModeEnum.TIME"
  [timeType]="TimeTypeEnum.STRING"
  [disabled]="disable"
  [formGroupInput]="formGroup"
  formControlNameInput="timePickerInputControl"
  [placeholder]="''"
  [isRequired]="isRequired"
  [showSeconds]="showSeconds"></eqp-datetimerangepicker>

Using formControl (type = DATE_TIME)

<eqp-datetimerangepicker
  [minDate]="minDate"
  [maxDate]="maxDate"
  [type]="PickerModeEnum.DATE_TIME"
  [disabled]="disable"
  [formGroupInput]="formGroup"
  formControlNameInput="dateTimePickerWithTimeControl"
  [placeholder]="''"
  [isRequired]="isRequired"
  [showSeconds]="showSeconds"></eqp-datetimerangepicker>

Using formControl (type = DATE_RANGE)

<eqp-datetimerangepicker
  [customRangePreset]="customRangePreset"
  [minDate]="minDate"
  [maxDate]="maxDate"
  [type]="PickerModeEnum.DATE_RANGE"
  [disabled]="disable"
  [formGroupInput]="formGroup"
  formControlNameInputStart="dateRangeControlStart"
  formControlNameInputEnd="dateRangeControlEnd"
  [startPlaceholeder]="''"
  [endPlaceholeder]="''"
  [isRequired]="isRequired"
  [showRangePreset]="showRangePreset"
  [language]="presetLanguage"></eqp-datetimerangepicker>
//#region time picker fields
TimeTypeEnum = TimeTypeEnum;
timePickerInput: Date | string | null = null;
//#endregion

PickerModeEnum = PickerModeEnum;
formGroup = new FormGroup({
  timePickerInputControl: new FormControl(this.timePickerInput),
  dateTimePickerWithTimeControl: new FormControl(this.dateTimePickerWithTime),
  dateRangeControlStart: new FormControl(this.range?.from),
  dateRangeControlEnd: new FormControl(this.range?.to),
  dateControl: new FormControl(this.date)
});

Credits

This library has been developed by EqProject SRL, for more info contact: info@eqproject.it

2.5.4

3 months ago

3.0.2

3 months ago

3.0.1

3 months ago

2.5.3

3 months ago

3.0.0

4 months ago

2.5.2

4 months ago

0.1.4

4 months ago

0.1.5

4 months ago

2.5.0

4 months ago

2.5.1

4 months ago

0.1.3

4 months ago

0.0.10

8 months ago

0.1.0

8 months ago

0.1.2

6 months ago

0.1.1

7 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.7

10 months ago

0.0.6

11 months ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago