1.0.2 • Published 25 days ago

headless-react-datepicker v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
25 days ago

Headless React Datepicker

NPM NPM NPM

A headless, highly customizable, multi-calendar date picker component for React. It supports various calendars and locales.

Live demo

https://sepehr09.github.io/headless-react-datepicker/

Customization

alt text

Supported Calendars

All calendars are supported by the ECMAScript's Intl API:

  • Gregory
  • Persian
  • Indian
  • Iso8601
  • Japanese
  • Buddhist
  • Chinese
  • Coptic
  • Dangi
  • Ethioaa
  • Ethiopic
  • Hebrew
  • Islamic
  • Islamic-umalqura
  • Islamic-tbla
  • Islamic-civil
  • Islamic-rgsa
  • Islamicc
  • Roc

Supported Locales

All locales are supported by the Intl API.

Installation

1. install the package

npm install headless-react-datepicker

# or

yarn add headless-react-datepicker

# or

pnpm add headless-react-datepicker

2. import the css file

import "headless-react-datepicker/dist/styles.css";

Usage

import React from "react";
import DatePickerProvider, {
  Title,
  Header,
  WeekDays,
  DaySlots,
} from "headless-react-datepicker";

const MyAwesomeDatePicker = () => {
  return (
    <DatePickerProvider>
      <Title />
      <Header />
      <WeekDays />
      <DaySlots />
    </DatePickerProvider>
  );
};

Headless!

Headless React Datepicker structure

DatePickerProvider

Must be in place as the parent of the whole calendar component.

import { DatePickerProvider } from "headless-react-datepicker";

props

NameTypeDescription
initialValueDate | Date[]The initial value of the date picker.
defaultStartDateDateThe default start date. Useful when you want to be on a different month or year despite the initial value.
configTCalendarConfigThe configuration for the date picker.
isRangebooleanIndicates whether the date picker is a range picker.
calendarTCalendarThe calendar to use.
onChange(value: Date | Date[]) => voidon calendar selected date change
childrenReactNodeThe other parts of the calendar or your custom components.

TCalendarConfig

NameTypeDescriptionDefault
weekStartsOnTDay | undefinedThe first day of the week."monday"
localestring | undefinedThe locale to use."en-US"
showOtherDaysboolean | undefinedShow other days from the previous and next month or not.false
otherDaysSelectableboolean | undefinedAllow selecting other days from the previous and next month or not.false
weekdayFormat"long" | "short" | "narrow" | undefined"narrow"
dayFormat"numeric" | "2-digit" | undefined"numeric"
yearRangeFromnumber | undefinedlast 10 years if not provided
yearRangeTonumber | undefinedcurrent year if not provided
maxDateDate | undefinedPrevent selecting dates before this date.
minDateDate | undefinedPrevent selecting dates after this date.
weekendsTDay[] | undefinedSpecify which days of the week are holidays.undefined
weekendSelectableboolean | undefinedAllow selecting weekends or not.true
allowBackwardRangeboolean | undefinedIf user select a date before the previous selected date, it will be considered as a range or start from beginning.false

TCalendar

"gregory" | "persian" | "islamic" | "islamic-umalqura" | "islamic-tbla" | "islamic-civil" | "islamic-rgsa" | "iso8601" | "japanese" | "islamicc" | "roc" | "chinese" | "indian" | "buddhist" | "coptic" | "dangi" | "ethioaa" | "ethiopic" | "hebrew"

Components

Title component

The Title component is used to display the month and year based on the selected locale and calendar.

import { Title } from "headless-react-datepicker";

props

NameTypeOptionsDefault
monthFormatstring | undefined"numeric", "2-digit" , "long" , "short" , "narrow""short"
yearFormatstring | undefined"numeric" , "2-digit""numeric"

Header component

The Header component is used to navigate to the next and previous month and select month and year from the drop-down list.

You can customize arrow icons with React Node and the dropdowns with className and CSS stylesheets.

import { Header } from "headless-react-datepicker";

props

NameTypeDescription
leftIconReactNode
rightIconReactNode
monthSelectClassNamestringClass name of the month select dropdown
monthSelectStylesCSSPropertiescss styles of the month select dropdown
monthOptionClassNamestringclassName of the month Options in the dropdown
monthOptionStylesCSSPropertiescss styles of the month Options in the dropdown
monthSelectedOptionClassNamestringclassName the selected option in the month dropdown
monthSelectedOptionStylesCSSPropertiescss styles the selected option in the month dropdown
yearSelectClassNamestringclassName of the year select dropdown
yearSelectStylesCSSPropertiescss styles of the year select dropdown
yearOptionClassNamestringclassName of the year Options in the dropdown
yearOptionStylesCSSPropertiescss styles of the year Options in the dropdown
yearSelectedOptionClassNamestringclassName the selected option in the year dropdown
yearSelectedOptionStylesCSSPropertiescss styles the selected option in the year dropdown

WeekDays component

