0.1.6 • Published 5 years ago

react-nice-calendar v0.1.6

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

About

It is a calendar that aims to be simple and efficient for anyone using. Using CalendarProvider to display years, months and days.

Everything you will see here refers to all calendars in the repository, if there is a question about the calendar for a specific framework / library click on one of the buttons above (Available to).

Live demo

Getting Started

Install:

yarn add react-nice-calendar

Usage

const [selectedDate, setSelectedDate] = useState()

return (
  <Calendar
    selectedDate={selectedDate}
    onChangeSelectedDate={setSelectedDate}
  />
)

Props

NameTypeDescriptionDefault value
pick'single' | 'multiple' | 'range'Select the type of capture. See examples here.single'
pickLimitNon-negative integerUse when the "pick" prop is "multiple". It defines the maximum number of dates that can be selected.Infinity
rangeSize{min: Non-negative integer,max: Non-negative integer}Use when the "pick" prop is "range". Defines the minimum and maximum number of the range.{min: 1,max: Infinity}
onChangeSelectedDateWhen "pick" prop is "single": (date: Datenull) => voidWhen "pick" prop is "multiple" or "range": (date: Date[]) => voidFunction called when the user clicks on a date.n/a
selectedDateWhen "pick" prop is "single": Date | nullWhen "pick" prop is "multiple" or "range": Date[]n/a
monthsDictionarystring[]Each index in the array represents the month 0 through 11.['January','February','March','April','May','June','July','August','September','October','November','December']
daysDictionarystring[]Each index in the array represents the day 0 through 6.['S','M','T','W','T','F','S']
classNames{ [key in classNameKeys]: string }It receives the classes of each element of the calendar. See the ClassNameKeys type here.n/a
startDateDateThe date that the calendar should display on the first render.n/a
filterInvalidDates(date: Date) => booleanA function that will be called the date iteration. The "true" return indicates that the date is invalid, so it should not be selected by the user.n/a
bindCalendarBindIt is an object that is returned by "useCalendarBind". Click here to learn more.n/a
HeaderPropsHTMLDivElement props.Example: className, id & etc...n/a
PrevButtonPropsHTMLButtonElement props with custom onClick event.See custom events event here.n/a
NextButtonPropsHTMLButtonElement props with custom onClick event.See custom events event here.n/a
HeaderTextPropsHTMLButtonElement props with custom onClick event.See custom events event here.n/a
DaysPropsHTMLDivElement props.Example: className, id & etc...n/a
DayPropsHTMLDivElement props.Example: className, id & etc...n/a
BodyPropsHTMLDivElement props.Example: className, id & etc...n/a
CellsPropsHTMLDivElement props.Example: className, id & etc...n/a
DatePropsHTMLButtonElement props with custom onClick, mouseEnter & mouseLeave event.See custom events event here.n/a
MonthPropsHTMLButtonElement props with custom onClick event.See custom events event here.n/a
YearPropsHTMLButtonElement props with custom onClick event.See custom events event here.n/a

Custom events

When the element already has an event handler and you want to apply one more: The function returns the original handler and the original event object. Example:

  dateProps={{
    onClick: ({ originalEvent, originalHandler ) => {
      console.log(originalEvent.target)
      originalHandler()
    }
  }}

useCalendarBind

A hook to connect the calendars. The props, events and styles will be shared between them. Example:

import Calendar, { useCalendarBind } from 'react-nice-calendar'

const myComponent = () => {
  const [selectedDate, setSelectedDate] = useState()
  const bind = useCalendarBind()

  return (
    <>
      <Calendar
        selectedDate={selectedDate}
        onChangeSelectedDate={setSelectedDate}
        bind={bind}
      />
      <Calendar bind={bind} />
    </>
  )
}

Result:

ClassNameKeys

'Container'
'CalendarBinded'
'Header'
'HeaderMonth'
'HeaderYear'
'HeaderYearsRange'
'Days'
'Day'
'PrevButton'
'NextButton'
'HeaderText'
'Body'
'BodyDays'
'BodyMonths'
'BodyYears'
'Cells'
'Cell'
'DayCell'
'MonthCell'
'YearCell'
'ValidDate'
'InvalidDate'
'DayBelongCurrentMonth'
'CurrentDate'
'SelectedDate'
'BetweenRange'
'BetweenSelectedRange'
'UnderMinRange'
'OverMaxRange'
'InvalidDateBetweenRange'
'StartRangeDate'
'EndRangeDate'
'MouseOverEndRange'

The 'Calendar' class (which is the same element as the 'Container') is useful for knowing the order of this calendar when using the "bind" prop.

return (
  <Calendar
    selectedDate={selectedDate}
    onChangeSelectedDate={setSelectedDate}
    bind={bind}
  /> // Has the Calendar1 class
  <Calendar bind={bind} /> // Has the Calendar2 class
  <Calendar bind={bind} /> // Has the Calendar3 class
)