react-day-picker 8.0.3 → 8.0.4

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 (196) hide show
  1. package/package.json +13 -11
  2. package/src/DayPicker.tsx +113 -0
  3. package/src/components/Button/Button.test.tsx +47 -0
  4. package/src/components/Button/Button.tsx +36 -0
  5. package/src/components/Button/index.ts +1 -0
  6. package/src/components/Caption/Caption.test.tsx +86 -0
  7. package/src/components/Caption/Caption.tsx +54 -0
  8. package/src/components/Caption/index.ts +1 -0
  9. package/src/components/CaptionDropdowns/CaptionDropdowns.test.tsx +123 -0
  10. package/src/components/CaptionDropdowns/CaptionDropdowns.tsx +43 -0
  11. package/src/components/CaptionDropdowns/index.ts +1 -0
  12. package/src/components/CaptionLabel/CaptionLabel.test.tsx +29 -0
  13. package/src/components/CaptionLabel/CaptionLabel.tsx +32 -0
  14. package/src/components/CaptionLabel/index.ts +1 -0
  15. package/src/components/CaptionNavigation/CaptionNavigation.test.tsx +172 -0
  16. package/src/components/CaptionNavigation/CaptionNavigation.tsx +63 -0
  17. package/src/components/CaptionNavigation/index.ts +1 -0
  18. package/src/components/Day/Day.test.tsx +84 -0
  19. package/src/components/Day/Day.tsx +30 -0
  20. package/src/components/Day/index.ts +1 -0
  21. package/src/components/DayContent/DayContent.test.tsx +51 -0
  22. package/src/components/DayContent/DayContent.tsx +36 -0
  23. package/src/components/DayContent/index.ts +1 -0
  24. package/src/components/Dropdown/Dropdown.test.tsx +73 -0
  25. package/src/components/Dropdown/Dropdown.tsx +56 -0
  26. package/src/components/Dropdown/index.ts +1 -0
  27. package/src/components/Footer/Footer.test.tsx +29 -0
  28. package/src/components/Footer/Footer.tsx +20 -0
  29. package/src/components/Footer/index.ts +1 -0
  30. package/src/components/Head/Head.test.tsx +117 -0
  31. package/src/components/Head/Head.tsx +51 -0
  32. package/src/components/Head/index.ts +1 -0
  33. package/src/components/Head/utils/getWeekdays.test.ts +36 -0
  34. package/src/components/Head/utils/getWeekdays.ts +22 -0
  35. package/src/components/Head/utils/index.ts +1 -0
  36. package/src/components/IconDropdown/IconDropdown.test.tsx +20 -0
  37. package/src/components/IconDropdown/IconDropdown.tsx +24 -0
  38. package/src/components/IconDropdown/index.ts +1 -0
  39. package/src/components/IconLeft/IconLeft.test.tsx +20 -0
  40. package/src/components/IconLeft/IconLeft.tsx +18 -0
  41. package/src/components/IconLeft/index.ts +1 -0
  42. package/src/components/IconRight/IconRight.test.tsx +20 -0
  43. package/src/components/IconRight/IconRight.tsx +17 -0
  44. package/src/components/IconRight/index.ts +1 -0
  45. package/src/components/Month/Month.test.tsx +216 -0
  46. package/src/components/Month/Month.tsx +53 -0
  47. package/src/components/Month/index.ts +1 -0
  48. package/src/components/MonthsDropdown/MonthsDropdown.test.tsx +99 -0
  49. package/src/components/MonthsDropdown/MonthsDropdown.tsx +75 -0
  50. package/src/components/MonthsDropdown/index.ts +1 -0
  51. package/src/components/Navigation/Navigation.test.tsx +129 -0
  52. package/src/components/Navigation/Navigation.tsx +102 -0
  53. package/src/components/Navigation/index.ts +1 -0
  54. package/src/components/Root/Root.test.tsx +123 -0
  55. package/src/components/Root/Root.tsx +58 -0
  56. package/src/components/Root/index.ts +1 -0
  57. package/src/components/Row/Row.test.tsx +69 -0
  58. package/src/components/Row/Row.tsx +51 -0
  59. package/src/components/Row/index.ts +1 -0
  60. package/src/components/Table/Table.test.tsx +42 -0
  61. package/src/components/Table/Table.tsx +60 -0
  62. package/src/components/Table/__snapshots__/Table.test.tsx.snap +1453 -0
  63. package/src/components/Table/index.ts +1 -0
  64. package/src/components/Table/utils/daysToMonthWeeks.ts +47 -0
  65. package/src/components/Table/utils/getMonthWeeks.test.ts +68 -0
  66. package/src/components/Table/utils/getMonthWeeks.ts +55 -0
  67. package/src/components/WeekNumber/WeekNumber.test.tsx +46 -0
  68. package/src/components/WeekNumber/WeekNumber.tsx +58 -0
  69. package/src/components/WeekNumber/__snapshots__/WeekNumber.test.tsx.snap +11 -0
  70. package/src/components/WeekNumber/index.ts +1 -0
  71. package/src/components/YearsDropdown/YearsDropdown.test.tsx +98 -0
  72. package/src/components/YearsDropdown/YearsDropdown.tsx +76 -0
  73. package/src/components/YearsDropdown/index.ts +1 -0
  74. package/src/contexts/DayPicker/DayPickerContext.tsx +156 -0
  75. package/src/contexts/DayPicker/defaultClassNames.ts +58 -0
  76. package/src/contexts/DayPicker/defaultContextValue.ts +37 -0
  77. package/src/contexts/DayPicker/formatters/formatCaption.test.ts +15 -0
  78. package/src/contexts/DayPicker/formatters/formatCaption.ts +12 -0
  79. package/src/contexts/DayPicker/formatters/formatDay.test.ts +7 -0
  80. package/src/contexts/DayPicker/formatters/formatDay.ts +9 -0
  81. package/src/contexts/DayPicker/formatters/formatMonthCaption.test.ts +15 -0
  82. package/src/contexts/DayPicker/formatters/formatMonthCaption.ts +12 -0
  83. package/src/contexts/DayPicker/formatters/formatWeekNumber.test.ts +5 -0
  84. package/src/contexts/DayPicker/formatters/formatWeekNumber.ts +6 -0
  85. package/src/contexts/DayPicker/formatters/formatWeekdayName.test.ts +15 -0
  86. package/src/contexts/DayPicker/formatters/formatWeekdayName.ts +12 -0
  87. package/src/contexts/DayPicker/formatters/formatYearCaption.test.ts +7 -0
  88. package/src/contexts/DayPicker/formatters/formatYearCaption.ts +11 -0
  89. package/src/contexts/DayPicker/formatters/index.ts +6 -0
  90. package/src/contexts/DayPicker/index.ts +2 -0
  91. package/src/contexts/DayPicker/labels/index.ts +7 -0
  92. package/src/contexts/DayPicker/labels/labelDay.test.ts +7 -0
  93. package/src/contexts/DayPicker/labels/labelDay.ts +10 -0
  94. package/src/contexts/DayPicker/labels/labelMonthDropdown.test.ts +5 -0
  95. package/src/contexts/DayPicker/labels/labelMonthDropdown.ts +6 -0
  96. package/src/contexts/DayPicker/labels/labelNext.test.ts +5 -0
  97. package/src/contexts/DayPicker/labels/labelNext.ts +8 -0
  98. package/src/contexts/DayPicker/labels/labelPrevious.test.ts +5 -0
  99. package/src/contexts/DayPicker/labels/labelPrevious.ts +8 -0
  100. package/src/contexts/DayPicker/labels/labelWeekNumber.test.ts +5 -0
  101. package/src/contexts/DayPicker/labels/labelWeekNumber.ts +8 -0
  102. package/src/contexts/DayPicker/labels/labelWeekday.test.ts +15 -0
  103. package/src/contexts/DayPicker/labels/labelWeekday.ts +10 -0
  104. package/src/contexts/DayPicker/labels/labelYearDropdown.test.ts +5 -0
  105. package/src/contexts/DayPicker/labels/labelYearDropdown.ts +6 -0
  106. package/src/contexts/DayPicker/useDayPicker.test.ts +297 -0
  107. package/src/contexts/DayPicker/useDayPicker.ts +17 -0
  108. package/src/contexts/DayPicker/utils/index.ts +1 -0
  109. package/src/contexts/DayPicker/utils/parseFromToProps.test.ts +47 -0
  110. package/src/contexts/DayPicker/utils/parseFromToProps.ts +32 -0
  111. package/src/contexts/Focus/FocusContext.tsx +174 -0
  112. package/src/contexts/Focus/index.ts +2 -0
  113. package/src/contexts/Focus/useFocusContext.test.ts +183 -0
  114. package/src/contexts/Focus/useFocusContext.ts +12 -0
  115. package/src/contexts/Focus/utils/getInitialFocusTarget.test.tsx +12 -0
  116. package/src/contexts/Focus/utils/getInitialFocusTarget.tsx +44 -0
  117. package/src/contexts/Modifiers/ModifiersContext.tsx +44 -0
  118. package/src/contexts/Modifiers/index.ts +2 -0
  119. package/src/contexts/Modifiers/useModifiers.test.ts +46 -0
  120. package/src/contexts/Modifiers/useModifiers.ts +17 -0
  121. package/src/contexts/Modifiers/utils/getActiveModifiers.test.ts +53 -0
  122. package/src/contexts/Modifiers/utils/getActiveModifiers.ts +33 -0
  123. package/src/contexts/Modifiers/utils/getCustomModifiers.test.ts +14 -0
  124. package/src/contexts/Modifiers/utils/getCustomModifiers.ts +14 -0
  125. package/src/contexts/Modifiers/utils/getInternalModifiers.test.ts +146 -0
  126. package/src/contexts/Modifiers/utils/getInternalModifiers.ts +58 -0
  127. package/src/contexts/Modifiers/utils/isDateInRange.test.ts +28 -0
  128. package/src/contexts/Modifiers/utils/isDateInRange.ts +27 -0
  129. package/src/contexts/Modifiers/utils/isMatch.test.ts +92 -0
  130. package/src/contexts/Modifiers/utils/isMatch.ts +76 -0
  131. package/src/contexts/Modifiers/utils/matcherToArray.test.ts +22 -0
  132. package/src/contexts/Modifiers/utils/matcherToArray.ts +14 -0
  133. package/src/contexts/Navigation/NavigationContext.tsx +84 -0
  134. package/src/contexts/Navigation/index.ts +2 -0
  135. package/src/contexts/Navigation/useNavigation.test.ts +126 -0
  136. package/src/contexts/Navigation/useNavigation.ts +12 -0
  137. package/src/contexts/Navigation/useNavigationState.test.ts +36 -0
  138. package/src/contexts/Navigation/useNavigationState.ts +25 -0
  139. package/src/contexts/Navigation/utils/getDisplayMonths.ts +31 -0
  140. package/src/contexts/Navigation/utils/getInitialMonth.test.ts +56 -0
  141. package/src/contexts/Navigation/utils/getInitialMonth.ts +24 -0
  142. package/src/contexts/Navigation/utils/getNextMonth.test.ts +75 -0
  143. package/src/contexts/Navigation/utils/getNextMonth.ts +45 -0
  144. package/src/contexts/Navigation/utils/getPreviousMonth.test.ts +55 -0
  145. package/src/contexts/Navigation/utils/getPreviousMonth.ts +44 -0
  146. package/src/contexts/RootProvider.tsx +37 -0
  147. package/src/contexts/SelectMultiple/SelectMultipleContext.tsx +135 -0
  148. package/src/contexts/SelectMultiple/index.ts +2 -0
  149. package/src/contexts/SelectMultiple/useSelectMultiple.test.ts +191 -0
  150. package/src/contexts/SelectMultiple/useSelectMultiple.ts +17 -0
  151. package/src/contexts/SelectRange/SelectRangeContext.tsx +158 -0
  152. package/src/contexts/SelectRange/index.ts +2 -0
  153. package/src/contexts/SelectRange/useSelectRange.test.ts +282 -0
  154. package/src/contexts/SelectRange/useSelectRange.ts +15 -0
  155. package/src/contexts/SelectRange/utils/addToRange.test.ts +119 -0
  156. package/src/contexts/SelectRange/utils/addToRange.ts +43 -0
  157. package/src/contexts/SelectSingle/SelectSingleContext.tsx +80 -0
  158. package/src/contexts/SelectSingle/index.ts +2 -0
  159. package/src/contexts/SelectSingle/useSelectSingle.test.ts +81 -0
  160. package/src/contexts/SelectSingle/useSelectSingle.ts +17 -0
  161. package/src/hooks/useActiveModifiers/index.ts +1 -0
  162. package/src/hooks/useActiveModifiers/useActiveModifiers.test.tsx +36 -0
  163. package/src/hooks/useActiveModifiers/useActiveModifiers.tsx +18 -0
  164. package/src/hooks/useControlledValue/index.ts +1 -0
  165. package/src/hooks/useControlledValue/useControlledValue.test.ts +68 -0
  166. package/src/hooks/useControlledValue/useControlledValue.ts +24 -0
  167. package/src/hooks/useDayEventHandlers/index.ts +1 -0
  168. package/src/hooks/useDayEventHandlers/useDayEventHandlers.test.tsx +213 -0
  169. package/src/hooks/useDayEventHandlers/useDayEventHandlers.tsx +195 -0
  170. package/src/hooks/useDayRender/index.ts +1 -0
  171. package/src/hooks/useDayRender/useDayRender.test.tsx +304 -0
  172. package/src/hooks/useDayRender/useDayRender.tsx +123 -0
  173. package/src/hooks/useDayRender/utils/getDayClassNames.test.ts +63 -0
  174. package/src/hooks/useDayRender/utils/getDayClassNames.ts +32 -0
  175. package/src/hooks/useDayRender/utils/getDayStyle.ts +19 -0
  176. package/src/hooks/useInput/index.ts +1 -0
  177. package/src/hooks/useInput/useInput.ts +175 -0
  178. package/src/hooks/useInput/utils/isValidDate.tsx +4 -0
  179. package/src/hooks/useSelectedDays/index.ts +1 -0
  180. package/src/hooks/useSelectedDays/useSelectedDays.test.ts +72 -0
  181. package/src/hooks/useSelectedDays/useSelectedDays.ts +32 -0
  182. package/src/index.ts +43 -0
  183. package/src/style.css +310 -0
  184. package/src/style.css.d.ts +38 -0
  185. package/src/types/DayPickerBase.ts +267 -0
  186. package/src/types/DayPickerDefault.ts +15 -0
  187. package/src/types/DayPickerMultiple.ts +26 -0
  188. package/src/types/DayPickerRange.ts +27 -0
  189. package/src/types/DayPickerSingle.ts +24 -0
  190. package/src/types/EventHandlers.ts +87 -0
  191. package/src/types/Formatters.ts +29 -0
  192. package/src/types/Labels.ts +36 -0
  193. package/src/types/Matchers.ts +106 -0
  194. package/src/types/Modifiers.ts +62 -0
  195. package/src/types/Styles.ts +108 -0
  196. package/tsconfig.json +24 -0
