react-day-picker 9.0.7 → 9.0.9

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.
Files changed (52) hide show
  1. package/dist/cjs/DayPicker.js +8 -3
  2. package/dist/cjs/DayPicker.js.map +1 -1
  3. package/dist/cjs/components/Chevron.d.ts +1 -0
  4. package/dist/cjs/helpers/getFormatters.d.ts +5 -5
  5. package/dist/cjs/types/shared.d.ts +11 -9
  6. package/dist/cjs/useCalendar.js +3 -28
  7. package/dist/cjs/useCalendar.js.map +1 -1
  8. package/dist/cjs/useDayPicker.d.ts +11 -1
  9. package/dist/cjs/useDayPicker.js.map +1 -1
  10. package/dist/cjs/useGetModifiers.js +16 -16
  11. package/dist/cjs/useGetModifiers.js.map +1 -1
  12. package/dist/cjs/utils/dateMatchModifiers.d.ts +8 -3
  13. package/dist/cjs/utils/dateMatchModifiers.js +10 -3
  14. package/dist/cjs/utils/dateMatchModifiers.js.map +1 -1
  15. package/dist/cjs/utils/typeguards.d.ts +5 -1
  16. package/dist/cjs/utils/typeguards.js +5 -1
  17. package/dist/cjs/utils/typeguards.js.map +1 -1
  18. package/dist/esm/DayPicker.js +8 -3
  19. package/dist/esm/DayPicker.js.map +1 -1
  20. package/dist/esm/components/Chevron.d.ts +1 -0
  21. package/dist/esm/helpers/getFormatters.d.ts +5 -5
  22. package/dist/esm/types/shared.d.ts +11 -9
  23. package/dist/esm/useCalendar.js +3 -28
  24. package/dist/esm/useCalendar.js.map +1 -1
  25. package/dist/esm/useDayPicker.d.ts +11 -1
  26. package/dist/esm/useDayPicker.js.map +1 -1
  27. package/dist/esm/useGetModifiers.js +16 -16
  28. package/dist/esm/useGetModifiers.js.map +1 -1
  29. package/dist/esm/utils/dateMatchModifiers.d.ts +8 -3
  30. package/dist/esm/utils/dateMatchModifiers.js +9 -3
  31. package/dist/esm/utils/dateMatchModifiers.js.map +1 -1
  32. package/dist/esm/utils/typeguards.d.ts +5 -1
  33. package/dist/esm/utils/typeguards.js +5 -1
  34. package/dist/esm/utils/typeguards.js.map +1 -1
  35. package/examples/TestCase2389.test.tsx +41 -0
  36. package/examples/TestCase2389.tsx +18 -0
  37. package/examples/index.ts +1 -0
  38. package/package.json +9 -9
  39. package/src/DayPicker.test.tsx +1 -15
  40. package/src/DayPicker.tsx +8 -1
  41. package/src/components/Chevron.tsx +2 -0
  42. package/src/style.css +13 -18
  43. package/src/style.module.css +13 -18
  44. package/src/types/shared.ts +11 -8
  45. package/src/useCalendar.ts +7 -43
  46. package/src/useDayPicker.ts +18 -1
  47. package/src/useGetModifiers.tsx +16 -16
  48. package/src/utils/dateMatchModifiers.ts +10 -3
  49. package/src/utils/typeguards.ts +5 -1
  50. package/website/docs/docs/styling.mdx +1 -1
  51. package/website/docs/guides/input-fields.mdx +1 -1
  52. package/website/docs/upgrading.mdx +1 -1
@@ -10,6 +10,7 @@ import {
10
10
  formatMonthDropdown,
11
11
  formatWeekdayName,
12
12
  formatWeekNumber,
13
+ formatWeekNumberHeader,
13
14
  formatYearCaption,
14
15
  formatYearDropdown
15
16
  } from "../formatters/index.js";
