UNPKG

20.6 kBTypeScriptView Raw
1import React from "react";
2import type { DeprecatedUI } from "../UI.js";
3import type { Locale, DateLib } from "../classes/DateLib.js";
4import type { ClassNames, ModifiersClassNames, Styles, ModifiersStyles, CustomComponents, Matcher, Labels, Formatters, MonthChangeEventHandler, DayEventHandler, Modifiers, DateRange, Mode } from "./shared.js";
5/**
6 * The props for the `<DayPicker />` component.
7 *
8 * @group DayPicker
9 */
10export type DayPickerProps = PropsBase & (PropsSingle | PropsSingleRequired | PropsMulti | PropsMultiRequired | PropsRange | PropsRangeRequired | {
11 mode?: undefined;
12 required?: undefined;
13});
14/**
15 * Props for customizing the calendar, handling localization, and managing
16 * events. These exclude the selection mode props.
17 *
18 * @group DayPicker
19 * @see https://daypicker.dev/api/interfaces/PropsBase
20 */
21export interface PropsBase {
22 /**
23 * Enable the selection of a single day, multiple days, or a range of days.
24 *
25 * @see https://daypicker.dev/docs/selection-modes
26 */
27 mode?: Mode | undefined;
28 /**
29 * Whether the selection is required.
30 *
31 * @see https://daypicker.dev/docs/selection-modes
32 */
33 required?: boolean | undefined;
34 /** Class name to add to the root element. */
35 className?: string;
36 /**
37 * Change the class names used by DayPicker.
38 *
39 * Use this prop when you need to change the default class names — for
40 * example, when importing the style via CSS modules or when using a CSS
41 * framework.
42 *
43 * @see https://daypicker.dev/docs/styling
44 */
45 classNames?: Partial<ClassNames> & Partial<DeprecatedUI<string>>;
46 /**
47 * Change the class name for the day matching the `modifiers`.
48 *
49 * @see https://daypicker.dev/guides/custom-modifiers
50 */
51 modifiersClassNames?: ModifiersClassNames;
52 /** Style to apply to the root element. */
53 style?: React.CSSProperties;
54 /**
55 * Change the inline styles of the HTML elements.
56 *
57 * @see https://daypicker.dev/docs/styling
58 */
59 styles?: Partial<Styles> & Partial<DeprecatedUI<React.CSSProperties>>;
60 /**
61 * Change the class name for the day matching the {@link modifiers}.
62 *
63 * @see https://daypicker.dev/guides/custom-modifiers
64 */
65 modifiersStyles?: ModifiersStyles;
66 /** A unique id to add to the root element. */
67 id?: string;
68 /**
69 * The initial month to show in the calendar.
70 *
71 * Use this prop to let DayPicker control the current month. If you need to
72 * set the month programmatically, use {@link month} and {@link onMonthChange}.
73 *
74 * @defaultValue The current month
75 * @see https://daypicker.dev/docs/navigation
76 */
77 defaultMonth?: Date;
78 /**
79 * The month displayed in the calendar.
80 *
81 * As opposed to `defaultMonth`, use this prop with `onMonthChange` to change
82 * the month programmatically.
83 *
84 * @see https://daypicker.dev/docs/navigation
85 */
86 month?: Date;
87 /**
88 * The number of displayed months.
89 *
90 * @defaultValue 1
91 * @see https://daypicker.dev/docs/customization#multiplemonths
92 */
93 numberOfMonths?: number;
94 /**
95 * The earliest month to start the month navigation.
96 *
97 * @since 9.0.0
98 * @see https://daypicker.dev/docs/navigation#start-and-end-dates
99 */
100 startMonth?: Date | undefined;
101 /**
102 * @private
103 * @deprecated This prop has been removed. Use `hidden={{ before: date }}`
104 * instead.
105 * @see https://daypicker.dev/docs/navigation#start-and-end-dates
106 */
107 fromDate?: Date | undefined;
108 /**
109 * @private
110 * @deprecated This prop has been renamed to `startMonth`.
111 * @see https://daypicker.dev/docs/navigation#start-and-end-dates
112 */
113 fromMonth?: Date | undefined;
114 /**
115 * @private
116 * @deprecated Use `startMonth` instead. E.g. `startMonth={new Date(year,
117 * 0)}`.
118 * @see https://daypicker.dev/docs/navigation#start-and-end-dates
119 */
120 fromYear?: number | undefined;
121 /**
122 * The latest month to end the month navigation.
123 *
124 * @since 9.0.0
125 * @see https://daypicker.dev/docs/navigation#start-and-end-dates
126 */
127 endMonth?: Date;
128 /**
129 * @private
130 * @deprecated This prop has been removed. Use `hidden={{ after: date }}`
131 * instead.
132 * @see https://daypicker.dev/docs/navigation#start-and-end-dates
133 */
134 toDate?: Date;
135 /**
136 * @private
137 * @deprecated This prop has been renamed to `endMonth`.
138 * @see https://daypicker.dev/docs/navigation#start-and-end-dates
139 */
140 toMonth?: Date;
141 /**
142 * @private
143 * @deprecated Use `endMonth` instead. E.g. `endMonth={new Date(year, 0)}`.
144 * @see https://daypicker.dev/docs/navigation#start-and-end-dates
145 */
146 toYear?: number;
147 /**
148 * Paginate the month navigation displaying the `numberOfMonths` at a time.
149 *
150 * @see https://daypicker.dev/docs/customization#multiplemonths
151 */
152 pagedNavigation?: boolean;
153 /**
154 * Render the months in reversed order (when {@link numberOfMonths} is set) to
155 * display the most recent month first.
156 *
157 * @see https://daypicker.dev/docs/customization#multiplemonths
158 */
159 reverseMonths?: boolean;
160 /**
161 * Hide the navigation buttons. This prop won't disable the navigation: to
162 * disable the navigation, use {@link disableNavigation}.
163 *
164 * @since 9.0.0
165 * @see https://daypicker.dev/docs/navigation#hidenavigation
166 */
167 hideNavigation?: boolean;
168 /**
169 * Disable the navigation between months. This prop won't hide the navigation:
170 * to hide the navigation, use {@link hideNavigation}.
171 *
172 * @see https://daypicker.dev/docs/navigation#disablenavigation
173 */
174 disableNavigation?: boolean;
175 /**
176 * Show dropdowns to navigate between months or years.
177 *
178 * - `true`: display the dropdowns for both month and year
179 * - `label`: display the month and the year as a label. Change the label with
180 * the `formatCaption` formatter.
181 * - `month`: display only the dropdown for the months
182 * - `year`: display only the dropdown for the years
183 *
184 * **Note:** showing the dropdown will set the start/end months
185 * {@link startMonth} to 100 years ago, and {@link endMonth} to the end of the
186 * current year.
187 *
188 * @see https://daypicker.dev/docs/customization#caption-layouts
189 */
190 captionLayout?: "label" | "dropdown" | "dropdown-months" | "dropdown-years";
191 /**
192 * Display always 6 weeks per each month, regardless of the month’s number of
193 * weeks. Weeks will be filled with the days from the next month.
194 *
195 * @see https://daypicker.dev/docs/customization#fixed-weeks
196 */
197 fixedWeeks?: boolean;
198 /**
199 * Hide the row displaying the weekday row header.
200 *
201 * @since 9.0.0
202 */
203 hideWeekdays?: boolean;
204 /**
205 * Show the outside days (days falling in the next or the previous month).
206 *
207 * **Note:** when {@link broadcastCalendar} is set, this prop defaults to true.
208 *
209 * @see https://daypicker.dev/docs/customization#outside-days
210 */
211 showOutsideDays?: boolean;
212 /**
213 * Show the week numbers column. Weeks are numbered according to the local
214 * week index.
215 *
216 * - To use ISO week numbering, use the `ISOWeek` prop.
217 * - To change how the week numbers are displayed, use the `formatters` prop.
218 *
219 * @see https://daypicker.dev/docs/customization#showweeknumber
220 */
221 showWeekNumber?: boolean;
222 /**
223 * Display the weeks in the month following the broadcast calendar. Setting
224 * this prop will ignore {@link weekStartsOn} (always Monday) and
225 * {@link showOutsideDays} will default to true.
226 *
227 * @since 9.4.0
228 * @see https://daypicker.dev/docs/localization#broadcast-calendar
229 * @see https://en.wikipedia.org/wiki/Broadcast_calendar
230 */
231 broadcastCalendar?: boolean;
232 /**
233 * Use ISO week dates instead of the locale setting. Setting this prop will
234 * ignore `weekStartsOn` and `firstWeekContainsDate`.
235 *
236 * @see https://daypicker.dev/docs/localization#iso-week-dates
237 * @see https://en.wikipedia.org/wiki/ISO_week_date
238 */
239 ISOWeek?: boolean;
240 /**
241 * The time zone (IANA or UTC offset) to use in the calendar (experimental).
242 * See
243 * [Wikipedia](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
244 * for the possible values.
245 *
246 * Time zones are supported by the `TZDate` object by the
247 * [@date-fns/tz](https://github.com/date-fns/tz) package. Please refer to the
248 * package documentation for more information.
249 *
250 * @since 9.1.1
251 * @experimental
252 * @see https://daypicker.dev/docs/time-zone
253 */
254 timeZone?: string | undefined;
255 /**
256 * Change the components used for rendering the calendar elements.
257 *
258 * @see https://daypicker.dev/guides/custom-components
259 */
260 components?: Partial<CustomComponents>;
261 /**
262 * Add a footer to the calendar, acting as a live region.
263 *
264 * Use this prop to communicate the calendar's status to screen readers.
265 * Prefer strings over complex UI elements.
266 *
267 * @see https://daypicker.dev/guides/accessibility#footer
268 */
269 footer?: React.ReactNode | string;
270 /**
271 * When a selection mode is set, DayPicker will focus the first selected day
272 * (if set) or today's date (if not disabled).
273 *
274 * Use this prop when you need to focus DayPicker after a user action, for
275 * improved accessibility.
276 *
277 * @see https://daypicker.dev/guides/accessibility#autofocus
278 */
279 autoFocus?: boolean;
280 /**
281 * @private
282 * @deprecated This prop will be removed. Use {@link autoFocus} instead.
283 */
284 initialFocus?: boolean;
285 /**
286 * Apply the `disabled` modifier to the matching days.
287 *
288 * @see https://daypicker.dev/docs/selection-modes#disabling-dates
289 */
290 disabled?: Matcher | Matcher[] | undefined;
291 /**
292 * Apply the `hidden` modifier to the matching days. Will hide them from the
293 * calendar.
294 *
295 * @see https://daypicker.dev/guides/custom-modifiers#hidden-modifier
296 */
297 hidden?: Matcher | Matcher[] | undefined;
298 /**
299 * The today’s date. Default is the current date. This date will get the
300 * `today` modifier to style the day.
301 *
302 * @see https://daypicker.dev/guides/custom-modifiers#today-modifier
303 */
304 today?: Date;
305 /**
306 * Add modifiers to the matching days.
307 *
308 * @see https://daypicker.dev/guides/custom-modifiers
309 */
310 modifiers?: Record<string, Matcher | Matcher[] | undefined> | undefined;
311 /**
312 * Labels creators to override the defaults. Use this prop to customize the
313 * aria-label attributes in DayPicker.
314 *
315 * @see https://daypicker.dev/docs/translation#aria-labels
316 */
317 labels?: Partial<Labels>;
318 /**
319 * Formatters used to format dates to strings. Use this prop to override the
320 * default functions.
321 *
322 * @see https://daypicker.dev/docs/translation#custom-formatters
323 */
324 formatters?: Partial<Formatters>;
325 /**
326 * The text direction of the calendar. Use `ltr` for left-to-right (default)
327 * or `rtl` for right-to-left.
328 *
329 * @see https://daypicker.dev/docs/translation#rtl-text-direction
330 */
331 dir?: HTMLDivElement["dir"];
332 /**
333 * The aria-label attribute to add to the container element.
334 *
335 * @since 9.4.1
336 * @see https://daypicker.dev/guides/accessibility
337 */
338 ["aria-label"]?: string;
339 /**
340 * The role attribute to add to the container element.
341 *
342 * @since 9.4.1
343 * @see https://daypicker.dev/guides/accessibility
344 */
345 role?: "application" | "dialog" | undefined;
346 /**
347 * A cryptographic nonce ("number used once") which can be used by Content
348 * Security Policy for the inline `style` attributes.
349 */
350 nonce?: HTMLDivElement["nonce"];
351 /** Add a `title` attribute to the container element. */
352 title?: HTMLDivElement["title"];
353 /** Add the language tag to the container element. */
354 lang?: HTMLDivElement["lang"];
355 /**
356 * The locale object used to localize dates. Pass a locale from
357 * `react-day-picker/locale` to localize the calendar.
358 *
359 * @example
360 * import { es } from "react-day-picker/locale";
361 * <DayPicker locale={es} />
362 *
363 * @defaultValue enUS - The English locale default of `date-fns`.
364 * @see https://daypicker.dev/docs/localization
365 * @see https://github.com/date-fns/date-fns/tree/main/src/locale for a list of the supported locales
366 */
367 locale?: Partial<Locale> | undefined;
368 /**
369 * The index of the first day of the week (0 - Sunday). Overrides the locale's
370 * one.
371 *
372 * @see https://daypicker.dev/docs/localization#first-date-of-the-week
373 */
374 weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined;
375 /**
376 * The day of January, which is always in the first week of the year.
377 *
378 * @see https://daypicker.dev/docs/localization#first-week-contains-date
379 */
380 firstWeekContainsDate?: 1 | 4;
381 /**
382 * Enable `DD` and `DDDD` for week year tokens when formatting or parsing
383 * dates.
384 *
385 * @see https://date-fns.org/docs/Unicode-Tokens
386 */
387 useAdditionalWeekYearTokens?: boolean | undefined;
388 /**
389 * Enable `YY` and `YYYY` for day of year tokens when formatting or parsing
390 * dates.
391 *
392 * @see https://date-fns.org/docs/Unicode-Tokens
393 */
394 useAdditionalDayOfYearTokens?: boolean | undefined;
395 /**
396 * Event fired when the user navigates between months.
397 *
398 * @see https://daypicker.dev/docs/navigation#onmonthchange
399 */
400 onMonthChange?: MonthChangeEventHandler;
401 /**
402 * Event handler when the next month button is clicked.
403 *
404 * @see https://daypicker.dev/docs/navigation
405 */
406 onNextClick?: MonthChangeEventHandler;
407 /**
408 * Event handler when the previous month button is clicked.
409 *
410 * @see https://daypicker.dev/docs/navigation
411 */
412 onPrevClick?: MonthChangeEventHandler;
413 /**
414 * Event handler when a week number is clicked.
415 *
416 * @private
417 * @deprecated Use a custom `WeekNumber` component instead.
418 * @see https://daypicker.dev/docs/customization#showweeknumber
419 */
420 onWeekNumberClick?: any;
421 /** Event handler when a day is clicked. */
422 onDayClick?: DayEventHandler<React.MouseEvent>;
423 /** Event handler when a day is focused. */
424 onDayFocus?: DayEventHandler<React.FocusEvent>;
425 /** Event handler when a day is blurred. */
426 onDayBlur?: DayEventHandler<React.FocusEvent>;
427 /** Event handler when a key is pressed on a day. */
428 onDayKeyDown?: DayEventHandler<React.KeyboardEvent>;
429 /** Event handler when the mouse enters a day. */
430 onDayMouseEnter?: DayEventHandler<React.MouseEvent>;
431 /** Event handler when the mouse leaves a day. */
432 onDayMouseLeave?: DayEventHandler<React.MouseEvent>;
433 /**
434 * Replace the default date library with a custom one. Experimental: not
435 * guaranteed to be stable (may not respect semver).
436 *
437 * @since 9.0.0
438 * @experimental
439 */
440 dateLib?: Partial<typeof DateLib.prototype> | undefined;
441 /**
442 * @private
443 * @deprecated Use a custom `DayButton` component instead.
444 */
445 onDayKeyUp?: DayEventHandler<React.KeyboardEvent>;
446 /**
447 * @private
448 * @deprecated Use a custom `DayButton` component instead.
449 */
450 onDayKeyPress?: DayEventHandler<React.KeyboardEvent>;
451 /**
452 * @private
453 * @deprecated Use a custom `DayButton` component instead.
454 */
455 onDayPointerEnter?: DayEventHandler<React.PointerEvent>;
456 /**
457 * @private
458 * @deprecated Use a custom `DayButton` component instead.
459 */
460 onDayPointerLeave?: DayEventHandler<React.PointerEvent>;
461 /**
462 * @private
463 * @deprecated Use a custom `DayButton` component instead.
464 */
465 onDayTouchCancel?: DayEventHandler<React.TouchEvent>;
466 /**
467 * @private
468 * @deprecated Use a custom `DayButton` component instead.
469 */
470 onDayTouchEnd?: DayEventHandler<React.TouchEvent>;
471 /**
472 * @private
473 * @deprecated Use a custom `DayButton` component instead.
474 */
475 onDayTouchMove?: DayEventHandler<React.TouchEvent>;
476 /**
477 * @private
478 * @deprecated Use a custom `DayButton` component instead.
479 */
480 onDayTouchStart?: DayEventHandler<React.TouchEvent>;
481}
482/**
483 * Shared handler type for `onSelect` callback when a selection mode is set.
484 *
485 * @template T - The type of the selected item.
486 * @callback OnSelectHandler
487 * @param {T} selected - The selected item after the event.
488 * @param {Date} triggerDate - The date when the event was triggered.
489 * @param {Modifiers} modifiers - The modifiers associated with the event.
490 * @param {React.MouseEvent | React.KeyboardEvent} e - The event object.
491 * @group DayPicker
492 */
493export type OnSelectHandler<T> = (selected: T, triggerDate: Date, modifiers: Modifiers, e: React.MouseEvent | React.KeyboardEvent) => void;
494/**
495 * The props when the single selection is required.
496 *
497 * @group DayPicker
498 * @see https://daypicker.dev/docs/selection-modes#single-mode
499 */
500export interface PropsSingleRequired {
501 mode: "single";
502 required: true;
503 /** The selected date. */
504 selected: Date | undefined;
505 /** Event handler when a day is selected. */
506 onSelect?: OnSelectHandler<Date>;
507}
508/**
509 * The props when the single selection is optional.
510 *
511 * @group DayPicker
512 * @see https://daypicker.dev/docs/selection-modes#single-mode
513 */
514export interface PropsSingle {
515 mode: "single";
516 required?: false | undefined;
517 /** The selected date. */
518 selected?: Date | undefined;
519 /** Event handler when a day is selected. */
520 onSelect?: OnSelectHandler<Date | undefined>;
521}
522/**
523 * The props when the multiple selection is required.
524 *
525 * @group DayPicker
526 * @see https://daypicker.dev/docs/selection-modes#multiple-mode
527 */
528export interface PropsMultiRequired {
529 mode: "multiple";
530 required: true;
531 /** The selected dates. */
532 selected: Date[] | undefined;
533 /** Event handler when days are selected. */
534 onSelect?: OnSelectHandler<Date[]>;
535 /** The minimum number of selectable days. */
536 min?: number;
537 /** The maximum number of selectable days. */
538 max?: number;
539}
540/**
541 * The props when the multiple selection is optional.
542 *
543 * @group DayPicker
544 * @see https://daypicker.dev/docs/selection-modes#multiple-mode
545 */
546export interface PropsMulti {
547 mode: "multiple";
548 required?: false | undefined;
549 /** The selected dates. */
550 selected?: Date[] | undefined;
551 /** Event handler when days are selected. */
552 onSelect?: OnSelectHandler<Date[] | undefined>;
553 /** The minimum number of selectable days. */
554 min?: number;
555 /** The maximum number of selectable days. */
556 max?: number;
557}
558/**
559 * The props when the range selection is required.
560 *
561 * @group DayPicker
562 * @see https://daypicker.dev/docs/selection-modes#range-mode
563 */
564export interface PropsRangeRequired {
565 mode: "range";
566 required: true;
567 disabled?: Matcher | Matcher[] | undefined;
568 /**
569 * When `true`, the range will reset when including a disabled day.
570 *
571 * @since V9.0.2
572 */
573 excludeDisabled?: boolean | undefined;
574 /** The selected range. */
575 selected: DateRange | undefined;
576 /** Event handler when a range is selected. */
577 onSelect?: OnSelectHandler<DateRange>;
578 /** The minimum number of days to include in the range. */
579 min?: number;
580 /** The maximum number of days to include in the range. */
581 max?: number;
582}
583/**
584 * The props when the range selection is optional.
585 *
586 * @group DayPicker
587 * @see https://daypicker.dev/docs/selection-modes#range-mode
588 */
589export interface PropsRange {
590 mode: "range";
591 required?: false | undefined;
592 disabled?: Matcher | Matcher[] | undefined;
593 /**
594 * When `true`, the range will reset when including a disabled day.
595 *
596 * @since V9.0.2
597 */
598 excludeDisabled?: boolean | undefined;
599 /** The selected range. */
600 selected?: DateRange | undefined;
601 /** Event handler when the selection changes. */
602 onSelect?: OnSelectHandler<DateRange | undefined>;
603 /** The minimum number of days to include in the range. */
604 min?: number;
605 /** The maximum number of days to include in the range. */
606 max?: number;
607}