The WeekDays component is used to display the weekday header.

import { WeekDays } from "headless-react-datepicker";

props

NameTypeDescription
renderer(args: TWeekDaysRendererArgs) => ReactNodeCustom renderer. If provided, the whole component will be ignored
classNamestringCustom class name for the element
rootClassNamestringCustom class name for the parent root element

TWeekDaysRendererArgs

NameTypeOptionsDescription
formattedTitlestringTitle based on calendar config.weekdayFormat which follows locale and calendar.
weekIndexnumber
weekDayTDay"monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"

DaySlots component

The DaySlots component is used to display the month and year based on the selected locale and calendar.

import { DaySlots } from "headless-react-datepicker";

props

NameTypeDescriptionDefault
dayRenderer(args: TDaySlotsDayRendererArgs) => ReactNodeCustom renderer
slotParentClassNamestring
slotParentStylesCSSProperties
slotClassNamestring
slotStylesCSSProperties
todayStylesCSSProperties
todayClassNamestring
todayParentStylesCSSProperties
todayParentClassNamestring
disableStylesCSSProperties
disableClassNamestring
disableParentStylesCSSProperties
disableParentClassNamestring
weekendStylesCSSProperties
weekendClassNamestring
weekendParentStylesCSSProperties
weekendParentClassNamestring
selectedStylesCSSProperties
selectedClassNamestring
selectedParentStylesCSSProperties
selectedParentClassNamestring
selectableStylesCSSProperties
selectableClassNamestring
selectableParentStylesCSSProperties
selectableParentClassNamestring
inSelectedRangeStylesCSSProperties
inSelectedRangeClassNamestring
inSelectedRangeParentStylesCSSProperties
inSelectedRangeParentClassNamestring
startOfRangeStylesCSSProperties
startOfRangeClassNamestring
startOfRangeParentStylesCSSProperties
startOfRangeParentClassNamestring
endOfRangeStylesCSSProperties
endOfRangeClassNamestring
endOfRangeParentStylesCSSProperties
endOfRangeParentClassNamestring

TDaySlotsDayRendererArgs props

NameTypeOptionsDefault
dateDatebased on calendar config.dayFormat which follows locale and calendar.
formattedDaystringFormatted date based on locale and calendar which is in the calendar config.
IsTodaybooleanIndicate that is the day is today or not.
isSelectablebooleanIs the day can be selected or not.
isDisabledbooleanIs the day is disabled or not.
isInSelectedRangebooleanIs in the selected range (if calendar type is range) or not.
isStartOfRangeboolean
isEndOfRangeboolean
isInWeekendboolean
isSelectedboolean
handleClickSlot(date: Date) => void

Date picker Context

You can access almost all props and functions of the date picker from the date picker context, so you can customize and build your own custom component easily.

example

import { useDatePickerContext } from "headless-react-datepicker";

const MyCustomAwesomeHeader = () => {
  const { goToCurrentMonth, yearInTheCalendar } = useDatePickerContext();

  return <div onClick={goToCurrentMonth}>{yearInTheCalendar}</div>;
};

returned props

NameTypeDescription
goToNextMonth() => voidFunction to navigate to the next month
goToPrevMonth() => voidFunction to navigate to the previous month
goToDate(date: Date) => voidFunction to navigate to a specific date
goToCurrentMonth() => voidFunction to navigate to the current month
goToMonth(month: number) => voidLocal month (based on the desired calendar)
goToYear(year: number) => voidHandle go to year (based on desire calendar)
daysOfMonthDate[]All the dates of the month rendered in the calendar
startDateIncludeOtherDaysDateEnd date of the month rendered in the calendar (include previous month days (if in the week))
endDateIncludeOtherDaysDateStart date of the month rendered in the calendar (include next month days (if in the week))
firstDayOfMonthDateFirst day of the month
lastDayOfMonthDateLast day of the month
selectedDayDate | Date[] | undefinedThe selected day in the calendar
handleClickSlot(date: Date) => voidCallback function when a date is clicked
monthInTheCalendarnumberCurrent month in the calendar (based on desire calendar)
totalDaysInTheCalendarnumberIndicate the total days in the month
yearInTheCalendarnumberCurrent year in the desire calendar
monthsListTMonthListItem[]List of all month based on culture
yearsListnumber[]List of all years based on yearRangeFrom and yearRangeTo
initialValueDate | Date[]The initial value of the date picker.
defaultStartDateDateThe default start date. Useful when you want to be on different month or year despite the initial value.
configTCalendarConfigThe configuration for the date picker.
isRangebooleanIndicates whether the date picker is a range picker.
calendarTCalendarThe calendar to use.

Dependencies

Using the Intl API which is ECMAScript Internationalization API with a very good browser support and depends on Temporal for converting other calendars into gregory.

License

MIT @ Sepehr09

Contributing

We're eagerly welcoming to contributors who want to help build and maintain this repo. PRs are always welcome!

Change log

you can see the change log here