1.3.0 • Published 5 years ago

ngx-aircal v1.3.0

Weekly downloads
77
License
MIT
Repository
github
Last release
5 years ago

ngx-aircal

version license weight build

Aircal

Description

ngx-aircal is a modern, configurable and lightweight Angular date range picker.

Notes

  • When setting the dateFormat option, be aware that you must use Unicode tokens

How do I install the library?

To install this component to an external project, follow the procedure:

npm install ngx-aircal --save

How do I import the calendar?

Add 'NgxAircalModule' module to your imports array in your module

import { NgxAircalModule } from "ngx-aircal";
@NgModule({
  imports: [
     ...,
    NgxAircalModule
})

How do I use the calendar?

Add the following to your html template. ngx-aircal uses a data attribute selector to keep your HTML valid:

<div data-ngx-aircal [options]="calendarOptions" 
    name="dateRange" 
    [(ngModel)]="dateRange"
    (onDateRangeCommitted)="onDateRangeCommitted($event)" 
    (onDateRangeChanged)="onDateRangeChanged($event)"
    (onCalendarViewChanged)="onCalendarViewChanged($event)" 
    (onInputFieldChanged)="onInputFieldChanged($event)"
    (onDateRangeInitialised)="onDateRangeInitialised($event)"
    (onDateRangeCleared)="onDateRangeCleared($event)">
</div>

Options

Initialise an options object as a component property. Now, pass this to the calendar component:

import { AircalOptions } from "ngx-aircal";
export class AppComponent {
    ...
    public calendarOptions: AircalOptions = new AircalOptions();
    ...
}

You can update options by passing an object into the constructor:

export class AppComponent {
    ...
    public calendarOptions: AircalOptions = new AircalOptions({
      inlineMode: true,
      daysSelectedCounterVisible: false
    });
    ...
}
Option nameDefault valueTypeDescription
defaultStartnew Date()DateSet the default start view when the calendar is opened
inlineModefalseBooleanDisplay the calendar without the input field
disablefalseBooleanDisable the ability to set the date and apply it
singlePickerfalseBooleanUse a single range picker instead of a double to save space
startDatenullDateSet a starting date. If no end is provided, then the datepicker will be in highlighted mode on the UI, ready for an end date selection.
endDatenullDateSet an end date. If no start date is set, it will fall back to become the start date.
dayLabelsnew AircalDayLabels()AircalDayLabelsSet the label values for each day of the week
selectionShortcuts{ "7.days": "7 Days", "14.days": "14 Days", "1.months": "1 Month", "6.months": "6 Months", "1.years": "1 Year" }ObjectOptions and values to pass to the quick selection component when selectionShortcutVisible is true. Key is the value parsed by the plugin, and the value is what is shown in the input dropdown. Separate the key length and duration values by a '.'. It can parse different durations. e.g. "7.weeks": "7 Weeks"
dateFormat"dd/MM/yyyy"StringFormat the date displayed in the input, and given in the callback response. Format is Unicode Technical Standard #35. Uses date-fns library to format the dates. When setting the dateFormat option, be aware that you must use Unicode tokens
previousMonthWrapAroundtrueBooleanShould dates be shown in the previous month spaces
nextMonthWrapAroundtrueBooleanShould dates be shown in the next month spaces
daysSelectedCounterVisibletrueBooleanShould the calendar display the number of days selected
selectionShortcutVisiblefalseBooleanShould the range shortcut be visible
backgroundVisibletrueBooleanMake the background transparent
width""StringOverwrite the width set in the css
height""StringOverwrite the width set in the css
applyText"Apply"StringChange the text to commit the date range
autoApplyAndClosefalseBooleanWhen an end date is selected (or a start date if a start and end already exist), automatically apply the date selection and close the calendar
includeExamplePlaceholdertrueBooleanShow an placeholder example for the input field. Dynamically generates example dates based on dateFormat option
autoCloseWhenAppliedfalseBooleanClose the calendar when the apply button is pressed
clearText"Clear"StringChange the text to clear the date range
highlightTodaytrueBooleanHighlight the cell colour for today
showClearBtntrueBooleanShould the clear button be shown on the UI
showApplyBtntrueBooleanShould the apply button be shown on the UI
minYear1000NumberThe minimum year selectable
maxYear9999NumberThe maximum year selectable
disablePreviousSelectionfalseBooleanDisable the previous selection arrow
disableForwardSelectionfalseBooleanDisable the next selection arrow
disableFromHereBackwardsnullDateDisable the cells from being selected from the date supplied going backwards
disableFromHereForwardsnullDateDisable the cells from being selected from the date supplied going forwards
indicateInvalidDateRangetrueBooleanIndicate whether or not to show the red border on the input field
hasArrowtrueBooleanShould the calendar have the arrow pointer
arrowBias"left"arrowBiasWhat edge should the arrow display on
calendarPositioncalendarBias"bottom"Where does the calendar display in relation to the input
allowQuicksetMonthfalseBooleanAllow the quick selection on months by clicking the month
allowQuicksetYearfalseBooleanAllow the quick selection of years by clicking the year
allowUserInputFieldtrueBooleanShould the default view be an input field, or just a button that contains text
allowInfiniteEndDatefalseBooleanAllow no end date to be set
closeOnOutsideClickfalseBooleanAllow the calendar to be closed by clicking outside the component
icons{leftArrow: null, rightArrow: null }{ leftArrow?: null | string, rightArrow?: null | string }Option to provide a base64 image to replace the existing arrows on the main calendar

Models

You can use ngModel or a reactive form to work with the calendar.

ngModel

Define an object with 2 props, startDate, and endDate. Both properties must be of type 'Date'.

ngOnInit() {
	this.dateRange = {
    startDate: new Date(
      2018,
      6,
      27
    ), 
    endDate: new Date(
      2018,
      8,
      27
    )
  };
}

Your should pass in the object into the ngModel box of bananas on your template:

<div data-ngx-aircal [options]="calendarOptions" [(ngModel)]="dateRange"></div>

Reactive forms

ngOnInit() {
  this.form = this._FormBuilder.group({
    dateRange: [{
      startDate: new Date(
        2018,
        6,
        27
      ),
      endDate: new Date(
        2018,
        8,
        27
      )
    }, Validators.required]
  }); 
}

Add the following inside your template:

<form [formGroup]="myForm">
    <div data-ngx-aircal [options]="calendarOptions" formControlName="dateRange"></div>
</form>

Events, callbacks & responses

There are many callbacks you can use to communicate with your parent component. A full list of callbacks is below.

<div data-ngx-aircal [options]="calendarOptions" 
    name="dateRange" 
    [(ngModel)]="dateRange"
    (onDateRangeCommitted)="onDateRangeCommitted($event)" 
    (onDateRangeChanged)="onDateRangeChanged($event)"
    (onCalendarViewChanged)="onCalendarViewChanged($event)" 
    (onInputFieldChanged)="onInputFieldChanged($event)"
    (onDateRangeInitialised)="onDateRangeInitialised($event)"
    (onDateRangeCleared)="onDateRangeCleared($event)">
</div>

Note the event name, and the provided callback function, which takes in $event as a parameter:

(onDateRangeCommitted)="onDateRangeCommitted($event)"

The plugin will respond with a specific set of properties - either:

class AircalResponse {
    public startDate: Date;
    public endDate: Date;
    public formattedStartDate: string;
    public formattedEndDate: string;
}

class AircalInputResponse {
    public startDate: Date;
    public endDate: Date;
    public formattedStartDate: string;
    public formattedEndDate: string;
    public isRangeValid: boolean;
}

Example usage:

public onDateRangeChanged(event: AircalResponse) {
	this.updatePageResults(event.startDate, event.endDate);
}
List of callbacks

All callbacks will return AircalResponse or AircalInputResponse type. AircalInputResponse has an extra boolean value to show you if the date is valid.

Callback nameResponse typeDescription
onDateRangeCommittedAircalResponseWhen the range is committed by the user. Normally by the apply button, but in case showApplyBtn is false, it will respond automatically when the end date is selected
onDateRangeChangedAircalResponseWhen the start or end date is changed by the user.
onCalendarViewChangedAircalResponseWhen the view is changed left or right using the arrows
onInputFieldChangedAircalInputResponseWhen the user modifies the input field.
onDateRangeInitialisedAircalResponseAs soon as the component initialises, a response will be returned for consistency.
onDateRangeClearedAircalResponseWhen the showClearBtn option is true, and the user presses the button, a response will be returned.

Author

  • Author: Chris Swarbrick (chrisswarb)

Credit

  • Icons by Dave Gandy and Lyolya from www.flaticon.com
1.3.0

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

6 years ago