@@ -98,20 +99,22 @@ export type DateLib = typeof dateLib;
98
99
  export type Formatters = {
99
100
  /** Format the caption of a month grid. */
100
101
  formatCaption: typeof formatCaption;
101
- /** @deprecated Use {@link Formatters.formatCaption} instead. */
102
- formatMonthCaption: typeof formatMonthCaption;
103
- /** Format the label in the month dropdown. */
104
- formatMonthDropdown: typeof formatMonthDropdown;
105
- /** @deprecated Use {@link Formatters.formatYearDropdown} instead. */
106
- formatYearCaption: typeof formatYearCaption;
107
- /** Format the label in the year dropdown. */
108
- formatYearDropdown: typeof formatYearDropdown;
109
102
  /** Format the day in the day cell. */
110
103
  formatDay: typeof formatDay;
104
+ /** Format the label in the month dropdown. */
105
+ formatMonthDropdown: typeof formatMonthDropdown;
106
+ /** @deprecated Use {@link Formatters.formatCaption} instead. */
107
+ formatMonthCaption: typeof formatMonthCaption;
111
108
  /** Format the week number. */
112
109
  formatWeekNumber: typeof formatWeekNumber;
110
+ /** Format the header of the week number column. */
111
+ formatWeekNumberHeader: typeof formatWeekNumberHeader;
113
112
  /** Format the week day name in the header. */
114
113
  formatWeekdayName: typeof formatWeekdayName;
114
+ /** Format the label in the year dropdown. */
115
+ formatYearDropdown: typeof formatYearDropdown;
116
+ /** @deprecated Use {@link Formatters.formatYearDropdown} instead. */
117
+ formatYearCaption: typeof formatYearCaption;
115
118
  };
116
119
 
117
120
  /** Map of functions to translate ARIA labels for the relative elements. */
