react-day-picker 8.0.2 → 8.0.5

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 (213) hide show
  1. package/dist/components/Head/utils/getWeekdays.d.ts +1 -1
  2. package/dist/components/Table/utils/daysToMonthWeeks.d.ts +1 -1
  3. package/dist/components/Table/utils/getMonthWeeks.d.ts +1 -1
  4. package/dist/contexts/DayPicker/formatters/formatCaption.d.ts +1 -1
  5. package/dist/contexts/DayPicker/formatters/formatDay.d.ts +1 -1
  6. package/dist/contexts/DayPicker/formatters/formatMonthCaption.d.ts +1 -1
  7. package/dist/contexts/DayPicker/formatters/formatWeekdayName.d.ts +1 -1
  8. package/dist/hooks/useDayRender/useDayRender.d.ts +1 -1
  9. package/dist/index.esm.js +30 -7
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/index.js +154 -107
  12. package/dist/index.js.map +1 -1
  13. package/dist/react-day-picker.min.js +1 -1
  14. package/dist/style.css +11 -10
  15. package/dist/style.module.css +11 -10
  16. package/dist/types/DayPickerBase.d.ts +1 -1
  17. package/dist/types/Labels.d.ts +1 -1
  18. package/package.json +15 -14
  19. package/src/DayPicker.tsx +113 -0
  20. package/src/components/Button/Button.test.tsx +47 -0
  21. package/src/components/Button/Button.tsx +36 -0
  22. package/src/components/Button/index.ts +1 -0
  23. package/src/components/Caption/Caption.test.tsx +86 -0
  24. package/src/components/Caption/Caption.tsx +54 -0
  25. package/src/components/Caption/index.ts +1 -0
  26. package/src/components/CaptionDropdowns/CaptionDropdowns.test.tsx +123 -0
  27. package/src/components/CaptionDropdowns/CaptionDropdowns.tsx +43 -0
  28. package/src/components/CaptionDropdowns/index.ts +1 -0
  29. package/src/components/CaptionLabel/CaptionLabel.test.tsx +29 -0
  30. package/src/components/CaptionLabel/CaptionLabel.tsx +32 -0
  31. package/src/components/CaptionLabel/index.ts +1 -0
  32. package/src/components/CaptionNavigation/CaptionNavigation.test.tsx +172 -0
  33. package/src/components/CaptionNavigation/CaptionNavigation.tsx +63 -0
  34. package/src/components/CaptionNavigation/index.ts +1 -0
  35. package/src/components/Day/Day.test.tsx +84 -0
  36. package/src/components/Day/Day.tsx +30 -0
  37. package/src/components/Day/index.ts +1 -0
  38. package/src/components/DayContent/DayContent.test.tsx +51 -0
  39. package/src/components/DayContent/DayContent.tsx +36 -0
  40. package/src/components/DayContent/index.ts +1 -0
  41. package/src/components/Dropdown/Dropdown.test.tsx +73 -0
  42. package/src/components/Dropdown/Dropdown.tsx +56 -0
  43. package/src/components/Dropdown/index.ts +1 -0
  44. package/src/components/Footer/Footer.test.tsx +29 -0
  45. package/src/components/Footer/Footer.tsx +20 -0
  46. package/src/components/Footer/index.ts +1 -0
  47. package/src/components/Head/Head.test.tsx +117 -0
  48. package/src/components/Head/Head.tsx +51 -0
  49. package/src/components/Head/index.ts +1 -0
  50. package/src/components/Head/utils/getWeekdays.test.ts +36 -0
  51. package/src/components/Head/utils/getWeekdays.ts +22 -0
  52. package/src/components/Head/utils/index.ts +1 -0
  53. package/src/components/IconDropdown/IconDropdown.test.tsx +20 -0
  54. package/src/components/IconDropdown/IconDropdown.tsx +24 -0
  55. package/src/components/IconDropdown/index.ts +1 -0
  56. package/src/components/IconLeft/IconLeft.test.tsx +20 -0
  57. package/src/components/IconLeft/IconLeft.tsx +18 -0
  58. package/src/components/IconLeft/index.ts +1 -0
  59. package/src/components/IconRight/IconRight.test.tsx +20 -0
  60. package/src/components/IconRight/IconRight.tsx +17 -0
  61. package/src/components/IconRight/index.ts +1 -0
  62. package/src/components/Month/Month.test.tsx +216 -0
  63. package/src/components/Month/Month.tsx +53 -0
  64. package/src/components/Month/index.ts +1 -0
  65. package/src/components/MonthsDropdown/MonthsDropdown.test.tsx +99 -0
  66. package/src/components/MonthsDropdown/MonthsDropdown.tsx +75 -0
  67. package/src/components/MonthsDropdown/index.ts +1 -0
  68. package/src/components/Navigation/Navigation.test.tsx +129 -0
  69. package/src/components/Navigation/Navigation.tsx +102 -0
  70. package/src/components/Navigation/index.ts +1 -0
  71. package/src/components/Root/Root.test.tsx +123 -0
  72. package/src/components/Root/Root.tsx +58 -0
  73. package/src/components/Root/index.ts +1 -0
  74. package/src/components/Row/Row.test.tsx +69 -0
  75. package/src/components/Row/Row.tsx +51 -0
  76. package/src/components/Row/index.ts +1 -0
  77. package/src/components/Table/Table.test.tsx +42 -0
  78. package/src/components/Table/Table.tsx +60 -0
  79. package/src/components/Table/__snapshots__/Table.test.tsx.snap +1453 -0
  80. package/src/components/Table/index.ts +1 -0
  81. package/src/components/Table/utils/daysToMonthWeeks.ts +47 -0
  82. package/src/components/Table/utils/getMonthWeeks.test.ts +68 -0
  83. package/src/components/Table/utils/getMonthWeeks.ts +55 -0
  84. package/src/components/WeekNumber/WeekNumber.test.tsx +46 -0
  85. package/src/components/WeekNumber/WeekNumber.tsx +58 -0
  86. package/src/components/WeekNumber/__snapshots__/WeekNumber.test.tsx.snap +11 -0
  87. package/src/components/WeekNumber/index.ts +1 -0
  88. package/src/components/YearsDropdown/YearsDropdown.test.tsx +98 -0
  89. package/src/components/YearsDropdown/YearsDropdown.tsx +76 -0
  90. package/src/components/YearsDropdown/index.ts +1 -0
  91. package/src/contexts/DayPicker/DayPickerContext.tsx +156 -0
  92. package/src/contexts/DayPicker/defaultClassNames.ts +58 -0
  93. package/src/contexts/DayPicker/defaultContextValue.ts +37 -0
  94. package/src/contexts/DayPicker/formatters/formatCaption.test.ts +15 -0
  95. package/src/contexts/DayPicker/formatters/formatCaption.ts +12 -0
  96. package/src/contexts/DayPicker/formatters/formatDay.test.ts +7 -0
  97. package/src/contexts/DayPicker/formatters/formatDay.ts +9 -0
  98. package/src/contexts/DayPicker/formatters/formatMonthCaption.test.ts +15 -0
  99. package/src/contexts/DayPicker/formatters/formatMonthCaption.ts +12 -0
  100. package/src/contexts/DayPicker/formatters/formatWeekNumber.test.ts +5 -0
  101. package/src/contexts/DayPicker/formatters/formatWeekNumber.ts +6 -0
  102. package/src/contexts/DayPicker/formatters/formatWeekdayName.test.ts +15 -0
  103. package/src/contexts/DayPicker/formatters/formatWeekdayName.ts +12 -0
  104. package/src/contexts/DayPicker/formatters/formatYearCaption.test.ts +7 -0
  105. package/src/contexts/DayPicker/formatters/formatYearCaption.ts +11 -0
  106. package/src/contexts/DayPicker/formatters/index.ts +6 -0
  107. package/src/contexts/DayPicker/index.ts +2 -0
  108. package/src/contexts/DayPicker/labels/index.ts +7 -0
  109. package/src/contexts/DayPicker/labels/labelDay.test.ts +7 -0
  110. package/src/contexts/DayPicker/labels/labelDay.ts +10 -0
  111. package/src/contexts/DayPicker/labels/labelMonthDropdown.test.ts +5 -0
  112. package/src/contexts/DayPicker/labels/labelMonthDropdown.ts +6 -0
  113. package/src/contexts/DayPicker/labels/labelNext.test.ts +5 -0
  114. package/src/contexts/DayPicker/labels/labelNext.ts +8 -0
  115. package/src/contexts/DayPicker/labels/labelPrevious.test.ts +5 -0
  116. package/src/contexts/DayPicker/labels/labelPrevious.ts +8 -0
  117. package/src/contexts/DayPicker/labels/labelWeekNumber.test.ts +5 -0
  118. package/src/contexts/DayPicker/labels/labelWeekNumber.ts +8 -0
  119. package/src/contexts/DayPicker/labels/labelWeekday.test.ts +15 -0
  120. package/src/contexts/DayPicker/labels/labelWeekday.ts +10 -0
  121. package/src/contexts/DayPicker/labels/labelYearDropdown.test.ts +5 -0
  122. package/src/contexts/DayPicker/labels/labelYearDropdown.ts +6 -0
  123. package/src/contexts/DayPicker/useDayPicker.test.ts +297 -0
  124. package/src/contexts/DayPicker/useDayPicker.ts +17 -0
  125. package/src/contexts/DayPicker/utils/index.ts +1 -0
  126. package/src/contexts/DayPicker/utils/parseFromToProps.test.ts +47 -0
  127. package/src/contexts/DayPicker/utils/parseFromToProps.ts +32 -0
  128. package/src/contexts/Focus/FocusContext.tsx +174 -0
  129. package/src/contexts/Focus/index.ts +2 -0
  130. package/src/contexts/Focus/useFocusContext.test.ts +183 -0
  131. package/src/contexts/Focus/useFocusContext.ts +12 -0
  132. package/src/contexts/Focus/utils/getInitialFocusTarget.test.tsx +12 -0
  133. package/src/contexts/Focus/utils/getInitialFocusTarget.tsx +44 -0
  134. package/src/contexts/Modifiers/ModifiersContext.tsx +44 -0
  135. package/src/contexts/Modifiers/index.ts +2 -0
  136. package/src/contexts/Modifiers/useModifiers.test.ts +46 -0
  137. package/src/contexts/Modifiers/useModifiers.ts +17 -0
  138. package/src/contexts/Modifiers/utils/getActiveModifiers.test.ts +53 -0
  139. package/src/contexts/Modifiers/utils/getActiveModifiers.ts +33 -0
  140. package/src/contexts/Modifiers/utils/getCustomModifiers.test.ts +14 -0
  141. package/src/contexts/Modifiers/utils/getCustomModifiers.ts +14 -0
  142. package/src/contexts/Modifiers/utils/getInternalModifiers.test.ts +146 -0
  143. package/src/contexts/Modifiers/utils/getInternalModifiers.ts +58 -0
  144. package/src/contexts/Modifiers/utils/isDateInRange.test.ts +28 -0
  145. package/src/contexts/Modifiers/utils/isDateInRange.ts +27 -0
  146. package/src/contexts/Modifiers/utils/isMatch.test.ts +92 -0
  147. package/src/contexts/Modifiers/utils/isMatch.ts +76 -0
  148. package/src/contexts/Modifiers/utils/matcherToArray.test.ts +22 -0
  149. package/src/contexts/Modifiers/utils/matcherToArray.ts +14 -0
  150. package/src/contexts/Navigation/NavigationContext.tsx +84 -0
  151. package/src/contexts/Navigation/index.ts +2 -0
  152. package/src/contexts/Navigation/useNavigation.test.ts +126 -0
  153. package/src/contexts/Navigation/useNavigation.ts +12 -0
  154. package/src/contexts/Navigation/useNavigationState.test.ts +36 -0
  155. package/src/contexts/Navigation/useNavigationState.ts +25 -0
  156. package/src/contexts/Navigation/utils/getDisplayMonths.ts +31 -0
  157. package/src/contexts/Navigation/utils/getInitialMonth.test.ts +56 -0
  158. package/src/contexts/Navigation/utils/getInitialMonth.ts +24 -0
  159. package/src/contexts/Navigation/utils/getNextMonth.test.ts +75 -0
  160. package/src/contexts/Navigation/utils/getNextMonth.ts +45 -0
  161. package/src/contexts/Navigation/utils/getPreviousMonth.test.ts +55 -0
  162. package/src/contexts/Navigation/utils/getPreviousMonth.ts +44 -0
  163. package/src/contexts/RootProvider.tsx +37 -0
  164. package/src/contexts/SelectMultiple/SelectMultipleContext.tsx +135 -0
  165. package/src/contexts/SelectMultiple/index.ts +2 -0
  166. package/src/contexts/SelectMultiple/useSelectMultiple.test.ts +191 -0
  167. package/src/contexts/SelectMultiple/useSelectMultiple.ts +17 -0
  168. package/src/contexts/SelectRange/SelectRangeContext.tsx +158 -0
  169. package/src/contexts/SelectRange/index.ts +2 -0
  170. package/src/contexts/SelectRange/useSelectRange.test.ts +282 -0
  171. package/src/contexts/SelectRange/useSelectRange.ts +15 -0
  172. package/src/contexts/SelectRange/utils/addToRange.test.ts +119 -0
  173. package/src/contexts/SelectRange/utils/addToRange.ts +43 -0
  174. package/src/contexts/SelectSingle/SelectSingleContext.tsx +80 -0
  175. package/src/contexts/SelectSingle/index.ts +2 -0
  176. package/src/contexts/SelectSingle/useSelectSingle.test.ts +81 -0
  177. package/src/contexts/SelectSingle/useSelectSingle.ts +17 -0
  178. package/src/hooks/useActiveModifiers/index.ts +1 -0
  179. package/src/hooks/useActiveModifiers/useActiveModifiers.test.tsx +36 -0
  180. package/src/hooks/useActiveModifiers/useActiveModifiers.tsx +18 -0
  181. package/src/hooks/useControlledValue/index.ts +1 -0
  182. package/src/hooks/useControlledValue/useControlledValue.test.ts +68 -0
  183. package/src/hooks/useControlledValue/useControlledValue.ts +24 -0
  184. package/src/hooks/useDayEventHandlers/index.ts +1 -0
  185. package/src/hooks/useDayEventHandlers/useDayEventHandlers.test.tsx +213 -0
  186. package/src/hooks/useDayEventHandlers/useDayEventHandlers.tsx +195 -0
  187. package/src/hooks/useDayRender/index.ts +1 -0
  188. package/src/hooks/useDayRender/useDayRender.test.tsx +304 -0
  189. package/src/hooks/useDayRender/useDayRender.tsx +123 -0
  190. package/src/hooks/useDayRender/utils/getDayClassNames.test.ts +63 -0
  191. package/src/hooks/useDayRender/utils/getDayClassNames.ts +32 -0
  192. package/src/hooks/useDayRender/utils/getDayStyle.ts +19 -0
  193. package/src/hooks/useInput/index.ts +1 -0
  194. package/src/hooks/useInput/useInput.ts +175 -0
  195. package/src/hooks/useInput/utils/isValidDate.tsx +4 -0
  196. package/src/hooks/useSelectedDays/index.ts +1 -0
  197. package/src/hooks/useSelectedDays/useSelectedDays.test.ts +72 -0
  198. package/src/hooks/useSelectedDays/useSelectedDays.ts +32 -0
  199. package/src/index.ts +43 -0
  200. package/src/style.css +311 -0
  201. package/src/style.css.d.ts +38 -0
  202. package/src/types/DayPickerBase.ts +267 -0
  203. package/src/types/DayPickerDefault.ts +15 -0
  204. package/src/types/DayPickerMultiple.ts +26 -0
  205. package/src/types/DayPickerRange.ts +27 -0
  206. package/src/types/DayPickerSingle.ts +24 -0
  207. package/src/types/EventHandlers.ts +87 -0
  208. package/src/types/Formatters.ts +29 -0
  209. package/src/types/Labels.ts +36 -0
  210. package/src/types/Matchers.ts +106 -0
  211. package/src/types/Modifiers.ts +62 -0
  212. package/src/types/Styles.ts +108 -0
  213. package/tsconfig.json +24 -0
