@primer/components 0.0.0-2021929153151 → 0.0.0-202192923313

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 (186) hide show
  1. package/CHANGELOG.md +1 -25
  2. package/dist/browser.esm.js +49 -53
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +46 -50
  5. package/dist/browser.umd.js.map +1 -1
  6. package/lib/ActionList/Item.d.ts +0 -6
  7. package/lib/ActionList/Item.js +1 -5
  8. package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +4 -2
  9. package/lib/Autocomplete/Autocomplete.d.ts +32 -30
  10. package/lib/Autocomplete/AutocompleteInput.d.ts +32 -30
  11. package/lib/Button/Button.d.ts +27 -27
  12. package/lib/Button/ButtonBase.d.ts +1 -1
  13. package/lib/Button/ButtonClose.d.ts +45 -45
  14. package/lib/Button/ButtonDanger.d.ts +27 -27
  15. package/lib/Button/ButtonInvisible.d.ts +27 -27
  16. package/lib/Button/ButtonOutline.d.ts +27 -27
  17. package/lib/Button/ButtonPrimary.d.ts +27 -27
  18. package/lib/CircleBadge.d.ts +2 -2
  19. package/lib/CircleOcticon.d.ts +43 -43
  20. package/lib/DatePicker/DatePicker.d.ts +48 -0
  21. package/lib/DatePicker/DatePicker.js +106 -0
  22. package/lib/DatePicker/DatePickerAnchor.d.ts +5 -0
  23. package/lib/DatePicker/DatePickerAnchor.js +223 -0
  24. package/lib/DatePicker/DatePickerOverlay.d.ts +3 -0
  25. package/lib/DatePicker/DatePickerOverlay.js +48 -0
  26. package/lib/DatePicker/DatePickerPanel.d.ts +2 -0
  27. package/lib/DatePicker/DatePickerPanel.js +126 -0
  28. package/lib/DatePicker/Day.d.ts +14 -0
  29. package/lib/DatePicker/Day.js +192 -0
  30. package/lib/DatePicker/Month.d.ts +9 -0
  31. package/lib/DatePicker/Month.js +122 -0
  32. package/lib/DatePicker/dateParser.d.ts +11 -0
  33. package/lib/DatePicker/dateParser.js +192 -0
  34. package/lib/DatePicker/index.d.ts +2 -0
  35. package/lib/DatePicker/index.js +13 -0
  36. package/lib/DatePicker/useDatePicker.d.ts +89 -0
  37. package/lib/DatePicker/useDatePicker.js +439 -0
  38. package/lib/Dialog.d.ts +46 -46
  39. package/lib/Dropdown.d.ts +178 -178
  40. package/lib/DropdownMenu/DropdownButton.d.ts +47 -47
  41. package/lib/FilterList.d.ts +42 -42
  42. package/lib/Flash.d.ts +1 -1
  43. package/lib/Label.d.ts +1 -1
  44. package/lib/Position.d.ts +4 -4
  45. package/lib/ProgressBar.d.ts +1 -1
  46. package/lib/SelectMenu/SelectMenu.d.ts +248 -246
  47. package/lib/SelectMenu/SelectMenuItem.d.ts +1 -1
  48. package/lib/SelectMenu/SelectMenuModal.d.ts +1 -1
  49. package/lib/TextInputWithTokens.d.ts +32 -34
  50. package/lib/TextInputWithTokens.js +29 -102
  51. package/lib/Timeline.d.ts +44 -44
  52. package/lib/Token/AvatarToken.d.ts +1 -1
  53. package/lib/Token/IssueLabelToken.d.ts +1 -1
  54. package/lib/Token/Token.d.ts +1 -1
  55. package/lib/Token/Token.js +2 -13
  56. package/lib/Token/TokenBase.js +4 -0
  57. package/lib/Token/_RemoveTokenButton.js +2 -15
  58. package/lib/_TextInputWrapper.d.ts +1 -1
  59. package/lib/_TextInputWrapper.js +1 -1
  60. package/lib/hooks/useDebounce.d.ts +2 -0
  61. package/lib/hooks/useDebounce.js +24 -0
  62. package/lib/sx.d.ts +2 -10
  63. package/lib/sx.js +0 -8
  64. package/lib/theme-preval.js +2 -2
  65. package/lib/theme.d.ts +0 -78
  66. package/lib/theme.js +1 -3
  67. package/lib/utils/testing.d.ts +1 -1
  68. package/lib-esm/ActionList/Item.d.ts +0 -6
  69. package/lib-esm/ActionList/Item.js +1 -5
  70. package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +4 -2
  71. package/lib-esm/Autocomplete/Autocomplete.d.ts +32 -30
  72. package/lib-esm/Autocomplete/AutocompleteInput.d.ts +32 -30
  73. package/lib-esm/Button/Button.d.ts +27 -27
  74. package/lib-esm/Button/ButtonBase.d.ts +1 -1
  75. package/lib-esm/Button/ButtonClose.d.ts +45 -45
  76. package/lib-esm/Button/ButtonDanger.d.ts +27 -27
  77. package/lib-esm/Button/ButtonInvisible.d.ts +27 -27
  78. package/lib-esm/Button/ButtonOutline.d.ts +27 -27
  79. package/lib-esm/Button/ButtonPrimary.d.ts +27 -27
  80. package/lib-esm/CircleBadge.d.ts +2 -2
  81. package/lib-esm/CircleOcticon.d.ts +43 -43
  82. package/lib-esm/DatePicker/DatePicker.d.ts +48 -0
  83. package/lib-esm/DatePicker/DatePicker.js +89 -0
  84. package/lib-esm/DatePicker/DatePickerAnchor.d.ts +5 -0
  85. package/lib-esm/DatePicker/DatePickerAnchor.js +196 -0
  86. package/lib-esm/DatePicker/DatePickerOverlay.d.ts +3 -0
  87. package/lib-esm/DatePicker/DatePickerOverlay.js +29 -0
  88. package/lib-esm/DatePicker/DatePickerPanel.d.ts +2 -0
  89. package/lib-esm/DatePicker/DatePickerPanel.js +100 -0
  90. package/lib-esm/DatePicker/Day.d.ts +14 -0
  91. package/lib-esm/DatePicker/Day.js +169 -0
  92. package/lib-esm/DatePicker/Month.d.ts +9 -0
  93. package/lib-esm/DatePicker/Month.js +98 -0
  94. package/lib-esm/DatePicker/dateParser.d.ts +11 -0
  95. package/lib-esm/DatePicker/dateParser.js +178 -0
  96. package/lib-esm/DatePicker/index.d.ts +2 -0
  97. package/lib-esm/DatePicker/index.js +1 -0
  98. package/lib-esm/DatePicker/useDatePicker.d.ts +89 -0
  99. package/lib-esm/DatePicker/useDatePicker.js +408 -0
  100. package/lib-esm/Dialog.d.ts +46 -46
  101. package/lib-esm/Dropdown.d.ts +178 -178
  102. package/lib-esm/DropdownMenu/DropdownButton.d.ts +47 -47
  103. package/lib-esm/FilterList.d.ts +42 -42
  104. package/lib-esm/Flash.d.ts +1 -1
  105. package/lib-esm/Label.d.ts +1 -1
  106. package/lib-esm/Position.d.ts +4 -4
  107. package/lib-esm/ProgressBar.d.ts +1 -1
  108. package/lib-esm/SelectMenu/SelectMenu.d.ts +248 -246
  109. package/lib-esm/SelectMenu/SelectMenuItem.d.ts +1 -1
  110. package/lib-esm/SelectMenu/SelectMenuModal.d.ts +1 -1
  111. package/lib-esm/TextInputWithTokens.d.ts +32 -34
  112. package/lib-esm/TextInputWithTokens.js +30 -101
  113. package/lib-esm/Timeline.d.ts +44 -44
  114. package/lib-esm/Token/AvatarToken.d.ts +1 -1
  115. package/lib-esm/Token/IssueLabelToken.d.ts +1 -1
  116. package/lib-esm/Token/Token.d.ts +1 -1
  117. package/lib-esm/Token/Token.js +2 -13
  118. package/lib-esm/Token/TokenBase.js +4 -0
  119. package/lib-esm/Token/_RemoveTokenButton.js +2 -11
  120. package/lib-esm/_TextInputWrapper.d.ts +1 -1
  121. package/lib-esm/_TextInputWrapper.js +1 -1
  122. package/lib-esm/hooks/useDebounce.d.ts +2 -0
  123. package/lib-esm/hooks/useDebounce.js +16 -0
  124. package/lib-esm/sx.d.ts +2 -10
  125. package/lib-esm/sx.js +1 -3
  126. package/lib-esm/theme-preval.js +2 -2
  127. package/lib-esm/theme.d.ts +0 -78
  128. package/lib-esm/theme.js +1 -2
  129. package/lib-esm/utils/testing.d.ts +1 -1
  130. package/package.json +14 -16
  131. package/lib/ActionList2/Description.d.ts +0 -6
  132. package/lib/ActionList2/Description.js +0 -53
  133. package/lib/ActionList2/Divider.d.ts +0 -5
  134. package/lib/ActionList2/Divider.js +0 -33
  135. package/lib/ActionList2/Group.d.ts +0 -11
  136. package/lib/ActionList2/Group.js +0 -55
  137. package/lib/ActionList2/Header.d.ts +0 -26
  138. package/lib/ActionList2/Header.js +0 -55
  139. package/lib/ActionList2/Item.d.ts +0 -48
  140. package/lib/ActionList2/Item.js +0 -204
  141. package/lib/ActionList2/List.d.ts +0 -26
  142. package/lib/ActionList2/List.js +0 -56
  143. package/lib/ActionList2/Selection.d.ts +0 -5
  144. package/lib/ActionList2/Selection.js +0 -73
  145. package/lib/ActionList2/Visuals.d.ts +0 -11
  146. package/lib/ActionList2/Visuals.js +0 -90
  147. package/lib/ActionList2/hacks.d.ts +0 -30
  148. package/lib/ActionList2/hacks.js +0 -38
  149. package/lib/ActionList2/index.d.ts +0 -28
  150. package/lib/ActionList2/index.js +0 -42
  151. package/lib/experiments.d.ts +0 -1
  152. package/lib/experiments.js +0 -18
  153. package/lib/utils/create-slots.d.ts +0 -17
  154. package/lib/utils/create-slots.js +0 -105
  155. package/lib/utils/types/KeyPaths.d.ts +0 -3
  156. package/lib/utils/types/KeyPaths.js +0 -1
  157. package/lib/utils/use-force-update.d.ts +0 -1
  158. package/lib/utils/use-force-update.js +0 -19
  159. package/lib-esm/ActionList2/Description.d.ts +0 -6
  160. package/lib-esm/ActionList2/Description.js +0 -37
  161. package/lib-esm/ActionList2/Divider.d.ts +0 -5
  162. package/lib-esm/ActionList2/Divider.js +0 -21
  163. package/lib-esm/ActionList2/Group.d.ts +0 -11
  164. package/lib-esm/ActionList2/Group.js +0 -38
  165. package/lib-esm/ActionList2/Header.d.ts +0 -26
  166. package/lib-esm/ActionList2/Header.js +0 -45
  167. package/lib-esm/ActionList2/Item.d.ts +0 -48
  168. package/lib-esm/ActionList2/Item.js +0 -176
  169. package/lib-esm/ActionList2/List.d.ts +0 -26
  170. package/lib-esm/ActionList2/List.js +0 -38
  171. package/lib-esm/ActionList2/Selection.d.ts +0 -5
  172. package/lib-esm/ActionList2/Selection.js +0 -55
  173. package/lib-esm/ActionList2/Visuals.d.ts +0 -11
  174. package/lib-esm/ActionList2/Visuals.js +0 -68
  175. package/lib-esm/ActionList2/hacks.d.ts +0 -30
  176. package/lib-esm/ActionList2/hacks.js +0 -30
  177. package/lib-esm/ActionList2/index.d.ts +0 -28
  178. package/lib-esm/ActionList2/index.js +0 -29
  179. package/lib-esm/experiments.d.ts +0 -1
  180. package/lib-esm/experiments.js +0 -2
  181. package/lib-esm/utils/create-slots.d.ts +0 -17
  182. package/lib-esm/utils/create-slots.js +0 -84
  183. package/lib-esm/utils/types/KeyPaths.d.ts +0 -3
  184. package/lib-esm/utils/types/KeyPaths.js +0 -1
  185. package/lib-esm/utils/use-force-update.d.ts +0 -1
  186. package/lib-esm/utils/use-force-update.js +0 -6
