0.1.11 • Published 4 years ago

use-events-calendar-react v0.1.11

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

use-events-calendar-react

install: yarn add use-events-calendar-react

demo: https://github.com/DeathstarNovember/scheduler

Basic Usage

The useEventsCalendar() hook takes one required argument: initialEvents, and one optional argument initialSelectedDate.

initialEvents is an array of CalendarEvent to place on the calendar whan it loads.

initialSelectedDate is the date on which the calendar will focus when it loads, this is today by default.

useEventsCalendar() uses two other hooks under the hood, useCalendar: (initialSelectedDate?: Date) => UseCalendar and useEvents: (initialEvents: CalendarEvent[]) => UseEvents

For convenience, defaultCalendar and defaultEvents will satisfy types as an initializer for createContext<UseEventsCalendar>.

All types are named exports.

import React, {createContext, useContext} from "react";

import useEventsCalendar, {UseCalendar, UseEvents, defaultCalendar, defaultEvents} from "use-events-calendar-react";

const initialEvents = [];

const initialSelectedDate = new Date();

cosnt EventsCalendarContext = createContext<{calendar: UseCalendar, calendarEvents: UseEvents}>(
  {calendar: defaultCalendar, calendarEvents: defaultEvents}
)

const App = () => {

  const {calendar, calendarEvents} = useEventsCalendar(initialSelectedDate, initialEvents);

  const {
    today,
    months,
    weekdays,
    currentDay,
    currentWeek,
    currentYear,
    currentMonth,
    monthDisplayDays,
    weekDisplayDays,
    msInAMinute,
    msInAnHour,
    msInADay,
    msInAWeek,
    selectDay,
    getDayStyle,
    getFirstDayOfWeek,
    getWeekByDate,
    getNextWeek,
    getLastWeek,
    getDaysInMonth,
    getLastMonth,
    getNextMonth,
    getMonthFromDate,
    getFirstDayOfYear,
    getLastDayOfYear,
    getFirstWeekOfYear,
    getWeekdayFromWeek,
    getWeekNumber,
    loadPrevWeek,
    loadNextWeek,
    loadPrevMonth,
    loadNextMonth,
  } = calendar

  const {
    events, //event array
    getHourEvents,
    getDayEvents,
    getMonthEvents,
    setEvents,
    addEvent,
    updateEvent,
    deleteEvent,
  } = calendarEvents

  //recommend to leave the calendar and events separate in context.

  return(
    <EventsCalendarContext.Provider calendar={calendar} calendarEvents={calendarEvents}>
      <YourCustomCalendar />
    </EventsCalendarContext.Provider>
  )
}

Descriptions

Calendar State

ValueDescriptionType
currentDayThe selected DayDay
currentWeekThe Week containing the selected DayWeek
currentYearThe year as a number of the selectedDaynumber
currentMonthThe Month containing the selected DayMonth
monthDisplayDaysDay array containing all necessary Days to fill a w x 7 grid, including days from the next and previous months.Day[]
weekDisplayDaysDay array containing all days in the currentWeekDay[]
selectDayFunction to select a Day. Setting the current day will update all current{period} values.(day: Day) => void
loadPrevWeekChanges the currentDay to one week earlier.() => void
loadNextWeekChanges the currentDay to one week later.() => void
loadPrevMonthChanges the currentDay to one month earlier.() => void
loadNextMonthChanges the currentDay to one month later.() => void

Calendar Utils

ValueDescriptionType
getFirstDayOfWeekThe date of the first day of the week containing the given date.(Date: Date) => Date
getNextWeekInfo about the week after the currentWeek() => Week
getLastWeekInfo about the week before the currentWeek() => Week
getDaysInMonthGets the number of days in a given month and year.(m: number, y: number) => number
getLastMonthInfo about the month before the currentMonth() => Month
getNextMonthInfo about the month after the currentMonth() => Month
getWeekByDateInfo about the week containing a given date.(date: Date) => Week
getMonthFromDateInfo about the month containing a given date.(date: Date) => Month
getFirstDayOfYearThe first Day of a given year(year: number) => Date
getLastDayOfYearThe last Day of a given year(year: number) => Date
getFirstWeekOfYearThe first Week of a given year.(year: number) => Week
getWeekdayFromWeekThe Day that is a given weekday from a given week.(week: Week, weekday: DayOfWeek) => Day
getWeekNumberThe ISO week number of the week containing a given date.(date: Date) => number

Events State

ValueDescriptionType
eventsCalendarEvent array managed by useEventCalandar. Are initialized in the initial call to useEventsCalendar as the first argument.CalendarEvent[]
setEventsUpdates events.(event: NewCalendarEvent) => void
addEventAdds a given CalendarEvent to events.(event: CalendarEvent) => void
updateEventUpdates a given CalendarEvent from events.(event: CalendarEvent) => void
deleteEventRemoves a given CalendarEvent from events.(event: CalendarEvent) => void

Events Utilties

ValueDescriptionType
getHourEventsAll events from a given list where startDate is within an hour of start arg.(events: CalendarEvent[], start: Date) => CalendarEvent[]
getDayEventsAll events from a given list where startDate is on a given Day.(events: CalendarEvent[], day: Day) => CalendarEvent[]
getMonthEventsAll events from a given list where startDate is within a given Month in a given year.(events: CalendarEvent[], month: Month, year: number) => CalendarEvent[]

Static Data

ValueDescriptionType
todayToday’s DayDay
monthsStatic dataMonth[]
weekdaysStatic dataDayOfWeek[]
msInAMinuteStatic datanumber
msInAnHourStatic datanumber
msInADayStatic datanumber
msInAWeekStatic datanumber

Styles

ValueDescriptionType
getDayStyleGets a CSS style object for a given date.(day: Day, currentDay: Day, options?: DayStyleOptions) => CSS

Types

ValueDescriptionType
CSSAliasReact.CSSProperties
MonthInfo about a given Month{ name: string; shortName: string; id: number }
WeekInfo about a given Week{id: string; firstDay: Date;}
DayInfo about a given Day{date: Date; weekday: DayOfWeek; currentPeriod: boolean}
DayOfWeekInfo about a given weekday.{ name: string; shortName: string; id: number }
NewCalendarEventHandy for creating an event. Sets the id to Date.now().toString()Omit<\CalendarEvent, 'id'>\
DayStyleOptionsOptions object for use with getDayStyles(){currentDayStyles: CSS, todayStyles: CSS, pastDayStyles: CSS;futureDayStyles: CSS}
0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago