项目作者: rubencodes

项目描述 :
A minimal, accessible React/Preact calendar component using modern CSS.
高级语言: JavaScript
项目地址: git://github.com/rubencodes/recal.git
创建时间: 2018-04-05T20:55:53Z
项目社区:https://github.com/rubencodes/recal

开源协议:MIT License

下载






recal logo



📅 recal 📅

npm version
license
bundle size
Lighthouse score: 100/100

A minimal, accessible React/Preact calendar component using modern CSS, for modern browsers. Now with a 100 Lighthouse Accessibility Audit score. It works with native Javascript dates, so there’s no need to import any heavy dependencies like moment. For a set of functions for working with Javascript Dates, we recommend date-fns. For a more flexible, fully-featured set of calendar components, see react-dates.



example calendar

See a Live Demo

Try on CodePen

Installation

Using recal is simple. Just install the npm package:

  1. npm i -S recal

Or, import recal and its stylesheet via CDN:

  1. <link rel="stylesheet" type="text/css" href="https://unpkg.com/recal/lib/index.css" />
  2. <script src="https://unpkg.com/recal"></script>

Usage

If you’re using recal from npm, be sure to import the necessary modules into the file you wish to use it in.

  1. // You can use React or Preact here—just make sure you have the proper aliasing.
  2. import React from 'react';
  3. // Calendar components.
  4. import { DatePicker, DateRangePicker } from 'recal';
  5. // Stylesheet for calendar.
  6. import 'recal/lib/index.css';

Date Picker

To create a single date picker, use the DatePicker component as follows:

  1. class MyDatePicker extends React.Component {
  2. state = {};
  3. onDateSelected = (selectedDate) => {
  4. this.setState({
  5. selectedDate
  6. });
  7. }
  8. render() {
  9. return (
  10. <DatePicker
  11. date={ this.state.selectedDate }
  12. onDateSelected={ this.onDateSelected } ></DatePicker>
  13. );
  14. }
  15. }

Date Range Picker

To create a date range picker, use the DateRangePicker component as follows:

  1. class MyDateRangePicker extends React.Component {
  2. state = {};
  3. onStartDateSelected = (startDate) => {
  4. this.setState({
  5. startDate
  6. });
  7. }
  8. onEndDateSelected = (endDate) => {
  9. this.setState({
  10. endDate
  11. });
  12. }
  13. render() {
  14. return (
  15. <DateRangePicker
  16. startDate={ this.state.startDate }
  17. endDate={ this.state.endDate }
  18. onStartDateSelected={ this.onStartDateSelected }
  19. onEndDateSelected={ this.onEndDateSelected } ></DateRangePicker>
  20. );
  21. }
  22. }

Options

Both calendars have some required and some optional props.

  1. // Used by DatePicker
  2. selectedDate: PropTypes.instanceOf(Date),
  3. onDateSelected: PropTypes.func,
  4. // Used by DateRangePicker
  5. startDate: PropTypes.instanceOf(Date),
  6. endDate: PropTypes.instanceOf(Date),
  7. onStartDateSelected: PropTypes.func,
  8. onEndDateSelected: PropTypes.func,
  9. // Used by either (optional)
  10. onDateHovered: PropTypes.func,
  11. onDateFocused: PropTypes.func,
  12. isDateHighlighted: PropTypes.func,
  13. isDateEnabled: PropTypes.func,
  14. locale: PropTypes.string,
  15. disabled: PropTypes.bool

Localization

Use the locale string prop on the calendar components to localize the month and days of the week into other languages (e.g. “en-US”, “es-MX”, etc.).

Accessibility

This set of calendars are optimized for screen readers as well as for keyboard-based navigation. The following shortcuts are available when the calendar is focused:

Key Direction Time
Left Arrow Back 1 day
Right Arrow Forward 1 day
Up Arrow Back 1 week
Down Arrow Forward 1 week
Page Up Back 1 month
Page Down Forward 1 month
Shift + Page Up Back 1 year
Shift + Page Down Forward 1 year