7.16.4 • Published 2 years ago

@heliomarpm/ion-calendar v7.16.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

DeepScan grade CodeFactor CodeQL Publish NPM version Downloads

@angular/core @ionic/angular luxon

screenshot screenshot2

live demo: click here

Summary

The @heliomarpm/ion-calendar is a calendar component for Ionic Framework-based applications. \ It uses Luxon to handle dates and times, so it's fully compatible with International Organization for Standardization (ISO) 8601 formats.

The main features are:

  • Fully customizable layout;
  • Customizable date format;
  • Multiple selection mode;
  • Range selection mode;
  • Calendar can be placed in content area or as an overlay;
  • Theming using SCSS variables.
  • Disable weekdays or weekends.
  • Setting days event.
  • Setting localization.
  • Material design from Ionic Framework.
  • Adapted for Ionic dark theme

Support

  • @angular/core ^16.+"
  • @ionic/angular ^6.+"

Installation

You can install the library using npm or yarn:

npm i @heliomarpm/ion-calendar 
# or 
yarn add @heliomarpm/ion-calendar 

Example Usage

Import module

import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from '@ionic/angular';
...
import { AppComponent } from './app.component';
import { IonCalendarModule } from '@heliomarpm/ion-calendar';

@NgModule({
  declarations: [AppComponent],
  imports: [
    ...,
    IonicModule.forRoot(),
    IonCalendarModule
  ],
  ...
})
export class AppModule {}

Change Defaults

import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from '@ionic/angular';
...
import { AppComponent } from './app.component';
import { IonCalendarModule } from '@heliomarpm/ion-calendar';

@NgModule({
  declarations: [AppComponent],
  imports: [
    ...,
    IonicModule.forRoot(),
    // See ICalendarComponentOptions for options
    IonCalendarModule.forRoot({
      doneLabel: 'Confirm',
      closeIcon: true
    })
  ],
  ...
})
export class AppModule {}

Components Mode

Basic

<ion-calendar [(ngModel)]="date"
              (onChange)="onChange($event)"
              [type]="type"
              format="yyyy-MM-dd">
</ion-calendar>
import { Component } from '@angular/core';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  date: string;
  type: 'string'; // 'string' | 'js-date' | 'luxon' | 'time' | 'object'
  constructor() { }

  onChange($event) {
    console.log($event);
  }
  ...
}

Date range

<ion-calendar [(ngModel)]="dateRange"
              [options]="optionsRange"
              [type]="type"
              [format]="'yyyy-MM-dd'">
</ion-calendar>
import { Component } from '@angular/core';
import { ICalendarComponentOptions } from '@heliomarpm/ion-calendar';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  dateRange: { from: string; to: string; };
  type: 'string'; // 'string' | 'js-date' | 'moment' | 'time' | 'object'
  optionsRange: ICalendarComponentOptions = {
    pickMode: 'range'
  };

  constructor() { }
  ...
}

Multi Date

<ion-calendar [(ngModel)]="dateMulti"
              [options]="optionsMulti"
              [type]="type"
              format="yyyy-MM-dd">
</ion-calendar>
import { Component } from '@angular/core';
import { ICalendarComponentOptions } from '@heliomarpm/ion-calendar';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  dateMulti: string[];
  type: 'string'; // 'string' | 'js-date' | 'moment' | 'time' | 'object'
  optionsMulti: ICalendarComponentOptions = {
    pickMode: 'multi'
  };

  constructor() { }
  ...
}

Input Properties

NameTypeDefaultDescription
optionsICalendarComponentOptionsnulloptions
formatstring'yyyy-MM-dd'value format
typestring'string'value type
readonlybooleanfalsereadonly

Output Events

NameTypeDescription
onChangeEventEmitterevent for model change
onMonthChangeEventEmitterevent for month change when displayMode = month
onWeekChangeEventEmitterevent for month change when displayMode = week
onSelectEventEmitterevent for click day-button
onSelectStartEventEmitterevent for click day-button
onSelectEndEventEmitterevent for click day-button

ICalendarComponentOptions