@@ -1,5 +1,3 @@
1
- import { useEffect, useState } from "react";
2
-
3
1
  import type {
4
2
  CalendarWeek,
5
3
  CalendarDay,
@@ -14,6 +12,7 @@ import { getNavMonths } from "./helpers/getNavMonth.js";
14
12
  import { getNextMonth } from "./helpers/getNextMonth.js";
15
13
  import { getPreviousMonth } from "./helpers/getPreviousMonth.js";
16
14
  import { getWeeks } from "./helpers/getWeeks.js";
15
+ import { useControlledValue } from "./helpers/useControlledValue.js";
17
16
  import type { DayPickerProps } from "./types/props.js";
18
17
  import type { DateLib } from "./types/shared.js";
19
18
 
@@ -88,51 +87,16 @@ export function useCalendar(
88
87
  >,
89
88
  dateLib: DateLib
90
89
  ): Calendar {
91
- const {
92
- fromYear,
93
- toYear,
94
- startMonth,
95
- endMonth,
96
- today,
97
- numberOfMonths,
98
- month,
99
- defaultMonth
100
- } = props;
101
90
  const [navStart, navEnd] = getNavMonths(props, dateLib);
102
91
 
103
92
  const { startOfMonth, endOfMonth } = dateLib;
104
93
 
105
- const initialDisplayMonth = getInitialMonth(props, dateLib);
106
-
107
- // The first month displayed in the calendar
108
- const [firstMonth, setFirstMonth] = useState(initialDisplayMonth);
109
-
110
- useEffect(() => {
111
- const initialDisplayMonth = getInitialMonth(
112
- {
113
- fromYear,
114
- toYear,
115
- startMonth,
116
- endMonth,
117
- month,
118
- defaultMonth,
119
- today,
120
- numberOfMonths
121
- },
122
- dateLib
123
- );
124
- setFirstMonth(initialDisplayMonth);
125
- }, [
126
- dateLib,
127
- defaultMonth,
128
- endMonth,
129
- fromYear,
130
- month,
131
- numberOfMonths,
132
- startMonth,
133
- toYear,
134
- today
135
- ]);
94
+ const initialMonth = getInitialMonth(props, dateLib);
95
+
96
+ const [firstMonth, setFirstMonth] = useControlledValue(
97
+ initialMonth,
98
+ props.month ? startOfMonth(props.month) : undefined
99
+ );
136
100
 
137
101
  /** The months displayed in the calendar. */
138
102
  const displayMonths = getDisplayMonths(firstMonth, navEnd, props, dateLib);
@@ -4,7 +4,14 @@ import { CalendarDay } from "./classes/CalendarDay.js";
4
4
  import { CalendarMonth } from "./classes/CalendarMonth.js";
5
5
  import type { DayPickerProps } from "./types/props.js";
6
6
  import type { SelectedValue, SelectHandler } from "./types/selection.js";
7
- import { Modifiers } from "./types/shared.js";
7
+ import {
8
+ ClassNames,
9
+ CustomComponents,
10
+ Formatters,
11
+ Labels,
12
+ Modifiers,
13
+ Styles
14
+ } from "./types/shared.js";
8
15
 
9
16
  /** @private */
10
17
  export const dayPickerContext = createContext<
@@ -28,6 +35,16 @@ export type DayPickerContext<T extends DayPickerProps> = {
28
35
  select: SelectHandler<T> | undefined;
29
36
  /** Whether the given date is selected. */
30
37
  isSelected: ((date: Date) => boolean) | undefined;
38
+ /** The components used internally by DayP. */
39
+ components: CustomComponents;
40
+ /** The class names for the UI elements. */
41
+ classNames: ClassNames;
42
+ /** The styles for the UI elements. */
43
+ styles: Partial<Styles> | undefined;
44
+ /** The labels used in the UI. */
45
+ labels: Labels;
46
+ /** The formatters used to format the UI elements. */
47
+ formatters: Formatters;
31
48
  };
32
49
 
33
50
  /**
@@ -17,7 +17,7 @@ export function useGetModifiers(
17
17
 
18
18
  const { isSameDay, isSameMonth, Date } = dateLib;
19
19
 
20
- const internal: Record<DayFlag, CalendarDay[]> = {
20
+ const internalModifiersMap: Record<DayFlag, CalendarDay[]> = {
21
21
  [DayFlag.focused]: [],
22
22
  [DayFlag.outside]: [],
23
23
  [DayFlag.disabled]: [],
@@ -25,9 +25,9 @@ export function useGetModifiers(
25
25
  [DayFlag.today]: []
26
26
  };
27
27
 
28
- const custom: Record<string, CalendarDay[]> = {};
28
+ const customModifiersMap: Record<string, CalendarDay[]> = {};
29
29
 
30
- const selection: Record<SelectionState, CalendarDay[]> = {
30
+ const selectionModifiersMap: Record<SelectionState, CalendarDay[]> = {
31
31
  [SelectionState.range_end]: [],
32
32
  [SelectionState.range_middle]: [],
33
33
  [SelectionState.range_start]: [],
@@ -49,10 +49,10 @@ export function useGetModifiers(
49
49
 
50
50
  const isToday = isSameDay(date, today ?? new Date());
51
51
 
52
- if (isOutside) internal.outside.push(day);
53
- if (isDisabled) internal.disabled.push(day);
54
- if (isHidden) internal.hidden.push(day);
55
- if (isToday) internal.today.push(day);
52
+ if (isOutside) internalModifiersMap.outside.push(day);
53
+ if (isDisabled) internalModifiersMap.disabled.push(day);
54
+ if (isHidden) internalModifiersMap.hidden.push(day);
55
+ if (isToday) internalModifiersMap.today.push(day);
56
56
 
57
57
  // Add custom modifiers
58
58
  if (modifiers) {
@@ -62,10 +62,10 @@ export function useGetModifiers(
62
62
  ? dateMatchModifiers(date, modifierValue, dateLib)
63
63
  : false;
64
64
  if (!isMatch) return;
65
- if (custom[name]) {
66
- custom[name].push(day);
65
+ if (customModifiersMap[name]) {
66
+ customModifiersMap[name].push(day);
67
67
  } else {
68
- custom[name] = [day];
68
+ customModifiersMap[name] = [day];
69
69
  }
70
70
  });
71
71
  }
@@ -89,16 +89,16 @@ export function useGetModifiers(
89
89
  const customModifiers: Modifiers = {};
90
90
 
91
91
  // Find the modifiers for the given day
92
- for (const name in internal) {
93
- const days = internal[name as DayFlag];
92
+ for (const name in internalModifiersMap) {
93
+ const days = internalModifiersMap[name as DayFlag];
94
94
  dayFlags[name as DayFlag] = days.some((d) => d === day);
95
95
  }
96
- for (const name in selection) {
97
- const days = selection[name as SelectionState];
96
+ for (const name in selectionModifiersMap) {
97
+ const days = selectionModifiersMap[name as SelectionState];
98
98
  selectionStates[name as SelectionState] = days.some((d) => d === day);
99
99
  }
100
- for (const name in custom) {
101
- customModifiers[name] = custom[name].some((d) => d === day);
100
+ for (const name in customModifiersMap) {
101
+ customModifiers[name] = customModifiersMap[name].some((d) => d === day);
102
102
  }
103
103
 
104
104
  return {
@@ -1,3 +1,4 @@
1
+ import { dateLib as defaultDateLib } from "../lib/dateLib.js";
1
2
  import type { DateLib, Matcher } from "../types/index.js";
2
3
 
3
4
  import { rangeIncludesDate } from "./rangeIncludesDate.js";
@@ -15,7 +16,7 @@ import {
15
16
  * {@link Matcher}.
16
17
  *
17
18
  * ```tsx
18
- * const day = new Date(2022, 5, 19);
19
+ * const date = new Date(2022, 5, 19);
19
20
  * const matcher1: DateRange = {
20
21
  * from: new Date(2021, 12, 21),
21
22
  * to: new Date(2021, 12, 30)
@@ -24,7 +25,7 @@ import {
24
25
  * from: new Date(2022, 5, 1),
25
26
  * to: new Date(2022, 5, 23)
26
27
  * }
27
- * const isMatch(day, [matcher1, matcher2]); // true, since day is in the matcher1 range.
28
+ * const dateMatchModifiers(date, [matcher1, matcher2]); // true, since day is in the matcher1 range.
28
29
  * ```
29
30
  *
30
31
  * @group Utilities
@@ -32,7 +33,7 @@ import {
32
33
  export function dateMatchModifiers(
33
34
  date: Date,
34
35
  matchers: Matcher | Matcher[],
35
- dateLib: DateLib
36
+ dateLib: DateLib = defaultDateLib
36
37
  ): boolean {
37
38
  const matchersArr = !Array.isArray(matchers) ? [matchers] : matchers;
38
39
  const { isSameDay, differenceInCalendarDays, isAfter } = dateLib;
@@ -79,3 +80,9 @@ export function dateMatchModifiers(
79
80
  return false;
80
81
  });
81
82
  }
83
+
84
+ /**
85
+ * @private
86
+ * @deprecated Use {@link dateMatchModifiers} instead.
87
+ */
88
+ export const isMatch = dateMatchModifiers;
@@ -57,7 +57,11 @@ export function isDayOfWeekType(value: unknown): value is DayOfWeek {
57
57
  return Boolean(value && typeof value === "object" && "dayOfWeek" in value);
58
58
  }
59
59
 
60
- /** Returns true if `value` is an array of valid dates. */
60
+ /**
61
+ * Returns true if `value` is an array of valid dates.
62
+ *
63
+ * @private
64
+ */
61
65
  export function isDatesArray(
62
66
  value: unknown,
63
67
  dateLib: DateLib
@@ -79,7 +79,7 @@ The following table lists the CSS variables used by DayPicker inside the `.rdp-r
79
79
  | `--rdp-nav-height` | The height of the navigation bar. |
80
80
  | `--rdp-range_middle-background-color` | The color of the background for days in the middle of a range. |
81
81
  | `--rdp-range_middle-font` | The font for days in the middle of a range. |
82
- | `-- ` | The color of the text for days in the middle of a range. |
82
+ | `--rdp-range_middle-foreground-color` | The color of the text for days in the middle of a range. |
83
83
  | `--rdp-range_start-color` | The color of the range text at the start of the range. |
84
84
  | `--rdp-range_start-background` | Used for the background of the start of the selected range. |
85
85
  | `--rdp-range_start-date-background-color` | The background color of the date at the start of the selected range. |
@@ -16,7 +16,7 @@ Browsers offer [native date pickers](https://developer.mozilla.org/en-US/docs/We
16
16
 
17
17
  ### Input with Inline Calendar
18
18
 
19
- See also the [full source code](https://github.com/gpbl/react-day-picker/blob/main/website/examples/Input.tsx) and [the unit tests](https://github.com/gpbl/react-day-picker/blob/main/website/examples/Input.test.tsx) for this example.
19
+ See also the [full source code](https://github.com/gpbl/react-day-picker/blob/main/examples/Input.tsx) and [the unit tests](https://github.com/gpbl/react-day-picker/blob/main/examples/Input.test.tsx) for this example.
20
20
 
21
21
  <BrowserWindow>
22
22
  <Examples.Input />
@@ -259,7 +259,7 @@ If you are using TypeScript, some typings have been deprecated in favor of clari
259
259
  + import type { PropsBase } from 'react-day-picker';
260
260
  ```
261
261
 
262
- See also the source of [types-deprecated.ts](https://github.com/gpbl/react-day-picker/blob/next/src/types-deprecated.ts).
262
+ See also the source of [types/deprecated.ts](https://github.com/gpbl/react-day-picker/blob/next/src/types/deprecated.ts).
263
263
 
264
264
  <details>
265
265
  <summary>**List of Deprecated Types**</summary>