@@ -0,0 +1,89 @@
1
+ import React from 'react';
2
+ export declare type AnchorVariant = 'input' | 'button' | 'icon-only';
3
+ export declare type DateFormat = 'short' | 'long' | string;
4
+ export declare type SelectionVariant = 'single' | 'multi' | 'range';
5
+ export interface DatePickerConfiguration {
6
+ anchorVariant?: AnchorVariant;
7
+ blockedDates?: Array<Date>;
8
+ confirmation?: boolean;
9
+ confirmUnsavedClose?: boolean;
10
+ dateFormat?: DateFormat;
11
+ disableWeekends?: boolean;
12
+ iconPlacement?: 'start' | 'end' | 'none';
13
+ maxDate?: Date | null;
14
+ maxSelections?: number;
15
+ maxRangeSize?: number;
16
+ minDate?: Date | null;
17
+ placeholder?: string;
18
+ rangeIncrement?: number;
19
+ showInputPrompt?: boolean;
20
+ variant?: SelectionVariant;
21
+ view?: '1-month' | '2-month';
22
+ weekStartsOn?: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday';
23
+ }
24
+ export declare type RangeSelection = {
25
+ from: Date;
26
+ to: Date | null;
27
+ };
28
+ export declare type StringRangeSelection = {
29
+ from: string;
30
+ to: string;
31
+ };
32
+ export interface DatePickerContext {
33
+ disabled?: boolean;
34
+ configuration: DatePickerConfiguration;
35
+ currentViewingDate: Date;
36
+ goToMonth: (date: Date) => void;
37
+ hoverRange?: RangeSelection | null;
38
+ selection?: Selection;
39
+ softSelection?: Partial<RangeSelection> | null;
40
+ selectionActive?: boolean;
41
+ formattedDate: string;
42
+ nextMonth: () => void;
43
+ onClose: () => void;
44
+ onDateInput: (updatedSelection: Selection) => void;
45
+ onDayFocus: (date: Date) => void;
46
+ onSelection: (date: Date) => void;
47
+ previousMonth: () => void;
48
+ revertValue: () => void;
49
+ saveValue: (selection?: Selection) => void;
50
+ }
51
+ export declare type Selection = Date | Array<Date> | RangeSelection | null;
52
+ export declare type StringSelection = string | Array<string> | {
53
+ to: string;
54
+ from: string;
55
+ } | null;
56
+ export declare type DaySelection = boolean | 'start' | 'middle' | 'end';
57
+ declare const useDatePicker: (date?: Date | undefined) => {
58
+ blocked: boolean | undefined;
59
+ disabled: boolean;
60
+ selected: DaySelection;
61
+ today: boolean;
62
+ configuration: DatePickerConfiguration;
63
+ currentViewingDate: Date;
64
+ goToMonth: (date: Date) => void;
65
+ hoverRange?: RangeSelection | null | undefined;
66
+ selection?: Selection | undefined;
67
+ softSelection?: Partial<RangeSelection> | null | undefined;
68
+ selectionActive?: boolean | undefined;
69
+ formattedDate: string;
70
+ nextMonth: () => void;
71
+ onClose: () => void;
72
+ onDateInput: (updatedSelection: Selection) => void;
73
+ onDayFocus: (date: Date) => void;
74
+ onSelection: (date: Date) => void;
75
+ previousMonth: () => void;
76
+ revertValue: () => void;
77
+ saveValue: (selection?: Selection | undefined) => void;
78
+ };
79
+ export default useDatePicker;
80
+ export interface DatePickerProviderProps {
81
+ closePicker?: () => void;
82
+ configuration?: DatePickerConfiguration;
83
+ value?: Selection | StringSelection;
84
+ }
85
+ export declare function isSingleSelection(selection: Selection): selection is Date;
86
+ export declare function isMultiSelection(selection: Selection | StringSelection): selection is Array<Date> | Array<string>;
87
+ export declare function isRangeSelection(selection: Selection | StringSelection): selection is RangeSelection | StringRangeSelection;
88
+ export declare function isStringRangeSelection(selection: StringSelection): selection is StringRangeSelection;
89
+ export declare const DatePickerProvider: React.FC<DatePickerProviderProps>;
@@ -0,0 +1,408 @@
1
+ import { CheckIcon, TrashIcon } from '@primer/octicons-react';
2
+ import { isEqual, isAfter, isBefore, addMonths, subMonths, isToday, isWeekend, differenceInDays, addDays, subDays } from 'date-fns';
3
+ import deepmerge from 'deepmerge';
4
+ import React, { createContext, useCallback, useContext, useMemo, useEffect, useState } from 'react';
5
+ import { Text, useConfirm } from '..';
6
+ import { formatDate } from './dateParser';
7
+ const DatePickerContext = /*#__PURE__*/createContext(null);
8
+
9
+ const useDatePicker = date => {
10
+ const value = useContext(DatePickerContext);
11
+ const [selected, setSelected] = useState(false);
12
+ const today = date ? isToday(date) : false;
13
+
14
+ if (!value) {
15
+ throw new Error('useDatePicker must be used inside a DatePickerProvider');
16
+ }
17
+
18
+ useEffect(() => {
19
+ if (date) {
20
+ if (value.hoverRange) {
21
+ if (isRangeSelection(value.hoverRange)) {
22
+ if (isEqual(date, value.hoverRange.from)) {
23
+ setSelected('start');
24
+ } else if (value.hoverRange.to && isEqual(date, value.hoverRange.to)) {
25
+ setSelected('end');
26
+ } else if (isAfter(date, value.hoverRange.from) && value.hoverRange.to && isBefore(date, value.hoverRange.to)) {
27
+ setSelected('middle');
28
+ } else {
29
+ setSelected(false);
30
+ }
31
+ }
32
+ } else if (value.selection) {
33
+ if (isMultiSelection(value.selection)) {
34
+ setSelected(!!value.selection.find(d => isEqual(d, date)));
35
+ } else if (isRangeSelection(value.selection)) {
36
+ if (isEqual(date, value.selection.from)) {
37
+ setSelected('start');
38
+ } else if (value.selection.to && isEqual(date, value.selection.to)) {
39
+ setSelected('end');
40
+ } else if (isAfter(date, value.selection.from) && value.selection.to && isBefore(date, value.selection.to)) {
41
+ setSelected('middle');
42
+ } else {
43
+ setSelected(false);
44
+ }
45
+ } else {
46
+ setSelected(isEqual(date, value.selection));
47
+ }
48
+ }
49
+ }
50
+ }, [date, value.hoverRange, value.selection, today]);
51
+ let blocked,
52
+ disabled = false;
53
+
54
+ if (date) {
55
+ // Determine if date is blocked out
56
+ if (value.configuration.blockedDates) {
57
+ blocked = !!value.configuration.blockedDates.find(d => isEqual(d, date));
58
+ } // Determine if date is disabled
59
+
60
+
61
+ if (value.configuration.minDate || value.configuration.maxDate || value.configuration.disableWeekends) {
62
+ disabled = (value.configuration.minDate ? isBefore(date, value.configuration.minDate) : false) || (value.configuration.maxDate ? isAfter(date, value.configuration.maxDate) : false) || (value.configuration.disableWeekends ? isWeekend(date) : false);
63
+ }
64
+ }
65
+
66
+ return { ...value,
67
+ blocked,
68
+ disabled,
69
+ selected,
70
+ today
71
+ };
72
+ };
73
+
74
+ export default useDatePicker;
75
+ export function isSingleSelection(selection) {
76
+ return selection instanceof Date;
77
+ }
78
+ export function isMultiSelection(selection) {
79
+ return Array.isArray(selection);
80
+ }
81
+ export function isRangeSelection(selection) {
82
+ return !!selection.from;
83
+ }
84
+ export function isStringRangeSelection(selection) {
85
+ return !!selection.from;
86
+ }
87
+
88
+ function parseSelection(selection, variant) {
89
+ if (!selection) return;
90
+
91
+ if (variant === 'multi') {
92
+ if (isMultiSelection(selection)) {
93
+ const parsedSelection = [];
94
+
95
+ for (const d of selection) {
96
+ parsedSelection.push(new Date(new Date(d).toDateString()));
97
+ }
98
+
99
+ return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
100
+ } else if (selection instanceof Date) {
101
+ return [new Date(new Date(selection).toDateString())];
102
+ } else if (isRangeSelection(selection)) {
103
+ const parsedSelection = [];
104
+ parsedSelection.push(new Date(new Date(selection.from).toDateString()));
105
+
106
+ if (selection.to) {
107
+ parsedSelection.push(new Date(new Date(selection.to).toDateString()));
108
+ }
109
+
110
+ return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
111
+ }
112
+ } else if (variant === 'range') {
113
+ if (isRangeSelection(selection)) {
114
+ return {
115
+ from: new Date(new Date(selection.from).toDateString()),
116
+ to: selection.to ? new Date(new Date(selection.to).toDateString()) : null
117
+ };
118
+ } else if (isMultiSelection(selection)) {
119
+ return {
120
+ from: new Date(new Date(selection[0]).toDateString()),
121
+ to: selection[1] ? new Date(new Date(selection[1]).toDateString()) : null
122
+ };
123
+ } else if (selection instanceof Date) {
124
+ return {
125
+ from: new Date(new Date(selection).toDateString()),
126
+ to: null
127
+ };
128
+ }
129
+ } else {
130
+ if (selection instanceof Date) {
131
+ return new Date(new Date(selection).toDateString());
132
+ } else if (isMultiSelection(selection)) {
133
+ return new Date(new Date(selection[0]).toDateString());
134
+ } else if (isRangeSelection(selection)) {
135
+ return new Date(new Date(selection.from).toDateString());
136
+ } else {
137
+ return;
138
+ }
139
+ }
140
+ }
141
+
142
+ const defaultConfiguration = {
143
+ anchorVariant: 'button',
144
+ confirmation: false,
145
+ confirmUnsavedClose: false,
146
+ disableWeekends: false,
147
+ iconPlacement: 'start',
148
+ placeholder: 'Select a Date...',
149
+ showInputPrompt: false,
150
+ variant: 'single',
151
+ view: '2-month',
152
+ weekStartsOn: 'Sunday'
153
+ };
154
+ export const DatePickerProvider = ({
155
+ configuration: externalConfig = {},
156
+ children,
157
+ closePicker,
158
+ value
159
+ }) => {
160
+ const [configuration, setConfiguration] = useState(deepmerge(defaultConfiguration, externalConfig));
161
+ const [previousSelection, setPreviousSelection] = useState(parseSelection(value, configuration.variant));
162
+ const [isDirty, setIsDirty] = useState(false);
163
+ const [selection, setSelection] = useState(parseSelection(value, configuration.variant));
164
+ const [hoverRange, setHoverRange] = useState(null);
165
+ const [currentViewingDate, setCurrentViewingDate] = useState(new Date());
166
+ const confirm = useConfirm();
167
+ useEffect(() => {
168
+ setConfiguration(deepmerge(defaultConfiguration, externalConfig));
169
+ setSelection(parseSelection(selection, configuration.variant)); // Don't want this to run every time selection gets updated
170
+ // eslint-disable-next-line react-hooks/exhaustive-deps
171
+ }, [configuration.variant, externalConfig]);
172
+ const goToMonth = useCallback(date => {
173
+ setCurrentViewingDate(new Date(new Date(date).toDateString()));
174
+ }, []);
175
+ const nextMonth = useCallback(() => {
176
+ setCurrentViewingDate(addMonths(currentViewingDate, 1));
177
+ }, [currentViewingDate]);
178
+ const previousMonth = useCallback(() => {
179
+ setCurrentViewingDate(subMonths(currentViewingDate, 1));
180
+ }, [currentViewingDate]);
181
+ const getFormattedDate = useMemo(() => {
182
+ const {
183
+ anchorVariant,
184
+ dateFormat,
185
+ placeholder,
186
+ variant
187
+ } = configuration;
188
+ return formatDate({
189
+ selection,
190
+ anchorVariant,
191
+ dateFormat,
192
+ placeholder,
193
+ variant
194
+ });
195
+ }, [configuration, selection]);
196
+ const saveValue = useCallback(updatedSelection => {
197
+ setPreviousSelection(updatedSelection !== null && updatedSelection !== void 0 ? updatedSelection : selection);
198
+ setIsDirty(false);
199
+ closePicker === null || closePicker === void 0 ? void 0 : closePicker();
200
+ }, [closePicker, selection]);
201
+ const revertValue = useCallback(() => {
202
+ setSelection(previousSelection);
203
+ setIsDirty(false);
204
+ }, [previousSelection]);
205
+ const handleClose = useCallback(async () => {
206
+ if (configuration.confirmUnsavedClose) {
207
+ if (isDirty) {
208
+ const result = await confirm({
209
+ title: 'Save Changes?',
210
+ content: 'You have unsaved changes, would you like to save them?',
211
+ confirmButtonContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CheckIcon, null), /*#__PURE__*/React.createElement(Text, {
212
+ sx: {
213
+ ml: 1
214
+ }
215
+ }, "Save")),
216
+ cancelButtonContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TrashIcon, null), /*#__PURE__*/React.createElement(Text, {
217
+ sx: {
218
+ ml: 1
219
+ }
220
+ }, "Discard"))
221
+ });
222
+
223
+ if (result) {
224
+ saveValue();
225
+ } else {
226
+ revertValue();
227
+ }
228
+ }
229
+ } else if (isDirty) revertValue();
230
+ }, [configuration.confirmUnsavedClose, confirm, isDirty, revertValue, saveValue]);
231
+ const inputHandler = useCallback(updatedSelection => {
232
+ if (!updatedSelection) return;
233
+ const {
234
+ maxDate,
235
+ minDate,
236
+ variant,
237
+ maxSelections,
238
+ maxRangeSize
239
+ } = configuration;
240
+
241
+ switch (variant) {
242
+ case 'single':
243
+ {
244
+ if (updatedSelection instanceof Date) {
245
+ if (maxDate && isAfter(updatedSelection, maxDate)) {
246
+ setSelection(maxDate);
247
+ } else if (minDate && isBefore(minDate, updatedSelection)) {
248
+ setSelection(minDate);
249
+ } else {
250
+ setSelection(updatedSelection);
251
+ }
252
+ }
253
+
254
+ break;
255
+ }
256
+
257
+ case 'multi':
258
+ {
259
+ if (Array.isArray(updatedSelection)) {
260
+ let validSelections = updatedSelection.filter(d => (maxDate ? isBefore(d, maxDate) : true) && (minDate ? isAfter(d, minDate) : true));
261
+
262
+ if (maxSelections) {
263
+ validSelections = validSelections.slice(0, maxSelections);
264
+ }
265
+
266
+ setSelection(validSelections);
267
+ }
268
+
269
+ break;
270
+ }
271
+
272
+ case 'range':
273
+ {
274
+ if (isRangeSelection(updatedSelection)) {
275
+ const validRange = updatedSelection;
276
+
277
+ if (minDate) {
278
+ validRange.from = isAfter(updatedSelection.from, minDate) ? updatedSelection.from : minDate;
279
+
280
+ if (updatedSelection.to) {
281
+ validRange.to = isAfter(updatedSelection.to, minDate) ? updatedSelection.to : minDate;
282
+ }
283
+ }
284
+
285
+ if (maxDate) {
286
+ validRange.from = isBefore(updatedSelection.from, maxDate) ? updatedSelection.from : maxDate;
287
+
288
+ if (updatedSelection.to) {
289
+ validRange.to = isBefore(updatedSelection.to, maxDate) ? updatedSelection.to : maxDate;
290
+ }
291
+ }
292
+
293
+ if (maxRangeSize && validRange.to && Math.abs(differenceInDays(validRange.from, validRange.to)) >= maxRangeSize) {
294
+ validRange.to = addDays(validRange.from, maxRangeSize - 1);
295
+ }
296
+
297
+ setSelection(updatedSelection);
298
+ }
299
+
300
+ break;
301
+ }
302
+ }
303
+ }, [configuration]);
304
+ const selectionHandler = useCallback(date => {
305
+ setIsDirty(true);
306
+
307
+ if (configuration.variant === 'multi') {
308
+ const selections = [...selection];
309
+ const existingIndex = selections.findIndex(s => isEqual(s, date));
310
+
311
+ if (existingIndex > -1) {
312
+ selections.splice(existingIndex, 1);
313
+ setSelection(selections.sort((a, b) => a.getTime() - b.getTime()));
314
+ } else {
315
+ if (configuration.maxSelections && selections.length + 1 > configuration.maxSelections) return;
316
+ setSelection([...selections, date].sort((a, b) => a.getTime() - b.getTime()));
317
+ }
318
+ } else if (configuration.variant === 'range') {
319
+ if (selection && isRangeSelection(selection) && !selection.to) {
320
+ let toDate = date;
321
+
322
+ if (configuration.maxRangeSize && Math.abs(differenceInDays(selection.from, date)) >= configuration.maxRangeSize) {
323
+ toDate = isBefore(date, selection.from) ? subDays(selection.from, configuration.maxRangeSize - 1) : addDays(selection.from, configuration.maxRangeSize - 1);
324
+ }
325
+
326
+ const updatedSelection = isBefore(toDate, selection.from) ? {
327
+ from: toDate,
328
+ to: selection.from
329
+ } : {
330
+ from: selection.from,
331
+ to: toDate
332
+ };
333
+ setSelection(updatedSelection);
334
+ setHoverRange(null);
335
+
336
+ if (!configuration.confirmation) {
337
+ saveValue(updatedSelection);
338
+ }
339
+ } else {
340
+ setHoverRange({
341
+ from: date,
342
+ to: date
343
+ });
344
+ setSelection({
345
+ from: date,
346
+ to: null
347
+ });
348
+ }
349
+ } else {
350
+ setSelection(date);
351
+
352
+ if (!configuration.confirmation) {
353
+ saveValue(date);
354
+ }
355
+ }
356
+ }, [configuration.confirmation, configuration.maxRangeSize, configuration.maxSelections, configuration.variant, saveValue, selection]);
357
+ const focusHnadler = useCallback(date => {
358
+ if (!selection) return;
359
+ const {
360
+ minDate,
361
+ maxDate,
362
+ maxRangeSize,
363
+ variant
364
+ } = configuration;
365
+
366
+ if (variant === 'range' && isRangeSelection(selection) && hoverRange) {
367
+ let hoverDate = date;
368
+ if (minDate) hoverDate = isBefore(date, minDate) ? minDate : hoverDate;
369
+ if (maxDate) hoverDate = isAfter(date, maxDate) ? maxDate : hoverDate;
370
+
371
+ if (maxRangeSize && Math.abs(differenceInDays(selection.from, hoverDate)) >= maxRangeSize) {
372
+ hoverDate = isBefore(hoverDate, selection.from) ? subDays(selection.from, configuration.maxRangeSize - 1) : addDays(selection.from, configuration.maxRangeSize - 1);
373
+ }
374
+
375
+ setHoverRange(isBefore(hoverDate, selection.from) ? {
376
+ from: hoverDate,
377
+ to: selection.from
378
+ } : {
379
+ from: selection.from,
380
+ to: hoverDate
381
+ });
382
+ }
383
+ }, [configuration, hoverRange, selection]);
384
+ const datePickerCtx = useMemo(() => {
385
+ return {
386
+ configuration,
387
+ currentViewingDate,
388
+ disabled: false,
389
+ formattedDate: getFormattedDate,
390
+ goToMonth,
391
+ hoverRange,
392
+ nextMonth,
393
+ onClose: handleClose,
394
+ onDateInput: inputHandler,
395
+ onDayFocus: focusHnadler,
396
+ onSelection: selectionHandler,
397
+ previousMonth,
398
+ revertValue,
399
+ saveValue,
400
+ selectionActive: false,
401
+ selection
402
+ };
403
+ }, [configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, handleClose, hoverRange, inputHandler, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
404
+ return /*#__PURE__*/React.createElement(DatePickerContext.Provider, {
405
+ value: datePickerCtx
406
+ }, children);
407
+ };
408
+ DatePickerProvider.displayName = "DatePickerProvider";