@datepicker-react/hooks

中文文档
2.8.4发布于 2021/10/24MITTypeScriptESM + CJS近 30 天下载 13.5万

React hooks for datepicker.

原始使用文档
文档目录17 节
  1. Getting Started
  2. Install
  3. Include hooks
  4. Example (codesandbox)
  5. Articles
  6. useDatepicker
  7. useDatepickerProps
  8. unavailableDates?: Date[]
  9. useDatepickerResult
  10. useMonth
  11. useMonthProps
  12. useMonthResult
  13. useDay
  14. useDayProps
  15. useDayResult
  16. Who's using
  17. License

@datepicker-react/hooks

Gzip size Coverage Status Build Status Netlify Status

NPM

Getting Started

Install

yarn add @datepicker-react/hooks

Include hooks

import {useDatepicker, useMonth, useDay} from '@datepicker-react/hooks'

Example (codesandbox)

Articles

useDatepicker

The useDatepicker hook returns functions like goToPreviousMonths, goToNextMonths, etc., which allows us to control the datepicker.

useDatepickerProps

onDatesChange: (data: OnDatesChangeProps) => void

A callback is triggered when the date is selected (onDaySelect).

minBookingDate: Date | undefined

If prop is set, then all dates before minBookingDate are disabled.

maxBookingDate: Date | undefined

If prop is set, then all dates after maxBookingDate are disabled.

startDate: Date | null

Current start date.

endDate: Date | null

Current end date.

focusedInput: 'startDate' | 'endDate' | null

Current focused date.

numberOfMonths: number | undefined (Default: 2)

Number of visible months.

minBookingDays: number | undefined (Default: 1)

Minimum allowed date range in days.

exactMinBookingDays: boolean | undefined (Default: false)

If exactMinBookingDays is true, then the range of dates is always the same as minBookingDays.

firstDayOfWeek: 0 | 1 | 2 | 3 | 4 | 5 | 6 (Default: 1 - Monday)

First day of the week.

initialVisibleMonth? Date

Initial visible month

isDateBlocked: (date: Date) => boolean

If isDateBlocked returns true, then the date is blocked.

changeActiveMonthOnSelect?: boolean

If this is false, the active month panel will not change when selecting the start date.

unavailableDates?: Date[]

Receives unavailable dates in array.

useDatepickerResult

firstDayOfWeek: 0 | 1 | 2 | 3 | 4 | 5 | 6

First day of the week.

activeMonths: ({year: number, month: number})[]

Array of visible months. Each month is an object that contains year and month.

numberOfMonths: number

Number of visible months.

focusedDate: Date | null

Focused date.

isDateSelected: (date: Date) => boolean

Returns true if a date is selected or within the selected range, otherwise false.

isDateHovered: (date: Date) => boolean

Returns true if a date is hovered, otherwise false.

isDateBlocked: (date: Date) => boolean

Returns true if a date is blocked, otherwise false.

isDateFocused: (date: Date) => boolean

Returns true if a date is focused, otherwise false.

isFirstOrLastSelectedDate: (date: Date) => boolean

Returns true if a date is the first or the last date in the selected range, otherwise false.

isStartDate: (date: Date) => boolean

Returns true if a date is the first or the last date in the selected range, otherwise false.

isEndDate: (date: Date) => boolean

Returns true if a date is the first or the last date in the selected range, otherwise false.

onResetDates: () => void

Reset start and end date.

onDateHover: (date: Date) => void

Set internal hovered state.

onDateSelect: (date: Date) => void

Select a date. Which date is selected, depends on focusedInput.

onDateFocus: (date: Date) => void

Set focusedDate.

goToDate: (date: Date) => void

Updates activeMonths to specific date.

goToNextMonths: () => void

Updates activeMonths (next months) in accordance with the numberOfMonths prop.

goToNextMonthsByOneMonth: () => void

Updates activeMonths (next months) by one month.

goToPreviousMonths: () => void

Updates activeMonths (previous months) in accordance with the numberOfMonths prop.

goToPreviousMonthsByOneMonth: () => void

Updates activeMonths (previous months) by one month.

goToPreviousYear: (numYears: number = 1) => void

Go to the previous year by default.

goToNextYear: (numYears: number = 1) => void

Go to the next year by default.

useMonth

The useMonth returns all days of the month, weekday labels and month label.

useMonthProps

year

Year

month

Month

firstDayOfWeek: number | undefined (Default: 1)

First day of the week.

dayLabelFormat: (date: Date) => string | undefined (Default: (date: Date) => format(date, 'MMMM yyyy'))

Formats day label.

weekdayLabelFormat: (date: Date) => string | undefined (Default: (date: Date) => format(date, 'eeeeee'))

Formats weekday label.

monthLabelFormat: (date: Date) => string | undefined (Default: (date: Date) => format(date, 'dd'))

Formats month label.

useMonthResult

days: ({date: Date, dayLabel: string})[]

Array of day objects. Each object contains date and dayLabel.

weekdayLabels: (string)[]

Array of weekday labels.

monthLabel: string

Month label.

useDay

useDayProps

date: Date

Day date

dayRef: React.RefObject<HTMLButtonElement>

Ref of a day button.

focusedDate: Date | null

isDateFocused: (date: Date) => boolean

isDateSelected: (date: Date) => boolean

isDateHovered: (date: Date) => boolean

isDateBlocked: (date: Date) => boolean

isFirstOrLastSelectedDate: (date: Date) => boolean

onDateFocus: (date: Date) => void

onDateSelect: (date: Date) => void

onDateHover: (date: Date) => void

useDayResult

tabIndex: 0 | -1

tabIndex that you pass to the day button tabIndex.

onKeyDown: (e: KeyboardEvent) => void

onKeyDown callback that you can pass to the day button onKeyDown.

onClick: () => void

onClick callback that you pass to the day button onClick.

onMouseEnter: () => void

onMouseEnter callback that you pass to the day button onMouseEnter.

disabledDate: boolean

If true, then the date is disabled.

isWithinHoverRange: boolean

If true, then the date is within hover range.

isSelectedStartOrEnd: boolean

If true, then the date is the first or the last selected date.

isSelected: boolean

If true, then the date is selected.

Who's using

LifeOnScreen
@datepicker-react/styled

License

Licensed under the MIT License, Copyright © 2019-present Miha Sedej.

See LICENSE for more information.


Buy me a coffee

版本与发布记录

@datepicker-react/hooks 共发布 37 个版本,首次发布于 2019/04/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.8.4latest2021/10/245 年前
2.8.02021/02/115 年前
2.7.02020/07/016 年前
2.6.02020/06/236 年前
2.5.02020/06/196 年前
2.4.12020/06/176 年前

其他发布通道:canary → 1.1.1-alpha.41。

关于 @datepicker-react/hooks 的常见问题

@datepicker-react/hooks 是什么?

React hooks for datepicker.

如何安装 @datepicker-react/hooks?

在项目目录运行 npm install @datepicker-react/hooks,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>= 16.8.5)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@datepicker-react/hooks 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@datepicker-react/hooks 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@datepicker-react/hooks 可以商用吗?

@datepicker-react/hooks 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@datepicker-react/hooks 的下载量有多少?

根据 npm 官方下载统计,@datepicker-react/hooks 在 2026/10/12 – 2026/11/10 这 30 天内被下载 134,834 次,日均约 4,494 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@datepicker-react/hooks 还在维护吗?

最新版本 2.8.4 发布于 2021/10/24(5 年前);至今共发布 37 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/04/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。