1.0.0 • Published 5 years ago

react-use-calendar v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

react-use-calendar

Custom React Hook for implementing a calendar with events

Build Status Coverage Status dependencies Status npm version

Installation

npm install react-use-calendar --save

Usage

import React from 'react';
import useCalendar from 'react-use-calendar';

function App() {

  const [state, actions] = useCalendar(null, {
    events: [
      { 
        startDate: new Date(2019, 1, 27), 
        endDate: new Date(2019, 1, 27),  
        note: 'Meeting with clients' 
      },
      { 
        startDate: new Date(2019, 1, 22), 
        endDate: new Date(2019, 1, 25),
        note: 'Vacation'
      }
    ]
  });

  return (
    <table>
      <thead>
        <tr>
          <td colSpan={5} style={{ textAlign: 'center' }}>
            <strong>{state.month} - {state.year}</strong>
          </td>
          <td colSpan={2} style={{ textAlign: 'right' }}>
            <button onClick={() => actions.getPrevMonth()}>
              &lt;
            </button>              
            <button onClick={() => actions.getNextMonth()}>
              &gt;
            </button>              
          </td>
        </tr>
        <tr>
          {state.days.map(day => <th key={day}>{day}</th>)}
        </tr>
      </thead>
      <tbody>
        {state.weeks.map((week, index) => 
          <tr key={index}>
            {week.map(day =>
              <td key={day.dayOfMonth} style={{ textAlign: 'center', backgroundColor: day.isToday ? '#ff0' : '#fff' }}>
                {day.dayOfMonth}
              </td>
            )}
          </tr>
        )}
      </tbody>
    </table>
  );

}

API

useCalendar

const [state, actions] = useCalendar(date, config);

Parameters

FieldTypeDescription
datedateInitialize calendar with starting date
configobjectConfiguration

config

KeyTypeDescription
eventsarrayCalendar events as an array of objects. [{ startDate: date, endDate: date, note: string }]
numOfWeeksnumberNumber of calendar weeks. default: 6
numOfDaysnumberNumber of days per week. default: 7
rtlbooleanEnable right-to-left
localeobjectdate-fns locale

Returns

state

KeyTypeDescription
daysarrayShort names for days of week ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
weeksarrayWeeks of calendar [{ day: object }]
monthstringCurrent month in full month format
yearnumberCurrent year
{
  date: date,
  dayIndex: number,
  dayOfMonth: number,
  dayOfWeek: string,
  dayOfYear: number,
  events: array,
  isToday: boolean,
  isSameMonth: boolean,
  isWeekend: boolean,
  weekIndex: number
}

actions

KeyTypeDescription
setDatefunctionSet current day for Calendar function(today: date)
getNextMonthfunctionSet calendar to next month
getPrevMonthfunctionSet calendar to previous month
addEventfunctionAdd an event to calendar. function(event: { startDate: date, endDate: date, note: string })
removeEventfunctionRemove event from calendar function(id: number)

Localization

import React from 'react';
import useCalendar from 'react-use-calendar';

import ruLocale from 'date-fns/locale/ru';

function App() {

  const [state, actions] = useCalendar(null, { locale: ruLocale });

  return (
    <div>
      ...
    </div>
  );
}

License

The files included in this repository are licensed under the MIT license.