react-calendar

repository·main·Indexed 26 days ago

https://github.com/wojtekmaj/react-calendar

A lightweight, highly customizable calendar component for React applications. It supports picking days, months, years, or decades, as well as range selection, without requiring moment.js. The library provides a primary Calendar component with controlled and uncontrolled modes, specific view components (CenturyView, DecadeView, MonthView, YearView), and an imperative API via refs. It includes utilities for localized date formatting and supports various calendar systems and locales.

Tokens
2.3K
Snippets
7
Records
16
Agent score
87%

What's inside react-calendar

  1. Configure date return values and range selection

    main

    Control how dates are returned to your handlers using these props:

    • selectRange: Set to true to allow selecting a start and end date.
    • returnValue: Determines which part of the date is returned when selectRange is false. Options are 'start', 'end', or 'range' (returns an array of two values).
    • allowPartialRange: When selectRange is true, setting this to true allows onChange to be called with a partial result (e.g., only the start date).
  2. Configure the Calendar component via props

    main

    The Calendar component is the primary entry point for react-calendar. It can be used in both controlled (using value and view) and uncontrolled (using defaultValue and defaultView) modes.

    Key configuration categories include:

    • Date Range & Limits: Control selectable dates using minDate, maxDate, and selectRange (for selecting two dates).
    • View Control: Manage the visible granularity using view, maxDetail, and minDetail (options: 'month', 'year', 'decade', 'century').
    • Formatting: Override default date labels using functions like formatDay, formatMonth, formatYear, etc.
    • Customization: Inject custom styles or content into tiles using tileClassName, tileContent, and tileDisabled.
  3. Use the Calendar imperative API via ref

    main

    You can access the internal state and methods of the Calendar component using a ref. The ref exposes the following properties and methods:

    • activeStartDate: The current beginning of the displayed period.
    • value: The currently selected value(s).
    • view: The current view granularity.
    • drillDown(): Programmatically drills down to a more detailed view.
    • drillUp(): Programmatically drills up to a less detailed view.
    • onChange(value, event): Programmatically triggers a change.
    • setActiveStartDate(date, action): Programmatically changes the active start date.
  4. Format dates using react-calendar utilities

    main

    The react-calendar package exports several utility functions for formatting dates into localized strings. These functions use Intl.DateTimeFormat internally and include a workaround (toSafeHour) to prevent formatting bugs in WebKit and Firefox when dealing with historical dates and Daylight Saving Time (DST).

    Available formatting utilities:

    • formatDate(locale, date): Formats as numeric day, month, and year.
    • formatDay(locale, date): Formats as numeric day.
    • formatLongDate(locale, date): Formats as numeric day, long month, and numeric year.
    • formatMonth(locale, date): Formats as long month.
    • formatMonthYear(locale, date): Formats as long month and numeric year.
    • formatShortWeekday(locale, date): Formats as short weekday.
    • formatWeekday(locale, date): Formats as long weekday.
    • formatYear(locale, date): Formats as numeric year.

    All functions accept an optional locale string. If locale is undefined, the user's locale is detected automatically via get-user-locale.

  5. Handle calendar change and interaction events

    main

    The Calendar component provides several event handlers to respond to user interactions:

    • onChange: Called when a user selects a date. If selectRange is enabled, it returns an array of two dates.
    • onActiveStartDateChange: Called when the user navigates between periods (e.g., clicking next/prev).
    • onViewChange: Called when the user changes the view granularity (e.g., drilling up or down).
    • onDrillDown / onDrillUp: Specific handlers for view depth changes.
    • onClickDay, onClickMonth, onClickYear, onClickDecade: Specific handlers for clicking items in their respective views.
    • onClickWeekNumber: Called when a user clicks a week number in the MonthView.
  6. Reference Calendar types and interfaces

    main

    The package exports several types for configuring the calendar and defining callback functions. Use these for type safety in your React components:

    Component Props

    • CalendarProps: The configuration object for the Calendar component.

    Callback and Utility Types

    • CalendarType: Represents the calendar instance/type.
    • NavigationLabelFunc: Function type for generating navigation labels.
    • OnArgs: Arguments passed to event handlers.
    • OnClickFunc: Function type for click events.
    • OnClickWeekNumberFunc: Function type for clicking week numbers.
    • TileArgs: Arguments passed to tile-related functions.
    • TileClassNameFunc: Function type for determining tile CSS classes.
    • TileContentFunc: Function type for rendering custom content inside tiles.
    • TileDisabledFunc: Function type for determining if a tile should be disabled.
  7. Supported Locales for Calendar Types

    main

    The CALENDAR_TYPE_LOCALES object maps specific CalendarType values to arrays of supported locale strings. This can be used to understand which locales are compatible with specific calendar systems (e.g., hebrew supports ['he', 'he-IL']).

    export const CALENDAR_TYPE_LOCALES: Partial<Record<CalendarType, string[]>> = {
      gregory: [
        'en-CA',
        'en-US',
        // ... other en/es/pt locales
      ],
      hebrew: ['he', 'he-IL'],
      islamic: [
        'ar',
        'ar-AE',
        // ... other ar/dv/ps locales
      ],
    };