NameTypeDefaultDescription
fromDatenew Date()start date
toDate0 (Infinite)end date
colorstring'primary''primary', 'secondary','tertiary', 'success', 'warning', 'danger', 'dark', 'medium', 'light', 'custom', 'transparent'
colorSubtitlestringundefined'primary', 'secondary','tertiary', 'success', 'warning', 'danger', 'dark', 'medium', 'light', 'custom', 'transparent'
pickModestringsingle'multi', 'range', 'single'
showToggleButtonsbooleantrueshow toggle buttons
monthsTitleArray['JAN', 'FEB', ..., 'NOV', 'DEC']month picker format
showMonthPickerbooleantrueshow month picker
showYearPickerbooleantrueshow year picker
defaultTitlestring''default title in days
defaultSubtitlestring''default subtitle in days
disableWeeksArray[]week to be disabled (0-6)
monthFormatstring'MMM yyyy'month title format
weekdaysArray['S', 'M', 'T', 'W', 'T', 'F', 'S']weeks text
weekStartnumber0set week start day 0 of sundaty, 1 of monday
IDayConfigArray<IDayConfig>[]days configuration
displayModestringmonth'month', 'week'
showAdjacentMonthDaybooleantrueshow days of adjacent months when displayMode: 'month'
showMonthAdjacentDaysbooleanfalseshow the month on days adjacent to the selected month when displayMode: 'week'
weeksnumber1number of week to show in week display mode
localeICalendarLocale{locale: 'en', weekdays: 'initial' }change calendar locale and set default name to weeks

Modal Mode

Basic

Import ion2-calendar in component controller.

import { Component } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { CalendarModalComponent, ICalendarModalOptions, IDayConfig, ICalendarResult } from '@heliomarpm/ion-calendar';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  constructor(public modalCtrl: ModalController) {}

  openCalendar() {
    const options: ICalendarModalOptions = {
      title: 'BASIC'
    };

    const myCalendar = await this.modalCtrl.create({
      component: CalendarModalComponent,
      componentProps: { options }
    });

    myCalendar.present();

    const event: any = await myCalendar.onDidDismiss();
    const date: ICalendarResult = event.data;
    if (event.role === 'done') {
      console.log('date:', date); // date selected
    }
  }
}

Date range

Set pickMode to 'range'.

openCalendar() {
  const options: ICalendarModalOptions = {
    pickMode: 'range',
    title: 'RANGE'
  };

  const myCalendar = await this.modalCtrl.create({
    component: CalendarModal,
    componentProps: { options }
  });

  myCalendar.present();

  const event: any = await myCalendar.onDidDismiss();
  const date = event.data;
  const from: ICalendarResult = date.from;
  const to: ICalendarResult = date.to;

  console.log(date, from, to);
}

Multi Date

Set pickMode to 'multi'.

openCalendar() {
  const options = {
    pickMode: 'multi',
    title: 'MULTI'
  };

  const myCalendar = await this.modalCtrl.create({
    component: CalendarModal,
    componentProps: { options }
  });

  myCalendar.present();

  const event: any = await myCalendar.onDidDismiss();
  const date: ICalendarResult = event.data;
  console.log(date);
}

Disable weeks

Use index eg: [0, 6] denote Sunday and Saturday.

openCalendar() {
  const options: ICalendarModalOptions = {
    disableWeeks: [0, 6]
  };

  const myCalendar = await this.modalCtrl.create({
    component: CalendarModal,
    componentProps: { options }
  });

  myCalendar.present();

  const event: any = await myCalendar.onDidDismiss();
  const date: ICalendarResult = event.data;
  console.log(date);
}

Localization

openCalendar() {
  const options: ICalendarModalOptions = {
    locale: { locale: 'zn-CN', weekdays:'short' },
    weekStart: 1,
    monthFormat: 'yyyy 年 MM 月',
    defaultDate: new Date()
  };

  const myCalendar = await this.modalCtrl.create({
    component: CalendarModal,
    componentProps: { options }
  });

  myCalendar.present();

  const event: any = await myCalendar.onDidDismiss();
  const date: ICalendarResult = event.data;
  console.log(date);
}