@@ -0,0 +1,37 @@
1
+ import enUS from 'date-fns/locale/en-US';
2
+
3
+ import { CaptionLayout } from 'components/Caption';
4
+ import { DayPickerContextValue } from 'contexts/DayPicker';
5
+
6
+ import { defaultClassNames } from './defaultClassNames';
7
+ import * as formatters from './formatters';
8
+ import * as labels from './labels';
9
+
10
+ /**
11
+ * Returns the default values to use in the DayPickerContext, in case they are
12
+ * not passed down with the DayPicker initial props.
13
+ */
14
+ export function getDefaultContextValue(): DayPickerContextValue {
15
+ const captionLayout: CaptionLayout = 'buttons';
16
+ const classNames = defaultClassNames;
17
+ const locale = enUS;
18
+ const modifiersClassNames = {};
19
+ const modifiers = {};
20
+ const numberOfMonths = 1;
21
+ const styles = {};
22
+ const today = new Date();
23
+
24
+ return {
25
+ captionLayout,
26
+ classNames,
27
+ formatters,
28
+ labels,
29
+ locale,
30
+ modifiersClassNames,
31
+ modifiers,
32
+ numberOfMonths,
33
+ styles,
34
+ today,
35
+ mode: 'default'
36
+ };
37
+ }
@@ -0,0 +1,15 @@
1
+ import es from 'date-fns/locale/es';
2
+
3
+ import { formatCaption } from './formatCaption';
4
+
5
+ const date = new Date(2022, 10, 21);
6
+
7
+ test('should return the formatted caption', () => {
8
+ expect(formatCaption(date)).toEqual('November 2022');
9
+ });
10
+
11
+ describe('when a locale is passed in', () => {
12
+ test('should format using the locale', () => {
13
+ expect(formatCaption(date, { locale: es })).toEqual('noviembre 2022');
14
+ });
15
+ });
@@ -0,0 +1,12 @@
1
+ import type { Locale } from 'date-fns';
2
+ import format from 'date-fns/format';
3
+
4
+ /**
5
+ * The default formatter for the caption.
6
+ */
7
+ export function formatCaption(
8
+ month: Date,
9
+ options?: { locale?: Locale }
10
+ ): string {
11
+ return format(month, 'LLLL y', options);
12
+ }
@@ -0,0 +1,7 @@
1
+ import { formatDay } from './formatDay';
2
+
3
+ const date = new Date(2022, 10, 21);
4
+
5
+ test('should return the formatted day', () => {
6
+ expect(formatDay(date)).toEqual('21');
7
+ });
@@ -0,0 +1,9 @@
1
+ import type { Locale } from 'date-fns';
2
+ import format from 'date-fns/format';
3
+
4
+ /**
5
+ * The default formatter for the Day button.
6
+ */
7
+ export function formatDay(day: Date, options?: { locale?: Locale }): string {
8
+ return format(day, 'd', options);
9
+ }
@@ -0,0 +1,15 @@
1
+ import es from 'date-fns/locale/es';
2
+
3
+ import { formatMonthCaption } from './formatMonthCaption';
4
+
5
+ const date = new Date(2022, 10, 21);
6
+
7
+ test('should return the formatted month caption', () => {
8
+ expect(formatMonthCaption(date)).toEqual('November');
9
+ });
10
+
11
+ describe('when a locale is passed in', () => {
12
+ test('should format using the locale', () => {
13
+ expect(formatMonthCaption(date, { locale: es })).toEqual('noviembre');
14
+ });
15
+ });
@@ -0,0 +1,12 @@
1
+ import type { Locale } from 'date-fns';
2
+ import format from 'date-fns/format';
3
+
4
+ /**
5
+ * The default formatter for the Month caption.
6
+ */
7
+ export function formatMonthCaption(
8
+ month: Date,
9
+ options?: { locale?: Locale }
10
+ ): string {
11
+ return format(month, 'LLLL', options);
12
+ }
@@ -0,0 +1,5 @@
1
+ import { formatWeekNumber } from './formatWeekNumber';
2
+
3
+ test('should return the formatted week number', () => {
4
+ expect(formatWeekNumber(10)).toEqual('10');
5
+ });
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The default formatter for the week number.
3
+ */
4
+ export function formatWeekNumber(weekNumber: number): string {
5
+ return `${weekNumber}`;
6
+ }
@@ -0,0 +1,15 @@
1
+ import es from 'date-fns/locale/es';
2
+
3
+ import { formatWeekdayName } from './formatWeekdayName';
4
+
5
+ const date = new Date(2022, 10, 21);
6
+
7
+ test('should return the formatted weekday name', () => {
8
+ expect(formatWeekdayName(date)).toEqual('Mo');
9
+ });
10
+
11
+ describe('when a locale is passed in', () => {
12
+ test('should format using the locale', () => {
13
+ expect(formatWeekdayName(date, { locale: es })).toEqual('lu');
14
+ });
15
+ });
@@ -0,0 +1,12 @@
1
+ import type { Locale } from 'date-fns';
2
+ import format from 'date-fns/format';
3
+
4
+ /**
5
+ * The default formatter for the name of the weekday.
6
+ */
7
+ export function formatWeekdayName(
8
+ weekday: Date,
9
+ options?: { locale?: Locale }
10
+ ): string {
11
+ return format(weekday, 'cccccc', options);
12
+ }
@@ -0,0 +1,7 @@
1
+ import { formatYearCaption } from './formatYearCaption';
2
+
3
+ const date = new Date(2022, 10, 21);
4
+
5
+ test('should return the formatted weekday name', () => {
6
+ expect(formatYearCaption(date)).toEqual('2022');
7
+ });
@@ -0,0 +1,11 @@
1
+ import format from 'date-fns/format';
2
+
3
+ /**
4
+ * The default formatter for the Year caption.
5
+ */
6
+ export function formatYearCaption(
7
+ year: Date,
8
+ options?: { locale?: Locale }
9
+ ): string {
10
+ return format(year, 'yyyy', options);
11
+ }
@@ -0,0 +1,6 @@
1
+ export * from './formatCaption';
2
+ export * from './formatDay';
3
+ export * from './formatMonthCaption';
4
+ export * from './formatWeekNumber';
5
+ export * from './formatWeekdayName';
6
+ export * from './formatYearCaption';
@@ -0,0 +1,2 @@
1
+ export * from './useDayPicker';
2
+ export * from './DayPickerContext';
@@ -0,0 +1,7 @@
1
+ export * from './labelDay';
2
+ export * from './labelMonthDropdown';
3
+ export * from './labelNext';
4
+ export * from './labelPrevious';
5
+ export * from './labelWeekday';
6
+ export * from './labelWeekNumber';
7
+ export * from './labelYearDropdown';
@@ -0,0 +1,7 @@
1
+ import { labelDay } from './labelDay';
2
+
3
+ const day = new Date(2022, 10, 21);
4
+
5
+ test('should return the day label', () => {
6
+ expect(labelDay(day, {})).toEqual('21st November (Monday)');
7
+ });
@@ -0,0 +1,10 @@
1
+ import format from 'date-fns/format';
2
+
3
+ import { DayLabel } from 'types/Labels';
4
+
5
+ /**
6
+ * The default ARIA label for the day button.
7
+ */
8
+ export const labelDay: DayLabel = (day, activeModifiers, options): string => {
9
+ return format(day, 'do MMMM (EEEE)', options);
10
+ };
@@ -0,0 +1,5 @@
1
+ import { labelMonthDropdown } from './labelMonthDropdown';
2
+
3
+ test('should return the label', () => {
4
+ expect(labelMonthDropdown()).toEqual('Month: ');
5
+ });
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The default ARIA label for the WeekNumber element.
3
+ */
4
+ export const labelMonthDropdown = (): string => {
5
+ return 'Month: ';
6
+ };
@@ -0,0 +1,5 @@
1
+ import { labelNext } from './labelNext';
2
+
3
+ test('should return the label', () => {
4
+ expect(labelNext()).toEqual('Go to next month');
5
+ });
@@ -0,0 +1,8 @@
1
+ import { NavButtonLabel } from 'types/Labels';
2
+
3
+ /**
4
+ * The default ARIA label for next month button in navigation
5
+ */
6
+ export const labelNext: NavButtonLabel = (): string => {
7
+ return 'Go to next month';
8
+ };
@@ -0,0 +1,5 @@
1
+ import { labelPrevious } from './labelPrevious';
2
+
3
+ test('should return the label', () => {
4
+ expect(labelPrevious()).toEqual('Go to previous month');
5
+ });
@@ -0,0 +1,8 @@
1
+ import { NavButtonLabel } from 'types/Labels';
2
+
3
+ /**
4
+ * The default ARIA label for previous month button in navigation
5
+ */
6
+ export const labelPrevious: NavButtonLabel = (): string => {
7
+ return 'Go to previous month';
8
+ };
@@ -0,0 +1,5 @@
1
+ import { labelWeekNumber } from './labelWeekNumber';
2
+
3
+ test('should return the label', () => {
4
+ expect(labelWeekNumber(2)).toEqual('Week n. 2');
5
+ });
@@ -0,0 +1,8 @@
1
+ import { WeekNumberLabel } from 'types/Labels';
2
+
3
+ /**
4
+ * The default ARIA label for the WeekNumber element.
5
+ */
6
+ export const labelWeekNumber: WeekNumberLabel = (n): string => {
7
+ return `Week n. ${n}`;
8
+ };
@@ -0,0 +1,15 @@
1
+ import es from 'date-fns/locale/es';
2
+
3
+ import { labelWeekday } from './labelWeekday';
4
+
5
+ const weekDay = new Date(2022, 10, 21);
6
+
7
+ test('should return the formatted weekday name', () => {
8
+ expect(labelWeekday(weekDay)).toEqual('Monday');
9
+ });
10
+
11
+ describe('when a locale is passed in', () => {
12
+ test('should format using the locale', () => {
13
+ expect(labelWeekday(weekDay, { locale: es })).toEqual('lunes');
14
+ });
15
+ });
@@ -0,0 +1,10 @@
1
+ import format from 'date-fns/format';
2
+
3
+ import { WeekdayLabel } from 'types/Labels';
4
+
5
+ /**
6
+ * The default ARIA label for the Weekday element.
7
+ */
8
+ export const labelWeekday: WeekdayLabel = (day, options): string => {
9
+ return format(day, 'cccc', options);
10
+ };
@@ -0,0 +1,5 @@
1
+ import { labelYearDropdown } from './labelYearDropdown';
2
+
3
+ test('should return the label', () => {
4
+ expect(labelYearDropdown()).toEqual('Year: ');
5
+ });
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The default ARIA label for the WeekNumber element.
3
+ */
4
+ export const labelYearDropdown = (): string => {
5
+ return 'Year: ';
6
+ };
@@ -0,0 +1,297 @@
1
+ import { RenderResult } from '@testing-library/react-hooks';
2
+ import es from 'date-fns/locale/es';
3
+ import { DayPickerProps } from 'DayPicker';
4
+
5
+ import { customRenderHook } from 'test/render';
6
+ import { freezeBeforeAll } from 'test/utils';
7
+
8
+ import { CaptionLayout } from 'components/Caption';
9
+ import { DayPickerContextValue, useDayPicker } from 'contexts/DayPicker';
10
+ import { getDefaultContextValue } from 'contexts/DayPicker/defaultContextValue';
11
+ import { CustomComponents } from 'types/DayPickerBase';
12
+ import { Formatters } from 'types/Formatters';
13
+ import { Labels } from 'types/Labels';
14
+ import { DayModifiers, ModifiersClassNames } from 'types/Modifiers';
15
+ import { ClassNames, Styles } from 'types/Styles';
16
+
17
+ const today = new Date(2022, 5, 13);
18
+ const defaults = getDefaultContextValue();
19
+
20
+ freezeBeforeAll(today);
21
+
22
+ let renderResult: RenderResult<DayPickerContextValue>;
23
+ function setup(dayPickerProps?: DayPickerProps) {
24
+ const { result } = customRenderHook(() => useDayPicker(), dayPickerProps);
25
+ renderResult = result;
26
+ }
27
+
28
+ describe('when passing rendered without props', () => {
29
+ type PropName =
30
+ | 'captionLayout'
31
+ | 'classNames'
32
+ | 'formatters'
33
+ | 'labels'
34
+ | 'locale'
35
+ | 'modifiersClassNames'
36
+ | 'modifiers'
37
+ | 'numberOfMonths'
38
+ | 'styles'
39
+ | 'today';
40
+
41
+ const testPropNames: PropName[] = [
42
+ 'captionLayout',
43
+ 'classNames',
44
+ 'formatters',
45
+ 'labels',
46
+ 'locale',
47
+ 'modifiersClassNames',
48
+ 'modifiers',
49
+ 'numberOfMonths',
50
+ 'styles'
51
+ // 'today' SKIPPED: this test doesn't pass
52
+ ];
53
+ beforeAll(() => {
54
+ setup();
55
+ });
56
+ test.each(testPropNames)('should use the %s default value', (propName) => {
57
+ expect(renderResult.current[propName]).toEqual(defaults[propName]);
58
+ });
59
+ });
60
+
61
+ describe('when passing "locale" from props', () => {
62
+ const locale = es;
63
+ const dayPickerProps: DayPickerProps = { locale };
64
+ beforeEach(() => {
65
+ setup(dayPickerProps);
66
+ });
67
+ test('should override the default locale', () => {
68
+ expect(renderResult.current.locale).not.toBe(defaults.locale);
69
+ });
70
+ test('should return the custom locale', () => {
71
+ expect(renderResult.current.locale).toBe(locale);
72
+ });
73
+ });
74
+
75
+ describe('when passing "numberOfMonths" from props', () => {
76
+ const numberOfMonths = 4;
77
+ const dayPickerProps: DayPickerProps = { numberOfMonths };
78
+ beforeEach(() => {
79
+ setup(dayPickerProps);
80
+ });
81
+ test('should override the default numberOfMonths', () => {
82
+ expect(renderResult.current.numberOfMonths).not.toBe(
83
+ defaults.numberOfMonths
84
+ );
85
+ });
86
+ test('should return the custom numberOfMonths', () => {
87
+ expect(renderResult.current.numberOfMonths).toBe(4);
88
+ });
89
+ });
90
+
91
+ describe('when passing "today" from props', () => {
92
+ const today = new Date(2010, 9, 11);
93
+ const dayPickerProps: DayPickerProps = { today };
94
+ beforeEach(() => {
95
+ setup(dayPickerProps);
96
+ });
97
+ test('should override the default "today"', () => {
98
+ expect(renderResult.current.today).not.toBe(defaults.today);
99
+ });
100
+ test('should return the custom "today"', () => {
101
+ expect(renderResult.current.today).toBe(today);
102
+ });
103
+ });
104
+
105
+ describe('when passing "captionLayout" from props', () => {
106
+ const captionLayout: CaptionLayout = 'dropdown';
107
+ const fromYear = 2000;
108
+ const toYear = 2010;
109
+ const dayPickerProps: DayPickerProps = { captionLayout, fromYear, toYear };
110
+ beforeEach(() => {
111
+ setup(dayPickerProps);
112
+ });
113
+ test('should override the default "captionLayout"', () => {
114
+ expect(renderResult.current.captionLayout).not.toBe(defaults.captionLayout);
115
+ });
116
+ test('should return the custom "captionLayout"', () => {
117
+ expect(renderResult.current.captionLayout).toBe(captionLayout);
118
+ });
119
+ });
120
+
121
+ describe('when "fromDate" and "toDate" are undefined', () => {
122
+ const fromDate = undefined;
123
+ const toDate = undefined;
124
+
125
+ describe('when using "dropdown" as "captionLayout"', () => {
126
+ const captionLayout: CaptionLayout = 'dropdown';
127
+ beforeEach(() => {
128
+ setup({ fromDate, toDate, captionLayout });
129
+ });
130
+ test('should return "buttons" as "captionLayout"', () => {
131
+ expect(renderResult.current.captionLayout).toBe('buttons');
132
+ });
133
+ });
134
+ });
135
+
136
+ describe('when "fromDate" is undefined, but not "toDate"', () => {
137
+ const fromDate = undefined;
138
+ const toDate = new Date();
139
+
140
+ describe('when using "dropdown" as "captionLayout"', () => {
141
+ const captionLayout: CaptionLayout = 'dropdown';
142
+ beforeEach(() => {
143
+ setup({ fromDate, toDate, captionLayout });
144
+ });
145
+ test('should return "buttons" as "captionLayout"', () => {
146
+ expect(renderResult.current.captionLayout).toBe('buttons');
147
+ });
148
+ });
149
+ });
150
+
151
+ describe('when "toDate" is undefined, but not "fromDate"', () => {
152
+ const fromDate = new Date();
153
+ const toDate = undefined;
154
+
155
+ describe('when using "dropdown" as "captionLayout"', () => {
156
+ const captionLayout: CaptionLayout = 'dropdown';
157
+ beforeEach(() => {
158
+ setup({ fromDate, toDate, captionLayout });
159
+ });
160
+ test('should return "buttons" as "captionLayout"', () => {
161
+ expect(renderResult.current.captionLayout).toBe('buttons');
162
+ });
163
+ });
164
+ });
165
+
166
+ describe('when using "dropdown" as "captionLayout"', () => {
167
+ const captionLayout: CaptionLayout = 'dropdown';
168
+ const fromYear = 2000;
169
+ const toYear = 2010;
170
+ const dayPickerProps: DayPickerProps = { captionLayout, fromYear, toYear };
171
+ beforeEach(() => {
172
+ setup(dayPickerProps);
173
+ });
174
+ test('should override the default "captionLayout"', () => {
175
+ expect(renderResult.current.captionLayout).not.toBe(defaults.captionLayout);
176
+ });
177
+ test('should return the custom "captionLayout"', () => {
178
+ expect(renderResult.current.captionLayout).toBe(captionLayout);
179
+ });
180
+ });
181
+
182
+ describe('when passing "modifiers" from props', () => {
183
+ const modifiers: DayModifiers = { foo: new Date() };
184
+ const dayPickerProps: DayPickerProps = { modifiers };
185
+ beforeEach(() => {
186
+ setup(dayPickerProps);
187
+ });
188
+ test('should override the default "modifiers"', () => {
189
+ expect(renderResult.current.modifiers).not.toBe(defaults.modifiers);
190
+ });
191
+ test('should return the custom "modifiers"', () => {
192
+ expect(renderResult.current.modifiers).toStrictEqual(modifiers);
193
+ });
194
+ });
195
+
196
+ describe('when passing "modifiersClassNames" from props', () => {
197
+ const modifiersClassNames: ModifiersClassNames = { foo: 'bar' };
198
+ const dayPickerProps: DayPickerProps = { modifiersClassNames };
199
+ beforeEach(() => {
200
+ setup(dayPickerProps);
201
+ });
202
+ test('should override the default "modifiersClassNames"', () => {
203
+ expect(renderResult.current.modifiersClassNames).not.toBe(
204
+ defaults.modifiersClassNames
205
+ );
206
+ });
207
+ test('should return the custom "modifiersClassNames"', () => {
208
+ expect(renderResult.current.modifiersClassNames).toStrictEqual(
209
+ modifiersClassNames
210
+ );
211
+ });
212
+ });
213
+
214
+ describe('when passing "styles" from props', () => {
215
+ const styles: Styles = { caption: { color: 'red ' } };
216
+ const dayPickerProps: DayPickerProps = { styles };
217
+ beforeEach(() => {
218
+ setup(dayPickerProps);
219
+ });
220
+ test('should override the default "styles"', () => {
221
+ expect(renderResult.current.styles).not.toBe(defaults.styles);
222
+ });
223
+ test('should include the custom "styles"', () => {
224
+ expect(renderResult.current.styles).toStrictEqual({
225
+ ...defaults.styles,
226
+ ...styles
227
+ });
228
+ });
229
+ });
230
+
231
+ describe('when passing "classNames" from props', () => {
232
+ const classNames: ClassNames = { caption: 'foo' };
233
+ const dayPickerProps: DayPickerProps = { classNames };
234
+ beforeEach(() => {
235
+ setup(dayPickerProps);
236
+ });
237
+ test('should override the default "classNames"', () => {
238
+ expect(renderResult.current.classNames).not.toBe(defaults.classNames);
239
+ });
240
+ test('should include the custom "classNames"', () => {
241
+ expect(renderResult.current.classNames).toStrictEqual({
242
+ ...defaults.classNames,
243
+ ...classNames
244
+ });
245
+ });
246
+ });
247
+
248
+ describe('when passing "formatters" from props', () => {
249
+ const formatters: Partial<Formatters> = { formatCaption: jest.fn() };
250
+ const dayPickerProps: DayPickerProps = { formatters };
251
+ beforeEach(() => {
252
+ setup(dayPickerProps);
253
+ });
254
+ test('should override the default "formatters"', () => {
255
+ expect(renderResult.current.formatters).not.toBe(defaults.formatters);
256
+ });
257
+ test('should include the custom "formatters"', () => {
258
+ expect(renderResult.current.formatters).toStrictEqual({
259
+ ...defaults.formatters,
260
+ ...formatters
261
+ });
262
+ });
263
+ });
264
+
265
+ describe('when passing "labels" from props', () => {
266
+ const labels: Partial<Labels> = { labelDay: jest.fn() };
267
+ const dayPickerProps: DayPickerProps = { labels };
268
+ beforeEach(() => {
269
+ setup(dayPickerProps);
270
+ });
271
+ test('should override the default "labels"', () => {
272
+ expect(renderResult.current.labels).not.toBe(defaults.labels);
273
+ });
274
+ test('should include the custom "labels"', () => {
275
+ expect(renderResult.current.labels).toStrictEqual({
276
+ ...defaults.labels,
277
+ ...labels
278
+ });
279
+ });
280
+ });
281
+
282
+ describe('when passing "components" from props', () => {
283
+ const components: CustomComponents = { Day: jest.fn() };
284
+ const dayPickerProps: DayPickerProps = { components };
285
+ beforeEach(() => {
286
+ setup(dayPickerProps);
287
+ });
288
+ test('should override the default "components"', () => {
289
+ expect(renderResult.current.components).not.toBe(defaults.components);
290
+ });
291
+ test('should include the custom "components"', () => {
292
+ expect(renderResult.current.components).toStrictEqual({
293
+ ...defaults.components,
294
+ ...components
295
+ });
296
+ });
297
+ });
@@ -0,0 +1,17 @@
1
+ import { useContext } from 'react';
2
+
3
+ import { DayPickerContext, DayPickerContextValue } from './DayPickerContext';
4
+
5
+ /**
6
+ * Hook to access the [[DayPickerContext]].
7
+ *
8
+ * To use this hook make sure to wrap the components with a one
9
+ * [[DayPickerProvider]].
10
+ */
11
+ export function useDayPicker(): DayPickerContextValue {
12
+ const context = useContext(DayPickerContext);
13
+ if (!context) {
14
+ throw new Error(`useDayPicker must be used within a DayPickerProvider.`);
15
+ }
16
+ return context;
17
+ }
@@ -0,0 +1 @@
1
+ export * from './parseFromToProps';