UNPKG

1.94 kBPlain TextView Raw
1import type { DayPickerProps } from "./props.js";
2import type { DateRange, Modifiers } from "./shared.js";
3
4export type Selection<T extends DayPickerProps> = {
5 /** The selected date(s). */
6 selected: SelectedValue<T> | undefined;
7 /** Set a selection. */
8 select: SelectHandler<T> | undefined;
9 /** Whether the given date is selected. */
10 isSelected: (date: Date) => boolean;
11};
12
13export type SelectedSingle<T extends { required?: boolean }> =
14 T["required"] extends true ? Date : Date | undefined;
15export type SelectedMulti<T extends { required?: boolean }> =
16 T["required"] extends true ? Date[] : Date[] | undefined;
17export type SelectedRange<T extends { required?: boolean }> =
18 T["required"] extends true ? DateRange : DateRange | undefined;
19
20export type SelectedValue<T> = T extends { mode: "single"; required?: boolean }
21 ? SelectedSingle<T>
22 : T extends { mode: "multiple"; required?: boolean }
23 ? SelectedMulti<T>
24 : T extends { mode: "range"; required?: boolean }
25 ? SelectedRange<T>
26 : undefined;
27
28export type SelectHandlerSingle<T extends DayPickerProps> = (
29 triggerDate: Date,
30 modifiers: Modifiers,
31 e: React.MouseEvent | React.KeyboardEvent
32) => T["required"] extends true ? Date : Date | undefined;
33
34export type SelectHandlerMulti<T extends DayPickerProps> = (
35 triggerDate: Date,
36 modifiers: Modifiers,
37 e: React.MouseEvent | React.KeyboardEvent
38) => T["required"] extends true ? Date[] : Date[] | undefined;
39
40export type SelectHandlerRange<T extends DayPickerProps> = (
41 triggerDate: Date,
42 modifiers: Modifiers,
43 e: React.MouseEvent | React.KeyboardEvent
44) => T["required"] extends true ? DateRange : DateRange | undefined;
45
46export type SelectHandler<T extends DayPickerProps> = T extends {
47 mode: "single";
48}
49 ? SelectHandlerSingle<T>
50 : T extends { mode: "multiple" }
51 ? SelectHandlerMulti<T>
52 : T extends { mode: "range" }
53 ? SelectHandlerRange<T>
54 : undefined;