@@ -0,0 +1,123 @@
1
+ import React from 'react';
2
+
3
+ import { RenderResult } from '@testing-library/react';
4
+ import { addDays } from 'date-fns';
5
+ import { DayPickerProps } from 'DayPicker';
6
+
7
+ import { focusDay } from 'test/actions';
8
+ import { getDayButton, queryMonthGrids } from 'test/po';
9
+ import { customRender } from 'test/render';
10
+ import { freezeBeforeAll } from 'test/utils';
11
+
12
+ import { defaultClassNames } from 'contexts/DayPicker/defaultClassNames';
13
+ import { ClassNames } from 'types/Styles';
14
+
15
+ import { Root } from './Root';
16
+
17
+ const today = new Date(2020, 10, 4);
18
+ freezeBeforeAll(today);
19
+
20
+ let container: HTMLElement;
21
+ let view: RenderResult;
22
+
23
+ function setup(dayPickerProps: DayPickerProps = {}) {
24
+ view = customRender(<Root />, dayPickerProps);
25
+ container = view.container;
26
+ }
27
+
28
+ describe('when the number of months is 1', () => {
29
+ const props: DayPickerProps = { numberOfMonths: 1 };
30
+ beforeEach(() => {
31
+ setup(props);
32
+ });
33
+ test('should display one month grid', () => {
34
+ expect(queryMonthGrids()).toHaveLength(1);
35
+ });
36
+ });
37
+
38
+ describe('when the number of months is greater than 1', () => {
39
+ const props: DayPickerProps = { numberOfMonths: 3 };
40
+ beforeEach(() => {
41
+ setup(props);
42
+ });
43
+ test('should display the specified number of month grids', () => {
44
+ expect(queryMonthGrids()).toHaveLength(3);
45
+ });
46
+ });
47
+
48
+ describe('when using the "classNames" prop', () => {
49
+ const classNames: ClassNames = {
50
+ root: 'foo'
51
+ };
52
+ beforeEach(() => {
53
+ setup({ classNames });
54
+ });
55
+ test('should display the specified number of month grids', () => {
56
+ expect(container.firstChild).toHaveClass('foo');
57
+ });
58
+ });
59
+
60
+ describe('when using the "className" prop', () => {
61
+ const props: DayPickerProps = { className: 'foo' };
62
+ beforeEach(() => {
63
+ setup(props);
64
+ });
65
+ test('should append the class name to the root element', () => {
66
+ expect(container.firstChild).toHaveClass('foo');
67
+ });
68
+ });
69
+
70
+ describe('when the "numberOfMonths" is greater than 1', () => {
71
+ const props: DayPickerProps = { numberOfMonths: 3 };
72
+ const expectedClassName = defaultClassNames.multiple_months;
73
+ beforeEach(() => {
74
+ setup(props);
75
+ });
76
+ test(`should have the ${expectedClassName} class name`, () => {
77
+ expect(container.firstChild).toHaveClass(expectedClassName);
78
+ });
79
+ });
80
+
81
+ describe('when showing the week numbers', () => {
82
+ const props: DayPickerProps = { showWeekNumber: true };
83
+ const expectedClassName = defaultClassNames.with_weeknumber;
84
+ beforeEach(() => {
85
+ setup(props);
86
+ });
87
+ test(`should have the ${expectedClassName} class name`, () => {
88
+ expect(container.firstChild).toHaveClass(expectedClassName);
89
+ });
90
+ });
91
+
92
+ describe('when "initialFocus" is set', () => {
93
+ const baseProps: DayPickerProps = {
94
+ initialFocus: true,
95
+ mode: 'single'
96
+ };
97
+ describe('when a day is not selected', () => {
98
+ beforeEach(() => {
99
+ setup(baseProps);
100
+ });
101
+ test('should focus today', () => {
102
+ expect(getDayButton(today)).toHaveFocus();
103
+ });
104
+ describe('when a new day is focused', () => {
105
+ beforeEach(() => {
106
+ focusDay(addDays(today, 1));
107
+ });
108
+ describe('and the calendar is rerendered', () => {
109
+ test.todo('should focus the new day');
110
+ });
111
+ });
112
+ });
113
+ describe('when a day is selected', () => {
114
+ const selected = addDays(today, 1);
115
+ const props: DayPickerProps = { ...baseProps, selected };
116
+ beforeEach(() => {
117
+ setup(props);
118
+ });
119
+ test('should focus the selected day', () => {
120
+ expect(getDayButton(selected)).toHaveFocus();
121
+ });
122
+ });
123
+ });
@@ -0,0 +1,58 @@
1
+ import React, { useEffect, useState } from 'react';
2
+
3
+ import { Month } from 'components/Month';
4
+ import { useDayPicker } from 'contexts/DayPicker';
5
+ import { useFocusContext } from 'contexts/Focus';
6
+ import { useNavigation } from 'contexts/Navigation';
7
+
8
+ /** Render the container with the months according to the number of months to display. */
9
+ export function Root(): JSX.Element {
10
+ const dayPicker = useDayPicker();
11
+ const focusContext = useFocusContext();
12
+ const navigation = useNavigation();
13
+
14
+ const [hasInitialFocus, setHasInitialFocus] = useState(false);
15
+
16
+ // Focus the focus target when initialFocus is passed in
17
+ useEffect(() => {
18
+ if (!dayPicker.initialFocus) return;
19
+ if (!focusContext.focusTarget) return;
20
+ if (hasInitialFocus) return;
21
+
22
+ focusContext.focus(focusContext.focusTarget);
23
+ setHasInitialFocus(true);
24
+ }, [
25
+ dayPicker.initialFocus,
26
+ hasInitialFocus,
27
+ focusContext.focus,
28
+ focusContext.focusTarget,
29
+ focusContext
30
+ ]);
31
+
32
+ // Apply classnames according to props
33
+ const classNames = [dayPicker.className ?? dayPicker.classNames.root];
34
+ if (dayPicker.numberOfMonths > 1) {
35
+ classNames.push(dayPicker.classNames.multiple_months);
36
+ }
37
+ if (dayPicker.showWeekNumber) {
38
+ classNames.push(dayPicker.classNames.with_weeknumber);
39
+ }
40
+
41
+ const style = {
42
+ ...dayPicker.styles.root,
43
+ ...dayPicker.style
44
+ };
45
+
46
+ return (
47
+ <div className={classNames.join(' ')} style={style} dir={dayPicker.dir}>
48
+ <div
49
+ className={dayPicker.classNames.months}
50
+ style={dayPicker.styles.months}
51
+ >
52
+ {navigation.displayMonths.map((month, i) => (
53
+ <Month key={i} displayIndex={i} displayMonth={month} />
54
+ ))}
55
+ </div>
56
+ </div>
57
+ );
58
+ }
@@ -0,0 +1 @@
1
+ export * from './Root';
@@ -0,0 +1,69 @@
1
+ import React from 'react';
2
+
3
+ import { screen } from '@testing-library/react';
4
+ import { DayPickerProps } from 'DayPicker';
5
+
6
+ import { customRender } from 'test/render/customRender';
7
+
8
+ import { CustomComponents } from 'types/DayPickerBase';
9
+
10
+ import { Row, RowProps } from './Row';
11
+
12
+ function setup(props: RowProps, dayPickerProps?: DayPickerProps) {
13
+ customRender(<Row {...props} />, dayPickerProps);
14
+ }
15
+
16
+ const props: RowProps = {
17
+ displayMonth: new Date(2020, 1),
18
+ weekNumber: 4,
19
+ dates: [new Date(2020, 1, 1), new Date(2020, 1, 2), new Date(2020, 1, 3)]
20
+ };
21
+
22
+ describe('when "showWeekNumber" is set', () => {
23
+ const dayPickerProps = {
24
+ showWeekNumber: true,
25
+ classNames: { cell: 'cell' },
26
+ styles: { cell: { background: 'red' } }
27
+ };
28
+ beforeEach(() => {
29
+ setup(props, dayPickerProps);
30
+ });
31
+ test('should display the cell with the week number', () => {
32
+ const cell = screen.getByRole('cell', { name: `${props.weekNumber}` });
33
+ expect(cell).toBeInTheDocument();
34
+ });
35
+ test('the cell should have the "cell" class name', () => {
36
+ const cell = screen.getByRole('cell', { name: `${props.weekNumber}` });
37
+ expect(cell).toHaveClass(dayPickerProps.classNames.cell);
38
+ });
39
+ test('the cell should have the "cell" style', () => {
40
+ const cell = screen.getByRole('cell', { name: `${props.weekNumber}` });
41
+ expect(cell).toHaveStyle(dayPickerProps.styles.cell);
42
+ });
43
+ });
44
+
45
+ describe('when using a custom Day component', () => {
46
+ const components: CustomComponents = {
47
+ Day: () => <div>CustomDay</div>
48
+ };
49
+ const dayPickerProps = { components };
50
+ beforeEach(() => {
51
+ setup(props, dayPickerProps);
52
+ });
53
+ test('it should render the custom component instead', () => {
54
+ expect(screen.getAllByText('CustomDay')).toHaveLength(props.dates.length);
55
+ });
56
+ });
57
+
58
+ describe('when using a custom WeekNumber component', () => {
59
+ const components: CustomComponents = {
60
+ WeekNumber: () => <div>WeekNumber</div>
61
+ };
62
+ const dayPickerProps: DayPickerProps = { components, showWeekNumber: true };
63
+ beforeEach(() => {
64
+ setup(props, dayPickerProps);
65
+ });
66
+ test('it should render the custom component instead', () => {
67
+ expect(screen.getByText('WeekNumber')).toBeInTheDocument();
68
+ });
69
+ });
@@ -0,0 +1,51 @@
1
+ import React from 'react';
2
+
3
+ import getUnixTime from 'date-fns/getUnixTime';
4
+
5
+ import { Day } from 'components/Day';
6
+ import { WeekNumber } from 'components/WeekNumber';
7
+ import { useDayPicker } from 'contexts/DayPicker';
8
+
9
+ /**
10
+ * The props for the [[Row]] component.
11
+ */
12
+ export interface RowProps {
13
+ /** The month where the row is displayed. */
14
+ displayMonth: Date;
15
+ /** The number of the week to render. */
16
+ weekNumber: number;
17
+ /** The days contained in the week. */
18
+ dates: Date[];
19
+ }
20
+
21
+ /** Render a row in the calendar, with the days and the week number. */
22
+ export function Row(props: RowProps): JSX.Element {
23
+ const { styles, classNames, showWeekNumber, components } = useDayPicker();
24
+
25
+ const DayComponent = components?.Day ?? Day;
26
+ const WeeknumberComponent = components?.WeekNumber ?? WeekNumber;
27
+
28
+ let weekNumberCell;
29
+ if (showWeekNumber) {
30
+ weekNumberCell = (
31
+ <td className={classNames.cell} style={styles.cell}>
32
+ <WeeknumberComponent number={props.weekNumber} dates={props.dates} />
33
+ </td>
34
+ );
35
+ }
36
+
37
+ return (
38
+ <tr className={classNames.row} style={styles.row}>
39
+ {weekNumberCell}
40
+ {props.dates.map((date) => (
41
+ <td
42
+ className={classNames.cell}
43
+ style={styles.cell}
44
+ key={getUnixTime(date)}
45
+ >
46
+ <DayComponent displayMonth={props.displayMonth} date={date} />
47
+ </td>
48
+ ))}
49
+ </tr>
50
+ );
51
+ }
@@ -0,0 +1 @@
1
+ export * from './Row';
@@ -0,0 +1,42 @@
1
+ import React from 'react';
2
+
3
+ import { DayPickerProps } from 'DayPicker';
4
+
5
+ import { customRender } from 'test/render/customRender';
6
+
7
+ import { Table, TableProps } from './Table';
8
+
9
+ function setup(props: TableProps, dayPickerProps?: DayPickerProps) {
10
+ return customRender(<Table {...props} />, dayPickerProps);
11
+ }
12
+
13
+ const props: TableProps = {
14
+ displayMonth: new Date(2020, 1)
15
+ };
16
+
17
+ test('should render correctly', () => {
18
+ const { container } = setup(props);
19
+ expect(container.firstChild).toMatchSnapshot();
20
+ });
21
+
22
+ describe('when showing the week numbers', () => {
23
+ const dayPickerProps = { showWeekNumber: true };
24
+ test('should render correctly', () => {
25
+ const { container } = setup(props, dayPickerProps);
26
+ expect(container.firstChild).toMatchSnapshot();
27
+ });
28
+ });
29
+
30
+ describe('when using custom components', () => {
31
+ const dayPickerProps: DayPickerProps = {
32
+ components: {
33
+ Head: () => <div>CustomHead </div>,
34
+ Row: () => <div>CustomRow </div>,
35
+ Footer: () => <div>CustomFooter</div>
36
+ }
37
+ };
38
+ test('should render correctly', () => {
39
+ const { container } = setup(props, dayPickerProps);
40
+ expect(container.firstChild).toMatchSnapshot();
41
+ });
42
+ });
@@ -0,0 +1,60 @@
1
+ import React from 'react';
2
+
3
+ import { Footer } from 'components/Footer';
4
+ import { Head } from 'components/Head';
5
+ import { Row } from 'components/Row';
6
+ import { useDayPicker } from 'contexts/DayPicker';
7
+
8
+ import { getMonthWeeks } from './utils/getMonthWeeks';
9
+
10
+ /** The props for the [[Table]] component. */
11
+ export interface TableProps {
12
+ /** The ID of the label of the table (the same given to the Caption). */
13
+ ['aria-labelledby']?: string;
14
+ /** The month where the table is displayed. */
15
+ displayMonth: Date;
16
+ }
17
+
18
+ /** Render the table with the calendar. */
19
+ export function Table(props: TableProps): JSX.Element {
20
+ const {
21
+ locale,
22
+ classNames,
23
+ styles,
24
+ hideHead,
25
+ fixedWeeks,
26
+ components,
27
+ weekStartsOn
28
+ } = useDayPicker();
29
+
30
+ const weeks = getMonthWeeks(props.displayMonth, {
31
+ useFixedWeeks: Boolean(fixedWeeks),
32
+ locale,
33
+ weekStartsOn
34
+ });
35
+
36
+ const HeadComponent = components?.Head ?? Head;
37
+ const RowComponent = components?.Row ?? Row;
38
+ const FooterComponent = components?.Footer ?? Footer;
39
+ return (
40
+ <table
41
+ className={classNames.table}
42
+ style={styles.table}
43
+ role="grid"
44
+ aria-labelledby={props['aria-labelledby']}
45
+ >
46
+ {!hideHead && <HeadComponent />}
47
+ <tbody className={classNames.tbody} style={styles.tbody}>
48
+ {weeks.map((week) => (
49
+ <RowComponent
50
+ displayMonth={props.displayMonth}
51
+ key={week.weekNumber}
52
+ dates={week.dates}
53
+ weekNumber={week.weekNumber}
54
+ />
55
+ ))}
56
+ </tbody>
57
+ <FooterComponent />
58
+ </table>
59
+ );
60
+ }