react-day-picker 9.1.0 → 9.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/dist/cjs/DayPicker.js +11 -11
- package/dist/cjs/DayPicker.js.map +1 -1
- package/dist/cjs/classes/CalendarDay.d.ts +1 -1
- package/dist/cjs/classes/CalendarDay.js.map +1 -1
- package/dist/cjs/components/WeekNumber.d.ts +1 -1
- package/dist/cjs/components/WeekNumber.js +2 -2
- package/dist/cjs/formatters/formatCaption.d.ts +1 -2
- package/dist/cjs/formatters/formatCaption.js.map +1 -1
- package/dist/cjs/formatters/formatDay.d.ts +1 -2
- package/dist/cjs/formatters/formatDay.js.map +1 -1
- package/dist/cjs/formatters/formatWeekdayName.d.ts +1 -2
- package/dist/cjs/formatters/formatWeekdayName.js.map +1 -1
- package/dist/cjs/helpers/getDateLib.d.ts +2 -1
- package/dist/cjs/helpers/getDateLib.js.map +1 -1
- package/dist/cjs/helpers/getDates.d.ts +1 -1
- package/dist/cjs/helpers/getDates.js +3 -3
- package/dist/cjs/helpers/getDates.js.map +1 -1
- package/dist/cjs/helpers/getDisplayMonths.d.ts +2 -1
- package/dist/cjs/helpers/getDisplayMonths.js.map +1 -1
- package/dist/cjs/helpers/getFocusableDate.d.ts +2 -1
- package/dist/cjs/helpers/getInitialMonth.d.ts +1 -1
- package/dist/cjs/helpers/getInitialMonth.js +6 -1
- package/dist/cjs/helpers/getInitialMonth.js.map +1 -1
- package/dist/cjs/helpers/getMonthOptions.d.ts +2 -2
- package/dist/cjs/helpers/getMonthOptions.js +6 -3
- package/dist/cjs/helpers/getMonthOptions.js.map +1 -1
- package/dist/cjs/helpers/getMonths.d.ts +3 -2
- package/dist/cjs/helpers/getMonths.js.map +1 -1
- package/dist/cjs/helpers/getNavMonth.d.ts +3 -2
- package/dist/cjs/helpers/getNavMonth.js +16 -3
- package/dist/cjs/helpers/getNavMonth.js.map +1 -1
- package/dist/cjs/helpers/getNextFocus.d.ts +3 -2
- package/dist/cjs/helpers/getNextFocus.js.map +1 -1
- package/dist/cjs/helpers/getNextMonth.d.ts +2 -1
- package/dist/cjs/helpers/getNextMonth.js.map +1 -1
- package/dist/cjs/helpers/getPreviousMonth.d.ts +2 -1
- package/dist/cjs/helpers/getPreviousMonth.js.map +1 -1
- package/dist/cjs/helpers/getWeekdays.d.ts +2 -3
- package/dist/cjs/helpers/getWeekdays.js +9 -3
- package/dist/cjs/helpers/getWeekdays.js.map +1 -1
- package/dist/cjs/helpers/getYearOptions.d.ts +2 -1
- package/dist/cjs/helpers/getYearOptions.js +6 -5
- package/dist/cjs/helpers/getYearOptions.js.map +1 -1
- package/dist/cjs/labels/labelGrid.d.ts +1 -2
- package/dist/cjs/labels/labelGrid.js.map +1 -1
- package/dist/cjs/labels/labelWeekday.d.ts +1 -2
- package/dist/cjs/labels/labelWeekday.js.map +1 -1
- package/dist/cjs/lib/dateLib.d.ts +66 -3
- package/dist/cjs/lib/dateLib.js +0 -2
- package/dist/cjs/lib/dateLib.js.map +1 -1
- package/dist/cjs/lib/index.d.ts +1 -0
- package/dist/cjs/lib/index.js +3 -0
- package/dist/cjs/lib/index.js.map +1 -1
- package/dist/cjs/locale.d.ts +1 -0
- package/dist/cjs/locale.js +18 -0
- package/dist/cjs/locale.js.map +1 -0
- package/dist/cjs/selection/useMulti.d.ts +2 -1
- package/dist/cjs/selection/useMulti.js +5 -2
- package/dist/cjs/selection/useMulti.js.map +1 -1
- package/dist/cjs/selection/useRange.d.ts +2 -1
- package/dist/cjs/selection/useRange.js +5 -2
- package/dist/cjs/selection/useRange.js.map +1 -1
- package/dist/cjs/selection/useSingle.d.ts +2 -1
- package/dist/cjs/selection/useSingle.js +5 -2
- package/dist/cjs/selection/useSingle.js.map +1 -1
- package/dist/cjs/types/props.d.ts +31 -6
- package/dist/cjs/types/selection.d.ts +14 -5
- package/dist/cjs/types/shared.d.ts +17 -8
- package/dist/cjs/useCalendar.d.ts +2 -2
- package/dist/cjs/useCalendar.js +6 -0
- package/dist/cjs/useCalendar.js.map +1 -1
- package/dist/cjs/useDayPicker.d.ts +30 -6
- package/dist/cjs/useDayPicker.js +10 -2
- package/dist/cjs/useDayPicker.js.map +1 -1
- package/dist/cjs/useFocus.d.ts +2 -1
- package/dist/cjs/useGetModifiers.d.ts +2 -1
- package/dist/cjs/useGetModifiers.js +8 -2
- package/dist/cjs/useGetModifiers.js.map +1 -1
- package/dist/cjs/useSelection.d.ts +2 -1
- package/dist/cjs/useSelection.js.map +1 -1
- package/dist/cjs/utc.js +1 -2
- package/dist/cjs/utc.js.map +1 -1
- package/dist/cjs/utils/addToRange.d.ts +2 -1
- package/dist/cjs/utils/addToRange.js.map +1 -1
- package/dist/cjs/utils/dateMatchModifiers.d.ts +2 -1
- package/dist/cjs/utils/dateMatchModifiers.js.map +1 -1
- package/dist/cjs/utils/rangeIncludesDate.d.ts +2 -1
- package/dist/cjs/utils/rangeIncludesDate.js.map +1 -1
- package/dist/cjs/utils/typeguards.d.ts +2 -1
- package/dist/esm/DayPicker.js +11 -11
- package/dist/esm/DayPicker.js.map +1 -1
- package/dist/esm/classes/CalendarDay.d.ts +1 -1
- package/dist/esm/classes/CalendarDay.js.map +1 -1
- package/dist/esm/components/WeekNumber.d.ts +1 -1
- package/dist/esm/components/WeekNumber.js +2 -2
- package/dist/esm/formatters/formatCaption.d.ts +1 -2
- package/dist/esm/formatters/formatCaption.js.map +1 -1
- package/dist/esm/formatters/formatDay.d.ts +1 -2
- package/dist/esm/formatters/formatDay.js.map +1 -1
- package/dist/esm/formatters/formatWeekdayName.d.ts +1 -2
- package/dist/esm/formatters/formatWeekdayName.js.map +1 -1
- package/dist/esm/helpers/getDateLib.d.ts +2 -1
- package/dist/esm/helpers/getDateLib.js.map +1 -1
- package/dist/esm/helpers/getDates.d.ts +1 -1
- package/dist/esm/helpers/getDates.js +3 -3
- package/dist/esm/helpers/getDates.js.map +1 -1
- package/dist/esm/helpers/getDisplayMonths.d.ts +2 -1
- package/dist/esm/helpers/getDisplayMonths.js.map +1 -1
- package/dist/esm/helpers/getFocusableDate.d.ts +2 -1
- package/dist/esm/helpers/getInitialMonth.d.ts +1 -1
- package/dist/esm/helpers/getInitialMonth.js +6 -1
- package/dist/esm/helpers/getInitialMonth.js.map +1 -1
- package/dist/esm/helpers/getMonthOptions.d.ts +2 -2
- package/dist/esm/helpers/getMonthOptions.js +6 -3
- package/dist/esm/helpers/getMonthOptions.js.map +1 -1
- package/dist/esm/helpers/getMonths.d.ts +3 -2
- package/dist/esm/helpers/getMonths.js.map +1 -1
- package/dist/esm/helpers/getNavMonth.d.ts +3 -2
- package/dist/esm/helpers/getNavMonth.js +16 -3
- package/dist/esm/helpers/getNavMonth.js.map +1 -1
- package/dist/esm/helpers/getNextFocus.d.ts +3 -2
- package/dist/esm/helpers/getNextFocus.js.map +1 -1
- package/dist/esm/helpers/getNextMonth.d.ts +2 -1
- package/dist/esm/helpers/getNextMonth.js.map +1 -1
- package/dist/esm/helpers/getPreviousMonth.d.ts +2 -1
- package/dist/esm/helpers/getPreviousMonth.js.map +1 -1
- package/dist/esm/helpers/getWeekdays.d.ts +2 -3
- package/dist/esm/helpers/getWeekdays.js +9 -3
- package/dist/esm/helpers/getWeekdays.js.map +1 -1
- package/dist/esm/helpers/getYearOptions.d.ts +2 -1
- package/dist/esm/helpers/getYearOptions.js +6 -5
- package/dist/esm/helpers/getYearOptions.js.map +1 -1
- package/dist/esm/labels/labelGrid.d.ts +1 -2
- package/dist/esm/labels/labelGrid.js.map +1 -1
- package/dist/esm/labels/labelWeekday.d.ts +1 -2
- package/dist/esm/labels/labelWeekday.js.map +1 -1
- package/dist/esm/lib/dateLib.d.ts +66 -3
- package/dist/esm/lib/dateLib.js +0 -2
- package/dist/esm/lib/dateLib.js.map +1 -1
- package/dist/esm/lib/index.d.ts +1 -0
- package/dist/esm/lib/index.js +1 -0
- package/dist/esm/lib/index.js.map +1 -1
- package/dist/esm/locale.d.ts +1 -0
- package/dist/esm/locale.js +2 -0
- package/dist/esm/locale.js.map +1 -0
- package/dist/esm/selection/useMulti.d.ts +2 -1
- package/dist/esm/selection/useMulti.js +5 -2
- package/dist/esm/selection/useMulti.js.map +1 -1
- package/dist/esm/selection/useRange.d.ts +2 -1
- package/dist/esm/selection/useRange.js +5 -2
- package/dist/esm/selection/useRange.js.map +1 -1
- package/dist/esm/selection/useSingle.d.ts +2 -1
- package/dist/esm/selection/useSingle.js +5 -2
- package/dist/esm/selection/useSingle.js.map +1 -1
- package/dist/esm/types/props.d.ts +31 -6
- package/dist/esm/types/selection.d.ts +14 -5
- package/dist/esm/types/shared.d.ts +17 -8
- package/dist/esm/useCalendar.d.ts +2 -2
- package/dist/esm/useCalendar.js +6 -0
- package/dist/esm/useCalendar.js.map +1 -1
- package/dist/esm/useDayPicker.d.ts +30 -6
- package/dist/esm/useDayPicker.js +10 -2
- package/dist/esm/useDayPicker.js.map +1 -1
- package/dist/esm/useFocus.d.ts +2 -1
- package/dist/esm/useGetModifiers.d.ts +2 -1
- package/dist/esm/useGetModifiers.js +8 -2
- package/dist/esm/useGetModifiers.js.map +1 -1
- package/dist/esm/useSelection.d.ts +2 -1
- package/dist/esm/useSelection.js.map +1 -1
- package/dist/esm/utc.js +1 -2
- package/dist/esm/utc.js.map +1 -1
- package/dist/esm/utils/addToRange.d.ts +2 -1
- package/dist/esm/utils/addToRange.js.map +1 -1
- package/dist/esm/utils/dateMatchModifiers.d.ts +2 -1
- package/dist/esm/utils/dateMatchModifiers.js.map +1 -1
- package/dist/esm/utils/rangeIncludesDate.d.ts +2 -1
- package/dist/esm/utils/rangeIncludesDate.js.map +1 -1
- package/dist/esm/utils/typeguards.d.ts +2 -1
- package/examples/Input.test.tsx +1 -1
- package/examples/ItalianLabels.tsx +1 -1
- package/examples/ModifiersHidden.test.tsx +1 -1
- package/examples/NumberingSystem.test.tsx +3 -3
- package/examples/PastDatesDisabled.tsx +7 -0
- package/examples/Range.test.tsx +1 -1
- package/examples/RangeShiftKey.tsx +1 -1
- package/examples/SingleControlled.test.tsx +37 -0
- package/examples/SingleControlled.tsx +8 -0
- package/examples/SingleMockDate.test.tsx +39 -0
- package/examples/TimeZone.tsx +23 -0
- package/examples/WeekStartsOn.tsx +7 -0
- package/examples/Weeknumber.test.tsx +2 -2
- package/examples/WeeknumberCustom.tsx +2 -2
- package/examples/__snapshots__/Range.test.tsx.snap +84 -100
- package/examples/__snapshots__/StylingCssModules.test.tsx.snap +42 -50
- package/examples/index.ts +4 -0
- package/package.json +19 -3
- package/src/DayPicker.test.tsx +13 -0
- package/src/DayPicker.tsx +13 -10
- package/src/classes/CalendarDay.ts +1 -1
- package/src/components/WeekNumber.tsx +3 -3
- package/src/formatters/formatCaption.ts +5 -2
- package/src/formatters/formatDay.ts +1 -2
- package/src/formatters/formatWeekdayName.ts +1 -2
- package/src/helpers/getDateLib.ts +2 -2
- package/src/helpers/getDates.ts +4 -5
- package/src/helpers/getDisplayMonths.ts +2 -1
- package/src/helpers/getFocusableDate.ts +1 -1
- package/src/helpers/getInitialMonth.ts +8 -1
- package/src/helpers/getMonthOptions.ts +8 -5
- package/src/helpers/getMonths.ts +3 -1
- package/src/helpers/getNavMonth.ts +23 -5
- package/src/helpers/getNextFocus.tsx +8 -2
- package/src/helpers/getNextMonth.ts +2 -1
- package/src/helpers/getPreviousMonth.ts +2 -1
- package/src/helpers/getWeekdays.test.ts +1 -1
- package/src/helpers/getWeekdays.ts +11 -4
- package/src/helpers/getYearOptions.ts +8 -7
- package/src/labels/labelGrid.ts +1 -2
- package/src/labels/labelWeekday.ts +1 -2
- package/src/lib/dateLib.ts +95 -3
- package/src/lib/index.ts +1 -0
- package/src/locale.ts +1 -0
- package/src/selection/useMulti.test.tsx +42 -0
- package/src/selection/useMulti.tsx +7 -3
- package/src/selection/useRange.test.tsx +41 -0
- package/src/selection/useRange.tsx +7 -3
- package/src/selection/useSingle.test.tsx +39 -0
- package/src/selection/useSingle.tsx +7 -3
- package/src/types/props.ts +31 -7
- package/src/types/selection.ts +12 -9
- package/src/types/shared.ts +17 -9
- package/src/useCalendar.ts +10 -3
- package/src/useDayPicker.ts +30 -6
- package/src/useFocus.ts +1 -1
- package/src/useGetModifiers.tsx +14 -3
- package/src/useSelection.ts +2 -1
- package/src/utc.tsx +1 -3
- package/src/utils/addToRange.ts +2 -2
- package/src/utils/dateMatchModifiers.ts +2 -2
- package/src/utils/rangeIncludesDate.ts +2 -2
- package/src/utils/typeguards.ts +1 -1
- package/website/docs/development/code-of-conduct.md +5 -7
- package/website/docs/development/contributing.md +9 -0
- package/website/docs/docs/localization.mdx +71 -85
- package/website/docs/docs/selection-modes.mdx +40 -26
- package/website/docs/docs/translation.mdx +3 -3
- package/website/docs/guides/custom-components.mdx +10 -10
- package/website/docs/intro.mdx +1 -1
- package/website/docs/start.mdx +1 -1
- package/website/docs/development/contributing.mdx +0 -11
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
3
|
import { useControlledValue } from "../helpers/useControlledValue.js";
|
|
4
|
+
import type { DateLib } from "../lib/dateLib.js";
|
|
4
5
|
import type {
|
|
5
|
-
DateLib,
|
|
6
6
|
DayPickerProps,
|
|
7
7
|
Modifiers,
|
|
8
8
|
PropsMulti,
|
|
@@ -19,11 +19,13 @@ export function useMulti<T extends DayPickerProps>(
|
|
|
19
19
|
onSelect
|
|
20
20
|
} = props as PropsMulti;
|
|
21
21
|
|
|
22
|
-
const [
|
|
22
|
+
const [internallySelected, setSelected] = useControlledValue(
|
|
23
23
|
initiallySelected,
|
|
24
24
|
onSelect ? initiallySelected : undefined
|
|
25
25
|
);
|
|
26
26
|
|
|
27
|
+
const selected = !onSelect ? internallySelected : initiallySelected;
|
|
28
|
+
|
|
27
29
|
const { isSameDay } = dateLib;
|
|
28
30
|
|
|
29
31
|
const isSelected = (date: Date) => {
|
|
@@ -57,7 +59,9 @@ export function useMulti<T extends DayPickerProps>(
|
|
|
57
59
|
newDates = [...newDates, triggerDate];
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
|
-
|
|
62
|
+
if (!onSelect) {
|
|
63
|
+
setSelected(newDates);
|
|
64
|
+
}
|
|
61
65
|
onSelect?.(newDates, triggerDate, modifiers, e);
|
|
62
66
|
return newDates;
|
|
63
67
|
};
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { DayPickerProps } from "react-day-picker/types";
|
|
3
|
+
|
|
2
4
|
import { act, renderHook } from "@/test/render";
|
|
3
5
|
|
|
4
6
|
import { dateLib } from "../lib";
|
|
@@ -111,4 +113,43 @@ describe("useRange", () => {
|
|
|
111
113
|
to: new Date(2023, 6, 10)
|
|
112
114
|
});
|
|
113
115
|
});
|
|
116
|
+
it("uses the selected value from props when onSelect is provided", () => {
|
|
117
|
+
const mockOnSelect = jest.fn();
|
|
118
|
+
const selectedRange = {
|
|
119
|
+
from: new Date(2023, 9, 1),
|
|
120
|
+
to: new Date(2023, 9, 5)
|
|
121
|
+
};
|
|
122
|
+
const props: DayPickerProps = {
|
|
123
|
+
mode: "range",
|
|
124
|
+
|
|
125
|
+
selected: selectedRange,
|
|
126
|
+
onSelect: mockOnSelect
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const { result } = renderHook(() => useRange(props, dateLib));
|
|
130
|
+
|
|
131
|
+
expect(result.current.selected).toBe(selectedRange);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("uses the internally selected value when onSelect is not provided", () => {
|
|
135
|
+
const initialSelectedRange = {
|
|
136
|
+
from: new Date(2023, 9, 1),
|
|
137
|
+
to: new Date(2023, 9, 5)
|
|
138
|
+
};
|
|
139
|
+
const props: DayPickerProps = {
|
|
140
|
+
mode: "range",
|
|
141
|
+
selected: initialSelectedRange
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const { result } = renderHook(() => useRange(props, dateLib));
|
|
145
|
+
|
|
146
|
+
act(() => {
|
|
147
|
+
result.current.select?.(new Date(2023, 9, 6), {}, {} as React.MouseEvent);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
expect(result.current.selected).toEqual({
|
|
151
|
+
from: new Date(2023, 9, 1),
|
|
152
|
+
to: new Date(2023, 9, 6)
|
|
153
|
+
});
|
|
154
|
+
});
|
|
114
155
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
3
|
import { useControlledValue } from "../helpers/useControlledValue.js";
|
|
4
|
+
import type { DateLib } from "../lib/index.js";
|
|
4
5
|
import type {
|
|
5
|
-
DateLib,
|
|
6
6
|
DayPickerProps,
|
|
7
7
|
Modifiers,
|
|
8
8
|
PropsRange,
|
|
@@ -23,11 +23,13 @@ export function useRange<T extends DayPickerProps>(
|
|
|
23
23
|
onSelect
|
|
24
24
|
} = props as PropsRange;
|
|
25
25
|
|
|
26
|
-
const [
|
|
26
|
+
const [internallySelected, setSelected] = useControlledValue(
|
|
27
27
|
initiallySelected,
|
|
28
28
|
onSelect ? initiallySelected : undefined
|
|
29
29
|
);
|
|
30
30
|
|
|
31
|
+
const selected = !onSelect ? internallySelected : initiallySelected;
|
|
32
|
+
|
|
31
33
|
const isSelected = (date: Date) =>
|
|
32
34
|
selected && rangeIncludesDate(selected, date, false, dateLib);
|
|
33
35
|
|
|
@@ -58,7 +60,9 @@ export function useRange<T extends DayPickerProps>(
|
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
62
|
|
|
61
|
-
|
|
63
|
+
if (!onSelect) {
|
|
64
|
+
setSelected(newRange);
|
|
65
|
+
}
|
|
62
66
|
onSelect?.(newRange, triggerDate, modifiers, e);
|
|
63
67
|
|
|
64
68
|
return newRange;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { dateLib } from "react-day-picker/lib";
|
|
2
|
+
|
|
3
|
+
import { act, renderHook } from "@/test/render";
|
|
4
|
+
|
|
5
|
+
import { DayPickerProps } from "../types";
|
|
6
|
+
|
|
7
|
+
import { useSingle } from "./useSingle";
|
|
8
|
+
|
|
9
|
+
describe("useSingle", () => {
|
|
10
|
+
it("uses the selected value from props when onSelect is provided", () => {
|
|
11
|
+
const mockOnSelect = jest.fn();
|
|
12
|
+
const selectedDate = new Date(2023, 9, 1);
|
|
13
|
+
const props: DayPickerProps = {
|
|
14
|
+
mode: "single",
|
|
15
|
+
selected: selectedDate,
|
|
16
|
+
onSelect: mockOnSelect
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const { result } = renderHook(() => useSingle(props, dateLib));
|
|
20
|
+
|
|
21
|
+
expect(result.current.selected).toBe(selectedDate);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("uses the internally selected value when onSelect is not provided", () => {
|
|
25
|
+
const initialSelectedDate = new Date(2023, 9, 1);
|
|
26
|
+
const props: DayPickerProps = {
|
|
27
|
+
mode: "single",
|
|
28
|
+
selected: initialSelectedDate
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const { result } = renderHook(() => useSingle(props, dateLib));
|
|
32
|
+
|
|
33
|
+
act(() => {
|
|
34
|
+
result.current.select?.(new Date(2023, 9, 2), {}, {} as React.MouseEvent);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(result.current.selected).toEqual(new Date(2023, 9, 2));
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
3
|
import { useControlledValue } from "../helpers/useControlledValue.js";
|
|
4
|
+
import type { DateLib } from "../lib/index.js";
|
|
4
5
|
import type {
|
|
5
|
-
DateLib,
|
|
6
6
|
DayPickerProps,
|
|
7
7
|
Modifiers,
|
|
8
8
|
PropsSingle,
|
|
@@ -27,11 +27,13 @@ export function useSingle<T extends DayPickerProps>(
|
|
|
27
27
|
onSelect
|
|
28
28
|
} = props as PropsSingle;
|
|
29
29
|
|
|
30
|
-
const [
|
|
30
|
+
const [internallySelected, setSelected] = useControlledValue(
|
|
31
31
|
initiallySelected,
|
|
32
32
|
onSelect ? initiallySelected : undefined
|
|
33
33
|
);
|
|
34
34
|
|
|
35
|
+
const selected = !onSelect ? internallySelected : initiallySelected;
|
|
36
|
+
|
|
35
37
|
const { isSameDay } = dateLib;
|
|
36
38
|
|
|
37
39
|
const isSelected = (compareDate: Date) => {
|
|
@@ -48,7 +50,9 @@ export function useSingle<T extends DayPickerProps>(
|
|
|
48
50
|
// If the date is the same, clear the selection.
|
|
49
51
|
newDate = undefined;
|
|
50
52
|
}
|
|
51
|
-
|
|
53
|
+
if (!onSelect) {
|
|
54
|
+
setSelected(newDate);
|
|
55
|
+
}
|
|
52
56
|
if (required) {
|
|
53
57
|
onSelect?.(newDate as Date, triggerDate, modifiers, e);
|
|
54
58
|
} else {
|
package/src/types/props.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
3
|
import type { DeprecatedUI } from "../UI.js";
|
|
4
|
-
import type { Locale } from "../lib/dateLib.js";
|
|
4
|
+
import type { Locale, DateLib } from "../lib/dateLib.js";
|
|
5
5
|
|
|
6
6
|
import type {
|
|
7
7
|
ClassNames,
|
|
@@ -16,8 +16,7 @@ import type {
|
|
|
16
16
|
DayEventHandler,
|
|
17
17
|
Modifiers,
|
|
18
18
|
DateRange,
|
|
19
|
-
Mode
|
|
20
|
-
DateLib
|
|
19
|
+
Mode
|
|
21
20
|
} from "./shared.js";
|
|
22
21
|
|
|
23
22
|
/**
|
|
@@ -256,6 +255,21 @@ export interface PropsBase {
|
|
|
256
255
|
* @see https://en.wikipedia.org/wiki/ISO_week_date
|
|
257
256
|
*/
|
|
258
257
|
ISOWeek?: boolean;
|
|
258
|
+
/**
|
|
259
|
+
* The time zone (IANA or UTC offset) to use in the calendar (experimental).
|
|
260
|
+
* See
|
|
261
|
+
* [Wikipedia](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
|
|
262
|
+
* for the possible values.
|
|
263
|
+
*
|
|
264
|
+
* Time zones are supported by the `TZDate` object by the
|
|
265
|
+
* [@date-fns/tz](https://github.com/date-fns/tz) package. Please refer to the
|
|
266
|
+
* package documentation for more information.
|
|
267
|
+
*
|
|
268
|
+
* @since 9.1.1
|
|
269
|
+
* @experimental
|
|
270
|
+
* @see https://daypicker.dev/docs/localization#time-zone
|
|
271
|
+
*/
|
|
272
|
+
timeZone?: string | undefined;
|
|
259
273
|
/**
|
|
260
274
|
* Change the components used for rendering the calendar elements.
|
|
261
275
|
*
|
|
@@ -338,15 +352,16 @@ export interface PropsBase {
|
|
|
338
352
|
/** Add the language tag to the container element. */
|
|
339
353
|
lang?: HTMLDivElement["lang"];
|
|
340
354
|
/**
|
|
341
|
-
* The locale object used to localize dates. Pass a locale from
|
|
342
|
-
* localize the calendar.
|
|
355
|
+
* The locale object used to localize dates. Pass a locale from
|
|
356
|
+
* `react-day-picker/locale` to localize the calendar.
|
|
343
357
|
*
|
|
344
358
|
* @example
|
|
345
|
-
* import { es } from "
|
|
359
|
+
* import { es } from "react-day-picker/locale";
|
|
346
360
|
* <DayPicker locale={es} />
|
|
347
361
|
*
|
|
348
362
|
* @defaultValue enUS - The English locale default of `date-fns`.
|
|
349
363
|
* @see https://daypicker.dev/docs/localization
|
|
364
|
+
* @see https://github.com/date-fns/date-fns/tree/main/src/locale for a list of the supported locales
|
|
350
365
|
*/
|
|
351
366
|
locale?: Partial<Locale> | undefined;
|
|
352
367
|
/**
|
|
@@ -470,7 +485,16 @@ export interface PropsBase {
|
|
|
470
485
|
onDayTouchStart?: DayEventHandler<React.TouchEvent>;
|
|
471
486
|
}
|
|
472
487
|
|
|
473
|
-
/**
|
|
488
|
+
/**
|
|
489
|
+
* Shared handler type for `onSelect` callback when a selection mode is set.
|
|
490
|
+
*
|
|
491
|
+
* @template T - The type of the selected item.
|
|
492
|
+
* @callback OnSelectHandler
|
|
493
|
+
* @param {T} selected - The selected item after the event.
|
|
494
|
+
* @param {Date} triggerDate - The date when the event was triggered.
|
|
495
|
+
* @param {Modifiers} modifiers - The modifiers associated with the event.
|
|
496
|
+
* @param {React.MouseEvent | React.KeyboardEvent} e - The event object.
|
|
497
|
+
*/
|
|
474
498
|
export type OnSelectHandler<T> = (
|
|
475
499
|
selected: T,
|
|
476
500
|
triggerDate: Date,
|
package/src/types/selection.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DayPickerProps } from "./props.js";
|
|
2
|
-
import type { DateRange, Modifiers } from "./shared.js";
|
|
2
|
+
import type { DateRange, Mode, Modifiers } from "./shared.js";
|
|
3
3
|
|
|
4
4
|
export type Selection<T extends DayPickerProps> = {
|
|
5
5
|
/** The selected date(s). */
|
|
@@ -25,25 +25,28 @@ export type SelectedValue<T> = T extends { mode: "single"; required?: boolean }
|
|
|
25
25
|
? SelectedRange<T>
|
|
26
26
|
: undefined;
|
|
27
27
|
|
|
28
|
-
export type SelectHandlerSingle<T extends
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
export type SelectHandlerSingle<T extends { required?: boolean | undefined }> =
|
|
29
|
+
(
|
|
30
|
+
triggerDate: Date,
|
|
31
|
+
modifiers: Modifiers,
|
|
32
|
+
e: React.MouseEvent | React.KeyboardEvent
|
|
33
|
+
) => T["required"] extends true ? Date : Date | undefined;
|
|
33
34
|
|
|
34
|
-
export type SelectHandlerMulti<T extends
|
|
35
|
+
export type SelectHandlerMulti<T extends { required?: boolean | undefined }> = (
|
|
35
36
|
triggerDate: Date,
|
|
36
37
|
modifiers: Modifiers,
|
|
37
38
|
e: React.MouseEvent | React.KeyboardEvent
|
|
38
39
|
) => T["required"] extends true ? Date[] : Date[] | undefined;
|
|
39
40
|
|
|
40
|
-
export type SelectHandlerRange<T extends
|
|
41
|
+
export type SelectHandlerRange<T extends { required?: boolean | undefined }> = (
|
|
41
42
|
triggerDate: Date,
|
|
42
43
|
modifiers: Modifiers,
|
|
43
44
|
e: React.MouseEvent | React.KeyboardEvent
|
|
44
45
|
) => T["required"] extends true ? DateRange : DateRange | undefined;
|
|
45
46
|
|
|
46
|
-
export type SelectHandler<
|
|
47
|
+
export type SelectHandler<
|
|
48
|
+
T extends { mode?: Mode | undefined; required?: boolean | undefined }
|
|
49
|
+
> = T extends {
|
|
47
50
|
mode: "single";
|
|
48
51
|
}
|
|
49
52
|
? SelectHandlerSingle<T>
|
package/src/types/shared.ts
CHANGED
|
@@ -26,7 +26,6 @@ import {
|
|
|
26
26
|
labelWeekNumberHeader,
|
|
27
27
|
labelYearDropdown
|
|
28
28
|
} from "../labels/index.js";
|
|
29
|
-
import { dateLib } from "../lib/index.js";
|
|
30
29
|
|
|
31
30
|
/**
|
|
32
31
|
* Selection modes supported by DayPicker.
|
|
@@ -104,9 +103,6 @@ export type CustomComponents = {
|
|
|
104
103
|
YearsDropdown: typeof components.YearsDropdown;
|
|
105
104
|
};
|
|
106
105
|
|
|
107
|
-
/** @private */
|
|
108
|
-
export type DateLib = typeof dateLib;
|
|
109
|
-
|
|
110
106
|
/** Represent a map of formatters used to render localized content. */
|
|
111
107
|
export type Formatters = {
|
|
112
108
|
/** Format the caption of a month grid. */
|
|
@@ -323,15 +319,27 @@ export type Styles = {
|
|
|
323
319
|
};
|
|
324
320
|
|
|
325
321
|
/**
|
|
326
|
-
*
|
|
322
|
+
* Represents the modifiers that match a specific day in the calendar.
|
|
323
|
+
*
|
|
324
|
+
* - Retrieve modifiers using the {@link OnSelectHandler} via the `onSelect` prop,
|
|
325
|
+
* or within custom components using the {@link useDayPicker} hook.
|
|
326
|
+
* - Includes built-in modifiers from {@link DayFlag} and {@link SelectionState}.
|
|
327
|
+
* - Add custom modifiers using the `modifiers` prop.
|
|
327
328
|
*
|
|
328
329
|
* @example
|
|
329
330
|
* const modifiers: Modifiers = {
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
331
|
+
* today: false, // the day is not today
|
|
332
|
+
* selected: true, // the day is selected
|
|
333
|
+
* disabled: false, // the day is not disabled
|
|
334
|
+
* outside: false, // the day is not outside the month
|
|
335
|
+
* focused: false, // the day is not focused
|
|
336
|
+
*
|
|
337
|
+
* weekend: false // custom modifier example for matching a weekend
|
|
338
|
+
* booked: true // custom modifier example for matching a booked day
|
|
339
|
+
* available: false // custom modifier example for matching an available day
|
|
334
340
|
* };
|
|
341
|
+
*
|
|
342
|
+
* @see https://daypicker.dev/guides/custom-modifiers
|
|
335
343
|
*/
|
|
336
344
|
export type Modifiers = Record<string, boolean>;
|
|
337
345
|
|
package/src/useCalendar.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
CalendarWeek,
|
|
3
5
|
CalendarDay,
|
|
@@ -13,8 +15,8 @@ import { getNextMonth } from "./helpers/getNextMonth.js";
|
|
|
13
15
|
import { getPreviousMonth } from "./helpers/getPreviousMonth.js";
|
|
14
16
|
import { getWeeks } from "./helpers/getWeeks.js";
|
|
15
17
|
import { useControlledValue } from "./helpers/useControlledValue.js";
|
|
18
|
+
import type { DateLib } from "./lib/index.js";
|
|
16
19
|
import type { DayPickerProps } from "./types/props.js";
|
|
17
|
-
import type { DateLib } from "./types/shared.js";
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* Return the calendar object to work with the calendar in custom components.
|
|
@@ -79,6 +81,7 @@ export function useCalendar(
|
|
|
79
81
|
| "onMonthChange"
|
|
80
82
|
| "month"
|
|
81
83
|
| "defaultMonth"
|
|
84
|
+
| "timeZone"
|
|
82
85
|
// Deprecated:
|
|
83
86
|
| "fromMonth"
|
|
84
87
|
| "fromYear"
|
|
@@ -90,14 +93,18 @@ export function useCalendar(
|
|
|
90
93
|
const [navStart, navEnd] = getNavMonths(props, dateLib);
|
|
91
94
|
|
|
92
95
|
const { startOfMonth, endOfMonth } = dateLib;
|
|
93
|
-
|
|
94
96
|
const initialMonth = getInitialMonth(props, dateLib);
|
|
95
|
-
|
|
96
97
|
const [firstMonth, setFirstMonth] = useControlledValue(
|
|
97
98
|
initialMonth,
|
|
98
99
|
props.month ? startOfMonth(props.month) : undefined
|
|
99
100
|
);
|
|
100
101
|
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
const newInitialMonth = getInitialMonth(props, dateLib);
|
|
104
|
+
setFirstMonth(newInitialMonth);
|
|
105
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
106
|
+
}, [props.timeZone]);
|
|
107
|
+
|
|
101
108
|
/** The months displayed in the calendar. */
|
|
102
109
|
const displayMonths = getDisplayMonths(firstMonth, navEnd, props, dateLib);
|
|
103
110
|
|
package/src/useDayPicker.ts
CHANGED
|
@@ -2,23 +2,37 @@ import { createContext, useContext } from "react";
|
|
|
2
2
|
|
|
3
3
|
import { CalendarDay } from "./classes/CalendarDay.js";
|
|
4
4
|
import { CalendarMonth } from "./classes/CalendarMonth.js";
|
|
5
|
-
import type { DayPickerProps } from "./types/props.js";
|
|
6
5
|
import type { SelectedValue, SelectHandler } from "./types/selection.js";
|
|
7
6
|
import {
|
|
8
7
|
ClassNames,
|
|
9
8
|
CustomComponents,
|
|
10
9
|
Formatters,
|
|
11
10
|
Labels,
|
|
11
|
+
Mode,
|
|
12
12
|
Modifiers,
|
|
13
13
|
Styles
|
|
14
14
|
} from "./types/shared.js";
|
|
15
15
|
|
|
16
16
|
/** @private */
|
|
17
17
|
export const dayPickerContext = createContext<
|
|
18
|
-
DayPickerContext<
|
|
18
|
+
| DayPickerContext<{
|
|
19
|
+
mode?: Mode | undefined;
|
|
20
|
+
required?: boolean | undefined;
|
|
21
|
+
}>
|
|
22
|
+
| undefined
|
|
19
23
|
>(undefined);
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Represents the context for the DayPicker component, providing various
|
|
27
|
+
* properties and methods to interact with the calendar.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The type of the DayPicker props, which must optionally include
|
|
30
|
+
* `mode` and `required` properties. This type can be used to refine the type
|
|
31
|
+
* returned by the hook.
|
|
32
|
+
*/
|
|
33
|
+
export type DayPickerContext<
|
|
34
|
+
T extends { mode?: Mode | undefined; required?: boolean | undefined }
|
|
35
|
+
> = {
|
|
22
36
|
/** The months displayed in the calendar. */
|
|
23
37
|
months: CalendarMonth[];
|
|
24
38
|
/** The next month to display. */
|
|
@@ -48,15 +62,25 @@ export type DayPickerContext<T extends DayPickerProps> = {
|
|
|
48
62
|
};
|
|
49
63
|
|
|
50
64
|
/**
|
|
51
|
-
*
|
|
65
|
+
* Returns the context to work with `<DayPicker />` inside custom components.
|
|
66
|
+
*
|
|
67
|
+
* This hook provides access to the DayPicker context, which includes various
|
|
68
|
+
* properties and methods to interact with the DayPicker component. It must be
|
|
69
|
+
* used within a custom component.
|
|
52
70
|
*
|
|
71
|
+
* @template T - Use this type to refine the returned context type with a
|
|
72
|
+
* specific selection mode.
|
|
73
|
+
* @returns {DayPickerContext<T>} The context to work with DayPicker.
|
|
74
|
+
* @throws {Error} If the hook is used outside of a DayPicker provider.
|
|
53
75
|
* @group Hooks
|
|
54
76
|
* @see https://daypicker.dev/guides/custom-components
|
|
55
77
|
*/
|
|
56
|
-
export function useDayPicker<
|
|
78
|
+
export function useDayPicker<
|
|
79
|
+
T extends { mode?: Mode | undefined; required?: boolean | undefined }
|
|
80
|
+
>(): DayPickerContext<T> {
|
|
57
81
|
const context = useContext(dayPickerContext);
|
|
58
82
|
if (context === undefined) {
|
|
59
83
|
throw new Error("useDayPicker() must be used within a custom component.");
|
|
60
84
|
}
|
|
61
|
-
return context
|
|
85
|
+
return context;
|
|
62
86
|
}
|
package/src/useFocus.ts
CHANGED
|
@@ -3,10 +3,10 @@ import { useState } from "react";
|
|
|
3
3
|
import type { CalendarDay } from "./classes/index.js";
|
|
4
4
|
import { calculateFocusTarget } from "./helpers/calculateFocusTarget.js";
|
|
5
5
|
import { getNextFocus } from "./helpers/getNextFocus.js";
|
|
6
|
+
import type { DateLib } from "./lib/index.js";
|
|
6
7
|
import type {
|
|
7
8
|
MoveFocusBy,
|
|
8
9
|
MoveFocusDir,
|
|
9
|
-
DateLib,
|
|
10
10
|
DayPickerProps,
|
|
11
11
|
Modifiers
|
|
12
12
|
} from "./types/index.js";
|
package/src/useGetModifiers.tsx
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { TZDate } from "@date-fns/tz";
|
|
2
|
+
|
|
1
3
|
import { DayFlag, SelectionState } from "./UI.js";
|
|
2
4
|
import { CalendarDay } from "./classes/index.js";
|
|
3
|
-
import type { DateLib
|
|
5
|
+
import type { DateLib } from "./lib/index.js";
|
|
6
|
+
import type { DayPickerProps, Modifiers } from "./types/index.js";
|
|
4
7
|
import { dateMatchModifiers } from "./utils/dateMatchModifiers.js";
|
|
5
8
|
|
|
6
9
|
/**
|
|
@@ -15,7 +18,7 @@ export function useGetModifiers(
|
|
|
15
18
|
) {
|
|
16
19
|
const { disabled, hidden, modifiers, showOutsideDays, today } = props;
|
|
17
20
|
|
|
18
|
-
const { isSameDay, isSameMonth
|
|
21
|
+
const { isSameDay, isSameMonth } = dateLib;
|
|
19
22
|
|
|
20
23
|
const internalModifiersMap: Record<DayFlag, CalendarDay[]> = {
|
|
21
24
|
[DayFlag.focused]: [],
|
|
@@ -47,7 +50,15 @@ export function useGetModifiers(
|
|
|
47
50
|
Boolean(hidden && dateMatchModifiers(date, hidden, dateLib)) ||
|
|
48
51
|
(!showOutsideDays && isOutside);
|
|
49
52
|
|
|
50
|
-
const isToday = isSameDay(
|
|
53
|
+
const isToday = isSameDay(
|
|
54
|
+
date,
|
|
55
|
+
today ??
|
|
56
|
+
(props.timeZone
|
|
57
|
+
? TZDate.tz(props.timeZone)
|
|
58
|
+
: dateLib.Date
|
|
59
|
+
? new dateLib.Date()
|
|
60
|
+
: new Date())
|
|
61
|
+
);
|
|
51
62
|
|
|
52
63
|
if (isOutside) internalModifiersMap.outside.push(day);
|
|
53
64
|
if (isDisabled) internalModifiersMap.disabled.push(day);
|
package/src/useSelection.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { type DateLib } from "./lib/index.js";
|
|
1
2
|
import { useMulti } from "./selection/useMulti.js";
|
|
2
3
|
import { useRange } from "./selection/useRange.js";
|
|
3
4
|
import { useSingle } from "./selection/useSingle.js";
|
|
4
|
-
import type {
|
|
5
|
+
import type { DayPickerProps } from "./types/index.js";
|
|
5
6
|
import { Selection } from "./types/selection.js";
|
|
6
7
|
|
|
7
8
|
export function useSelection<T extends DayPickerProps>(
|
package/src/utc.tsx
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
|
-
import { UTCDate } from "@date-fns/utc";
|
|
4
|
-
|
|
5
3
|
import {
|
|
6
4
|
DayPicker as DayPickerComponent,
|
|
7
5
|
type DayPickerProps
|
|
8
6
|
} from "./index.js";
|
|
9
7
|
|
|
10
8
|
export function DayPicker(props: DayPickerProps) {
|
|
11
|
-
return <DayPickerComponent
|
|
9
|
+
return <DayPickerComponent timeZone="utc" {...props} />;
|
|
12
10
|
}
|
package/src/utils/addToRange.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { dateLib as defaultDateLib } from "../lib/index.js";
|
|
2
|
-
import type { DateRange
|
|
1
|
+
import { dateLib as defaultDateLib, type DateLib } from "../lib/index.js";
|
|
2
|
+
import type { DateRange } from "../types/index.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Add a day to an existing range.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { dateLib as defaultDateLib } from "../lib/dateLib.js";
|
|
2
|
-
import type {
|
|
1
|
+
import { dateLib as defaultDateLib, type DateLib } from "../lib/dateLib.js";
|
|
2
|
+
import type { Matcher } from "../types/index.js";
|
|
3
3
|
|
|
4
4
|
import { rangeIncludesDate } from "./rangeIncludesDate.js";
|
|
5
5
|
import {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { dateLib as defaultDateLib } from "../lib/index.js";
|
|
2
|
-
import type { DateRange
|
|
1
|
+
import { dateLib as defaultDateLib, type DateLib } from "../lib/index.js";
|
|
2
|
+
import type { DateRange } from "../types/index.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Determines whether a given date is inside a specified date range.
|
package/src/utils/typeguards.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
2
|
+
hide_title: true
|
|
3
|
+
title: Code Of Conduct
|
|
4
|
+
sidebar_position: 2
|
|
3
5
|
---
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
import CodeOfConduct from "../../../.github/CODE_OF_CONDUCT.md";
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
This documentation is still a work in progress. If you have any questions, ask to the [discussions](https://github.com/gpbl/react-day-picker/discussions) page on Github.
|
|
10
|
-
|
|
11
|
-
:::
|
|
9
|
+
<CodeOfConduct />
|