npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ryanwilldev/react-calendario

v1.1.2

Published

A flexible and customizable primitive React component for creating internationalized calendars and date pickers

Downloads

7

Readme

React Calendario

A flexible and highly customizable primitive React component written in Typescript that provides the base calendar functionality for creating internationalized calendars or date pickers using the render prop pattern.

The Problem

You need to create a calendar or date picker with a custom look and feel and multi-language support.

This Solution

This component provides the logic for generating and manipulating dates for a calendar or a date picker while allowing you to have full control over the UI. It uses the Intl.DateTimeFormat API to provide multi-language support for weekdays and months. It also takes advantage of the flexiblility provided by the Render Prop Pattern so you can decide exactly how your calendar or date picker should look.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save @ryanwilldev/react-calendario

This package also depends on react. Please make sure you have it installed as well.

Usage

<Calendario startDate={new Date(1990, 1)} language="es">
  {({
    dates,
    i18n,
    currentMonth,
    incrementMonth,
    decrementMonth,
    nextMonth,
    previousMonth,
    currentYear,
  }) => (
    <div>
      <h1>{i18n.monthsFull[currentMonth] + ' ' + currentYear}</h1>
      <div
        style={{
          marginBottom: '1rem',
          display: 'flex',
          justifyContent: 'space-around',
        }}
      >
        <button onClick={decrementMonth}>
          {'< ' + i18n.monthsFull[previousMonth]}
        </button>
        <button onClick={incrementMonth}>
          {i18n.monthsFull[nextMonth] + ' >'}
        </button>
      </div>
      <div
        style={{
          marginBottom: '.5rem',
          display: 'flex',
          justifyContent: 'space-around',
        }}
      >
        {i18n.weekDaysShort.map(d => (
          <span>{d}</span>
        ))}
      </div>
      {dates.map(w => (
        <div
          style={{
            marginBottom: '.5rem',
            display: 'flex',
            justifyContent: 'space-around',
          }}
        >
          {w.map(d => (
            <span>{d.day}</span>
          ))}
        </div>
      ))}
    </div>
  )}
</Calendario>

Live Examples

Basic usage

Types

CalendarioDate

interface CalendarioDate {
  day: number;
  month: number;
  siblingMonth: boolean;
  value: string;
  year: number;
}

Represents a date in the calendar used by the component. The siblingMonth prop is true when a day in the first or last week of the month falls in a the previous or next month.

i18n

interface i18n {
  monthsFull: String[];
  monthsShort: String[];
  weekDaysFull: String[];
  weekDaysNarrow: String[];
  weekDaysShort: String[];
}

i18n is a abbreviation for internationalization. This is passed to the child of Calendario to display internationalized month and weekday names.

FullCalendar

interface FullCalendar {
  currentMonth: number;
  currentYear: number;
  dates: Array<CalendarioDate[]>;
  i18n: i18n;
  nextMonth: number;
  previousMonth: number;
}

The full reprensentation of the calendar for the current month.

Props

Component Props

The props passed to the Calendario component. All component props are optional.

startDate

CalendarioDate | Date | defaults to undefined

The date to create the month from. The created calendar wil include all the days in the month that the startDate falls in.

render

Function | defaults to undefined

A function that returns JSX for Calendario to render.

language

string | defaults to undefined

A supported browser language code. If no language prop is passed the browser's current language will be used.

Render Function Props

The props passed to the renderProp that is given to Calendario either as a prop named render or as a child funciton to the Calendario component.

previousMonth

number

A zero indexed number for the month previous to the current month.

currentMonth

number

A zero indexed number for the current month.

previousMonth

number

A zero indexed number for the month after the current month.

currentYear

number

The current year for the calendar.

dates

`Array<CalendarioDate[]>

A nested array of CalendarioDate. Each inner array represents one week of the current month.

[
  [
    { day: 1, month: 1, year: 2018, siblingMonth: false },
    ...
  ]
]

i18n

{
  monthsFull: String[],
  monthsShort: String[],
  weekDaysFull: String[],
  weekDaysNarrow: String[],
  weekDaysShort: String[],
}

i18n is a abbreviation for internationalization. This object contains arrays of internationalized weekday and month names than can be used to display the months and weekdays in any language supported by the browser.

incrementMonth

() => void

This function should be placed on any button in your template that need to increment the current month by one.

decrementMonth

() => void

This function should be placed on any button in your template that need to decrement the current month by one.

convertToNativeDate

(d: CalendarioDate) => Date | undefined

A function to convert the CalendarioDates to the native Date object. If convertToNativeDate is not given a CalendarioDate to convert it will return undefined and log an error to the console.

Inspiration

The idea for using the render prop pattern to provide a flexible primitive component was inspired by downshift.

Other Solutions

react-calendar