2.6.11 • Published 4 years ago

mydatepicker v2.6.11

Weekly downloads
13,415
License
MIT
Repository
github
Last release
4 years ago

mydatepicker

New datepicker library

If your Angular version is >= 7.2 you can use a new version (angular-mydatepicker) of this component:

Version compatibility of this library

Library versionAngular versionBranch
< 9.0.0>= 2 and < 9master
>= 9.0.0>= 9angularIvy

Angular date picker

Build Status codecov npm npm

Description

Highly configurable Angular date picker. Compatible Angular2+.

Online demo is here

If you want to set own styles to the input box, the calendar and the clear buttons you can try this attribute directive date picker.

Installation

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

  1. npm install mydatepicker --save

  2. Add MyDatePickerModule import to your @NgModule like example below

    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    import { BrowserModule } from '@angular/platform-browser';
    import { MyTestApp } from './my-test-app';
    import { MyDatePickerModule } from 'mydatepicker';
    
    @NgModule({
        imports:      [ BrowserModule, FormsModule, MyDatePickerModule ],
        declarations: [ MyTestApp ],
        bootstrap:    [ MyTestApp ]
    })
    export class MyTestAppModule {}

Usage

Use one of the following three options.

1. ngModel binding

In this option the ngModel binding is used. Here is an example application. It shows how to use the ngModel.

To use ngModel define the application class as follows:

import {IMyDpOptions} from 'mydatepicker';
// other imports here...

export class MyTestApp {

    public myDatePickerOptions: IMyDpOptions = {
        // other options...
        dateFormat: 'dd.mm.yyyy',
    };

    // Initialized to specific date (09.10.2018).
    public model: any = { date: { year: 2018, month: 10, day: 9 } };

    constructor() { }
}

Add the following snippet inside your template:

<form #myForm="ngForm" novalidate>
    <my-date-picker name="mydate" [options]="myDatePickerOptions"
                    [(ngModel)]="model" required></my-date-picker>
</form>

2. Reactive forms

In this option the value accessor of reactive forms is used. Here is an example application. It shows how to use the formControlName.

To use reactive forms define the application class as follows:

import {IMyDpOptions} from 'mydatepicker';
// other imports here...

export class MyTestApp implements OnInit {

    public myDatePickerOptions: IMyDpOptions = {
        // other options...
        dateFormat: 'dd.mm.yyyy',
    };

    public myForm: FormGroup;

    constructor(private formBuilder: FormBuilder) { }

    ngOnInit() {
        this.myForm = this.formBuilder.group({
            // Empty string or null means no initial value. Can be also specific date for
            // example: {date: {year: 2018, month: 10, day: 9}} which sets this date to initial
            // value.

            myDate: [null, Validators.required]
            // other controls are here...
        });
    }

    setDate(): void {
        // Set today date using the patchValue function
        let date = new Date();
        this.myForm.patchValue({myDate: {
        date: {
            year: date.getFullYear(),
            month: date.getMonth() + 1,
            day: date.getDate()}
        }});
    }

    clearDate(): void {
        // Clear the date using the patchValue function
        this.myForm.patchValue({myDate: null});
    }
}

Add the following snippet inside your template:

<form [formGroup]="myForm" novalidate>
    <my-date-picker name="mydate" [options]="myDatePickerOptions"
                    formControlName="myDate"></my-date-picker>
  <!-- other controls are here... -->
</form>

3. Callbacks

In this option the mydatepicker sends data back to host application using callbacks. Here is an example application. It shows how to use callbacks.

To use callbacks define the application class as follows:

import {IMyDpOptions, IMyDateModel} from 'mydatepicker';
// other imports here...

export class MyTestApp {

    myDatePickerOptions: IMyDpOptions = {
        // other options...
        dateFormat: 'dd.mm.yyyy',
    };

    constructor() { }

    // dateChanged callback function called when the user select the date. This is mandatory callback
    // in this option. There are also optional inputFieldChanged and calendarViewChanged callbacks.
    onDateChanged(event: IMyDateModel) {
        // event properties are: event.date, event.jsdate, event.formatted and event.epoc
    }
}

Add the following snippet inside your template:

<my-date-picker [options]="myDatePickerOptions"
                (dateChanged)="onDateChanged($event)"></my-date-picker>

Attributes

options attribute

Value of the options attribute is a type of IMyDpOptions. It can contain the following properties.

OptionDefaultTypeDescription
dayLabels{su: 'Sun', mo: 'Mon', tu: 'Tue', we: 'Wed', th: 'Thu', fr: 'Fri', sa: 'Sat'}IMyDayLabelsDay labels visible on the selector.
monthLabels{ 1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec' }IMyMonthLabelsMonth labels visible on the selector.
dateFormatyyyy-mm-ddstringDate format on the selection area and the callback. For example: d.m.yyyy, dd.mm.yyyy, yyyy-m-d, yyyy-mm-dd, d mmm yyyy, dd mmm yyyy (d = Day not leading zero, dd = Day with leading zero, m = Month not leading zero, mm = Month with leading zero, mmm = Month as a text, yyyy = Year four digit)
showTodayBtntruebooleanShow 'Today' button on calendar.
todayBtnTxtTodaystringToday button text. Can be used if showTodayBtn = true.
firstDayOfWeekmostringFirst day of week on calendar. One of the following: mo, tu, we, th, fr, sa, su
sunHighlighttruebooleanSunday red colored on calendar.
satHighlightfalsebooleanSaturday red colored on calendar.
highlightDatesno default valueArray[IMyDate](https://github.com/kekeh/mydatepicker/blob/master/src/my-date-picker/interfaces/my-date.interface.ts)Dates red colored on calendar. For example: {year: 2016, month: 11, day: 14}, {year: 2016, month: 1, day: 15}
markCurrentDaytruebooleanIs current day (today) marked on calendar.
markCurrentMonthtruebooleanIs current month marked on calendar. Can be used if monthSelector = true.
markCurrentYeartruebooleanIs current year marked on calendar. Can be used if yearSelector = true.
monthSelectortruebooleanIf month label is selected opens a selector of months.
yearSelectortruebooleanIf year label is selected opens a selector of years.
minYear1000numberMinimum allowed year in calendar. Cannot be less than 1000.
maxYear9999numberMaximum allowed year in calendar. Cannot be more than 9999.
disableUntilno default valueIMyDateDisable dates backward starting from the given date. For example: {year: 2016, month: 6, day: 26}. To reset existing disableUntil value set: {year: 0, month: 0, day: 0}
disableSinceno default valueIMyDateDisable dates forward starting from the given date. For example: {year: 2016, month: 7, day: 22}. To reset existing disableSince value set: {year: 0, month: 0, day: 0}
disableDaysno default valueArray[IMyDate](https://github.com/kekeh/mydatepicker/blob/master/src/my-date-picker/interfaces/my-date.interface.ts)Disable single dates one by one. Array of disabled dates. For example: {year: 2016, month: 11, day: 14}, {year: 2016, month: 1, day: 15}. To reset existing disableDays value set empty array to it.
enableDaysno default valueArray[IMyDate](https://github.com/kekeh/mydatepicker/blob/master/src/my-date-picker/interfaces/my-date.interface.ts)Enable given dates one by one if the date is disabled. For example if you disable the date range and want to enable some dates in range. Array of enabled days. For example: {year: 2016, month: 11, day: 14}, {year: 2016, month: 1, day: 15}. To reset existing enableDays value set empty array to it.
disableDateRangesno default valueArray[IMyDateRange](https://github.com/kekeh/mydatepicker/blob/master/src/my-date-picker/interfaces/my-date-range.interface.ts)Disable date ranges. For example: {begin: {year: 2016, month: 11, day: 14}, end: {year: 2016, month: 11, day: 20}}. To reset existing disableDateRanges value set empty array to it.
disableWeekendsfalsebooleanDisable weekends (Saturday and Sunday).
disableWeekdaysno default valueArray< string >Disable weekdays. Array of weekdays to disable. Weekdays are same strings as the firstDayOfWeek option. For example: 'tu', 'we' which disables Tuesdays and Wednesdays.
markDatesno default valueArray[IMyMarkedDates](https://github.com/kekeh/mydatepicker/blob/master/src/my-date-picker/interfaces/my-marked-dates.interface.ts)Mark dates for different colors. For example: [{dates: {year: 2016, month: 11, day: 14}, {year: 2016, month: 12, day: 16}, color: '#004198'}, {dates: {year: 2017, month: 10, day: 1}, {year: 2017, month: 11, day: 4}, color: 'green'}]. To reset existing markDates value set empty array to it.
markWeekendsno default valueIMyMarkedDateMark weekends (Saturday and Sunday). For example: {marked: true, color: 'red'}. Value of color can be any CSS color code. To reset existing markWeekends set: {marked: false, color: ''}
disableHeaderButtonstruebooleanPrevent to change the calendar view with header buttons if previous or next month are fully disabled by disableUntil or disableSince.
showWeekNumbersfalsebooleanAre week numbers visible or not on calendar. Can be used if firstDayOfWeek = mo.
selectorHeight232pxstringSelector height.
selectorWidth252pxstringSelector width.
allowDeselectDatefalsebooleanIs deselect of selected date allowed or not.
inlinefalsebooleanShow mydatepicker in inline mode.
showClearDateBtntruebooleanIs clear date button shown or not. Can be used if inline = false.
showDecreaseDateBtnfalsebooleanIs decrease date button shown or not. Can be used if inline = false.
showIncreaseDateBtnfalsebooleanIs increase date button shown or not. Can be used if inline = false.
height34pxstringmydatepicker height in without selector. Can be used if inline = false.
width100%stringmydatepicker width. Can be used if inline = false.
selectionTxtFontSize14pxstringSelection area font size. Can be used if inline = false.
alignSelectorRightfalsebooleanAlign selector right. Can be used if inline = false.
openSelectorTopOfInputfalsebooleanOpen selector top of input field. The selector arrow cannot be shown if this option is true. Can be used if inline = false.
indicateInvalidDatetruebooleanIf user typed date is not same format as dateFormat, show red background in the selection area. Can be used if inline = false.
componentDisabledfalsebooleanIs selection area input field and buttons disabled or not (input disabled flag). You can also disable component by disabled attribute. Can be used if inline = false.
editableDateFieldtruebooleanIs selection area input field editable or not (input readonly flag). Can be used if inline = false.
showSelectorArrowtruebooleanIs selector (calendar) arrow shown or not. Can be used if inline = false.
showInputFieldtruebooleanIs selection area input field shown or not. If not, just show the icon. Can be used if inline = false.
openSelectorOnInputClickfalsebooleanOpen selector when the input field is clicked. Can be used if inline = false and editableDateField = false.
allowSelectionOnlyInCurrentMonthtruebooleanIs a date selection allowed or not other than current month.
ariaLabelInputFieldDate input fieldstringAria label text of input field.
ariaLabelClearDateClear DatestringAria label text of clear date button.
ariaLabelDecreaseDateDecrease DatestringAria label text of decrease date button.
ariaLabelIncreaseDateIncrease DatestringAria label text of increase date button.
ariaLabelOpenCalendarOpen CalendarstringAria label text of open calendar button.
ariaLabelPrevMonthPrevious MonthstringAria label text of previous month button.
ariaLabelNextMonthNext MonthstringAria label text of next month button.
ariaLabelPrevYearPrevious YearstringAria label text of previous year button.
ariaLabelNextYearNext YearstringAria label text of next year button.
ariaLabelDayDaysstringAria label text of day button.
  • Example of the options data (not all properties listed):
  myDatePickerOptions: IMyDpOptions = {
      todayBtnTxt: 'Today',
      dateFormat: 'yyyy-mm-dd',
      firstDayOfWeek: 'mo',
      sunHighlight: true,
      inline: false,
      disableUntil: {year: 2016, month: 8, day: 10}
  };

locale attribute

An ISO 639-1 language code can be provided as shorthand for the following options (dayLabels, monthLabels, dateFormat, todayBtnTxt, firstDayOfWeek and sunHighlight). Currently supported languages: en, fr, fr-ch, ja, fi, es, hu, sv, nl, ru, uk, no, tr, pt-br, de, de-ch, it, it-ch, pl, my, sk, sl, zh-cn, he, ro, ca, id, en-au, am-et, cs, el, kk, th, ko-kr, da, lt, vi, bn, bg, hr, ar, is, tw, lv and et.

The locale options can be override by options attribute.

  • new locale data can be added to this file. If you want to add a new locale create a pull request.
  • locales can be tested here

defaultMonth attribute

If you would prefer a different year and month to be the default for a freshly chosen date picking operation, specify a defaultMonth attribute.

Value of the defaultMonth attribute can be:

  • IMyDefaultMonth object. The value of defMonth property can be a string which contain year number and month number separated by delimiter. The delimiter can be any special character. For example: 08-2016 or 08/2016.
  • a string which contain year number and month number separated by delimiter. The delimiter can be any special character. For example: 08-2016 or 08/2016.

Here is an example on how to use this attribute.

placeholder attribute

Placeholder text in the input field. Here is an example on how to use this attribute.

disabled attribute

Boolean value indicating is the component disabled or not. Here is an example on how to use this attribute.

selector attribute

Selector can be opened or closed using this attribute. Value of the selector attribute can be:

  • IMySelector object. The value of open property is a boolean value indicating the state of the selector.

Here is an example on how to use this attribute. Another way is to call a function of mydatepicker. Here is an example.

Callbacks

dateChanged callback

  • called when the date is selected, removed or input field typing is valid
  • event parameter:
    • event.date: Date object in the following format: { day: 22, month: 11, year: 2016 }
    • event.jsdate: Javascript Date object
    • event.formatted: Date string in the same format as dateFormat option is: '2016-11-22'
    • event.epoc: Epoc time stamp number: 1479765600
  • event parameter type is IMyDateModel

  • Example of the dateChanged callback:

    onDateChanged(event: IMyDateModel) {
      console.log('onDateChanged(): ', event.date, ' - jsdate: ', new Date(event.jsdate).toLocaleDateString(), ' - formatted: ', event.formatted, ' - epoc timestamp: ', event.epoc);
    }

inputFieldChanged callback

  • called when the value change in the input field, date is selected or date is cleared (can be used in validation, returns true or false indicating is date valid or not in the input field)
  • event parameter:
    • event.value: Value of the input field. For example: '2016-11-22'
    • event.dateFormat: Date format string in the same format as dateFormat option is. For example: 'yyyy-mm-dd'
    • event.valid: Boolean value indicating is the input field value valid or not. For example: true
  • event parameter type is IMyInputFieldChanged

  • Example of the input field changed callback:

    onInputFieldChanged(event: IMyInputFieldChanged) {
      console.log('onInputFieldChanged(): Value: ', event.value, ' - dateFormat: ', event.dateFormat, ' - valid: ', event.valid);
    }

calendarViewChanged callback

  • called when the calendar view change (year or month change)
  • event parameter:
    • event.year: Year number in calendar. For example: 2016
    • event.month: Month number in calendar. For example: 11
    • event.first: First day of selected month and year. Type of IMyWeekday. For example: {number: 1, weekday: "tu"}
    • event.last: Last day of selected month and year. Type of IMyWeekday. For example: {number: 30, weekday: "we"}
  • event parameter type is IMyCalendarViewChanged
  • values of the weekday property are same as values of the firstDayOfWeek option

  • Example of the calendar view changed callback:

    onCalendarViewChanged(event: IMyCalendarViewChanged) {
      console.log('onCalendarViewChanged(): Year: ', event.year, ' - month: ', event.month, ' - first: ', event.first, ' - last: ', event.last);
    }

calendarToggle callback

  • called when the calendar is opened or closed

    • event: number from 1 to 4 indicating the reason of the event
      • 1 = calendar opened
      • 2 = calendar closed by date select
      • 3 = calendar closed by calendar button
      • 4 = calendar closed by outside click (document click)
      • 5 = calendar closed by ESC key
      • 6 = calendar closed by API call
  • Example of the calendar toggle callback:

      onCalendarToggle(event: number): void {
          console.log('onCalendarClosed(): Reason: ', event);
      }

inputFocusBlur callback

  • called when the input box get or lost focus
  • event parameter:
    • event.reason: Reason of the event:
      • 1 = focus to input box
      • 2 = focus out of input box
    • event.value: Value of input box
  • event parameter type is IMyInputFocusBlur

  • Example of the input focus blur callback:

      onInputFocusBlur(event: IMyInputFocusBlur): void {
          console.log('onInputFocusBlur(): Reason: ', event. reason, ' - Value: ', event.value);
      }

Change styles of the component

The styles of the component can be changed by overriding the styles.

Create a separate stylesheet file which contain the changed styles. Then import the stylesheet file in the place which is after the place where the component is loaded.

The sampleapp of the component contain an example:

Development of this component

  • At first fork and clone this repo.

  • Install all dependencies:

    1. npm install
    2. npm install --global gulp-cli
  • Build the npmdist folder and execute tslint:

    1. gulp all
  • Execute unit tests and coverage (output is generated to the test-output folder):

    1. npm test
  • Run sample application:

    1. npm start
    2. Open http://localhost:5000 to browser
  • Build a local npm installation package:

    1. gulp all
    2. cd npmdist
    3. npm pack
    • local installation package is created to the npmdist folder. For example: mydatepicker-1.1.1.tgz
  • Install local npm package to your project:

    1. npm install path_to_npmdist/mydatepicker-1.1.1.tgz

Demo

Online demo is here

Compatibility

  • Firefox
  • Chrome
  • Chromium
  • Edge
  • IE11
  • Safari

License

  • License: MIT

Author

  • Author: kekeh

Keywords

  • Date picker
  • Angular2+
  • typescript
9.0.2

4 years ago

2.6.11

4 years ago

9.0.1

4 years ago

9.0.0

4 years ago

2.6.10

4 years ago

2.6.9

4 years ago

2.6.8

4 years ago

2.6.7

4 years ago

2.6.6

6 years ago

2.6.5

6 years ago

2.6.4

6 years ago

2.6.3

6 years ago

2.6.2

6 years ago

2.6.1

6 years ago

2.6.0

6 years ago

2.5.3

6 years ago

2.5.2

6 years ago

2.5.1

6 years ago

2.5.0

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.32

7 years ago

2.0.31

7 years ago

2.0.30

7 years ago

2.0.29

7 years ago

2.0.28

7 years ago

2.0.27

7 years ago

2.0.26

7 years ago

2.0.25

7 years ago

2.0.24

7 years ago

2.0.23

7 years ago

2.0.22

7 years ago

2.0.21

7 years ago

2.0.20

7 years ago

2.0.19

7 years ago

2.0.18

7 years ago

2.0.17

7 years ago

2.0.16

7 years ago

2.0.15

7 years ago

2.0.14

7 years ago

2.0.13

7 years ago

2.0.12

7 years ago

2.0.11

7 years ago

2.0.10

7 years ago

2.0.9

7 years ago

2.0.8

7 years ago

2.0.7

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.10.2

7 years ago

1.10.1

7 years ago

1.10.0

7 years ago

1.9.15

7 years ago

1.9.14

7 years ago

1.9.13

7 years ago

1.9.12

7 years ago

1.9.11

7 years ago

1.9.10

7 years ago

1.9.9

7 years ago

1.9.8

7 years ago

1.9.7

7 years ago

1.9.6

7 years ago

1.9.5

7 years ago

1.9.4

7 years ago

1.9.3

7 years ago

1.9.2

7 years ago

1.9.1

7 years ago

1.9.0

7 years ago

1.8.6

7 years ago

1.8.5

7 years ago

1.8.4

7 years ago

1.8.3

7 years ago

1.8.2

7 years ago

1.8.1

7 years ago

1.8.0

7 years ago

1.7.16

7 years ago

1.7.15

7 years ago

1.7.14

7 years ago

1.7.13

7 years ago

1.7.12

7 years ago

1.7.11

7 years ago

1.7.10

7 years ago

1.7.9

7 years ago

1.7.8

7 years ago

1.7.7

7 years ago

1.7.6

7 years ago

1.7.5

7 years ago

1.7.4

7 years ago

1.7.3

7 years ago

1.7.2

7 years ago

1.7.1

7 years ago

1.7.0

7 years ago

1.6.20

7 years ago

1.6.19

7 years ago

1.6.18

7 years ago

1.6.17

7 years ago

1.6.16

7 years ago

1.6.15

7 years ago

1.6.14

7 years ago

1.6.13

7 years ago

1.6.12

7 years ago

1.6.11

7 years ago

1.6.10

7 years ago

1.6.9

7 years ago

1.6.8

7 years ago

1.6.7

7 years ago

1.6.6

7 years ago

1.6.5

7 years ago

1.6.4

7 years ago

1.6.3

7 years ago

1.6.2

7 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.5.2

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.9

7 years ago

1.2.8

7 years ago

1.2.7

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.16

7 years ago

1.1.15

7 years ago

1.1.14

7 years ago

1.1.13

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.39

7 years ago

1.0.38

7 years ago

1.0.37

7 years ago

1.0.36

7 years ago

1.0.35

7 years ago

1.0.34

7 years ago

1.0.33

7 years ago

1.0.32

7 years ago

1.0.31

7 years ago

1.0.30

7 years ago

1.0.29

7 years ago

1.0.28

7 years ago

1.0.27

7 years ago

1.0.26

7 years ago

1.0.25

7 years ago

1.0.24

7 years ago

1.0.23

7 years ago

1.0.22

7 years ago

1.0.21

7 years ago

1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

8 years ago

0.0.38

8 years ago

0.0.37

8 years ago

0.0.36

8 years ago

0.0.35

8 years ago

0.0.34

8 years ago

0.0.33

8 years ago

0.0.32

8 years ago

0.0.31

8 years ago

0.0.30

8 years ago

0.0.29

8 years ago

0.0.28

8 years ago

0.0.27

8 years ago

0.0.26

8 years ago

0.0.25

8 years ago

0.0.24

8 years ago

0.0.23

8 years ago

0.0.22

8 years ago

0.0.21

8 years ago

0.0.20

8 years ago

0.0.18

8 years ago

0.0.17

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago