1.0.3 • Published 10 months ago

@sanmanto-sds/ngx-daterangepicker-material v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

ngx-daterangepicker-material

Angular 17+ Date range picker.

This Angularl plugin is compatible with Angular 17. It leverages dayjs to handle date manipulation and parsing. The base for this plugin was originally the Bootstrap Date Range Picker, but its dependencies on jQuery, Bootstrap and dayjs.js were removed.


Versions

Angularngx-daterangepicker-material
>=17.0.0v1.x.x

Installation

Install the plugin from npm:

npm i @sanmanto-sds/ngx-daterangepicker-material .

import NgxDaterangepickerMd in your module:

...
import { FormsModule } from '@angular/forms';
import { NgxDaterangepickerMd } from '@sanmanto-sds/ngx-daterangepicker-material';
import { App } from './app';

@NgModule({
  imports: [
    FormsModule,
    NgxDaterangepickerMd.forRoot()
  ],
  declarations: [App],
  bootstrap: [App]
})

export class AppModule {
}

or add it in your app.config.ts file for standalone usage

...
import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { NgxDaterangepickerMd } from '@sanmanto-sds/ngx-daterangepicker-material';

export const appConfig: ApplicationConfig = {
  providers: [
    importProvidersFrom([
      NgxDaterangepickerMd.forRoot(),
    ]),
  ],
};

Usage example

Html:

<input type="text" ngxDaterangepickerMd [(ngModel)]="selected" class="form-control" />

Typescript:

selected: {
  startDate: Dayjs, endDate
:
  Dayjs
}
;

with some options:

Html:

<input type="text" matInput
       ngxDaterangepickerMd
       [locale]="{applyLabel: 'ok', format: 'DD-MM-YYYY'}"
       startKey="start"
       endKey="end"
       [(ngModel)]="selected"
       name="daterange" />

Typescript:

selected: {
  start: Dayjs,
    end
:
  Dayjs,
}
;

Inline usage

You can use the component directly in your templates, which will set its inline mode to true, in which case the calendar won't hide after date/range selection. You can then use the events: rangeClicked or datesUpdated or choosedDate to get its selection state.

<ngx-daterangepicker-material (choosedDate)="choosedDate($event)">
</ngx-daterangepicker-material>

Available options

Global locale

For setting the global locale, pass this object to NgxDaterangepickerMd.forRoot().

eg:

@NgModule({
  imports: [
    FormsModule,
    NgxDaterangepickerMd.forRoot({
      separator: ' - ',
      applyLabel: 'Okay',
    })
  ],
  declarations: [App],
  bootstrap: [App]
})

or for standalone:

...
import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { NgxDaterangepickerMd } from '@sanmanto-sds/ngx-daterangepicker-material';

export const appConfig: ApplicationConfig = {
  providers: [
    importProvidersFrom([
      NgxDaterangepickerMd.forRoot({
        separator: ' - ',
        applyLabel: 'Okay',
      }),
    ]),
  ],
};

Available locale options

Option nameOption type
direction?string
separator?string
weekLabel?string
applyLabel?string
cancelLabel?string
clearLabel?string
customRangeLabel?string
ongoingLabel?string
daysOfWeek?string[]
monthNames?string[]
firstDay?number
format?string
displayFormat?string
locale?any

you can also use locale like an object in props:

export class myComponent {
  locale = {
    format: 'MM/DD/YYYY', // could be 'YYYY-MM-DDTHH:mm:ss.SSSSZ'
    displayFormat: 'MM/DD/YYYY', // default is format value
    direction: 'ltr', // could be rtl
    weekLabel: 'W',
    separator: ' To ', // default is ' - '
    cancelLabel: 'Cancel', // detault is 'Cancel'
    applyLabel: 'Okay', // detault is 'Apply'
    clearLabel: 'Clear', // detault is 'Clear'
    customRangeLabel: 'Custom range',
    daysOfWeek: dayjs.weekdaysMin(),
    monthNames: dayjs.monthsShort(),
    firstDay: 1 // first day is monday
  }
}

Theming

You can pass the theme object to NgxDaterangepickerMd.forRoot() for changing theme variables.

For example:

@NgModule({
  imports: [
    FormsModule,
    NgxDaterangepickerMd.forRoot({}, {
      color: '#0a0a0a',
      bgColor: '#fafafa',
      width: '300px',
    }),
  ],
  declarations: [App],
  bootstrap: [App],
})

or for standalone:

...
import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { NgxDaterangepickerMd } from '@sanmanto-sds/ngx-daterangepicker-material';

export const appConfig: ApplicationConfig = {
  providers: [
    importProvidersFrom([
      NgxDaterangepickerMd.forRoot({}, {
        color: '#0a0a0a',
        bgColor: '#fafafa',
        width: '300px',
      }),
    ]),
  ],
};

Available theme options