Days Config

Configure one day.

openCalendar() {
  let holidays: IDayConfig[] = [];
  
  holidays.push({date: new Date(2023, 0, 1), title: '🎉', subTitle: 'New Year'});
  holidays.push({date: new Date(2023, 4, 1), subTitle: 'Labor Day', disable: true});
  holidays.push({date: new Date(2023, 11, 25), subTitle: '🎅', disable: true});

  const options: ICalendarModalOptions = {
    from: new Date(2023, 0, 1),
    to: new Date(2023, 11, 31),
    defaultDate: new Date(),
    efaultScrollTo: new Date(),
    IDayConfig: holidays,
    color: 'success'
  };

  const myCalendar = await this.modalCtrl.create({
    component: CalendarModal,
    componentProps: { options }
  });

  myCalendar.present();

  const event: any = await myCalendar.onDidDismiss();
  const date: ICalendarResult = event.data;
  console.log(date);
}

API

Modal Options

NameTypeDefaultDescription
fromDatenew Date()start date
toDateundefinedend date
titlestring'CALENDAR'title
colorstring'primary''primary', 'secondary', 'danger', 'light', 'dark'
defaultScrollToDatefromlet the view scroll to the default date
defaultDateDatenulldefault date data, apply to single
defaultDatesArraynulldefault dates data, apply to multi
defaultDateRange{ from: Date, to: Date }nulldefault date-range data, apply to range
defaultTitlestring''default title in days
defaultSubtitlestring''default subtitle in days
cssClassstring''Additional classes for custom styles, separated by spaces.
canBackwardsSelectedbooleanfalsecan backwards selected
pickModestringsingle'multi', 'range', 'single'
disableWeeksArray[]week to be disabled (0-6)
closeLabelstringCANCELcancel button label
doneLabelstringDONEdone button label
clearLabelstringnullclear button label
closeIconbooleanfalseshow cancel button icon
doneIconbooleanfalseshow done button icon
monthFormatstring'MMM yyyy'month title format
weekdaysArray['S', 'M', 'T', 'W', 'T', 'F', 'S']weeks text
weekStartnumber0 (0 or 1)set week start day
weeksnumber1number of weeks will be displayed when displayMode: week
IDayConfigArray<IDayConfig>[]days configuration
stepnumber12month load stepping interval to when scroll
autoDonebooleanfalsedone automatically when selecting date
showAdjacentMonthDaybooleantrueshow days of adjacent months

onDidDismiss Output { data } = event

pickModeType
single{ date: ICalendarResult }
range{ from: ICalendarResult, to: ICalendarResult }
multiArray<ICalendarResult>

onDidDismiss Output { role } = event

ValueDescription
'cancel'dismissed by click the cancel
'done'dismissed by click the done button
'backdrop'dismissed by click the backdrop

IDayConfig

NameTypeDefaultDescription
cssClassstring''separated by spaces
dateDaterequiredconfigured days
markedbooleanfalsehighlight color
disablebooleanfalsedisable
titlestringnonedisplayed title eg: 'today'
subTitlestringnonesubTitle subTitle eg: `'New Year\'s

ICalendarResult

NameType
timenumber
secondasnumber
dateObjDate
stringstring
yearnumber
monthnumber
daynumber

Dependencies

  • @angular/core: Angular - the core framework
  • @ionic/angular: Ionic Angular specific building blocks on top of @ionic/core components.
  • luxon: Luxon is a library for working with dates and times in JavaScript.

Contributing

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to project!

Made with contrib.rocks.

That said, there's a bunch of ways you can contribute to this project, like by:

  • :beetle: Reporting a bug
  • :page_facing_up: Improving this documentation
  • :rotating_light: Sharing this project and recommending it to your friends
  • :dollar: Supporting this project on GitHub Sponsors or Ko-fi
  • :star2: Giving a star on this repository

Donate

If you appreciate that, please consider donating to the Developer.

License

MIT © Heliomar P. Marques 🔝