3.0.2 • Published 9 months ago

rhdp v3.0.2

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

RHDP - React Hook Datepicker

A React Hook based primitive to build simple, flexible datepicker components that are WAI-ARIA compliant, and support localization.

The Problem

You need a datepicker that can be fully customized in appearance, but it needs to be accessible, and flexible enough to meet your needs. It should work with any design system, including one built in-house, and have zero dependencies (besides React of course). It should also meet accessibility specs for screenreaders, as well as keyboard accessibility. It should also only concern itself with dates, a datepicker should not concern itself with time, or timezones.

The Solution

RHDP offers a single react hook called useDatepicker. It provides functions and properties to populate your own React based datepicker component. It is fully typed with Typescript and backed by interaction and unit tests. You can lift and shift any of the existing datepicker components built with RHDP in the examples and adapt them to your needs.

Use your own design system or one of the many publicly available design systems to easily match the datepicker with the look for your site. Just provide css classes for the applicable styles for them to be applied at the component level.

This React Hook Datepicker allows several modes of operation: single select, range select, multi-select, and max multi-select. Selectable dates can be limited by min and max dates as well as blocked dates. It also supports Sunday or Monday as the start of the week.

RHDP takes in the ISO 8601 extended date format, that's to say YYYY-MM-DD and outputs the same. This format allows for lexicographical sorting of dates, and removes any concerns or bugs that may arise from dealing with disparate local and server times.

Zero dependencies & purpose built.

Props

All props are fully typed in Typescript. This small subset is provided here for quick reference. View the full types.

PropertyTypeDescription
blockedDatesstring[] \| stringYYYY-MM-DD format, prevents selection of specified dates
focusOnInitbooleanWill cause focus to be set to the first of an array of dates or today
hasFocusTrapbooleanWill cause focus to be constrained to the datepicker and controls while the datepicker is open as a popover
labelsobjectButton labels for accessibility, English defaults provided
labels.closeButtonstring
labels.dateSelectedstring
labels.nextMonthButtonstring
labels.nextYearButtonstring
labels.previousMonthButtonstring
labels.previousYearButtonstring
labels.todaystring
localeIntl.DateTimeFormatDefaults to en-US, see Localization section for more details
maxDatestringYYYY-MM-DD format, if set prevents selection & navigation past that date
minDatestringYYYY-MM-DD format, if set prevents selection & navigation before that date
mode'single' \| 'range' \| 'multiple' \| numberSets the selection mode for the datepicker, number is a max # of dates
onChangefunction(string[] \| string)Function called whenever the selected dates change (string in single mode, otherwise an array)
onClosefunction()Function which is executed when date selection criteria is satisfied
selectDatesstring[] \| stringYYYY-MM-DD format, pre-selects the supplied dates
weekStart0 \| 1Changes the start day of the week from Sunday to Monday

Returned props

All returned props are fully typed in Typescript. This small subset is provided here for quick reference. View the full types.

PropertyTypeDescription
calendarCalendarDay[][]Represents the current month's calendar, see CalendarDay type for more details
displayDaysOfTheWeekDisplayDaysOfTheWeek[]Each day of the week, localized, available in full, short and narrow formats
displayMonthstringFull month name, localized, of the displayed calendar month
displayYearstring4 digit year of the displayed calendar month
focusedDatestringThe date which currently has focus
getCalendarContainerPropsfunction()Getter to apply attributes, methods & props to the calendar container
getCalendarDayContainerPropsfunction()Getter to apply attributes, methods & props to the calendar day container
getCalendarWeekContainerPropsfunction()Getter to apply attributes, methods & props to the calendar week container
getDayOfTheWeekPropsfunction()Getter to apply attributes, methods & props to the each day of the week
getDaysOfTheWeekContainerPropsfunction()Getter to apply attributes, methods & props to the days of the week container
getControlsContainerPropsfunction()Getter to apply attributes, methods & props to the controls container (prev/next month/year)
getDatepickerContainerPropsfunction()Getter to apply attributes, methods & props to the datepicker container
getDayButtonPropsfunction()Getter to apply attributes, methods & props to the buttons of each day in the calendar
getMonthYearContainerPropsfunction()Getter to apply attributes, methods & props to the month/year container
getNextMonthButtonPropsfunction()Getter to apply attributes, methods & props to the next month button
getNextYearButtonPropsfunction()Getter to apply attributes, methods & props to the next year button
getOnCloseButtonPropsfunction()Getter to apply attributes, methods & props to the hide datepicker button
getPreviousMonthButtonPropsfunction()Getter to apply attributes, methods & props to the previous month button
getPreviousYearButtonPropsfunction()Getter to apply attributes, methods & props to the previous year button
hoveredDatestringThe date which is currently hovered
idstringThe id of the datepicker ]
setMonthfunction(number)Set the month of the year for the calendar in the datepicker, 1 based (1-12)
setYearfunction(number)Set the 4 digit year for the calendar in the datepicker
todaystringToday's date in YYYY-MM-DD format

CalendarDay type

PropertyTypeDescription
blockedbooleanWhether the day is blocked and should not be selectable
disabledbooleanWhether the day is disabled and should not be selectable
displayTextstringThe number displayed in each cell of the calendar grid
formattedstringThe YYYY-MM-DD formatted string of the day
hoveredbooleanWhether the day is hovered
indexnumberThe day of the week index for this particular day (0-6)
inMonthbooleanWhether the day is in the visible month
inRangebooleanWhether the day is in a selected range (mode: 'range')
rangeEndbooleanWhether the day is the end of the selected range (mode: 'range')
rangeStartbooleanWhether the day is the start of the selected range (mode: 'range')
selectedbooleanWhether the day is selected
todaybooleanWhether the day is today

Popover

If the datepicker is going to be contained in a popover, focusOnInit, hasFocusTrap and onClose should be set in useDatepicker for proper operation. The button which shows/hides the datepicker should have aria-haspopup={true}. If an icon is used in the button instead of descriptive text, it should also have the aria-label attribute with an appropriate label. aria-describedby should contain the id of the element which contains the updated selected date if the value of the date needs to be announced when focus is on the open datepicker button.

Localization

Intl.DateTimeFormat is used to automatically localize the days of the week and the months of the year for display if the locale prop is passed with a valid locale. There are labels that can be passed in for various elements which are used by screen readers, and by default English values are supplied, but for other languages, the labels will need to be supplied by your localization system.

Bundling

If your target project is bundled with Webpack, it should use Webpack 5 for proper support of modern Class syntax.

Installation

  • yarn add rhdp or npm install --save rhdp

Examples

Storybook

yarn

git clone git@github.com:roypearce/rhdp.git
cd rhdp
yarn
yarn storybook

npm

git clone git@github.com:roypearce/rhdp.git
cd rhdp
npm install
npm run storybook
3.0.2

9 months ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago