Home

Awesome

lit-flatpickr

npm version

Flatpickr for Lit Element

Installation

Lit-Flatpickr can be installed through npm

npm install lit-flatpickr

Usage

It is highly recommended that you use lit-flatpickr as a wrapper for your own custom input elements, to provide the best User experience for the users.

import 'lit-flatpickr';
import { html, LitElement } from 'lit';

class MyApp extends LitElement {
  getValue() {
    this.shadowRoot.querySelector('#my-date-picker').getValue();
  }

  render() {
    return html`
      <lit-flatpickr
        id="my-date-picker"
        altInput
        altFormat="F j, Y"
        dateFormat="Y-m-d"
        theme="material_orange"
        minDate="2020-01"
        maxDate="2020-12-31"
      ></lit-flatpickr>
    `;
  }
}

Styling the input component

html`
  <style>
    lit-flatpickr {
      cursor: pointer;
      font-size: 38px;
    }
  </style>
  <lit-flatpickr id="my-date-picker" altInput altFormat="F j, Y" dateFormat="Y-m-d" theme="material_orange" minDate="2020-01" maxDate="2020-12-31"></lit-flatpickr> `;
`

Using your own custom input Web Component

// Web Component
html`
  <lit-flatpickr>
    <my-custom-input></my-custom-input>
  </lit-flatpickr>
`;

// HTML
html`
  <lit-flatpickr>
    <div>
      <input />
    </div>
  </lit-flatpickr>
`;

Properties

PropertyAttributeTypeDefaultDescription
allowInputallowInputbooleanfalseAllows the user to enter a date directly input the input field. By default, direct entry is disabled.
altFormataltFormatstring"F j, Y"Exactly the same as date format, but for the altInput field
altInputaltInputbooleanfalseShow the user a readable date (as per altFormat), but return something totally different to the server.
altInputClassaltInputClassstring""This class will be added to the input element created by the altInput option.<br />Note that altInput already inherits classes from the original input.
ariaDateFormatariaDateFormatstring"F j, Y"Defines how the date will be formatted in the aria-label for calendar days, using the same tokens as dateFormat.<br />If you change this, you should choose a value that will make sense if a screen reader reads it out loud
clickOpensclickOpensbooleantrueWhether clicking on the input should open the picker.<br />You could disable this if you wish to open the calendar manually with.open()
dateFormatdateFormatstring"Y-m-d"A string of characters which are used to define how the date will be displayed in the input box.
defaultDatedefaultDateDateOption | DateOption[]Sets the initial selected date(s).<br /><br />If you're using mode: "multiple" or a range calendar supply an Array of<br />Date objects or an Array of date strings which follow your dateFormat.<br /><br />Otherwise, you can supply a single Date object or a date string.
defaultHourdefaultHournumber12Initial value of the hour element.
defaultMinutedefaultMinutenumber0Initial value of the minute element.
disabledisableDateLimit<DateOption>[][]Dates selected to be unavailable for selection.
disableMobiledisableMobilebooleanfalseSet disableMobile to true to always use the non-native picker.<br />By default, flatpickr utilizes native datetime widgets unless certain options (e.g. disable) are used.
enableenableDateLimit<DateOption>[][]Dates selected to be available for selection.
enableSecondsenableSecondsbooleanfalseEnables seconds in the time picker
enableTimeenableTimebooleanfalseEnables time picker
formatDateFnformatDateFn((date: Date, format: string, locale: Locale) => string) | undefinedAllows using a custom date formatting function instead of the built-in<br />handling for date formats using dateFormat, altFormat, etc.<br /><br />Function format: (date: Date, format: string, locale: Locale) => string
hourIncrementhourIncrementnumber1Adjusts the step for the hour input (incl. scrolling)
inlineinlinebooleanfalseDisplays the calendar inline
maxDatemaxDatestring | number | Date | undefinedThe maximum date that a user can pick to (inclusive).
minDateminDatestring | number | Date | undefinedThe minimum date that a user can pick to (inclusive).
minuteIncrementminuteIncrementnumber5Adjusts the step for the minute input (incl. scrolling)
modemode"single" | "multiple" | "range""single""single", "multiple", or "range"
nextArrownextArrowstring">"HTML for the arrow icon, used to switch months.
noCalendarnoCalendarbooleanfalseHides the day selection in calendar.<br />Use it along with enableTime to create a time picker.
onChangeonChangeHook | undefinedFunction(s) to trigger on every date selection
onCloseonCloseHook | undefinedFunction(s) to trigger every time the calendar is closed
onMonthChangeonMonthChangeHook | undefinedFunction(s) to trigger every time the calendar month is changed by the user or programmatically
onOpenonOpenHook | undefinedFunction(s) to trigger every time the calendar is opened
onReadyonReadyHook | undefinedFunction(s) to trigger when the calendar is ready
onValueUpdateonValueUpdateHook | undefinedFunction(s) to trigger when the input value is updated with a new date string
onYearChangeonYearChangeHook | undefinedFunction(s) to trigger every time the calendar year is changed by the user or programmatically
parseDateFnparseDateFn((date: string, format: string) => Date) | undefinedFunction that expects a date string and must return a Date object.<br /><br />Function format: (date: string, format: string) => string
positionposition"auto" | "above" | "below""auto"Where the calendar is rendered relative to the input
prevArrowprevArrowstring"<"HTML for the arrow icon, used to switch months.
shorthandCurrentMonthshorthandCurrentMonthbooleanfalseShow the month using the shorthand version (ie, Sep instead of September)
showMonthsshowMonthsnumber1The number of months showed
staticstaticbooleanfalsePosition the calendar inside the wrapper and next to the input element
themetheme"light" | "dark" | "material_blue" | "material_red" | "material_green" | "material_orange" | "airbnb" | "confetti" | "none""light"The set theme of flatpickr. Use "none" if you would like to provide custom theme on your own.
time_24hrtime_24hrbooleanfalseDisplays the time picker in 24 hour mode without AM/PM selection when enabled
weekNumbersweekNumbersbooleanfalseEnabled display of week numbers in calendar
wrapwrapbooleanfalseflatpickr can parse an input group of textboxes and buttons, common in Bootstrap and other frameworks.<br />This permits additional markup, as well as custom elements to trigger the state of the calendar.

Methods

MethodType
changeMonth(monthNum: number, isOffset?: boolean): void
clear(): void
close(): void
destroy(): void
formatDate(dateObj: Date, formatStr: string): string
getValue(): string
getConfig(): ParsedOptions
getCurrentMonth(): number
getCurrentYear(): number
getOptions(): Partial<BaseOptions>
getSelectedDates(): Date[]
init(): Promise<void>
initializeComponent(): void
jumpToDate(date: Date, triggerChange: boolean): void
open(): void
parseDate(dateStr: string, dateFormat: string): Date | undefined
redraw(): void
set(option: "allowInput" | "altFormat" | "altInput" | "altInputClass" | "animate" | "appendTo" | "ariaDateFormat" | "clickOpens" | "closeOnSelect" | "conjunction" | "dateFormat" | "defaultDate" | ... 48 more ... | { ...; }, value?: any): void
setDate(date: string | number | Date | DateOption[], triggerChange: boolean, dateStrFormat: string): void
toggle(): void