OptionTypeDefault value
color?string'inherit'
bgColor?string'#FFFFFF'
inRangeColor?string'#5A5A5A'
inRangeBorderColor?string'transparent'
inRangeBgColor?string'#FAFAFA'
inRangeFontWeight?string'600'
activeColor?string'#FFFFFF'
activeBgColor?string'#151F6D'
activeBorderColor?string'transparent'
activeFontWeight?string'600'
unselectedColor?string'rgba(90, 90, 90, .5)'
unselectedBgColor?string'transparent'
unselectedBorderColor?string'#FFFFFF'
width?string'278px'
widthDouble?string'auto'
borderColor?string'#CCCCCC'
calendarMargin?string'12px'
calendarBgColor?string'#FFFFFF'
calendarBorderSize?string'1px'
calendarBorderColor?string'transparent'
calendarBorderRadius?string'4px'
cellColor?string'#5A5A5A'
cellBorderColor?string'transparent'
cellBgColor?string'#FFFFFF'
cellFontSize?string'14px'
cellFontWeight?string'400'
cellHoverColor?string'#5A5A5A'
cellHoverBorderColor?string'transparent'
cellHoverBgColor?string'#FAFAFA'
cellHoverFontWeight?string'600'
cellSize?string'32px'
cellWidth?string'32px'
cellHeight?string'32px'
cellBorderRadius?string'4px'
cellBorderSize?string'1px'
cellWeekdayNameColor?string'#5A5A5A'
cellWeekdayNameFontWeight?string'700'
cellWeekdayNameFontSize?string'12px'
controlHeight?string'30px'
controlLineHeight?string'30px'
controlColor?string'#555'
controlBorderSize?string'1px'
controlBorderColor?string'#ccc'
controlBorderRadius?string'4px'
controlActiveBorderSize?string'1px'
controlActiveBorderColor?string'#08c'
controlActiveBorderRadius?string'4px'
controlDisabledColor?string'#ccc'
selectBorder?string'1px solid #E8E8E8'
selectBackground?string'#ffffff'
selectPadding?string'0 8px'
selectRadius?string'4px'
selectFontSize?string'14px'
selectColor?string'#000000'
inputHeight?string'#000000'
separatorColor?string'#E8E8E8'
hoverCustomRangeBorderColorstring'#BFBFBF'

Props that can be passed to component

NameTypeDefault valueDescription
startDatedayjs.Dayjsdayjs().startOf('day')Start date of range
endDatedayjs.Dayjsdayjs().endOf('day')End date of range
dateLimitnumber-Max date limit in range
autoApplybooleanfalseAuto apply on range select
singleDatePickerbooleanfalseDisplay only one calendar
showDropdownsbooleanfalseDisplay selects for year and month
showWeekNumbersbooleanfalseDisplay week numbers on calendar
showISOWeekNumbersbooleanfalseDisplay week numbers on calendar in ISO format
linkedCalendarsbooleanfalseWhen true, calendars display 2 month 1 by 1, when false, every calendar can show its own month
autoUpdateInputbooleantrueAutomatically update chosen label, input and date
alwaysShowCalendarsbooleanfalseAlways show calendars
lockStartDatebooleanfalseSet to true if you want to lock start date and change only the end date
timePickerbooleanfalseSet to true if you want to display timepicker
timePicker24Hourbooleanfalse24 hours for timepicker format
timePickerIncrementnumber1Increment for timepicker
timePickerSecondsbooleanfalseShow seconds on timepicker
showClearButtonbooleanfalseShow clear button
firstMonthDayClassstring-Class for element with first day of month
lastMonthDayClassstring-Class for element with last day of month
emptyWeekRowClassstring-Class for empty week row
emptyWeekColumnClassstring-Class for empty week column
firstDayOfNextMonthClassstring-Class for element with first day of next month
lastDayOfPreviousMonthClassstring-Class for element with last day of prev month
lastDayOfPreviousMonthClassstring-Class for element with last day of prev month
keepCalendarOpeningWithRangebooleanfalseSet to true if you want the calendar won't be closed after choosing a range
showCustomRangeLabelbooleanfalseShow custom label for range
showCancelbooleanfalseShow cansel button
showRangeLabelOnInputbooleanfalseSet to true if you want to display the range label on input
customRangeDirectionbooleanfalseSet to true if you want to allow selection range from end date first
rangeWithOngoingButtonbooleanfalseSet to true if you want to display ongoing button in ranges
dropsup, down-Position the calendar to the up or down of the calendar
opens'left' | 'center' | 'right'-Position the calendar from the input element
closeOnAutoApplybooleanfalseClose calendar when its automatically applied
minDatedayjs.Dayjs | string-Minimal enabled date
maxDatedayjs.Dayjs | string-Max enabled date
localeLocaleConfig-Locale config
rangesDateRanges{}Ranges object
ongoingbooleanfalseOngoing value
isInvalidDate(date: Dayjs) => boolean(date) => falsefunction for checking is date invalid
isCustomDate(date: Dayjs) => boolean(date) => falsefunction for checking is date custom
isTooltipDate(date: Dayjs) => boolean | string | null(date) => falseA function that is passed each date in the two calendars before they are displayed, and may return a text to be displayed as a tooltip.

Available events

Event nameDescription
choosedDateFires when date is chosen
rangeClickedFires when range label clicked
datesUpdatedFires when range is updated
startDateChangedFires when start date changed
endDateChangedFires when end date changed
cancelClickedFires when cancel button clicked
clearClickedFires when clear button clicked
ongoingChangeFires when ongoing value changed
calendarShowFires when calendar show
calendarHideFires when calendar hide

Open datepicker from outside

It is possible to open datepicker from outside. You should create an input with attached datepicker directive and a button with "ngx-daterangepicker-action" class (to prevent triggering of clickOutside).

<input
  ngxDaterangepickerMd
  [closeOnAutoApply]="true"
  [autoApply]="true"
  [singleDatePicker]="true"
  [linkedCalendars]="true"
  [(ngModel)]="selected"
  (datesUpdated)="datesUpdated($event)"
  class="datepicker-calendar-icon">

<a class="ngx-daterangepicker-action" (click)="openDatepicker()">
  Open
</a>
...
@ViewChild(DaterangepickerDirective, { static: false })
pickerDirective
:
DaterangepickerDirective;
...
...
openDatepicker()
{
  this.pickerDirective.open();
}

Development

Prepare your environment

Install local dependencies: npm install.

Development server

Run npm start to start a development server on a port 4200.

Open http//:localhost:4200 on your browser.

License

MIT

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago