1 | import { createContext, useContext } from "react";
|
2 |
|
3 | import { CalendarDay } from "./classes/CalendarDay.js";
|
4 | import { CalendarMonth } from "./classes/CalendarMonth.js";
|
5 | import type { DayPickerProps } from "./types/props.js";
|
6 | import type { SelectedValue, SelectHandler } from "./types/selection.js";
|
7 | import { Modifiers } from "./types/shared.js";
|
8 |
|
9 |
|
10 | export const dayPickerContext = createContext<
|
11 | DayPickerContext<DayPickerProps> | undefined
|
12 | >(undefined);
|
13 |
|
14 | export type DayPickerContext<T extends DayPickerProps> = {
|
15 |
|
16 | months: CalendarMonth[];
|
17 |
|
18 | nextMonth: Date | undefined;
|
19 |
|
20 | previousMonth: Date | undefined;
|
21 |
|
22 | goToMonth: (month: Date) => void;
|
23 |
|
24 | getModifiers: (day: CalendarDay) => Modifiers;
|
25 |
|
26 | selected: SelectedValue<T> | undefined;
|
27 |
|
28 | select: SelectHandler<T> | undefined;
|
29 |
|
30 | isSelected: ((date: Date) => boolean) | undefined;
|
31 | };
|
32 |
|
33 | /**
|
34 | * Return the context to work with `<DayPicker />` inside custom components.
|
35 | *
|
36 | * @group Hooks
|
37 | * @see https://daypicker.dev/guides/custom-components
|
38 | */
|
39 | export function useDayPicker<T extends DayPickerProps>(props?: T) {
|
40 | const context = useContext(dayPickerContext);
|
41 | if (context === undefined) {
|
42 | throw new Error("useDayPicker() must be used within a custom component.");
|
43 | }
|
44 | return context as DayPickerContext<T>;
|
45 | }
|
46 |
|
\ | No newline at end of file |