@primer/components 0.0.0-202192801738 → 0.0.0-20219283497

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 (134) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/dist/browser.esm.js +436 -439
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +440 -443
  5. package/dist/browser.umd.js.map +1 -1
  6. package/lib/ActionList/Item.d.ts +6 -0
  7. package/lib/ActionList/Item.js +5 -1
  8. package/lib/ActionMenu.js +2 -2
  9. package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
  10. package/lib/Autocomplete/Autocomplete.d.ts +11 -13
  11. package/lib/Autocomplete/AutocompleteInput.d.ts +11 -13
  12. package/lib/Button/Button.d.ts +11 -11
  13. package/lib/Button/ButtonBase.d.ts +1 -1
  14. package/lib/Button/ButtonClose.d.ts +29 -29
  15. package/lib/Button/ButtonDanger.d.ts +11 -11
  16. package/lib/Button/ButtonInvisible.d.ts +11 -11
  17. package/lib/Button/ButtonOutline.d.ts +11 -11
  18. package/lib/Button/ButtonPrimary.d.ts +11 -11
  19. package/lib/Button/ButtonStyles.js +1 -1
  20. package/lib/CircleBadge.d.ts +2 -2
  21. package/lib/CircleOcticon.d.ts +33 -33
  22. package/lib/Dialog/Dialog.d.ts +4 -4
  23. package/lib/Dialog/Dialog.js +12 -22
  24. package/lib/Dialog.d.ts +35 -35
  25. package/lib/Dropdown.d.ts +87 -165
  26. package/lib/Dropdown.js +6 -5
  27. package/lib/DropdownMenu/DropdownButton.d.ts +30 -30
  28. package/lib/FilterList.d.ts +26 -26
  29. package/lib/Label.d.ts +1 -1
  30. package/lib/NewButton/button.d.ts +581 -0
  31. package/lib/NewButton/button.js +297 -0
  32. package/lib/NewButton/counter.d.ts +6 -0
  33. package/lib/NewButton/counter.js +33 -0
  34. package/lib/NewButton/index.d.ts +4 -0
  35. package/lib/NewButton/index.js +21 -0
  36. package/lib/Position.d.ts +4 -4
  37. package/lib/ProgressBar.d.ts +1 -1
  38. package/lib/SelectMenu/SelectMenu.d.ts +151 -153
  39. package/lib/SelectMenu/SelectMenuItem.d.ts +1 -1
  40. package/lib/SelectMenu/SelectMenuModal.d.ts +1 -1
  41. package/lib/TextInputWithTokens.d.ts +11 -13
  42. package/lib/Timeline.d.ts +33 -33
  43. package/lib/Token/AvatarToken.d.ts +1 -1
  44. package/lib/Token/IssueLabelToken.d.ts +1 -1
  45. package/lib/Token/Token.d.ts +1 -1
  46. package/lib/_TextInputWrapper.d.ts +1 -1
  47. package/lib/constants.js +3 -1
  48. package/lib/index.d.ts +2 -0
  49. package/lib/index.js +8 -0
  50. package/lib/theme-preval.js +2 -2
  51. package/lib/utils/testing.d.ts +1 -1
  52. package/lib-esm/ActionList/Item.d.ts +6 -0
  53. package/lib-esm/ActionList/Item.js +5 -1
  54. package/lib-esm/ActionMenu.js +2 -2
  55. package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
  56. package/lib-esm/Autocomplete/Autocomplete.d.ts +11 -13
  57. package/lib-esm/Autocomplete/AutocompleteInput.d.ts +11 -13
  58. package/lib-esm/Button/Button.d.ts +11 -11
  59. package/lib-esm/Button/ButtonBase.d.ts +1 -1
  60. package/lib-esm/Button/ButtonClose.d.ts +29 -29
  61. package/lib-esm/Button/ButtonDanger.d.ts +11 -11
  62. package/lib-esm/Button/ButtonInvisible.d.ts +11 -11
  63. package/lib-esm/Button/ButtonOutline.d.ts +11 -11
  64. package/lib-esm/Button/ButtonPrimary.d.ts +11 -11
  65. package/lib-esm/Button/ButtonStyles.js +1 -1
  66. package/lib-esm/CircleBadge.d.ts +2 -2
  67. package/lib-esm/CircleOcticon.d.ts +33 -33
  68. package/lib-esm/Dialog/Dialog.d.ts +4 -4
  69. package/lib-esm/Dialog/Dialog.js +12 -21
  70. package/lib-esm/Dialog.d.ts +35 -35
  71. package/lib-esm/Dropdown.d.ts +87 -165
  72. package/lib-esm/Dropdown.js +4 -3
  73. package/lib-esm/DropdownMenu/DropdownButton.d.ts +30 -30
  74. package/lib-esm/FilterList.d.ts +26 -26
  75. package/lib-esm/Label.d.ts +1 -1
  76. package/lib-esm/NewButton/button.d.ts +581 -0
  77. package/lib-esm/NewButton/button.js +273 -0
  78. package/lib-esm/NewButton/counter.d.ts +6 -0
  79. package/lib-esm/NewButton/counter.js +21 -0
  80. package/lib-esm/NewButton/index.d.ts +4 -0
  81. package/lib-esm/NewButton/index.js +3 -0
  82. package/lib-esm/Position.d.ts +4 -4
  83. package/lib-esm/ProgressBar.d.ts +1 -1
  84. package/lib-esm/SelectMenu/SelectMenu.d.ts +151 -153
  85. package/lib-esm/SelectMenu/SelectMenuItem.d.ts +1 -1
  86. package/lib-esm/SelectMenu/SelectMenuModal.d.ts +1 -1
  87. package/lib-esm/TextInputWithTokens.d.ts +11 -13
  88. package/lib-esm/Timeline.d.ts +33 -33
  89. package/lib-esm/Token/AvatarToken.d.ts +1 -1
  90. package/lib-esm/Token/IssueLabelToken.d.ts +1 -1
  91. package/lib-esm/Token/Token.d.ts +1 -1
  92. package/lib-esm/_TextInputWrapper.d.ts +1 -1
  93. package/lib-esm/constants.js +3 -1
  94. package/lib-esm/index.d.ts +2 -0
  95. package/lib-esm/index.js +1 -0
  96. package/lib-esm/theme-preval.js +2 -2
  97. package/lib-esm/utils/testing.d.ts +1 -1
  98. package/package.json +13 -14
  99. package/lib/DatePicker/DatePicker.d.ts +0 -48
  100. package/lib/DatePicker/DatePicker.js +0 -104
  101. package/lib/DatePicker/DatePickerAnchor.d.ts +0 -5
  102. package/lib/DatePicker/DatePickerAnchor.js +0 -182
  103. package/lib/DatePicker/DatePickerOverlay.d.ts +0 -3
  104. package/lib/DatePicker/DatePickerOverlay.js +0 -48
  105. package/lib/DatePicker/DatePickerPanel.d.ts +0 -2
  106. package/lib/DatePicker/DatePickerPanel.js +0 -126
  107. package/lib/DatePicker/Day.d.ts +0 -14
  108. package/lib/DatePicker/Day.js +0 -190
  109. package/lib/DatePicker/Month.d.ts +0 -9
  110. package/lib/DatePicker/Month.js +0 -120
  111. package/lib/DatePicker/index.d.ts +0 -2
  112. package/lib/DatePicker/index.js +0 -13
  113. package/lib/DatePicker/useDatePicker.d.ts +0 -88
  114. package/lib/DatePicker/useDatePicker.js +0 -438
  115. package/lib/hooks/useDebounce.d.ts +0 -2
  116. package/lib/hooks/useDebounce.js +0 -24
  117. package/lib-esm/DatePicker/DatePicker.d.ts +0 -48
  118. package/lib-esm/DatePicker/DatePicker.js +0 -87
  119. package/lib-esm/DatePicker/DatePickerAnchor.d.ts +0 -5
  120. package/lib-esm/DatePicker/DatePickerAnchor.js +0 -155
  121. package/lib-esm/DatePicker/DatePickerOverlay.d.ts +0 -3
  122. package/lib-esm/DatePicker/DatePickerOverlay.js +0 -29
  123. package/lib-esm/DatePicker/DatePickerPanel.d.ts +0 -2
  124. package/lib-esm/DatePicker/DatePickerPanel.js +0 -100
  125. package/lib-esm/DatePicker/Day.d.ts +0 -14
  126. package/lib-esm/DatePicker/Day.js +0 -167
  127. package/lib-esm/DatePicker/Month.d.ts +0 -9
  128. package/lib-esm/DatePicker/Month.js +0 -96
  129. package/lib-esm/DatePicker/index.d.ts +0 -2
  130. package/lib-esm/DatePicker/index.js +0 -1
  131. package/lib-esm/DatePicker/useDatePicker.d.ts +0 -88
  132. package/lib-esm/DatePicker/useDatePicker.js +0 -408
  133. package/lib-esm/hooks/useDebounce.d.ts +0 -2
  134. package/lib-esm/hooks/useDebounce.js +0 -16
@@ -1,88 +0,0 @@
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
- variant?: SelectionVariant;
20
- view?: '1-month' | '2-month';
21
- weekStartsOn?: 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday';
22
- }
23
- export declare type RangeSelection = {
24
- from: Date;
25
- to: Date | null;
26
- };
27
- export declare type StringRangeSelection = {
28
- from: string;
29
- to: string;
30
- };
31
- export interface DatePickerContext {
32
- disabled?: boolean;
33
- configuration: DatePickerConfiguration;
34
- currentViewingDate: Date;
35
- goToMonth: (date: Date) => void;
36
- hoverRange?: RangeSelection | null;
37
- selection?: Selection;
38
- softSelection?: Partial<RangeSelection> | null;
39
- selectionActive?: boolean;
40
- formattedDate: string;
41
- nextMonth: () => void;
42
- onClose: () => void;
43
- onDateInput: (updatedSelection: Selection) => void;
44
- onDayFocus: (date: Date) => void;
45
- onSelection: (date: Date) => void;
46
- previousMonth: () => void;
47
- revertValue: () => void;
48
- saveValue: (selection?: Selection) => void;
49
- }
50
- export declare type Selection = Date | Array<Date> | RangeSelection | null;
51
- export declare type StringSelection = string | Array<string> | {
52
- to: string;
53
- from: string;
54
- } | null;
55
- export declare type DaySelection = boolean | 'start' | 'middle' | 'end';
56
- declare const useDatePicker: (date?: Date | undefined) => {
57
- blocked: boolean | undefined;
58
- disabled: boolean;
59
- selected: DaySelection;
60
- today: boolean;
61
- configuration: DatePickerConfiguration;
62
- currentViewingDate: Date;
63
- goToMonth: (date: Date) => void;
64
- hoverRange?: RangeSelection | null | undefined;
65
- selection?: Selection | undefined;
66
- softSelection?: Partial<RangeSelection> | null | undefined;
67
- selectionActive?: boolean | undefined;
68
- formattedDate: string;
69
- nextMonth: () => void;
70
- onClose: () => void;
71
- onDateInput: (updatedSelection: Selection) => void;
72
- onDayFocus: (date: Date) => void;
73
- onSelection: (date: Date) => void;
74
- previousMonth: () => void;
75
- revertValue: () => void;
76
- saveValue: (selection?: Selection | undefined) => void;
77
- };
78
- export default useDatePicker;
79
- export interface DatePickerProviderProps {
80
- closePicker?: () => void;
81
- configuration?: DatePickerConfiguration;
82
- value?: Selection | StringSelection;
83
- }
84
- export declare function isSingleSelection(selection: Selection): selection is Date;
85
- export declare function isMultiSelection(selection: Selection | StringSelection): selection is Array<Date> | Array<string>;
86
- export declare function isRangeSelection(selection: Selection | StringSelection): selection is RangeSelection | StringRangeSelection;
87
- export declare function isStringRangeSelection(selection: StringSelection): selection is StringRangeSelection;
88
- export declare const DatePickerProvider: React.FC<DatePickerProviderProps>;
@@ -1,408 +0,0 @@
1
- import { CheckIcon, TrashIcon } from '@primer/octicons-react';
2
- import { format, 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
- const DatePickerContext = /*#__PURE__*/createContext(null);
7
-
8
- const useDatePicker = date => {
9
- const value = useContext(DatePickerContext);
10
- const [selected, setSelected] = useState(false);
11
- const today = date ? isToday(date) : false;
12
-
13
- if (!value) {
14
- throw new Error('useDatePicker must be used inside a DatePickerProvider');
15
- }
16
-
17
- useEffect(() => {
18
- if (date) {
19
- if (value.hoverRange) {
20
- if (isRangeSelection(value.hoverRange)) {
21
- if (isEqual(date, value.hoverRange.from)) {
22
- setSelected('start');
23
- } else if (value.hoverRange.to && isEqual(date, value.hoverRange.to)) {
24
- setSelected('end');
25
- } else if (isAfter(date, value.hoverRange.from) && value.hoverRange.to && isBefore(date, value.hoverRange.to)) {
26
- setSelected('middle');
27
- } else {
28
- setSelected(false);
29
- }
30
- }
31
- } else if (value.selection) {
32
- if (isMultiSelection(value.selection)) {
33
- setSelected(!!value.selection.find(d => isEqual(d, date)));
34
- } else if (isRangeSelection(value.selection)) {
35
- if (isEqual(date, value.selection.from)) {
36
- setSelected('start');
37
- } else if (value.selection.to && isEqual(date, value.selection.to)) {
38
- setSelected('end');
39
- } else if (isAfter(date, value.selection.from) && value.selection.to && isBefore(date, value.selection.to)) {
40
- setSelected('middle');
41
- } else {
42
- setSelected(false);
43
- }
44
- } else {
45
- setSelected(isEqual(date, value.selection));
46
- }
47
- }
48
- }
49
- }, [date, value.hoverRange, value.selection, today]);
50
- let blocked,
51
- disabled = false;
52
-
53
- if (date) {
54
- // Determine if date is blocked out
55
- if (value.configuration.blockedDates) {
56
- blocked = !!value.configuration.blockedDates.find(d => isEqual(d, date));
57
- } // Determine if date is disabled
58
-
59
-
60
- if (value.configuration.minDate || value.configuration.maxDate || value.configuration.disableWeekends) {
61
- 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);
62
- }
63
- }
64
-
65
- return { ...value,
66
- blocked,
67
- disabled,
68
- selected,
69
- today
70
- };
71
- };
72
-
73
- export default useDatePicker;
74
- export function isSingleSelection(selection) {
75
- return selection instanceof Date;
76
- }
77
- export function isMultiSelection(selection) {
78
- return Array.isArray(selection);
79
- }
80
- export function isRangeSelection(selection) {
81
- return !!selection.from;
82
- }
83
- export function isStringRangeSelection(selection) {
84
- return !!selection.from;
85
- }
86
-
87
- function parseSelection(selection, variant) {
88
- if (!selection) return;
89
-
90
- if (variant === 'multi') {
91
- if (isMultiSelection(selection)) {
92
- const parsedSelection = [];
93
-
94
- for (const d of selection) {
95
- parsedSelection.push(new Date(new Date(d).toDateString()));
96
- }
97
-
98
- return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
99
- } else if (selection instanceof Date) {
100
- return [new Date(new Date(selection).toDateString())];
101
- } else if (isRangeSelection(selection)) {
102
- const parsedSelection = [];
103
- parsedSelection.push(new Date(new Date(selection.from).toDateString()));
104
-
105
- if (selection.to) {
106
- parsedSelection.push(new Date(new Date(selection.to).toDateString()));
107
- }
108
-
109
- return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
110
- }
111
- } else if (variant === 'range') {
112
- if (isRangeSelection(selection)) {
113
- return {
114
- from: new Date(new Date(selection.from).toDateString()),
115
- to: selection.to ? new Date(new Date(selection.to).toDateString()) : null
116
- };
117
- } else if (isMultiSelection(selection)) {
118
- return {
119
- from: new Date(new Date(selection[0]).toDateString()),
120
- to: selection[1] ? new Date(new Date(selection[1]).toDateString()) : null
121
- };
122
- } else if (selection instanceof Date) {
123
- return {
124
- from: new Date(new Date(selection).toDateString()),
125
- to: null
126
- };
127
- }
128
- } else {
129
- if (selection instanceof Date) {
130
- return new Date(new Date(selection).toDateString());
131
- } else if (isMultiSelection(selection)) {
132
- return new Date(new Date(selection[0]).toDateString());
133
- } else if (isRangeSelection(selection)) {
134
- return new Date(new Date(selection.from).toDateString());
135
- } else {
136
- return;
137
- }
138
- }
139
- }
140
-
141
- const defaultConfiguration = {
142
- anchorVariant: 'button',
143
- confirmation: false,
144
- confirmUnsavedClose: false,
145
- disableWeekends: false,
146
- iconPlacement: 'start',
147
- placeholder: 'Select a Date...',
148
- variant: 'single',
149
- view: '2-month',
150
- weekStartsOn: 'Sunday'
151
- };
152
- export const DatePickerProvider = ({
153
- configuration: externalConfig = {},
154
- children,
155
- closePicker,
156
- value
157
- }) => {
158
- const [configuration, setConfiguration] = useState(deepmerge(defaultConfiguration, externalConfig));
159
- const [previousSelection, setPreviousSelection] = useState(parseSelection(value, configuration.variant));
160
- const [isDirty, setIsDirty] = useState(false);
161
- const [selection, setSelection] = useState(parseSelection(value, configuration.variant));
162
- const [hoverRange, setHoverRange] = useState(null);
163
- const [currentViewingDate, setCurrentViewingDate] = useState(new Date());
164
- const confirm = useConfirm();
165
- useEffect(() => {
166
- setConfiguration(deepmerge(defaultConfiguration, externalConfig));
167
- setSelection(parseSelection(selection, configuration.variant)); // Don't want this to run every time selection gets updated
168
- // eslint-disable-next-line react-hooks/exhaustive-deps
169
- }, [configuration.variant, externalConfig]);
170
- const goToMonth = useCallback(date => {
171
- setCurrentViewingDate(new Date(new Date(date).toDateString()));
172
- }, []);
173
- const nextMonth = useCallback(() => {
174
- setCurrentViewingDate(addMonths(currentViewingDate, 1));
175
- }, [currentViewingDate]);
176
- const previousMonth = useCallback(() => {
177
- setCurrentViewingDate(subMonths(currentViewingDate, 1));
178
- }, [currentViewingDate]);
179
- const getFormattedDate = useMemo(() => {
180
- if (!selection) {
181
- return configuration.placeholder;
182
- }
183
-
184
- const {
185
- anchorVariant,
186
- dateFormat,
187
- variant
188
- } = configuration;
189
- let template = 'MMM d';
190
-
191
- if (anchorVariant !== 'input' && dateFormat) {
192
- switch (dateFormat) {
193
- case 'short':
194
- template = 'MMM d';
195
- break;
196
-
197
- case 'long':
198
- template = 'MMM d, yyyy';
199
- break;
200
-
201
- default:
202
- template = dateFormat;
203
- break;
204
- }
205
- } else {
206
- template = 'MM/dd/yyyy';
207
- }
208
-
209
- switch (variant) {
210
- case 'single':
211
- {
212
- if (selection instanceof Date) {
213
- return format(selection, template);
214
- } else if (Array.isArray(selection)) {
215
- return format(selection[0], template);
216
- } else if (isRangeSelection(selection)) {
217
- return format(selection.from, template);
218
- } else {
219
- return 'Invalid Selection';
220
- }
221
- }
222
-
223
- case 'multi':
224
- {
225
- if (Array.isArray(selection)) {
226
- if (selection.length > 3) return `${selection.length} Selected`;
227
- const formatted = selection.map(d => format(d, template)).join(', ');
228
- return formatted;
229
- } else if (selection instanceof Date) {
230
- return [selection].map(d => format(d, template)).join(', ');
231
- } else if (isRangeSelection(selection)) {
232
- return [selection.to, selection.from].map(d => d ? format(d, template) : '').join(', ');
233
- } else {
234
- return 'Invalid Selection';
235
- }
236
- }
237
-
238
- case 'range':
239
- {
240
- if (isRangeSelection(selection)) {
241
- return Object.entries(selection).map(([_, date]) => date ? format(date, template) : '').join(' - ');
242
- } else if (selection instanceof Date) {
243
- return Object.entries({
244
- from: selection,
245
- to: null
246
- }).map(([_, date]) => date ? format(date, template) : '').join(' - ');
247
- } else if (Array.isArray(selection)) {
248
- return Object.entries({
249
- from: selection[0],
250
- to: selection[1]
251
- }) // to date can still be null
252
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
253
- .map(([_, date]) => date ? format(date, template) : '').join(' - ');
254
- } else {
255
- return 'Invalid Selection';
256
- }
257
- }
258
-
259
- default:
260
- {
261
- return 'Invalid Configuration';
262
- }
263
- }
264
- }, [configuration, selection]);
265
- const saveValue = useCallback(updatedSelection => {
266
- setPreviousSelection(updatedSelection !== null && updatedSelection !== void 0 ? updatedSelection : selection);
267
- setIsDirty(false);
268
- closePicker === null || closePicker === void 0 ? void 0 : closePicker();
269
- }, [closePicker, selection]);
270
- const revertValue = useCallback(() => {
271
- setSelection(previousSelection);
272
- setIsDirty(false);
273
- }, [previousSelection]);
274
- const handleClose = useCallback(async () => {
275
- if (configuration.confirmUnsavedClose) {
276
- if (isDirty) {
277
- const result = await confirm({
278
- title: 'Save Changes?',
279
- content: 'You have unsaved changes, would you like to save them?',
280
- confirmButtonContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CheckIcon, null), /*#__PURE__*/React.createElement(Text, {
281
- sx: {
282
- ml: 1
283
- }
284
- }, "Save")),
285
- cancelButtonContent: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(TrashIcon, null), /*#__PURE__*/React.createElement(Text, {
286
- sx: {
287
- ml: 1
288
- }
289
- }, "Discard"))
290
- });
291
-
292
- if (result) {
293
- saveValue();
294
- } else {
295
- revertValue();
296
- }
297
- }
298
- } else if (isDirty) revertValue();
299
- }, [configuration.confirmUnsavedClose, confirm, isDirty, revertValue, saveValue]);
300
- const inputHandler = useCallback(updatedSelection => {
301
- // validate date falls within range
302
- setSelection(updatedSelection);
303
- }, []);
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";
@@ -1,2 +0,0 @@
1
- declare function useDebounce<T>(value: T, delay: number): T;
2
- export default useDebounce;
@@ -1,16 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
-
3
- function useDebounce(value, delay) {
4
- const [debouncedValue, setDebouncedValue] = useState(value);
5
- useEffect(() => {
6
- const handler = setTimeout(() => {
7
- setDebouncedValue(value);
8
- }, delay);
9
- return () => {
10
- clearTimeout(handler);
11
- };
12
- }, [value, delay]);
13
- return debouncedValue;
14
- }
15
-
16
- export default useDebounce;