@navikt/ds-react 7.14.0-alpha.1 → 7.14.1

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 (226) hide show
  1. package/cjs/date/date-utils/check-dates.js +2 -2
  2. package/cjs/date/date-utils/check-dates.js.map +1 -1
  3. package/cjs/form/combobox/Combobox.d.ts +1 -1
  4. package/cjs/form/combobox/Input/InputController.d.ts +1 -1
  5. package/esm/date/date-utils/check-dates.js +3 -3
  6. package/esm/date/date-utils/check-dates.js.map +1 -1
  7. package/esm/form/combobox/Combobox.d.ts +1 -1
  8. package/esm/form/combobox/Input/InputController.d.ts +1 -1
  9. package/package.json +5 -4
  10. package/src/date/date-utils/check-dates.ts +10 -3
  11. package/cjs/date/context/index.d.ts +0 -3
  12. package/cjs/date/context/index.js +0 -13
  13. package/cjs/date/context/index.js.map +0 -1
  14. package/cjs/date/context/useDateInputContext.d.ts +0 -21
  15. package/cjs/date/context/useDateInputContext.js +0 -14
  16. package/cjs/date/context/useDateInputContext.js.map +0 -1
  17. package/cjs/date/context/useDateTranslationContext.d.ts +0 -8
  18. package/cjs/date/context/useDateTranslationContext.js +0 -7
  19. package/cjs/date/context/useDateTranslationContext.js.map +0 -1
  20. package/cjs/date/context/useSharedMonthContext.d.ts +0 -21
  21. package/cjs/date/context/useSharedMonthContext.js +0 -64
  22. package/cjs/date/context/useSharedMonthContext.js.map +0 -1
  23. package/cjs/date/datepicker/DatePickerStandalone.d.ts +0 -18
  24. package/cjs/date/datepicker/DatePickerStandalone.js +0 -86
  25. package/cjs/date/datepicker/DatePickerStandalone.js.map +0 -1
  26. package/cjs/date/datepicker/parts/Caption.d.ts +0 -7
  27. package/cjs/date/datepicker/parts/Caption.js +0 -30
  28. package/cjs/date/datepicker/parts/Caption.js.map +0 -1
  29. package/cjs/date/datepicker/parts/DayButton.d.ts +0 -4
  30. package/cjs/date/datepicker/parts/DayButton.js +0 -43
  31. package/cjs/date/datepicker/parts/DayButton.js.map +0 -1
  32. package/cjs/date/datepicker/parts/DropdownCaption.d.ts +0 -7
  33. package/cjs/date/datepicker/parts/DropdownCaption.js +0 -46
  34. package/cjs/date/datepicker/parts/DropdownCaption.js.map +0 -1
  35. package/cjs/date/datepicker/parts/HeadRow.d.ts +0 -10
  36. package/cjs/date/datepicker/parts/HeadRow.js +0 -36
  37. package/cjs/date/datepicker/parts/HeadRow.js.map +0 -1
  38. package/cjs/date/datepicker/parts/Row.d.ts +0 -16
  39. package/cjs/date/datepicker/parts/Row.js +0 -25
  40. package/cjs/date/datepicker/parts/Row.js.map +0 -1
  41. package/cjs/date/datepicker/parts/TableHead.d.ts +0 -5
  42. package/cjs/date/datepicker/parts/TableHead.js +0 -19
  43. package/cjs/date/datepicker/parts/TableHead.js.map +0 -1
  44. package/cjs/date/datepicker/parts/WeekNumber.d.ts +0 -11
  45. package/cjs/date/datepicker/parts/WeekNumber.js +0 -33
  46. package/cjs/date/datepicker/parts/WeekNumber.js.map +0 -1
  47. package/cjs/date/datepicker/parts/WeekRow.d.ts +0 -5
  48. package/cjs/date/datepicker/parts/WeekRow.js +0 -36
  49. package/cjs/date/datepicker/parts/WeekRow.js.map +0 -1
  50. package/cjs/date/datepicker/types.d.ts +0 -105
  51. package/cjs/date/datepicker/types.js +0 -3
  52. package/cjs/date/datepicker/types.js.map +0 -1
  53. package/cjs/date/hooks/index.d.ts +0 -6
  54. package/cjs/date/hooks/index.js +0 -10
  55. package/cjs/date/hooks/index.js.map +0 -1
  56. package/cjs/date/hooks/useDatepicker.d.ts +0 -90
  57. package/cjs/date/hooks/useDatepicker.js +0 -163
  58. package/cjs/date/hooks/useDatepicker.js.map +0 -1
  59. package/cjs/date/hooks/useMonthPicker.d.ts +0 -85
  60. package/cjs/date/hooks/useMonthPicker.js +0 -174
  61. package/cjs/date/hooks/useMonthPicker.js.map +0 -1
  62. package/cjs/date/hooks/useRangeDatepicker.d.ts +0 -76
  63. package/cjs/date/hooks/useRangeDatepicker.js +0 -319
  64. package/cjs/date/hooks/useRangeDatepicker.js.map +0 -1
  65. package/cjs/date/monthpicker/MonthButton.d.ts +0 -11
  66. package/cjs/date/monthpicker/MonthButton.js +0 -82
  67. package/cjs/date/monthpicker/MonthButton.js.map +0 -1
  68. package/cjs/date/monthpicker/MonthCaption.d.ts +0 -3
  69. package/cjs/date/monthpicker/MonthCaption.js +0 -52
  70. package/cjs/date/monthpicker/MonthCaption.js.map +0 -1
  71. package/cjs/date/monthpicker/MonthPickerStandalone.d.ts +0 -11
  72. package/cjs/date/monthpicker/MonthPickerStandalone.js +0 -65
  73. package/cjs/date/monthpicker/MonthPickerStandalone.js.map +0 -1
  74. package/cjs/date/monthpicker/MonthSelector.d.ts +0 -3
  75. package/cjs/date/monthpicker/MonthSelector.js +0 -80
  76. package/cjs/date/monthpicker/MonthSelector.js.map +0 -1
  77. package/cjs/date/monthpicker/types.d.ts +0 -86
  78. package/cjs/date/monthpicker/types.js +0 -3
  79. package/cjs/date/monthpicker/types.js.map +0 -1
  80. package/cjs/date/parts/DateInput.d.ts +0 -28
  81. package/cjs/date/parts/DateInput.js +0 -91
  82. package/cjs/date/parts/DateInput.js.map +0 -1
  83. package/cjs/date/parts/DateWrapper.d.ts +0 -18
  84. package/cjs/date/parts/DateWrapper.js +0 -67
  85. package/cjs/date/parts/DateWrapper.js.map +0 -1
  86. package/cjs/date/utils/check-dates.d.ts +0 -3
  87. package/cjs/date/utils/check-dates.js +0 -14
  88. package/cjs/date/utils/check-dates.js.map +0 -1
  89. package/cjs/date/utils/dates-disabled.d.ts +0 -1
  90. package/cjs/date/utils/dates-disabled.js +0 -30
  91. package/cjs/date/utils/dates-disabled.js.map +0 -1
  92. package/cjs/date/utils/format-date.d.ts +0 -2
  93. package/cjs/date/utils/format-date.js +0 -13
  94. package/cjs/date/utils/format-date.js.map +0 -1
  95. package/cjs/date/utils/get-dates.d.ts +0 -2
  96. package/cjs/date/utils/get-dates.js +0 -52
  97. package/cjs/date/utils/get-dates.js.map +0 -1
  98. package/cjs/date/utils/get-initial-year.d.ts +0 -5
  99. package/cjs/date/utils/get-initial-year.js +0 -21
  100. package/cjs/date/utils/get-initial-year.js.map +0 -1
  101. package/cjs/date/utils/get-month-weeks.d.ts +0 -16
  102. package/cjs/date/utils/get-month-weeks.js +0 -46
  103. package/cjs/date/utils/get-month-weeks.js.map +0 -1
  104. package/cjs/date/utils/index.d.ts +0 -9
  105. package/cjs/date/utils/index.js +0 -29
  106. package/cjs/date/utils/index.js.map +0 -1
  107. package/cjs/date/utils/is-match.d.ts +0 -4
  108. package/cjs/date/utils/is-match.js +0 -61
  109. package/cjs/date/utils/is-match.js.map +0 -1
  110. package/cjs/date/utils/locale.d.ts +0 -36
  111. package/cjs/date/utils/locale.js +0 -54
  112. package/cjs/date/utils/locale.js.map +0 -1
  113. package/cjs/date/utils/navigation.d.ts +0 -2
  114. package/cjs/date/utils/navigation.js +0 -156
  115. package/cjs/date/utils/navigation.js.map +0 -1
  116. package/cjs/date/utils/parse-date.d.ts +0 -4
  117. package/cjs/date/utils/parse-date.js +0 -87
  118. package/cjs/date/utils/parse-date.js.map +0 -1
  119. package/esm/date/context/index.d.ts +0 -3
  120. package/esm/date/context/index.js +0 -4
  121. package/esm/date/context/index.js.map +0 -1
  122. package/esm/date/context/useDateInputContext.d.ts +0 -21
  123. package/esm/date/context/useDateInputContext.js +0 -10
  124. package/esm/date/context/useDateInputContext.js.map +0 -1
  125. package/esm/date/context/useDateTranslationContext.d.ts +0 -8
  126. package/esm/date/context/useDateTranslationContext.js +0 -3
  127. package/esm/date/context/useDateTranslationContext.js.map +0 -1
  128. package/esm/date/context/useSharedMonthContext.d.ts +0 -21
  129. package/esm/date/context/useSharedMonthContext.js +0 -36
  130. package/esm/date/context/useSharedMonthContext.js.map +0 -1
  131. package/esm/date/datepicker/DatePickerStandalone.d.ts +0 -18
  132. package/esm/date/datepicker/DatePickerStandalone.js +0 -57
  133. package/esm/date/datepicker/DatePickerStandalone.js.map +0 -1
  134. package/esm/date/datepicker/parts/Caption.d.ts +0 -7
  135. package/esm/date/datepicker/parts/Caption.js +0 -23
  136. package/esm/date/datepicker/parts/Caption.js.map +0 -1
  137. package/esm/date/datepicker/parts/DayButton.d.ts +0 -4
  138. package/esm/date/datepicker/parts/DayButton.js +0 -18
  139. package/esm/date/datepicker/parts/DayButton.js.map +0 -1
  140. package/esm/date/datepicker/parts/DropdownCaption.d.ts +0 -7
  141. package/esm/date/datepicker/parts/DropdownCaption.js +0 -39
  142. package/esm/date/datepicker/parts/DropdownCaption.js.map +0 -1
  143. package/esm/date/datepicker/parts/HeadRow.d.ts +0 -10
  144. package/esm/date/datepicker/parts/HeadRow.js +0 -29
  145. package/esm/date/datepicker/parts/HeadRow.js.map +0 -1
  146. package/esm/date/datepicker/parts/Row.d.ts +0 -16
  147. package/esm/date/datepicker/parts/Row.js +0 -20
  148. package/esm/date/datepicker/parts/Row.js.map +0 -1
  149. package/esm/date/datepicker/parts/TableHead.d.ts +0 -5
  150. package/esm/date/datepicker/parts/TableHead.js +0 -14
  151. package/esm/date/datepicker/parts/TableHead.js.map +0 -1
  152. package/esm/date/datepicker/parts/WeekNumber.d.ts +0 -11
  153. package/esm/date/datepicker/parts/WeekNumber.js +0 -28
  154. package/esm/date/datepicker/parts/WeekNumber.js.map +0 -1
  155. package/esm/date/datepicker/parts/WeekRow.d.ts +0 -5
  156. package/esm/date/datepicker/parts/WeekRow.js +0 -31
  157. package/esm/date/datepicker/parts/WeekRow.js.map +0 -1
  158. package/esm/date/datepicker/types.d.ts +0 -105
  159. package/esm/date/datepicker/types.js +0 -2
  160. package/esm/date/datepicker/types.js.map +0 -1
  161. package/esm/date/hooks/index.d.ts +0 -6
  162. package/esm/date/hooks/index.js +0 -4
  163. package/esm/date/hooks/index.js.map +0 -1
  164. package/esm/date/hooks/useDatepicker.d.ts +0 -90
  165. package/esm/date/hooks/useDatepicker.js +0 -159
  166. package/esm/date/hooks/useDatepicker.js.map +0 -1
  167. package/esm/date/hooks/useMonthPicker.d.ts +0 -85
  168. package/esm/date/hooks/useMonthPicker.js +0 -170
  169. package/esm/date/hooks/useMonthPicker.js.map +0 -1
  170. package/esm/date/hooks/useRangeDatepicker.d.ts +0 -76
  171. package/esm/date/hooks/useRangeDatepicker.js +0 -315
  172. package/esm/date/hooks/useRangeDatepicker.js.map +0 -1
  173. package/esm/date/monthpicker/MonthButton.d.ts +0 -11
  174. package/esm/date/monthpicker/MonthButton.js +0 -52
  175. package/esm/date/monthpicker/MonthButton.js.map +0 -1
  176. package/esm/date/monthpicker/MonthCaption.d.ts +0 -3
  177. package/esm/date/monthpicker/MonthCaption.js +0 -45
  178. package/esm/date/monthpicker/MonthCaption.js.map +0 -1
  179. package/esm/date/monthpicker/MonthPickerStandalone.d.ts +0 -11
  180. package/esm/date/monthpicker/MonthPickerStandalone.js +0 -36
  181. package/esm/date/monthpicker/MonthPickerStandalone.js.map +0 -1
  182. package/esm/date/monthpicker/MonthSelector.d.ts +0 -3
  183. package/esm/date/monthpicker/MonthSelector.js +0 -50
  184. package/esm/date/monthpicker/MonthSelector.js.map +0 -1
  185. package/esm/date/monthpicker/types.d.ts +0 -86
  186. package/esm/date/monthpicker/types.js +0 -2
  187. package/esm/date/monthpicker/types.js.map +0 -1
  188. package/esm/date/parts/DateInput.d.ts +0 -28
  189. package/esm/date/parts/DateInput.js +0 -62
  190. package/esm/date/parts/DateInput.js.map +0 -1
  191. package/esm/date/parts/DateWrapper.d.ts +0 -18
  192. package/esm/date/parts/DateWrapper.js +0 -37
  193. package/esm/date/parts/DateWrapper.js.map +0 -1
  194. package/esm/date/utils/check-dates.d.ts +0 -3
  195. package/esm/date/utils/check-dates.js +0 -9
  196. package/esm/date/utils/check-dates.js.map +0 -1
  197. package/esm/date/utils/dates-disabled.d.ts +0 -1
  198. package/esm/date/utils/dates-disabled.js +0 -26
  199. package/esm/date/utils/dates-disabled.js.map +0 -1
  200. package/esm/date/utils/format-date.d.ts +0 -2
  201. package/esm/date/utils/format-date.js +0 -9
  202. package/esm/date/utils/format-date.js.map +0 -1
  203. package/esm/date/utils/get-dates.d.ts +0 -2
  204. package/esm/date/utils/get-dates.js +0 -47
  205. package/esm/date/utils/get-dates.js.map +0 -1
  206. package/esm/date/utils/get-initial-year.d.ts +0 -5
  207. package/esm/date/utils/get-initial-year.js +0 -18
  208. package/esm/date/utils/get-initial-year.js.map +0 -1
  209. package/esm/date/utils/get-month-weeks.d.ts +0 -16
  210. package/esm/date/utils/get-month-weeks.js +0 -42
  211. package/esm/date/utils/get-month-weeks.js.map +0 -1
  212. package/esm/date/utils/index.d.ts +0 -9
  213. package/esm/date/utils/index.js +0 -10
  214. package/esm/date/utils/index.js.map +0 -1
  215. package/esm/date/utils/is-match.d.ts +0 -4
  216. package/esm/date/utils/is-match.js +0 -57
  217. package/esm/date/utils/is-match.js.map +0 -1
  218. package/esm/date/utils/locale.d.ts +0 -36
  219. package/esm/date/utils/locale.js +0 -45
  220. package/esm/date/utils/locale.js.map +0 -1
  221. package/esm/date/utils/navigation.d.ts +0 -2
  222. package/esm/date/utils/navigation.js +0 -152
  223. package/esm/date/utils/navigation.js.map +0 -1
  224. package/esm/date/utils/parse-date.d.ts +0 -4
  225. package/esm/date/utils/parse-date.js +0 -83
  226. package/esm/date/utils/parse-date.js.map +0 -1
