@_--/react-calendar 中文文档教程

发布于 3年前 浏览 114 更新于 3年前

@_--/react-calendar

用于反应的零依赖日历组件。

许多部分仍然不完整/待办事项,以及伪劣的编码风格哎呀。

features

  • single, multi, and range selection of dates; multi selection also supports drag-and-hold to (un)select multiple dates at once.
  • keyboard navigation; unfortunately not aria-compliant grid, but supports…
    • arrow keys to navigate along tiles with wrapping,
    • tab to toggle between nav and tiles
    • shift + tab from nav to escape focus from calendar; shift+tab again after that to go to the previos element
    • home/end) to go to the start (end) of the row
    • shift + home/end to go to the first (last) tile
    • esc to unselect partial range dates, or otherwise escape focus from the calendar; tab after that to go to the next element
  • highlight/disable dates
  • hide dates before/after/outside the current month
  • set which day of the week the calendar begins on
  • set which days of the week are "weekends" to highlight
  • customize how dates/months/years/decades are displayed
  • calendar view locking
  • separate css for core styles, default styles, and useful/common add-ons, all using low-precedence selectors and css custom properties

installation

# npm
npm install @_--/react-calendar

# yarn
yarn add @_--/react-calendar

usage

import React, { useState } from 'react';
import { Calendar } from '@_--/react-calendar';

const App = () => {
  const [selectedDates, setSelectedDates] = useState([]);
  return (
      <Calendar
        onChangeSelectedDates={(newState, oldState) => { setSelectedDates(newState); }}
      />
  )
}

styling

导入主样式表以制作一个最小的无样式日历

import '@_--/react-calendar/dist/styles/index.css';

,您可以单独使用或基于默认样式构建

import '@_--/react-calendar/dist/styles/default.css';

还提供预设以强制 month 视图中的日历图块为正方形

import '@_--/react-calendar/dist/styles/preset/square-tiles.css';

组件使用 css 自定义样式属性,使它们易于重新设计和定制

calendar props

types

nametype
DayOfWeek0 <= x < 7

props

propdescriptiontypedefault
classNameclassname to add to the calendar container elementstring
selectModehow dates are to be "selected"."single" | "multi" | "range""single"
weekStartwhich day of the week occupies the leftmost column of the calendar, where 0=Sunday, 1=Monday, …DayOfWeek0
weekendDayswhich days of the week constitute as "weekends". weekends can be styled differently.DayOfWeek[][0, 6]
initialDatethe date the calendar will initialize its displayed dates withDatenew Date()
initialViewwhat "view" the calendar should begin with"month" | "year" | "decade""month"
hideAdjacentMonthsDateswhether dates from the adjacent months should be hidden in the "month" view (ie the 30th of the previous month, or the 1st of the next month). true hides bothboolean | "prev" | "next"
lockViewwhether the calendar view cannot be changed from its initial view of decade, year, or monthbooleanfalse
lockTileswhether the calendar view and tiles should be locked.booleanfalse
disabledDatesfunction describing which dates should be disabled. disabled dates cannot be selected.(Date) => boolean | "past" | "today" | "future"
highlightedDatesfunction describing which dates should be highlighted. highlighted dates can be styled differently(Date) => boolean | "today""today"
prevButtonIconicon for the previous button for calendar navigation within a viewstring | JSX.element"←"
nextButtonIconicon for the next button for calendar navigation within a viewstring | JSX.element"→"
formatDecadehow to format the decade view heading, given the first year of the decade(default formatter; ie "2000 - 2009")
formatYearhow to format the year view heading and decade view tiles(default formatter; ie "2000")
formatMonthYearhow to format the month view heading(Date) => string(default formatter; ie "January 2000")
formatMonthhow to format the year view tile labels(Date) => string(default formatter; ie "January")
formatDayOfMonthhow to format the month view tiles(Date) => string(default formatter; ie "1")
formatDayOfWeekhow to format the day of week labels(DayOfWeek) => string(default formatter; ie "Mon")
onChangeViewfunction called whenever the calendar view is changed(newView, oldView) => unknown
onChangeSelectModefunction called whenever the select mode is changed(newSelectMode, oldSelectMode) => unknown(default callback: if switching from range to multi select mode, selects all dates in the range; otherwise, clears selected dates)
onChangeSelectedDatesfunction called whenever the selected dates change(newSelectedDates, oldSelectedDates) => unknown
onChangePartialRangeDatesfunction called whenever the partial range dates, used for "soft highlighting" while in range select or drag-and-holding in multi select(newPartialRangeDates, oldPartialRangeDates) => unknown

@_--/react-calendar

a zero-dependency calendar component for react.

many parts still incomplete/todo, and shoddy coding style oops.

features

  • single, multi, and range selection of dates; multi selection also supports drag-and-hold to (un)select multiple dates at once.
  • keyboard navigation; unfortunately not aria-compliant grid, but supports…
    • arrow keys to navigate along tiles with wrapping,
    • tab to toggle between nav and tiles
    • shift + tab from nav to escape focus from calendar; shift+tab again after that to go to the previos element
    • home/end) to go to the start (end) of the row
    • shift + home/end to go to the first (last) tile
    • esc to unselect partial range dates, or otherwise escape focus from the calendar; tab after that to go to the next element
  • highlight/disable dates
  • hide dates before/after/outside the current month
  • set which day of the week the calendar begins on
  • set which days of the week are "weekends" to highlight
  • customize how dates/months/years/decades are displayed
  • calendar view locking
  • separate css for core styles, default styles, and useful/common add-ons, all using low-precedence selectors and css custom properties

installation

# npm
npm install @_--/react-calendar

# yarn
yarn add @_--/react-calendar

usage

import React, { useState } from 'react';
import { Calendar } from '@_--/react-calendar';

const App = () => {
  const [selectedDates, setSelectedDates] = useState([]);
  return (
      <Calendar
        onChangeSelectedDates={(newState, oldState) => { setSelectedDates(newState); }}
      />
  )
}

styling

import the main stylesheet to make a minimally unstyled calendar

import '@_--/react-calendar/dist/styles/index.css';

you can separately use or build upon the default styles

import '@_--/react-calendar/dist/styles/default.css';

a preset is also provided to force calendar tiles in the month view to be square

import '@_--/react-calendar/dist/styles/preset/square-tiles.css';

the component is styled using css custom properties, making them easy to re-style and customize

calendar props

types

nametype
DayOfWeek0 <= x < 7

props

propdescriptiontypedefault
classNameclassname to add to the calendar container elementstring
selectModehow dates are to be "selected"."single" | "multi" | "range""single"
weekStartwhich day of the week occupies the leftmost column of the calendar, where 0=Sunday, 1=Monday, …DayOfWeek0
weekendDayswhich days of the week constitute as "weekends". weekends can be styled differently.DayOfWeek[][0, 6]
initialDatethe date the calendar will initialize its displayed dates withDatenew Date()
initialViewwhat "view" the calendar should begin with"month" | "year" | "decade""month"
hideAdjacentMonthsDateswhether dates from the adjacent months should be hidden in the "month" view (ie the 30th of the previous month, or the 1st of the next month). true hides bothboolean | "prev" | "next"
lockViewwhether the calendar view cannot be changed from its initial view of decade, year, or monthbooleanfalse
lockTileswhether the calendar view and tiles should be locked.booleanfalse
disabledDatesfunction describing which dates should be disabled. disabled dates cannot be selected.(Date) => boolean | "past" | "today" | "future"
highlightedDatesfunction describing which dates should be highlighted. highlighted dates can be styled differently(Date) => boolean | "today""today"
prevButtonIconicon for the previous button for calendar navigation within a viewstring | JSX.element"←"
nextButtonIconicon for the next button for calendar navigation within a viewstring | JSX.element"→"
formatDecadehow to format the decade view heading, given the first year of the decade(default formatter; ie "2000 - 2009")
formatYearhow to format the year view heading and decade view tiles(default formatter; ie "2000")
formatMonthYearhow to format the month view heading(Date) => string(default formatter; ie "January 2000")
formatMonthhow to format the year view tile labels(Date) => string(default formatter; ie "January")
formatDayOfMonthhow to format the month view tiles(Date) => string(default formatter; ie "1")
formatDayOfWeekhow to format the day of week labels(DayOfWeek) => string(default formatter; ie "Mon")
onChangeViewfunction called whenever the calendar view is changed(newView, oldView) => unknown
onChangeSelectModefunction called whenever the select mode is changed(newSelectMode, oldSelectMode) => unknown(default callback: if switching from range to multi select mode, selects all dates in the range; otherwise, clears selected dates)
onChangeSelectedDatesfunction called whenever the selected dates change(newSelectedDates, oldSelectedDates) => unknown
onChangePartialRangeDatesfunction called whenever the partial range dates, used for "soft highlighting" while in range select or drag-and-holding in multi select(newPartialRangeDates, oldPartialRangeDates) => unknown
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文