@@ -1,85 +0,0 @@
1
- import React from "react";
2
- import { MonthPickerProps } from "../monthpicker/types";
3
- import { DateInputProps } from "../parts/DateInput";
4
- export interface UseMonthPickerOptions extends Pick<MonthPickerProps, "locale" | "fromDate" | "toDate" | "disabled" | "defaultSelected"> {
5
- /**
6
- * Make Date-selection required
7
- */
8
- required?: boolean;
9
- /**
10
- * Callback for month-change
11
- */
12
- onMonthChange?: (date?: Date) => void;
13
- /**
14
- * Input-format
15
- * @default "MMMM yyyy"
16
- */
17
- inputFormat?: string;
18
- /**
19
- * validation-callback
20
- */
21
- onValidate?: (val: MonthValidationT) => void;
22
- /**
23
- * Default shown year
24
- */
25
- defaultYear?: Date;
26
- /**
27
- * Allows input of with `yy` year format.
28
- *
29
- * Decision between 20th and 21st century is based on before(todays year - 80) ? 21st : 20th.
30
- * In 2024 this equals to 1944 - 2043
31
- * @default true
32
- */
33
- allowTwoDigitYear?: boolean;
34
- }
35
- interface UseMonthPickerValue {
36
- /**
37
- * Use: <MonthPicker {...monthpickerProps} />
38
- */
39
- monthpickerProps: MonthPickerProps;
40
- /**
41
- * Use: <MonthPicker.Input {...inputProps} />
42
- */
43
- inputProps: Pick<DateInputProps, "onChange" | "onFocus" | "value"> & {
44
- /**
45
- * @private
46
- */
47
- setAnchorRef: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;
48
- };
49
- /**
50
- * Currently selected Date
51
- * Up to user to validate value and extract month
52
- */
53
- selectedMonth?: Date;
54
- /**
55
- * Manually set selected month if needed
56
- */
57
- setSelected: (date?: Date) => void;
58
- /**
59
- * Resets all states
60
- */
61
- reset: () => void;
62
- }
63
- export type MonthValidationT = {
64
- isDisabled: boolean;
65
- isEmpty: boolean;
66
- isInvalid: boolean;
67
- isValidMonth: boolean;
68
- isBefore: boolean;
69
- isAfter: boolean;
70
- };
71
- /**
72
- *
73
- * @see 🏷️ {@link UseMonthPickerOptions}
74
- * @see 🏷️ {@link UseMonthPickerValue}
75
- * @see 🏷️ {@link MonthValidationT}
76
- * @example
77
- * const { monthpickerProps, inputProps } = useMonthpicker({
78
- * fromDate: new Date("Aug 23 2019"),
79
- * toDate: new Date("Feb 23 2024"),
80
- * onMonthChange: console.log,
81
- * onValidate: console.log,
82
- * });
83
- */
84
- export declare const useMonthpicker: (opt?: UseMonthPickerOptions) => UseMonthPickerValue;
85
- export {};
@@ -1,174 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useMonthpicker = void 0;
4
- const react_1 = require("react");
5
- const i18n_hooks_1 = require("../../util/i18n/i18n.hooks");
6
- const utils_1 = require("../utils");
7
- const getValidationMessage = (val = {}) => (Object.assign({ isDisabled: false, isEmpty: false, isInvalid: false, isBefore: false, isAfter: false, isValidMonth: true }, val));
8
- const getIsBefore = (opt) => opt.fromDate &&
9
- opt.date &&
10
- (opt.fromDate.getFullYear() > opt.date.getFullYear() ||
11
- (opt.fromDate.getFullYear() === opt.date.getFullYear() &&
12
- opt.fromDate.getMonth() > opt.date.getMonth()));
13
- const getIsAfter = (opt) => opt.toDate &&
14
- opt.date &&
15
- (opt.toDate.getFullYear() < opt.date.getFullYear() ||
16
- (opt.toDate.getFullYear() === opt.date.getFullYear() &&
17
- opt.toDate.getMonth() < opt.date.getMonth()));
18
- /**
19
- *
20
- * @see 🏷️ {@link UseMonthPickerOptions}
21
- * @see 🏷️ {@link UseMonthPickerValue}
22
- * @see 🏷️ {@link MonthValidationT}
23
- * @example
24
- * const { monthpickerProps, inputProps } = useMonthpicker({
25
- * fromDate: new Date("Aug 23 2019"),
26
- * toDate: new Date("Feb 23 2024"),
27
- * onMonthChange: console.log,
28
- * onValidate: console.log,
29
- * });
30
- */
31
- const useMonthpicker = (opt = {}) => {
32
- var _a;
33
- const { locale: _locale, defaultSelected: _defaultSelected, fromDate, toDate, disabled, required, onMonthChange, inputFormat, onValidate, defaultYear, allowTwoDigitYear = true, } = opt;
34
- const [anchorRef, setAnchorRef] = (0, react_1.useState)(null);
35
- const [defaultSelected, setDefaultSelected] = (0, react_1.useState)(_defaultSelected);
36
- const today = (0, react_1.useMemo)(() => new Date(), []);
37
- const localeFromProvider = (0, i18n_hooks_1.useDateLocale)();
38
- const locale = _locale ? (0, utils_1.getLocaleFromString)(_locale) : localeFromProvider;
39
- // Initialize states
40
- const [year, setYear] = (0, react_1.useState)((_a = defaultSelected !== null && defaultSelected !== void 0 ? defaultSelected : defaultYear) !== null && _a !== void 0 ? _a : today);
41
- const [selectedMonth, setSelectedMonth] = (0, react_1.useState)(defaultSelected);
42
- const [open, setOpen] = (0, react_1.useState)(false);
43
- const defaultInputValue = defaultSelected
44
- ? (0, utils_1.formatDateForInput)(defaultSelected, locale, "month", inputFormat)
45
- : "";
46
- const [inputValue, setInputValue] = (0, react_1.useState)(defaultInputValue);
47
- const handleOpen = (0, react_1.useCallback)((newOpen) => {
48
- var _a, _b;
49
- setOpen(newOpen);
50
- newOpen &&
51
- setYear((_b = (_a = selectedMonth !== null && selectedMonth !== void 0 ? selectedMonth : defaultSelected) !== null && _a !== void 0 ? _a : defaultYear) !== null && _b !== void 0 ? _b : today);
52
- }, [defaultSelected, defaultYear, selectedMonth, today]);
53
- const updateMonth = (date) => {
54
- onMonthChange === null || onMonthChange === void 0 ? void 0 : onMonthChange(date);
55
- setSelectedMonth(date);
56
- };
57
- const updateValidation = (val = {}) => onValidate === null || onValidate === void 0 ? void 0 : onValidate(getValidationMessage(val));
58
- const reset = () => {
59
- var _a;
60
- updateMonth(defaultSelected);
61
- setYear((_a = defaultSelected !== null && defaultSelected !== void 0 ? defaultSelected : defaultYear) !== null && _a !== void 0 ? _a : today);
62
- setInputValue(defaultInputValue !== null && defaultInputValue !== void 0 ? defaultInputValue : "");
63
- setDefaultSelected(_defaultSelected);
64
- };
65
- const setSelected = (date) => {
66
- var _a;
67
- updateMonth(date);
68
- setYear((_a = date !== null && date !== void 0 ? date : defaultYear) !== null && _a !== void 0 ? _a : today);
69
- setInputValue(date ? (0, utils_1.formatDateForInput)(date, locale, "month", inputFormat) : "");
70
- };
71
- const handleFocus = (e) => {
72
- var _a;
73
- if (e.target.readOnly) {
74
- return;
75
- }
76
- const day = (0, utils_1.parseDate)(e.target.value, today, locale, "month", allowTwoDigitYear);
77
- const isBefore = getIsBefore({ fromDate, date: day });
78
- const isAfter = getIsAfter({ toDate, date: day });
79
- if ((0, utils_1.isValidDate)(day)) {
80
- !isBefore && !isAfter && setYear(day);
81
- setInputValue((0, utils_1.formatDateForInput)(day, locale, "month", inputFormat));
82
- }
83
- else {
84
- setYear((_a = defaultSelected !== null && defaultSelected !== void 0 ? defaultSelected : defaultYear) !== null && _a !== void 0 ? _a : today);
85
- }
86
- };
87
- const handleBlur = (e) => {
88
- const day = (0, utils_1.parseDate)(e.target.value, today, locale, "month", allowTwoDigitYear);
89
- (0, utils_1.isValidDate)(day) &&
90
- setInputValue((0, utils_1.formatDateForInput)(day, locale, "month", inputFormat));
91
- };
92
- /* Only allow de-selecting if not required */
93
- const handleMonthClick = (month) => {
94
- if (!month && required) {
95
- return;
96
- }
97
- if (month) {
98
- handleOpen(false);
99
- setYear(month);
100
- anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.focus();
101
- }
102
- if (!month) {
103
- updateMonth(undefined);
104
- updateValidation({ isValidMonth: false, isEmpty: true });
105
- setInputValue("");
106
- setYear(defaultYear !== null && defaultYear !== void 0 ? defaultYear : today);
107
- return;
108
- }
109
- updateMonth(month);
110
- updateValidation();
111
- setInputValue(month ? (0, utils_1.formatDateForInput)(month, locale, "month", inputFormat) : "");
112
- };
113
- // When changing the input field, save its value in state and check if the
114
- // string is a valid date. If it is a valid day, set it as selected and update
115
- // the calendar’s month.
116
- const handleChange = (e) => {
117
- setInputValue(e.target.value);
118
- const month = (0, utils_1.parseDate)(e.target.value, today, locale, "month", allowTwoDigitYear);
119
- const isBefore = getIsBefore({ fromDate, date: month });
120
- const isAfter = getIsAfter({ toDate, date: month });
121
- if (!(0, utils_1.isValidDate)(month) || (disabled && (0, utils_1.isMatch)(month, disabled))) {
122
- updateMonth(undefined);
123
- updateValidation({
124
- isInvalid: (0, utils_1.isValidDate)(month),
125
- isDisabled: disabled && (0, utils_1.isMatch)(month, disabled),
126
- isValidMonth: false,
127
- isEmpty: !e.target.value,
128
- isBefore: isBefore !== null && isBefore !== void 0 ? isBefore : false,
129
- isAfter: isAfter !== null && isAfter !== void 0 ? isAfter : false,
130
- });
131
- return;
132
- }
133
- if (isAfter ||
134
- isBefore ||
135
- (fromDate && toDate && !(0, utils_1.isMatch)(month, [{ from: fromDate, to: toDate }]))) {
136
- updateMonth(undefined);
137
- updateValidation({
138
- isValidMonth: false,
139
- isBefore: isBefore !== null && isBefore !== void 0 ? isBefore : false,
140
- isAfter: isAfter !== null && isAfter !== void 0 ? isAfter : false,
141
- });
142
- return;
143
- }
144
- updateMonth(month);
145
- updateValidation();
146
- setYear(month);
147
- };
148
- const monthpickerProps = {
149
- year,
150
- onYearChange: (y) => setYear(y !== null && y !== void 0 ? y : today),
151
- onMonthSelect: handleMonthClick,
152
- selected: selectedMonth,
153
- locale: _locale,
154
- fromDate,
155
- toDate,
156
- open,
157
- onOpenToggle: () => handleOpen(!open),
158
- onClose: () => {
159
- handleOpen(false);
160
- anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.focus();
161
- },
162
- disabled,
163
- };
164
- const inputProps = {
165
- onChange: handleChange,
166
- onFocus: handleFocus,
167
- onBlur: handleBlur,
168
- value: inputValue,
169
- setAnchorRef,
170
- };
171
- return { monthpickerProps, inputProps, reset, selectedMonth, setSelected };
172
- };
173
- exports.useMonthpicker = useMonthpicker;
174
- //# sourceMappingURL=useMonthPicker.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useMonthPicker.js","sourceRoot":"","sources":["../../../src/date/hooks/useMonthPicker.tsx"],"names":[],"mappings":";;;AAAA,iCAA8D;AAC9D,2DAA2D;AAG3D,oCAMkB;AA8ElB,MAAM,oBAAoB,GAAG,CAAC,GAAG,GAAG,EAAE,EAAoB,EAAE,CAAC,iBAC3D,UAAU,EAAE,KAAK,EACjB,OAAO,EAAE,KAAK,EACd,SAAS,EAAE,KAAK,EAChB,QAAQ,EAAE,KAAK,EACf,OAAO,EAAE,KAAK,EACd,YAAY,EAAE,IAAI,IACf,GAAG,EACN,CAAC;AAEH,MAAM,WAAW,GAAG,CAAC,GAAqC,EAAE,EAAE,CAC5D,GAAG,CAAC,QAAQ;IACZ,GAAG,CAAC,IAAI;IACR,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE;QAClD,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,EAAE,KAAK,GAAG,CAAC,IAAI,CA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QAAA,cAAc,kBAyMzB"}
@@ -1,76 +0,0 @@
1
- import React from "react";
2
- import { DateRange } from "react-day-picker";
3
- import { DatePickerProps } from "../datepicker/DatePicker";
4
- import { DateInputProps } from "../parts/DateInput";
5
- import { DateValidationT, UseDatepickerOptions } from "./useDatepicker";
6
- export type RangeValidationT = {
7
- from: DateValidationT;
8
- to: DateValidationT & {
9
- isBeforeFrom?: boolean;
10
- };
11
- };
12
- interface UseRangeDatepickerOptions extends Omit<UseDatepickerOptions, "defaultSelected" | "onDateChange" | "onValidate"> {
13
- /**
14
- * The initially selected DateRange
15
- */
16
- defaultSelected?: DateRange;
17
- /**
18
- * Callback for changed state
19
- */
20
- onRangeChange?: (val?: DateRange) => void;
21
- /**
22
- * validation-callback
23
- */
24
- onValidate?: (val: RangeValidationT) => void;
25
- }
26
- interface UseRangeDatepickerValue {
27
- /**
28
- * Use: <DatePicker {...datepickerProps}/>
29
- */
30
- datepickerProps: DatePickerProps;
31
- /**
32
- * Use: <DatePicker.Input label="from" {...fromInputProps}/>
33
- */
34
- fromInputProps: Pick<DateInputProps, "onChange" | "onFocus" | "onBlur" | "value"> & {
35
- /**
36
- * @private
37
- */
38
- setAnchorRef: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;
39
- };
40
- /**
41
- * Use: <DatePicker.Input label="to" {...toInputProps}/>
42
- */
43
- toInputProps: Pick<DateInputProps, "onChange" | "onFocus" | "onBlur" | "value"> & {
44
- /**
45
- * @private
46
- */
47
- setAnchorRef?: React.Dispatch<React.SetStateAction<HTMLButtonElement | null>>;
48
- };
49
- /**
50
- * Resets all states (callback)
51
- */
52
- reset: () => void;
53
- /**
54
- * Currently selected DateRange
55
- * Up to user to validate values
56
- */
57
- selectedRange?: DateRange;
58
- /**
59
- * Manually override currently selected day
60
- */
61
- setSelected: (date?: DateRange) => void;
62
- }
63
- /**
64
- *
65
- * @see 🏷️ {@link UseRangeDatepickerOptions}
66
- * @see 🏷️ {@link UseRangeDatepickerValue}
67
- * @see 🏷️ {@link RangeValidationT}
68
- * @example
69
- * const { datepickerProps, fromInputProps, toInputProps } = useRangeDatepicker({
70
- * fromDate: new Date("Aug 23 2019"),
71
- * onRangeChange: console.log,
72
- * onValidate: console.log,
73
- * });
74
- */
75
- export declare const useRangeDatepicker: (opt?: UseRangeDatepickerOptions) => UseRangeDatepickerValue;
76
- export {};
@@ -1,319 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useRangeDatepicker = void 0;
4
- const date_fns_1 = require("date-fns");
5
- const react_1 = require("react");
6
- const react_day_picker_1 = require("react-day-picker");
7
- const i18n_hooks_1 = require("../../util/i18n/i18n.hooks");
8
- const utils_1 = require("../utils");
9
- const getValidationMessage = (from = {}, to = {}) => ({
10
- from: Object.assign({ isDisabled: false, isWeekend: false, isEmpty: false, isInvalid: false, isBefore: false, isAfter: false, isValidDate: true }, from),
11
- to: Object.assign({ isDisabled: false, isWeekend: false, isEmpty: false, isInvalid: false, isBefore: false, isAfter: false, isBeforeFrom: false, isValidDate: true }, to),
12
- });
13
- const RANGE = {
14
- FROM: "FROM",
15
- TO: "TO",
16
- };
17
- const fromValidation = (day, opt) => {
18
- const isBefore = (opt === null || opt === void 0 ? void 0 : opt.fromDate) && day && (0, date_fns_1.differenceInCalendarDays)(opt === null || opt === void 0 ? void 0 : opt.fromDate, day) > 0;
19
- const isAfter = (opt === null || opt === void 0 ? void 0 : opt.toDate) && day && (0, date_fns_1.differenceInCalendarDays)(day, opt === null || opt === void 0 ? void 0 : opt.toDate) > 0;
20
- if ((0, utils_1.isValidDate)(day) &&
21
- !((opt === null || opt === void 0 ? void 0 : opt.disableWeekends) && (0, date_fns_1.isWeekend)(day)) &&
22
- !((opt === null || opt === void 0 ? void 0 : opt.disabled) && (0, react_day_picker_1.isMatch)(day, opt.disabled))) {
23
- return {
24
- isValidDate: false,
25
- isInvalid: !(0, utils_1.isValidDate)(day),
26
- isWeekend: (opt === null || opt === void 0 ? void 0 : opt.disableWeekends) && (0, date_fns_1.isWeekend)(day),
27
- isDisabled: (opt === null || opt === void 0 ? void 0 : opt.disabled) && (0, react_day_picker_1.isMatch)(day, opt.disabled),
28
- isBefore,
29
- isAfter,
30
- };
31
- }
32
- if (isBefore || isAfter) {
33
- return {
34
- isValidDate: false,
35
- isBefore,
36
- isAfter,
37
- };
38
- }
39
- };
40
- const toValidation = (day, from, opt) => {
41
- var _a;
42
- const isBefore = (opt === null || opt === void 0 ? void 0 : opt.fromDate) && day && (0, date_fns_1.differenceInCalendarDays)(opt === null || opt === void 0 ? void 0 : opt.fromDate, day) > 0;
43
- const isAfter = (opt === null || opt === void 0 ? void 0 : opt.toDate) && day && (0, date_fns_1.differenceInCalendarDays)(day, opt === null || opt === void 0 ? void 0 : opt.toDate) > 0;
44
- const isBeforeFrom = (_a = (from && (0, date_fns_1.differenceInCalendarDays)(from, day) > 0)) !== null && _a !== void 0 ? _a : false;
45
- if ((0, utils_1.isValidDate)(day) &&
46
- !((opt === null || opt === void 0 ? void 0 : opt.disableWeekends) && (0, date_fns_1.isWeekend)(day)) &&
47
- !((opt === null || opt === void 0 ? void 0 : opt.disabled) && (0, react_day_picker_1.isMatch)(day, opt.disabled))) {
48
- return {
49
- isValidDate: false,
50
- isInvalid: !(0, utils_1.isValidDate)(day),
51
- isWeekend: (opt === null || opt === void 0 ? void 0 : opt.disableWeekends) && (0, date_fns_1.isWeekend)(day),
52
- isDisabled: (opt === null || opt === void 0 ? void 0 : opt.disabled) && (0, react_day_picker_1.isMatch)(day, opt.disabled),
53
- isBefore,
54
- isAfter,
55
- isBeforeFrom,
56
- };
57
- }
58
- if (isBefore || isAfter || isBeforeFrom) {
59
- return {
60
- isValidDate: false,
61
- isBefore,
62
- isAfter,
63
- isBeforeFrom,
64
- };
65
- }
66
- };
67
- const initialValidation = (range, opt) => {
68
- if (!range || !(range === null || range === void 0 ? void 0 : range.from)) {
69
- return getValidationMessage({ isEmpty: true, isValidDate: false }, { isEmpty: true, isValidDate: false });
70
- }
71
- const fromVal = fromValidation(range.from, opt);
72
- const toVal = range.to
73
- ? toValidation(range.to, range.from, opt)
74
- : { isEmpty: true, isValidDate: false };
75
- return getValidationMessage(Object.assign({}, fromVal), Object.assign({}, toVal));
76
- };
77
- /**
78
- *
79
- * @see 🏷️ {@link UseRangeDatepickerOptions}
80
- * @see 🏷️ {@link UseRangeDatepickerValue}
81
- * @see 🏷️ {@link RangeValidationT}
82
- * @example
83
- * const { datepickerProps, fromInputProps, toInputProps } = useRangeDatepicker({
84
- * fromDate: new Date("Aug 23 2019"),
85
- * onRangeChange: console.log,
86
- * onValidate: console.log,
87
- * });
88
- */
89
- const useRangeDatepicker = (opt = {}) => {
90
- const { locale: _locale, defaultSelected: _defaultSelected, today = new Date(), fromDate, toDate, disabled, disableWeekends, onRangeChange, inputFormat, onValidate, defaultMonth, allowTwoDigitYear = true, } = opt;
91
- const [anchorRef, setAnchorRef] = (0, react_1.useState)(null);
92
- const localeFromProvider = (0, i18n_hooks_1.useDateLocale)();
93
- const locale = _locale ? (0, utils_1.getLocaleFromString)(_locale) : localeFromProvider;
94
- const [defaultSelected, setDefaultSelected] = (0, react_1.useState)(_defaultSelected);
95
- // Initialize states
96
- const [month, setMonth] = (0, react_1.useState)(defaultSelected ? defaultSelected === null || defaultSelected === void 0 ? void 0 : defaultSelected.from : defaultMonth !== null && defaultMonth !== void 0 ? defaultMonth : today);
97
- const [selectedRange, setSelectedRange] = (0, react_1.useState)(defaultSelected !== null && defaultSelected !== void 0 ? defaultSelected : { from: undefined, to: undefined });
98
- const [fromInputValue, setFromInputValue] = (0, react_1.useState)((defaultSelected === null || defaultSelected === void 0 ? void 0 : defaultSelected.from)
99
- ? (0, utils_1.formatDateForInput)(defaultSelected.from, locale, "date", inputFormat)
100
- : "");
101
- const [toInputValue, setToInputValue] = (0, react_1.useState)((defaultSelected === null || defaultSelected === void 0 ? void 0 : defaultSelected.to)
102
- ? (0, utils_1.formatDateForInput)(defaultSelected.to, locale, "date", inputFormat)
103
- : "");
104
- const [validation, setValidation] = (0, react_1.useState)(initialValidation(selectedRange, opt));
105
- const [open, setOpen] = (0, react_1.useState)(false);
106
- const updateRange = (range) => {
107
- onRangeChange === null || onRangeChange === void 0 ? void 0 : onRangeChange(range);
108
- setSelectedRange(range);
109
- };
110
- const updateValidation = (from = {}, to = {}) => {
111
- const msg = getValidationMessage(from, to);
112
- setValidation(msg);
113
- onValidate === null || onValidate === void 0 ? void 0 : onValidate(msg);
114
- };
115
- const reset = () => {
116
- updateRange(defaultSelected !== null && defaultSelected !== void 0 ? defaultSelected : { from: undefined, to: undefined });
117
- setMonth(defaultSelected ? defaultSelected === null || defaultSelected === void 0 ? void 0 : defaultSelected.from : defaultMonth !== null && defaultMonth !== void 0 ? defaultMonth : today);
118
- setValidation(initialValidation(defaultSelected !== null && defaultSelected !== void 0 ? defaultSelected : { from: undefined, to: undefined }, opt));
119
- setFromInputValue((defaultSelected === null || defaultSelected === void 0 ? void 0 : defaultSelected.from)
120
- ? (0, utils_1.formatDateForInput)(defaultSelected.from, locale, "date", inputFormat)
121
- : "");
122
- setToInputValue((defaultSelected === null || defaultSelected === void 0 ? void 0 : defaultSelected.to)
123
- ? (0, utils_1.formatDateForInput)(defaultSelected.to, locale, "date", inputFormat)
124
- : "");
125
- setDefaultSelected(_defaultSelected);
126
- };
127
- const setSelected = (range) => {
128
- updateRange(range);
129
- setFromInputValue((range === null || range === void 0 ? void 0 : range.from)
130
- ? (0, utils_1.formatDateForInput)(range.from, locale, "date", inputFormat)
131
- : "");
132
- setToInputValue((range === null || range === void 0 ? void 0 : range.to)
133
- ? (0, utils_1.formatDateForInput)(range === null || range === void 0 ? void 0 : range.to, locale, "date", inputFormat)
134
- : "");
135
- setValidation(initialValidation(range, opt));
136
- };
137
- const handleFocus = (e, src) => {
138
- if (e.target.readOnly) {
139
- return;
140
- }
141
- const day = (0, utils_1.parseDate)(e.target.value, today, locale, "date", allowTwoDigitYear);
142
- if ((0, utils_1.isValidDate)(day)) {
143
- src === RANGE.FROM
144
- ? setFromInputValue((0, utils_1.formatDateForInput)(day, locale, "date", inputFormat))
145
- : setToInputValue((0, utils_1.formatDateForInput)(day, locale, "date", inputFormat));
146
- const isBefore = fromDate && day && (0, date_fns_1.differenceInCalendarDays)(fromDate, day) > 0;
147
- const isAfter = toDate && day && (0, date_fns_1.differenceInCalendarDays)(day, toDate) > 0;
148
- !isBefore && !isAfter && setMonth(day);
149
- }
150
- };
151
- const handleBlur = (e, src) => {
152
- const day = (0, utils_1.parseDate)(e.target.value, today, locale, "date", allowTwoDigitYear);
153
- if (!(0, utils_1.isValidDate)(day)) {
154
- return;
155
- }
156
- if (src === RANGE.FROM) {
157
- setFromInputValue((0, utils_1.formatDateForInput)(day, locale, "date", inputFormat));
158
- }
159
- else if (src === RANGE.TO) {
160
- setToInputValue((0, utils_1.formatDateForInput)(day, locale, "date", inputFormat));
161
- }
162
- };
163
- const validateDay = (day) => {
164
- return ((0, utils_1.isValidDate)(day) &&
165
- !(disableWeekends && (0, date_fns_1.isWeekend)(day)) &&
166
- !(disabled && (0, react_day_picker_1.isMatch)(day, disabled)));
167
- };
168
- const handleSelect = (range) => {
169
- if ((range === null || range === void 0 ? void 0 : range.from) && (range === null || range === void 0 ? void 0 : range.to)) {
170
- setOpen(false);
171
- anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.focus();
172
- }
173
- const prevToRange = !(selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.from) && (selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.to) ? selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.to : range === null || range === void 0 ? void 0 : range.to;
174
- const resetTo = (0, date_fns_1.isBefore)(prevToRange, range === null || range === void 0 ? void 0 : range.from);
175
- (range === null || range === void 0 ? void 0 : range.from)
176
- ? setFromInputValue((0, utils_1.formatDateForInput)(range === null || range === void 0 ? void 0 : range.from, locale, "date", inputFormat))
177
- : setFromInputValue("");
178
- prevToRange && !resetTo
179
- ? setToInputValue((0, utils_1.formatDateForInput)(prevToRange, locale, "date", inputFormat))
180
- : setToInputValue("");
181
- updateRange({ from: range === null || range === void 0 ? void 0 : range.from, to: resetTo ? undefined : prevToRange });
182
- updateValidation({ isValidDate: !!(range === null || range === void 0 ? void 0 : range.from), isEmpty: !(range === null || range === void 0 ? void 0 : range.from) }, { isValidDate: !!(range === null || range === void 0 ? void 0 : range.to), isEmpty: !prevToRange || resetTo });
183
- };
184
- const fromChange = (val = "", day, isBefore = false, isAfter = false) => {
185
- setFromInputValue(val);
186
- if (!validateDay(day)) {
187
- updateRange(Object.assign(Object.assign({}, selectedRange), { from: undefined }));
188
- updateValidation({
189
- isEmpty: !val,
190
- isValidDate: false,
191
- isInvalid: !(0, utils_1.isValidDate)(day),
192
- isWeekend: disableWeekends && (0, date_fns_1.isWeekend)(day),
193
- isDisabled: disabled && (0, react_day_picker_1.isMatch)(day, disabled),
194
- isBefore,
195
- isAfter,
196
- }, validation.to);
197
- return;
198
- }
199
- if (isBefore || isAfter) {
200
- updateRange(Object.assign(Object.assign({}, selectedRange), { from: undefined }));
201
- updateValidation({
202
- isValidDate: false,
203
- isBefore,
204
- isAfter,
205
- }, validation.to);
206
- return;
207
- }
208
- if ((selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.to) &&
209
- (0, date_fns_1.differenceInCalendarDays)(day, selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.to) > 0) {
210
- updateRange({ to: day, from: day });
211
- setToInputValue((0, utils_1.formatDateForInput)(day, locale, "date", inputFormat));
212
- setMonth(day);
213
- updateValidation();
214
- return;
215
- }
216
- if (toInputValue && !(selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.to)) {
217
- const toDay = (0, utils_1.parseDate)(toInputValue, today, locale, "date", allowTwoDigitYear);
218
- if (validateDay(toDay)) {
219
- updateRange({ from: day, to: toDay });
220
- setMonth(day);
221
- updateValidation();
222
- return;
223
- }
224
- }
225
- updateRange(Object.assign(Object.assign({}, selectedRange), { from: day }));
226
- updateValidation({}, validation.to);
227
- setMonth(day);
228
- };
229
- const toChange = (val = "", day, isBefore = false, isAfter = false) => {
230
- setToInputValue(val);
231
- if (!validateDay(day)) {
232
- updateRange({ from: selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.from, to: undefined });
233
- updateValidation(validation.from, {
234
- isEmpty: !val,
235
- isValidDate: false,
236
- isInvalid: !(0, utils_1.isValidDate)(day),
237
- isWeekend: disableWeekends && (0, date_fns_1.isWeekend)(day),
238
- isDisabled: disabled && (0, react_day_picker_1.isMatch)(day, disabled),
239
- isBefore,
240
- isAfter,
241
- });
242
- return;
243
- }
244
- if (isBefore || isAfter) {
245
- updateRange({ from: selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.from, to: undefined });
246
- updateValidation(validation.from, {
247
- isValidDate: false,
248
- isBefore,
249
- isAfter,
250
- });
251
- return;
252
- }
253
- /* If to-value < from-value */
254
- if ((selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.from) &&
255
- (0, date_fns_1.differenceInCalendarDays)(selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.from, day) > 0) {
256
- updateRange({ from: selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.from, to: undefined });
257
- updateValidation(validation.from, {
258
- isValidDate: false,
259
- isBeforeFrom: true,
260
- });
261
- return;
262
- }
263
- updateRange({ from: selectedRange === null || selectedRange === void 0 ? void 0 : selectedRange.from, to: day });
264
- updateValidation(validation.from, {});
265
- setMonth(day);
266
- };
267
- /* live-update datepicker based on changes in inputfields */
268
- const handleChange = (e, src) => {
269
- const day = (0, utils_1.parseDate)(e.target.value, today, locale, "date", allowTwoDigitYear);
270
- const isBefore = fromDate && (0, date_fns_1.differenceInCalendarDays)(fromDate, day) > 0;
271
- const isAfter = toDate && (0, date_fns_1.differenceInCalendarDays)(day, toDate) > 0;
272
- return src === RANGE.FROM
273
- ? fromChange(e.target.value, day, isBefore, isAfter)
274
- : toChange(e.target.value, day, isBefore, isAfter);
275
- };
276
- const datepickerProps = {
277
- month,
278
- onMonthChange: setMonth,
279
- onSelect: handleSelect,
280
- selected: selectedRange,
281
- locale: _locale,
282
- fromDate,
283
- toDate,
284
- today,
285
- mode: "range",
286
- open,
287
- onOpenToggle: () => setOpen((x) => !x),
288
- onClose: () => {
289
- setOpen(false);
290
- anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.focus();
291
- },
292
- disabled,
293
- disableWeekends,
294
- };
295
- const fromInputProps = {
296
- onChange: (e) => handleChange(e, RANGE.FROM),
297
- onFocus: (e) => handleFocus(e, RANGE.FROM),
298
- onBlur: (e) => handleBlur(e, RANGE.FROM),
299
- value: fromInputValue,
300
- setAnchorRef,
301
- };
302
- const toInputProps = {
303
- onChange: (e) => handleChange(e, RANGE.TO),
304
- onFocus: (e) => handleFocus(e, RANGE.TO),
305
- onBlur: (e) => handleBlur(e, RANGE.TO),
306
- value: toInputValue,
307
- setAnchorRef,
308
- };
309
- return {
310
- datepickerProps,
311
- fromInputProps,
312
- toInputProps,
313
- reset,
314
- selectedRange,
315
- setSelected,
316
- };
317
- };
318
- exports.useRangeDatepicker = useRangeDatepicker;
319
- //# sourceMappingURL=useRangeDatepicker.js.map