@zendeskgarden/react-datepickers 9.0.0-next.1 → 9.0.0-next.11

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 (55) hide show
  1. package/README.md +7 -7
  2. package/dist/esm/elements/DatePicker/DatePicker.js +169 -0
  3. package/dist/esm/elements/DatePicker/components/Calendar.js +125 -0
  4. package/dist/esm/elements/DatePicker/components/Input.js +75 -0
  5. package/dist/esm/elements/DatePicker/components/MonthSelector.js +61 -0
  6. package/dist/esm/elements/DatePicker/utils/date-picker-reducer.js +187 -0
  7. package/dist/esm/elements/DatePicker/utils/useDatePickerContext.js +14 -0
  8. package/dist/esm/elements/DatePickerRange/DatePickerRange.js +101 -0
  9. package/dist/esm/elements/DatePickerRange/components/Calendar.js +42 -0
  10. package/dist/esm/elements/DatePickerRange/components/End.js +79 -0
  11. package/dist/esm/elements/DatePickerRange/components/Month.js +270 -0
  12. package/dist/esm/elements/DatePickerRange/components/Start.js +79 -0
  13. package/dist/esm/elements/DatePickerRange/utils/date-picker-range-reducer.js +319 -0
  14. package/dist/esm/elements/DatePickerRange/utils/useDatePickerRangeContext.js +14 -0
  15. package/dist/esm/index.js +8 -0
  16. package/dist/esm/node_modules/@zendeskgarden/svg-icons/src/16/chevron-left-stroke.svg.js +25 -0
  17. package/dist/esm/node_modules/@zendeskgarden/svg-icons/src/16/chevron-right-stroke.svg.js +25 -0
  18. package/dist/esm/styled/StyledCalendar.js +21 -0
  19. package/dist/esm/styled/StyledCalendarItem.js +34 -0
  20. package/dist/esm/styled/StyledDatePicker.js +32 -0
  21. package/dist/esm/styled/StyledDay.js +54 -0
  22. package/dist/esm/styled/StyledDayLabel.js +21 -0
  23. package/dist/esm/styled/StyledHeader.js +21 -0
  24. package/dist/esm/styled/StyledHeaderLabel.js +21 -0
  25. package/dist/esm/styled/StyledHeaderPaddle.js +38 -0
  26. package/dist/esm/styled/StyledHighlight.js +50 -0
  27. package/dist/esm/styled/StyledMenu.js +22 -0
  28. package/dist/esm/styled/StyledMenuWrapper.js +27 -0
  29. package/dist/esm/styled/StyledRangeCalendar.js +22 -0
  30. package/dist/esm/types/index.js +12 -0
  31. package/dist/esm/utils/calendar-utils.js +88 -0
  32. package/dist/index.cjs.js +195 -238
  33. package/dist/typings/elements/{Datepicker/Datepicker.d.ts → DatePicker/DatePicker.d.ts} +2 -2
  34. package/dist/typings/elements/DatePicker/components/Input.d.ts +16 -0
  35. package/dist/typings/elements/{Datepicker/utils/datepicker-reducer.d.ts → DatePicker/utils/date-picker-reducer.d.ts} +6 -6
  36. package/dist/typings/elements/DatePicker/utils/useDatePickerContext.d.ts +18 -0
  37. package/dist/typings/elements/{DatepickerRange/DatepickerRange.d.ts → DatePickerRange/DatePickerRange.d.ts} +3 -3
  38. package/dist/typings/elements/{DatepickerRange/utils/datepicker-range-reducer.d.ts → DatePickerRange/utils/date-picker-range-reducer.d.ts} +6 -6
  39. package/dist/typings/elements/{DatepickerRange/utils/useDatepickerRangeContext.d.ts → DatePickerRange/utils/useDatePickerRangeContext.d.ts} +7 -7
  40. package/dist/typings/index.d.ts +3 -5
  41. package/dist/typings/styled/{StyledDatepicker.d.ts → StyledDatePicker.d.ts} +3 -3
  42. package/dist/typings/styled/StyledMenuWrapper.d.ts +2 -2
  43. package/dist/typings/styled/index.d.ts +1 -1
  44. package/dist/typings/types/index.d.ts +5 -14
  45. package/dist/typings/utils/calendar-utils.d.ts +2 -2
  46. package/package.json +8 -7
  47. package/dist/index.esm.js +0 -1714
  48. package/dist/typings/elements/Datepicker/utils/garden-placements.d.ts +0 -21
  49. package/dist/typings/elements/Datepicker/utils/useDatepickerContext.d.ts +0 -18
  50. /package/dist/typings/elements/{Datepicker → DatePicker}/components/Calendar.d.ts +0 -0
  51. /package/dist/typings/elements/{Datepicker → DatePicker}/components/MonthSelector.d.ts +0 -0
  52. /package/dist/typings/elements/{DatepickerRange → DatePickerRange}/components/Calendar.d.ts +0 -0
  53. /package/dist/typings/elements/{DatepickerRange → DatePickerRange}/components/End.d.ts +0 -0
  54. /package/dist/typings/elements/{DatepickerRange → DatePickerRange}/components/Month.d.ts +0 -0
  55. /package/dist/typings/elements/{DatepickerRange → DatePickerRange}/components/Start.d.ts +0 -0
package/dist/index.esm.js DELETED
@@ -1,1714 +0,0 @@
1
- /**
2
- * Copyright Zendesk, Inc.
3
- *
4
- * Use of this source code is governed under the Apache License, Version 2.0
5
- * found at http://www.apache.org/licenses/LICENSE-2.0.
6
- */
7
-
8
- import * as React from 'react';
9
- import React__default, { createContext, useContext, useCallback, forwardRef, useReducer, useRef, useEffect, useState, useMemo } from 'react';
10
- import PropTypes from 'prop-types';
11
- import styled, { css, ThemeContext } from 'styled-components';
12
- import { Manager, Reference, Popper } from 'react-popper';
13
- import { composeEventHandlers, KEYS } from '@zendeskgarden/container-utilities';
14
- import { startOfMonth } from 'date-fns/startOfMonth';
15
- import { endOfMonth } from 'date-fns/endOfMonth';
16
- import { startOfWeek } from 'date-fns/startOfWeek';
17
- import { endOfWeek } from 'date-fns/endOfWeek';
18
- import { eachDayOfInterval } from 'date-fns/eachDayOfInterval';
19
- import { addDays } from 'date-fns/addDays';
20
- import { isToday } from 'date-fns/isToday';
21
- import { isSameDay } from 'date-fns/isSameDay';
22
- import { isSameMonth } from 'date-fns/isSameMonth';
23
- import { isBefore } from 'date-fns/isBefore';
24
- import { isAfter } from 'date-fns/isAfter';
25
- import { getDate } from 'date-fns/getDate';
26
- import { retrieveComponentStyles, DEFAULT_THEME, menuStyles, getColor } from '@zendeskgarden/react-theming';
27
- import { addMonths } from 'date-fns/addMonths';
28
- import { subMonths } from 'date-fns/subMonths';
29
- import { isValid } from 'date-fns/isValid';
30
- import { parse } from 'date-fns/parse';
31
- import { compareAsc } from 'date-fns/compareAsc';
32
- import { subDays } from 'date-fns/subDays';
33
-
34
- const WEEK_STARTS_ON = [0, 1, 2, 3, 4, 5, 6];
35
- const SHARED_PLACEMENT = ['auto', 'top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end'];
36
- const PLACEMENT = [...SHARED_PLACEMENT, 'end', 'end-top', 'end-bottom', 'start', 'start-top', 'start-bottom'];
37
-
38
- function getPopperPlacement(gardenPlacement) {
39
- switch (gardenPlacement) {
40
- case 'end':
41
- return 'right';
42
- case 'end-top':
43
- return 'right-start';
44
- case 'end-bottom':
45
- return 'right-end';
46
- case 'start':
47
- return 'left';
48
- case 'start-top':
49
- return 'left-start';
50
- case 'start-bottom':
51
- return 'left-end';
52
- default:
53
- return gardenPlacement;
54
- }
55
- }
56
- function getRtlPopperPlacement(gardenPlacement) {
57
- const popperPlacement = getPopperPlacement(gardenPlacement);
58
- switch (popperPlacement) {
59
- case 'left':
60
- return 'right';
61
- case 'left-start':
62
- return 'right-start';
63
- case 'left-end':
64
- return 'right-end';
65
- case 'top-start':
66
- return 'top-end';
67
- case 'top-end':
68
- return 'top-start';
69
- case 'right':
70
- return 'left';
71
- case 'right-start':
72
- return 'left-start';
73
- case 'right-end':
74
- return 'left-end';
75
- case 'bottom-start':
76
- return 'bottom-end';
77
- case 'bottom-end':
78
- return 'bottom-start';
79
- default:
80
- return popperPlacement;
81
- }
82
- }
83
- function getMenuPosition(popperPlacement) {
84
- return popperPlacement ? popperPlacement.split('-')[0] : 'bottom';
85
- }
86
-
87
- const COMPONENT_ID$b = 'datepickers.menu';
88
- const StyledMenu = styled.div.attrs({
89
- 'data-garden-id': COMPONENT_ID$b,
90
- 'data-garden-version': '9.0.0-next.1'
91
- }).withConfig({
92
- displayName: "StyledMenu",
93
- componentId: "sc-1npbkk0-0"
94
- })(["", ";"], props => retrieveComponentStyles(COMPONENT_ID$b, props));
95
- StyledMenu.defaultProps = {
96
- theme: DEFAULT_THEME
97
- };
98
-
99
- const COMPONENT_ID$a = 'datepickers.menu_wrapper';
100
- const StyledMenuWrapper = styled.div.attrs(props => ({
101
- className: props.isAnimated && 'is-animated'
102
- })).withConfig({
103
- displayName: "StyledMenuWrapper",
104
- componentId: "sc-6fowoz-0"
105
- })(["", ";", ";"], props => menuStyles(getMenuPosition(props.placement), {
106
- theme: props.theme,
107
- hidden: props.isHidden,
108
- margin: `${props.theme.space.base}px`,
109
- zIndex: props.zIndex,
110
- animationModifier: props.isAnimated ? '.is-animated' : undefined
111
- }), props => retrieveComponentStyles(COMPONENT_ID$a, props));
112
- StyledMenuWrapper.defaultProps = {
113
- theme: DEFAULT_THEME
114
- };
115
-
116
- const COMPONENT_ID$9 = 'datepickers.datepicker';
117
- const retrievePadding = _ref => {
118
- let {
119
- isCompact,
120
- theme
121
- } = _ref;
122
- let value = theme.space.base * 5;
123
- if (isCompact) {
124
- value = theme.space.base * 4;
125
- }
126
- return `margin: ${value}px;`;
127
- };
128
- const StyledDatepicker = styled.div.attrs({
129
- 'data-garden-id': COMPONENT_ID$9
130
- }).withConfig({
131
- displayName: "StyledDatepicker",
132
- componentId: "sc-w3zqsp-0"
133
- })(["direction:", ";", " background-color:", ";color:", ";", ";"], props => props.theme.rtl && 'rtl', retrievePadding, props => props.theme.colors.background, props => props.theme.colors.foreground, props => retrieveComponentStyles(COMPONENT_ID$9, props));
134
- StyledDatepicker.defaultProps = {
135
- theme: DEFAULT_THEME
136
- };
137
-
138
- const COMPONENT_ID$8 = 'datepickers.range_calendar';
139
- const StyledRangeCalendar = styled.div.attrs({
140
- 'data-garden-id': COMPONENT_ID$8
141
- }).withConfig({
142
- displayName: "StyledRangeCalendar",
143
- componentId: "sc-1og46sy-0"
144
- })(["display:flex;overflow:auto;", "{margin:0;", "}", ";"], StyledDatepicker, props => props.theme.rtl ? `&:last-of-type {margin-right: ${props.theme.space.base * 5}px}` : `&:first-of-type {margin-right: ${props.theme.space.base * 5}px}`, props => retrieveComponentStyles(COMPONENT_ID$8, props));
145
- StyledRangeCalendar.defaultProps = {
146
- theme: DEFAULT_THEME
147
- };
148
-
149
- const COMPONENT_ID$7 = 'datepickers.header';
150
- const StyledHeader = styled.div.attrs({
151
- 'data-garden-id': COMPONENT_ID$7
152
- }).withConfig({
153
- displayName: "StyledHeader",
154
- componentId: "sc-upq318-0"
155
- })(["display:flex;width:", "px;", ";"], props => props.isCompact ? props.theme.space.base * 56 : props.theme.space.base * 70, props => retrieveComponentStyles(COMPONENT_ID$7, props));
156
- StyledHeader.defaultProps = {
157
- theme: DEFAULT_THEME
158
- };
159
-
160
- const retrieveSizing = _ref => {
161
- let {
162
- isCompact,
163
- theme
164
- } = _ref;
165
- let size = theme.space.base * 10;
166
- if (isCompact) {
167
- size = theme.space.base * 8;
168
- }
169
- return css(["width:", "px;height:", "px;"], size, size);
170
- };
171
- const retrieveColor$1 = _ref2 => {
172
- let {
173
- theme
174
- } = _ref2;
175
- return css([":hover{background-color:", ";color:", ";}:active{background-color:", ";color:", ";}color:", ";"], getColor('primaryHue', 600, theme, 0.08), theme.colors.foreground, getColor('primaryHue', 600, theme, 0.2), theme.colors.foreground, getColor('neutralHue', 600, theme));
176
- };
177
- const COMPONENT_ID$6 = 'datepickers.header_paddle';
178
- const StyledHeaderPaddle = styled.div.attrs({
179
- 'data-garden-id': COMPONENT_ID$6
180
- }).withConfig({
181
- displayName: "StyledHeaderPaddle",
182
- componentId: "sc-2oqh0g-0"
183
- })(["display:flex;align-items:center;justify-content:center;transform:", ";visibility:", ";border-radius:50%;cursor:pointer;", " ", " svg{width:", ";height:", ";}", ";"], props => props.theme.rtl && 'rotate(180deg)', props => props.isHidden && 'hidden', retrieveSizing, retrieveColor$1, props => `${props.theme.iconSizes.md}`, props => `${props.theme.iconSizes.md}`, props => retrieveComponentStyles(COMPONENT_ID$6, props));
184
- StyledHeaderPaddle.defaultProps = {
185
- theme: DEFAULT_THEME
186
- };
187
-
188
- const COMPONENT_ID$5 = 'datepickers.header_label';
189
- const StyledHeaderLabel = styled.div.attrs({
190
- 'data-garden-id': COMPONENT_ID$5
191
- }).withConfig({
192
- displayName: "StyledHeaderLabel",
193
- componentId: "sc-1ryf5ub-0"
194
- })(["display:flex;flex-grow:1;align-items:center;justify-content:center;font-size:", ";font-weight:", ";", ";"], props => props.isCompact ? props.theme.fontSizes.sm : props.theme.fontSizes.md, props => props.theme.fontWeights.semibold, props => retrieveComponentStyles(COMPONENT_ID$5, props));
195
- StyledHeaderLabel.defaultProps = {
196
- theme: DEFAULT_THEME
197
- };
198
-
199
- const COMPONENT_ID$4 = 'datepickers.calendar';
200
- const StyledCalendar = styled.div.attrs({
201
- 'data-garden-id': COMPONENT_ID$4
202
- }).withConfig({
203
- displayName: "StyledCalendar",
204
- componentId: "sc-g5hoe8-0"
205
- })(["width:", "px;", ";"], props => props.isCompact ? props.theme.space.base * 56 : props.theme.space.base * 70, props => retrieveComponentStyles(COMPONENT_ID$4, props));
206
- StyledCalendar.defaultProps = {
207
- theme: DEFAULT_THEME
208
- };
209
-
210
- const COMPONENT_ID$3 = 'datepickers.calendar_item';
211
- const retrieveSize = _ref => {
212
- let {
213
- isCompact,
214
- theme
215
- } = _ref;
216
- let size;
217
- if (isCompact) {
218
- size = `${theme.space.base * 8}px`;
219
- } else {
220
- size = `${theme.space.base * 10}px`;
221
- }
222
- return css(["width:", ";height:", ";"], size, size);
223
- };
224
- const StyledCalendarItem = styled.div.attrs({
225
- 'data-garden-id': COMPONENT_ID$3
226
- }).withConfig({
227
- displayName: "StyledCalendarItem",
228
- componentId: "sc-143w8wb-0"
229
- })(["display:inline-block;position:relative;vertical-align:top;", " ", ";"], retrieveSize, props => retrieveComponentStyles(COMPONENT_ID$3, props));
230
- StyledCalendarItem.defaultProps = {
231
- theme: DEFAULT_THEME
232
- };
233
-
234
- const COMPONENT_ID$2 = 'datepickers.day_label';
235
- const StyledDayLabel = styled.div.attrs({
236
- 'data-garden-id': COMPONENT_ID$2
237
- }).withConfig({
238
- displayName: "StyledDayLabel",
239
- componentId: "sc-9bh1p7-0"
240
- })(["display:flex;align-items:center;justify-content:center;width:100%;height:100%;font-size:", ";font-weight:", ";", ";"], props => props.isCompact ? props.theme.fontSizes.sm : props.theme.fontSizes.md, props => props.theme.fontWeights.semibold, props => retrieveComponentStyles(COMPONENT_ID$2, props));
241
- StyledDayLabel.defaultProps = {
242
- theme: DEFAULT_THEME
243
- };
244
-
245
- const COMPONENT_ID$1 = 'datepickers.highlight';
246
- const retrieveBorderRadius = _ref => {
247
- let {
248
- theme,
249
- isEnd,
250
- isStart
251
- } = _ref;
252
- const startValue = 'border-radius: 0 50% 50% 0;';
253
- const endValue = 'border-radius: 50% 0 0 50%;';
254
- if (theme.rtl) {
255
- if (isStart) {
256
- return startValue;
257
- } else if (isEnd) {
258
- return endValue;
259
- }
260
- }
261
- if (isStart) {
262
- return endValue;
263
- } else if (isEnd) {
264
- return startValue;
265
- }
266
- return '';
267
- };
268
- const retrieveColor = _ref2 => {
269
- let {
270
- isHighlighted,
271
- theme
272
- } = _ref2;
273
- return css(["background-color:", ";"], isHighlighted && getColor('primaryHue', 600, theme, 0.08));
274
- };
275
- const StyledHighlight = styled.div.attrs({
276
- 'data-garden-id': COMPONENT_ID$1
277
- }).withConfig({
278
- displayName: "StyledHighlight",
279
- componentId: "sc-16vr32x-0"
280
- })(["position:absolute;top:0;left:0;width:100%;height:100%;", " ", " ", ";"], retrieveBorderRadius, retrieveColor, props => retrieveComponentStyles(COMPONENT_ID$1, props));
281
- StyledHighlight.defaultProps = {
282
- theme: DEFAULT_THEME
283
- };
284
-
285
- const retrieveStyledDayColors = _ref => {
286
- let {
287
- isSelected,
288
- isDisabled,
289
- isToday,
290
- isPreviousMonth,
291
- theme
292
- } = _ref;
293
- let backgroundColor = 'inherit';
294
- let color = getColor('primaryHue', 600, theme);
295
- if (isSelected && !isDisabled) {
296
- backgroundColor = getColor('primaryHue', 600, theme);
297
- color = theme.colors.background;
298
- } else if (isDisabled) {
299
- color = getColor('neutralHue', 400, theme);
300
- } else if (isToday) {
301
- color = 'inherit';
302
- } else if (isPreviousMonth) {
303
- color = getColor('neutralHue', 600, theme);
304
- }
305
- return css(["background-color:", ";color:", ";", ""], backgroundColor, color, !isSelected && !isDisabled && `
306
- :hover {
307
- background-color: ${getColor('primaryHue', 600, theme, 0.08)};
308
- color: ${getColor('primaryHue', 800, theme)};
309
- }
310
-
311
- :active {
312
- background-color: ${getColor('primaryHue', 600, theme, 0.2)};
313
- color: ${getColor('primaryHue', 800, theme)};
314
- }
315
- `);
316
- };
317
- const COMPONENT_ID = 'datepickers.day';
318
- const StyledDay = styled.div.attrs(props => ({
319
- 'data-garden-id': COMPONENT_ID,
320
- 'aria-disabled': props.isDisabled ? 'true' : 'false'
321
- })).withConfig({
322
- displayName: "StyledDay",
323
- componentId: "sc-v42uk5-0"
324
- })(["display:flex;position:absolute;align-items:center;justify-content:center;border-radius:50%;cursor:", ";width:100%;height:100%;font-size:", ";font-weight:", ";", " ", ";"], props => props.isDisabled ? 'inherit' : 'pointer', props => props.isCompact ? props.theme.fontSizes.sm : props.theme.fontSizes.md, props => props.isToday && !props.isDisabled ? props.theme.fontWeights.semibold : 'inherit', retrieveStyledDayColors, props => retrieveComponentStyles(COMPONENT_ID, props));
325
- StyledDay.defaultProps = {
326
- theme: DEFAULT_THEME
327
- };
328
-
329
- const DatepickerContext = createContext(undefined);
330
- const useDatepickerContext$1 = () => {
331
- return useContext(DatepickerContext);
332
- };
333
-
334
- const REGION_MAPPINGS = {
335
- 'ar-DZ': 0,
336
- 'ar-SA': 0,
337
- 'en-CA': 0,
338
- 'en-GB': 1,
339
- 'en-US': 0,
340
- 'fa-IR': 0,
341
- 'fr-CH': 1,
342
- 'nl-BE': 1,
343
- 'pt-BR': 0,
344
- 'zh-CN': 1,
345
- 'zh-TW': 1
346
- };
347
- const LANGUAGE_MAPPINGS = {
348
- af: 0,
349
- ar: 6,
350
- be: 1,
351
- bg: 1,
352
- bn: 0,
353
- ca: 1,
354
- cs: 1,
355
- da: 1,
356
- de: 1,
357
- el: 1,
358
- en: 0,
359
- eo: 1,
360
- es: 1,
361
- et: 1,
362
- fa: 0,
363
- fi: 1,
364
- fil: 0,
365
- fr: 1,
366
- gl: 1,
367
- he: 0,
368
- hr: 1,
369
- hu: 1,
370
- id: 1,
371
- is: 1,
372
- it: 1,
373
- ja: 1,
374
- ka: 1,
375
- ko: 0,
376
- lt: 1,
377
- lv: 1,
378
- mk: 1,
379
- ms: 1,
380
- nb: 1,
381
- nl: 1,
382
- nn: 1,
383
- pl: 1,
384
- pt: 0,
385
- ro: 1,
386
- ru: 1,
387
- sk: 1,
388
- sl: 1,
389
- sr: 1,
390
- sv: 1,
391
- th: 1,
392
- tr: 1,
393
- ug: 0,
394
- uk: 1,
395
- vi: 1,
396
- zh: 1
397
- };
398
- function getStartOfWeek(locale) {
399
- if (!locale) {
400
- return 0;
401
- }
402
- for (const region in REGION_MAPPINGS) {
403
- if (locale.startsWith(region)) {
404
- return REGION_MAPPINGS[region];
405
- }
406
- }
407
- for (const language in LANGUAGE_MAPPINGS) {
408
- if (locale.startsWith(language)) {
409
- return LANGUAGE_MAPPINGS[language];
410
- }
411
- }
412
- return 0;
413
- }
414
-
415
- var _path$1;
416
- function _extends$2() { _extends$2 = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$2.apply(this, arguments); }
417
- var SvgChevronLeftStroke = function SvgChevronLeftStroke(props) {
418
- return /*#__PURE__*/React.createElement("svg", _extends$2({
419
- xmlns: "http://www.w3.org/2000/svg",
420
- width: 16,
421
- height: 16,
422
- focusable: "false",
423
- viewBox: "0 0 16 16",
424
- "aria-hidden": "true"
425
- }, props), _path$1 || (_path$1 = /*#__PURE__*/React.createElement("path", {
426
- fill: "currentColor",
427
- d: "M10.39 12.688a.5.5 0 01-.718.69l-.062-.066-4-5a.5.5 0 01-.054-.542l.054-.082 4-5a.5.5 0 01.83.55l-.05.074L6.641 8l3.75 4.688z"
428
- })));
429
- };
430
-
431
- var _path;
432
- function _extends$1() { _extends$1 = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$1.apply(this, arguments); }
433
- var SvgChevronRightStroke = function SvgChevronRightStroke(props) {
434
- return /*#__PURE__*/React.createElement("svg", _extends$1({
435
- xmlns: "http://www.w3.org/2000/svg",
436
- width: 16,
437
- height: 16,
438
- focusable: "false",
439
- viewBox: "0 0 16 16",
440
- "aria-hidden": "true"
441
- }, props), _path || (_path = /*#__PURE__*/React.createElement("path", {
442
- fill: "currentColor",
443
- d: "M5.61 3.312a.5.5 0 01.718-.69l.062.066 4 5a.5.5 0 01.054.542l-.054.082-4 5a.5.5 0 01-.83-.55l.05-.074L9.359 8l-3.75-4.688z"
444
- })));
445
- };
446
-
447
- const MonthSelector = _ref => {
448
- let {
449
- locale,
450
- isCompact
451
- } = _ref;
452
- const {
453
- state,
454
- dispatch
455
- } = useDatepickerContext$1();
456
- const headerLabelFormatter = useCallback(date => {
457
- const formatter = new Intl.DateTimeFormat(locale, {
458
- month: 'long',
459
- year: 'numeric'
460
- });
461
- return formatter.format(date);
462
- }, [locale]);
463
- return React__default.createElement(StyledHeader, {
464
- isCompact: isCompact
465
- }, React__default.createElement(StyledHeaderPaddle, {
466
- isCompact: isCompact,
467
- onClick: () => {
468
- dispatch({
469
- type: 'PREVIEW_PREVIOUS_MONTH'
470
- });
471
- }
472
- }, React__default.createElement(SvgChevronLeftStroke, null)), React__default.createElement(StyledHeaderLabel, {
473
- isCompact: isCompact
474
- }, headerLabelFormatter(state.previewDate)), React__default.createElement(StyledHeaderPaddle, {
475
- isCompact: isCompact,
476
- onClick: () => {
477
- dispatch({
478
- type: 'PREVIEW_NEXT_MONTH'
479
- });
480
- }
481
- }, React__default.createElement(SvgChevronRightStroke, null)));
482
- };
483
-
484
- const Calendar$1 = forwardRef((_ref, ref) => {
485
- let {
486
- value,
487
- minValue,
488
- maxValue,
489
- isCompact,
490
- locale,
491
- weekStartsOn
492
- } = _ref;
493
- const {
494
- state,
495
- dispatch
496
- } = useDatepickerContext$1();
497
- const preferredWeekStartsOn = weekStartsOn || getStartOfWeek(locale);
498
- const monthStartDate = startOfMonth(state.previewDate);
499
- const monthEndDate = endOfMonth(monthStartDate);
500
- const startDate = startOfWeek(monthStartDate, {
501
- weekStartsOn: preferredWeekStartsOn
502
- });
503
- const endDate = endOfWeek(monthEndDate, {
504
- weekStartsOn: preferredWeekStartsOn
505
- });
506
- const dayLabelFormatter = useCallback(date => {
507
- const formatter = new Intl.DateTimeFormat(locale, {
508
- weekday: 'short'
509
- });
510
- return formatter.format(date);
511
- }, [locale]);
512
- const dayLabels = eachDayOfInterval({
513
- start: startDate,
514
- end: addDays(startDate, 6)
515
- }).map(date => {
516
- const formattedDayLabel = dayLabelFormatter(date);
517
- return React__default.createElement(StyledCalendarItem, {
518
- key: `day-label-${formattedDayLabel}`,
519
- isCompact: isCompact
520
- }, React__default.createElement(StyledDayLabel, {
521
- isCompact: isCompact
522
- }, formattedDayLabel));
523
- });
524
- const items = eachDayOfInterval({
525
- start: startDate,
526
- end: endDate
527
- }).map((date, itemsIndex) => {
528
- const formattedDayLabel = getDate(date);
529
- const isCurrentDate = isToday(date);
530
- const isPreviousMonth = !isSameMonth(date, state.previewDate);
531
- const isSelected = value && isSameDay(date, value);
532
- let isDisabled = false;
533
- if (minValue !== undefined) {
534
- isDisabled = isBefore(date, minValue) && !isSameDay(date, minValue);
535
- }
536
- if (maxValue !== undefined) {
537
- isDisabled = isDisabled || isAfter(date, maxValue) && !isSameDay(date, maxValue);
538
- }
539
- return React__default.createElement(StyledCalendarItem, {
540
- key: `day-${itemsIndex}`,
541
- isCompact: isCompact
542
- }, React__default.createElement(StyledDay, {
543
- isToday: isCurrentDate,
544
- isPreviousMonth: isPreviousMonth,
545
- isSelected: isSelected,
546
- isDisabled: isDisabled,
547
- isCompact: isCompact,
548
- onClick: () => {
549
- if (!isDisabled) {
550
- dispatch({
551
- type: 'SELECT_DATE',
552
- value: date
553
- });
554
- }
555
- }
556
- }, formattedDayLabel));
557
- });
558
- return React__default.createElement(StyledDatepicker, {
559
- ref: ref,
560
- isCompact: isCompact,
561
- onMouseDown: e => {
562
- e.preventDefault();
563
- }
564
- }, React__default.createElement(MonthSelector, {
565
- locale: locale,
566
- isCompact: isCompact
567
- }), React__default.createElement(StyledCalendar, {
568
- isCompact: isCompact
569
- }, dayLabels, items));
570
- });
571
- Calendar$1.displayName = 'Calendar';
572
-
573
- function parseInputValue$1(_ref) {
574
- let {
575
- inputValue,
576
- customParseDate
577
- } = _ref;
578
- if (customParseDate) {
579
- return customParseDate(inputValue);
580
- }
581
- const MINIMUM_DATE = new Date(1001, 0, 0);
582
- let tryParseDate = parse(inputValue, 'P', new Date());
583
- if (isValid(tryParseDate) && !isBefore(tryParseDate, MINIMUM_DATE)) {
584
- return tryParseDate;
585
- }
586
- tryParseDate = parse(inputValue, 'PP', new Date());
587
- if (isValid(tryParseDate) && !isBefore(tryParseDate, MINIMUM_DATE)) {
588
- return tryParseDate;
589
- }
590
- tryParseDate = parse(inputValue, 'PPP', new Date());
591
- if (isValid(tryParseDate) && !isBefore(tryParseDate, MINIMUM_DATE)) {
592
- return tryParseDate;
593
- }
594
- return new Date(NaN);
595
- }
596
- function formatInputValue(_ref2) {
597
- let {
598
- date,
599
- locale,
600
- formatDate
601
- } = _ref2;
602
- if (!date) {
603
- return '';
604
- }
605
- if (formatDate) {
606
- return formatDate(date);
607
- }
608
- return new Intl.DateTimeFormat(locale, {
609
- month: 'long',
610
- day: 'numeric',
611
- year: 'numeric'
612
- }).format(date);
613
- }
614
- const datepickerReducer = _ref3 => {
615
- let {
616
- value,
617
- formatDate,
618
- locale,
619
- customParseDate,
620
- onChange
621
- } = _ref3;
622
- return (state, action) => {
623
- switch (action.type) {
624
- case 'OPEN':
625
- return {
626
- ...state,
627
- isOpen: true,
628
- previewDate: value || new Date()
629
- };
630
- case 'CLOSE':
631
- {
632
- const inputValue = formatInputValue({
633
- date: value,
634
- locale,
635
- formatDate
636
- });
637
- return {
638
- ...state,
639
- isOpen: false,
640
- inputValue
641
- };
642
- }
643
- case 'PREVIEW_NEXT_MONTH':
644
- {
645
- const previewDate = addMonths(state.previewDate, 1);
646
- return {
647
- ...state,
648
- previewDate
649
- };
650
- }
651
- case 'PREVIEW_PREVIOUS_MONTH':
652
- {
653
- const previewDate = subMonths(state.previewDate, 1);
654
- return {
655
- ...state,
656
- previewDate
657
- };
658
- }
659
- case 'MANUALLY_UPDATE_INPUT':
660
- {
661
- const inputValue = action.value;
662
- const currentDate = parseInputValue$1({
663
- inputValue,
664
- customParseDate
665
- });
666
- if (onChange && currentDate && isValid(currentDate) && !isSameDay(value, currentDate)) {
667
- onChange(currentDate);
668
- }
669
- return {
670
- ...state,
671
- isOpen: true,
672
- inputValue
673
- };
674
- }
675
- case 'CONTROLLED_VALUE_CHANGE':
676
- {
677
- const previewDate = action.value || new Date();
678
- const inputValue = formatInputValue({
679
- date: action.value,
680
- locale,
681
- formatDate
682
- });
683
- return {
684
- ...state,
685
- previewDate,
686
- inputValue
687
- };
688
- }
689
- case 'CONTROLLED_LOCALE_CHANGE':
690
- {
691
- const inputValue = formatInputValue({
692
- date: value,
693
- locale,
694
- formatDate
695
- });
696
- return {
697
- ...state,
698
- inputValue
699
- };
700
- }
701
- case 'SELECT_DATE':
702
- {
703
- const inputValue = formatInputValue({
704
- date: action.value,
705
- locale,
706
- formatDate
707
- });
708
- if (onChange && action.value && isValid(action.value) && !isSameDay(value, action.value)) {
709
- onChange(action.value);
710
- }
711
- return {
712
- ...state,
713
- isOpen: false,
714
- inputValue
715
- };
716
- }
717
- default:
718
- throw new Error();
719
- }
720
- };
721
- };
722
- function retrieveInitialState$1(initialProps) {
723
- let previewDate = initialProps.value;
724
- if (previewDate === undefined || !isValid(previewDate)) {
725
- previewDate = new Date();
726
- }
727
- let inputValue = '';
728
- if (initialProps.value !== undefined) {
729
- if (initialProps.formatDate) {
730
- inputValue = initialProps.formatDate(initialProps.value);
731
- } else {
732
- inputValue = new Intl.DateTimeFormat(initialProps.locale, {
733
- month: 'long',
734
- day: 'numeric',
735
- year: 'numeric'
736
- }).format(previewDate);
737
- }
738
- }
739
- return {
740
- isOpen: false,
741
- previewDate,
742
- inputValue
743
- };
744
- }
745
-
746
- const Datepicker = forwardRef((props, calendarRef) => {
747
- const {
748
- children,
749
- placement,
750
- popperModifiers,
751
- eventsEnabled,
752
- zIndex,
753
- isAnimated,
754
- refKey,
755
- value,
756
- isCompact,
757
- onChange,
758
- formatDate,
759
- minValue,
760
- maxValue,
761
- locale,
762
- weekStartsOn,
763
- customParseDate,
764
- ...menuProps
765
- } = props;
766
- const theme = useContext(ThemeContext);
767
- const memoizedReducer = useCallback(datepickerReducer({
768
- value,
769
- formatDate,
770
- locale,
771
- customParseDate,
772
- onChange
773
- }), [value, formatDate, locale, onChange, customParseDate]);
774
- const [state, dispatch] = useReducer(memoizedReducer, retrieveInitialState$1(props));
775
- const scheduleUpdateRef = useRef(undefined);
776
- const inputRef = useRef(null);
777
- const isInputMouseDownRef = useRef(false);
778
- useEffect(() => {
779
- if (state.isOpen && scheduleUpdateRef.current) {
780
- scheduleUpdateRef.current();
781
- }
782
- });
783
- const [isVisible, setIsVisible] = useState(state.isOpen);
784
- useEffect(() => {
785
- let timeout;
786
- if (state.isOpen) {
787
- setIsVisible(true);
788
- } else if (isAnimated) {
789
- timeout = setTimeout(() => setIsVisible(false), 200);
790
- } else {
791
- setIsVisible(false);
792
- }
793
- return () => clearTimeout(timeout);
794
- }, [state.isOpen, isAnimated]);
795
- useEffect(() => {
796
- dispatch({
797
- type: 'CONTROLLED_VALUE_CHANGE',
798
- value
799
- });
800
- }, [value]);
801
- useEffect(() => {
802
- dispatch({
803
- type: 'CONTROLLED_LOCALE_CHANGE'
804
- });
805
- }, [locale]);
806
- const popperPlacement = theme.rtl ? getRtlPopperPlacement(placement) : getPopperPlacement(placement);
807
- const contextValue = useMemo(() => ({
808
- state,
809
- dispatch
810
- }), [state, dispatch]);
811
- return React__default.createElement(DatepickerContext.Provider, {
812
- value: contextValue
813
- }, React__default.createElement(Manager, null, React__default.createElement(Reference, null, _ref => {
814
- let {
815
- ref
816
- } = _ref;
817
- const childElement = React__default.Children.only(children);
818
- return React__default.cloneElement(childElement, {
819
- [refKey]: refValue => {
820
- ref(refValue);
821
- inputRef.current = refValue;
822
- },
823
- onMouseDown: composeEventHandlers(childElement.props.onMouseDown, () => {
824
- isInputMouseDownRef.current = true;
825
- }),
826
- onMouseUp: composeEventHandlers(childElement.props.onMouseUp, () => {
827
- setTimeout(() => {
828
- isInputMouseDownRef.current = false;
829
- }, 0);
830
- }),
831
- onClick: composeEventHandlers(childElement.props.onClick, () => {
832
- if (isInputMouseDownRef.current && !state.isOpen) {
833
- dispatch({
834
- type: 'OPEN'
835
- });
836
- }
837
- }),
838
- onBlur: composeEventHandlers(childElement.props.onBlur, () => {
839
- dispatch({
840
- type: 'CLOSE'
841
- });
842
- }),
843
- onChange: composeEventHandlers(childElement.props.onChange, e => {
844
- dispatch({
845
- type: 'MANUALLY_UPDATE_INPUT',
846
- value: e.target.value
847
- });
848
- }),
849
- onKeyDown: composeEventHandlers(childElement.props.onKeyDown, e => {
850
- switch (e.key) {
851
- case KEYS.ESCAPE:
852
- case KEYS.ENTER:
853
- dispatch({
854
- type: 'CLOSE'
855
- });
856
- break;
857
- case KEYS.UP:
858
- case KEYS.DOWN:
859
- case KEYS.SPACE:
860
- dispatch({
861
- type: 'OPEN'
862
- });
863
- break;
864
- }
865
- }),
866
- autoComplete: 'off',
867
- value: state.inputValue
868
- });
869
- }), React__default.createElement(Popper, {
870
- placement: popperPlacement,
871
- modifiers: popperModifiers
872
- ,
873
- eventsEnabled: state.isOpen && eventsEnabled
874
- }, _ref2 => {
875
- let {
876
- ref,
877
- style,
878
- scheduleUpdate,
879
- placement: currentPlacement
880
- } = _ref2;
881
- scheduleUpdateRef.current = scheduleUpdate;
882
- return React__default.createElement(StyledMenuWrapper, {
883
- ref: ref,
884
- style: style,
885
- isHidden: !state.isOpen,
886
- isAnimated: isAnimated && (state.isOpen || isVisible),
887
- placement: currentPlacement,
888
- zIndex: zIndex
889
- }, (state.isOpen || isVisible) && React__default.createElement(StyledMenu, menuProps, React__default.createElement(Calendar$1, {
890
- ref: calendarRef,
891
- isCompact: isCompact,
892
- value: value,
893
- minValue: minValue,
894
- maxValue: maxValue,
895
- locale: locale,
896
- weekStartsOn: weekStartsOn
897
- })));
898
- })));
899
- });
900
- Datepicker.displayName = 'Datepicker';
901
- Datepicker.propTypes = {
902
- value: PropTypes.any,
903
- onChange: PropTypes.any,
904
- formatDate: PropTypes.func,
905
- locale: PropTypes.any,
906
- weekStartsOn: PropTypes.oneOf(WEEK_STARTS_ON),
907
- minValue: PropTypes.any,
908
- maxValue: PropTypes.any,
909
- isCompact: PropTypes.bool,
910
- customParseDate: PropTypes.any,
911
- refKey: PropTypes.string,
912
- placement: PropTypes.oneOf(PLACEMENT),
913
- popperModifiers: PropTypes.any,
914
- isAnimated: PropTypes.bool,
915
- eventsEnabled: PropTypes.bool,
916
- zIndex: PropTypes.number
917
- };
918
- Datepicker.defaultProps = {
919
- placement: 'bottom-start',
920
- refKey: 'ref',
921
- isAnimated: true,
922
- eventsEnabled: true,
923
- zIndex: 1000,
924
- locale: 'en-US'
925
- };
926
-
927
- function formatValue(_ref) {
928
- let {
929
- value,
930
- locale,
931
- formatDate
932
- } = _ref;
933
- let stringValue = '';
934
- if (value !== undefined && isValid(value)) {
935
- if (formatDate) {
936
- stringValue = formatDate(value);
937
- } else {
938
- stringValue = new Intl.DateTimeFormat(locale, {
939
- month: 'long',
940
- day: 'numeric',
941
- year: 'numeric'
942
- }).format(value);
943
- }
944
- }
945
- return stringValue;
946
- }
947
- function parseInputValue(_ref2) {
948
- let {
949
- inputValue
950
- } = _ref2;
951
- const MINIMUM_DATE = new Date(1001, 0, 0);
952
- let tryParseDate = parse(inputValue || '', 'P', new Date());
953
- if (isValid(tryParseDate) && !isBefore(tryParseDate, MINIMUM_DATE)) {
954
- return tryParseDate;
955
- }
956
- tryParseDate = parse(inputValue || '', 'PP', new Date());
957
- if (isValid(tryParseDate) && !isBefore(tryParseDate, MINIMUM_DATE)) {
958
- return tryParseDate;
959
- }
960
- tryParseDate = parse(inputValue || '', 'PPP', new Date());
961
- if (isValid(tryParseDate) && !isBefore(tryParseDate, MINIMUM_DATE)) {
962
- return tryParseDate;
963
- }
964
- return new Date(NaN);
965
- }
966
- const datepickerRangeReducer = _ref3 => {
967
- let {
968
- startValue,
969
- endValue,
970
- locale,
971
- formatDate,
972
- customParseDate
973
- } = _ref3;
974
- return (state, action) => {
975
- switch (action.type) {
976
- case 'START_FOCUS':
977
- {
978
- let previewDate = state.previewDate;
979
- if (startValue) {
980
- if (compareAsc(startValue, startOfMonth(state.previewDate)) === 1 && compareAsc(startValue, addMonths(endOfMonth(state.previewDate), 1)) === -1) {
981
- previewDate = state.previewDate;
982
- } else {
983
- previewDate = startOfMonth(startValue);
984
- }
985
- }
986
- return {
987
- ...state,
988
- previewDate,
989
- isStartFocused: true,
990
- isEndFocused: false
991
- };
992
- }
993
- case 'END_FOCUS':
994
- {
995
- let previewDate = state.previewDate;
996
- if (endValue) {
997
- if (compareAsc(endValue, startOfMonth(state.previewDate)) === 1 && compareAsc(endValue, addMonths(endOfMonth(state.previewDate), 1)) === -1) {
998
- previewDate = state.previewDate;
999
- } else {
1000
- previewDate = startOfMonth(endValue);
1001
- }
1002
- }
1003
- return {
1004
- ...state,
1005
- previewDate,
1006
- isEndFocused: true,
1007
- isStartFocused: false
1008
- };
1009
- }
1010
- case 'START_BLUR':
1011
- {
1012
- let parsedDate;
1013
- if (customParseDate) {
1014
- parsedDate = customParseDate(state.startInputValue);
1015
- } else {
1016
- parsedDate = parseInputValue({
1017
- inputValue: state.startInputValue
1018
- });
1019
- }
1020
- const startInputValue = formatValue({
1021
- value: parsedDate,
1022
- locale,
1023
- formatDate
1024
- });
1025
- return {
1026
- ...state,
1027
- startInputValue: startInputValue || formatValue({
1028
- value: startValue,
1029
- locale,
1030
- formatDate
1031
- }),
1032
- isStartFocused: false
1033
- };
1034
- }
1035
- case 'END_BLUR':
1036
- {
1037
- let parsedDate;
1038
- if (customParseDate) {
1039
- parsedDate = customParseDate(state.endInputValue);
1040
- } else {
1041
- parsedDate = parseInputValue({
1042
- inputValue: state.endInputValue
1043
- });
1044
- }
1045
- const endInputValue = formatValue({
1046
- value: parsedDate,
1047
- locale,
1048
- formatDate
1049
- }) || formatValue({
1050
- value: endValue,
1051
- locale,
1052
- formatDate
1053
- });
1054
- return {
1055
- ...state,
1056
- endInputValue,
1057
- isEndFocused: false
1058
- };
1059
- }
1060
- case 'CONTROLLED_START_VALUE_CHANGE':
1061
- {
1062
- const startInputValue = formatValue({
1063
- value: action.value,
1064
- locale,
1065
- formatDate
1066
- });
1067
- let previewDate = state.previewDate;
1068
- if (action.value) {
1069
- if (compareAsc(action.value, startOfMonth(state.previewDate)) === 1 && compareAsc(action.value, addMonths(endOfMonth(state.previewDate), 1)) === -1) {
1070
- previewDate = state.previewDate;
1071
- } else {
1072
- previewDate = startOfMonth(action.value);
1073
- }
1074
- }
1075
- return {
1076
- ...state,
1077
- startInputValue,
1078
- hoverDate: undefined,
1079
- previewDate
1080
- };
1081
- }
1082
- case 'CONTROLLED_END_VALUE_CHANGE':
1083
- {
1084
- const endInputValue = formatValue({
1085
- value: action.value,
1086
- locale,
1087
- formatDate
1088
- });
1089
- let previewDate = state.previewDate;
1090
- if (action.value) {
1091
- if (compareAsc(action.value, startOfMonth(state.previewDate)) === 1 && compareAsc(action.value, addMonths(endOfMonth(state.previewDate), 1)) === -1) {
1092
- previewDate = state.previewDate;
1093
- } else {
1094
- previewDate = startOfMonth(action.value);
1095
- }
1096
- }
1097
- return {
1098
- ...state,
1099
- endInputValue,
1100
- hoverDate: undefined,
1101
- previewDate
1102
- };
1103
- }
1104
- case 'CLICK_DATE':
1105
- if (state.isStartFocused) {
1106
- if (endValue !== undefined && (isBefore(action.value, endValue) || isSameDay(action.value, endValue))) {
1107
- return {
1108
- ...state,
1109
- startInputValue: formatValue({
1110
- value: action.value
1111
- })
1112
- };
1113
- }
1114
- return {
1115
- ...state,
1116
- startInputValue: formatValue({
1117
- value: action.value
1118
- }),
1119
- endInputValue: undefined
1120
- };
1121
- } else if (state.isEndFocused) {
1122
- if (startValue !== undefined && (isAfter(action.value, startValue) || isSameDay(action.value, startValue))) {
1123
- return {
1124
- ...state,
1125
- endInputValue: formatValue({
1126
- value: action.value
1127
- })
1128
- };
1129
- }
1130
- return {
1131
- ...state,
1132
- startInputValue: formatValue({
1133
- value: action.value
1134
- })
1135
- };
1136
- } else if (startValue === undefined) {
1137
- return {
1138
- ...state,
1139
- startInputValue: formatValue({
1140
- value: action.value
1141
- }),
1142
- endInputValue: undefined
1143
- };
1144
- } else if (endValue === undefined) {
1145
- if (isBefore(action.value, startValue)) {
1146
- return {
1147
- ...state,
1148
- startInputValue: formatValue({
1149
- value: action.value
1150
- }),
1151
- endInputValue: undefined
1152
- };
1153
- }
1154
- return {
1155
- ...state,
1156
- endInputValue: formatValue({
1157
- value: action.value
1158
- })
1159
- };
1160
- }
1161
- return state;
1162
- case 'START_INPUT_ONCHANGE':
1163
- {
1164
- return {
1165
- ...state,
1166
- startInputValue: action.value
1167
- };
1168
- }
1169
- case 'END_INPUT_ONCHANGE':
1170
- {
1171
- return {
1172
- ...state,
1173
- endInputValue: action.value
1174
- };
1175
- }
1176
- case 'HOVER_DATE':
1177
- return {
1178
- ...state,
1179
- hoverDate: action.value
1180
- };
1181
- case 'PREVIEW_NEXT_MONTH':
1182
- {
1183
- const previewDate = addMonths(state.previewDate, 1);
1184
- return {
1185
- ...state,
1186
- previewDate,
1187
- hoverDate: undefined
1188
- };
1189
- }
1190
- case 'PREVIEW_PREVIOUS_MONTH':
1191
- {
1192
- const previewDate = subMonths(state.previewDate, 1);
1193
- return {
1194
- ...state,
1195
- previewDate,
1196
- hoverDate: undefined
1197
- };
1198
- }
1199
- default:
1200
- throw new Error();
1201
- }
1202
- };
1203
- };
1204
- function retrieveInitialState(initialProps) {
1205
- let previewDate = initialProps.startValue;
1206
- if (previewDate === undefined || !isValid(previewDate)) {
1207
- previewDate = new Date();
1208
- }
1209
- const startInputValue = formatValue({
1210
- value: initialProps.startValue,
1211
- locale: initialProps.locale,
1212
- formatDate: initialProps.formatDate
1213
- });
1214
- const endInputValue = formatValue({
1215
- value: initialProps.endValue,
1216
- locale: initialProps.locale,
1217
- formatDate: initialProps.formatDate
1218
- });
1219
- return {
1220
- previewDate,
1221
- startInputValue,
1222
- endInputValue,
1223
- isStartFocused: false,
1224
- isEndFocused: false
1225
- };
1226
- }
1227
-
1228
- const DatepickerRangeContext = createContext(undefined);
1229
- const useDatepickerContext = () => {
1230
- return useContext(DatepickerRangeContext);
1231
- };
1232
-
1233
- const Start = props => {
1234
- const {
1235
- state,
1236
- dispatch,
1237
- onChange,
1238
- startValue,
1239
- endValue,
1240
- startInputRef,
1241
- customParseDate
1242
- } = useDatepickerContext();
1243
- const onChangeCallback = useCallback(e => {
1244
- dispatch({
1245
- type: 'START_INPUT_ONCHANGE',
1246
- value: e.target.value
1247
- });
1248
- props.children.props.onChange && props.children.props.onChange(e);
1249
- }, [dispatch, props.children]);
1250
- const onFocusCallback = useCallback(e => {
1251
- dispatch({
1252
- type: 'START_FOCUS'
1253
- });
1254
- props.children.props.onFocus && props.children.props.onFocus(e);
1255
- }, [dispatch, props.children]);
1256
- const handleBlur = useCallback(() => {
1257
- let parsedDate;
1258
- if (customParseDate) {
1259
- parsedDate = customParseDate(state.startInputValue);
1260
- } else {
1261
- parsedDate = parseInputValue({
1262
- inputValue: state.startInputValue
1263
- });
1264
- }
1265
- dispatch({
1266
- type: 'START_BLUR'
1267
- });
1268
- if (parsedDate && isValid(parsedDate) && !isSameDay(parsedDate, startValue)) {
1269
- onChange && onChange({
1270
- startValue: parsedDate,
1271
- endValue
1272
- });
1273
- }
1274
- }, [dispatch, onChange, startValue, endValue, customParseDate, state.startInputValue]);
1275
- const onKeyDownCallback = useCallback(e => {
1276
- if (e.key === KEYS.ENTER) {
1277
- e.preventDefault();
1278
- handleBlur();
1279
- }
1280
- props.children.props.onKeyDown && props.children.props.onKeyDown(e);
1281
- }, [handleBlur, props.children]);
1282
- const onBlurCallback = useCallback(e => {
1283
- handleBlur();
1284
- props.children.props.onBlur && props.children.props.onBlur(e);
1285
- }, [handleBlur, props.children]);
1286
- const childElement = React__default.Children.only(props.children);
1287
- return React__default.cloneElement(childElement, {
1288
- value: state.startInputValue || '',
1289
- ref: startInputRef,
1290
- onChange: composeEventHandlers(childElement.props.onChange, onChangeCallback),
1291
- onFocus: composeEventHandlers(childElement.props.onFocus, onFocusCallback),
1292
- onKeyDown: composeEventHandlers(childElement.props.onKeyDown, onKeyDownCallback),
1293
- onBlur: composeEventHandlers(childElement.props.onBlur, onBlurCallback)
1294
- });
1295
- };
1296
- Start.displayName = 'DatepickerRange.Start';
1297
-
1298
- const End = props => {
1299
- const {
1300
- state,
1301
- dispatch,
1302
- onChange,
1303
- startValue,
1304
- endValue,
1305
- endInputRef,
1306
- customParseDate
1307
- } = useDatepickerContext();
1308
- const onChangeCallback = useCallback(e => {
1309
- dispatch({
1310
- type: 'END_INPUT_ONCHANGE',
1311
- value: e.target.value
1312
- });
1313
- props.children.props.onChange && props.children.props.onChange(e);
1314
- }, [dispatch, props.children]);
1315
- const onFocusCallback = useCallback(e => {
1316
- dispatch({
1317
- type: 'END_FOCUS'
1318
- });
1319
- props.children.props.onFocus && props.children.props.onFocus(e);
1320
- }, [dispatch, props.children]);
1321
- const handleBlur = useCallback(() => {
1322
- dispatch({
1323
- type: 'END_BLUR'
1324
- });
1325
- let parsedDate;
1326
- if (customParseDate) {
1327
- parsedDate = customParseDate(state.endInputValue);
1328
- } else {
1329
- parsedDate = parseInputValue({
1330
- inputValue: state.endInputValue
1331
- });
1332
- }
1333
- if (onChange && parsedDate && isValid(parsedDate) && !isSameDay(parsedDate, endValue)) {
1334
- onChange && onChange({
1335
- startValue,
1336
- endValue: parsedDate
1337
- });
1338
- }
1339
- }, [dispatch, onChange, startValue, endValue, customParseDate, state.endInputValue]);
1340
- const onKeydownCallback = useCallback(e => {
1341
- if (e.key === KEYS.ENTER) {
1342
- handleBlur();
1343
- e.preventDefault();
1344
- }
1345
- props.children.props.onKeyDown && props.children.props.onKeyDown(e);
1346
- }, [handleBlur, props.children]);
1347
- const onBlurCallback = useCallback(e => {
1348
- handleBlur();
1349
- props.children.props.onBlur && props.children.props.onBlur(e);
1350
- }, [handleBlur, props.children]);
1351
- const childElement = React__default.Children.only(props.children);
1352
- return React__default.cloneElement(childElement, {
1353
- value: state.endInputValue || '',
1354
- ref: endInputRef,
1355
- onChange: composeEventHandlers(childElement.props.onChange, onChangeCallback),
1356
- onFocus: composeEventHandlers(childElement.props.onFocus, onFocusCallback),
1357
- onKeyDown: composeEventHandlers(childElement.props.onKeyDown, onKeydownCallback),
1358
- onBlur: composeEventHandlers(childElement.props.onBlur, onBlurCallback)
1359
- });
1360
- };
1361
- End.displayName = 'DatepickerRange.End';
1362
-
1363
- function _extends() {
1364
- _extends = Object.assign ? Object.assign.bind() : function (target) {
1365
- for (var i = 1; i < arguments.length; i++) {
1366
- var source = arguments[i];
1367
- for (var key in source) {
1368
- if (Object.prototype.hasOwnProperty.call(source, key)) {
1369
- target[key] = source[key];
1370
- }
1371
- }
1372
- }
1373
- return target;
1374
- };
1375
- return _extends.apply(this, arguments);
1376
- }
1377
-
1378
- const Month = forwardRef((_ref, ref) => {
1379
- let {
1380
- displayDate,
1381
- isPreviousHidden,
1382
- isNextHidden
1383
- } = _ref;
1384
- const {
1385
- state,
1386
- dispatch,
1387
- locale,
1388
- weekStartsOn,
1389
- isCompact,
1390
- minValue,
1391
- maxValue,
1392
- startValue,
1393
- endValue,
1394
- onChange
1395
- } = useDatepickerContext();
1396
- const headerLabelFormatter = useCallback(date => {
1397
- const formatter = new Intl.DateTimeFormat(locale, {
1398
- month: 'long',
1399
- year: 'numeric'
1400
- });
1401
- return formatter.format(date);
1402
- }, [locale]);
1403
- const dayLabelFormatter = useCallback(date => {
1404
- const formatter = new Intl.DateTimeFormat(locale, {
1405
- weekday: 'short'
1406
- });
1407
- return formatter.format(date);
1408
- }, [locale]);
1409
- const dayFormatter = useCallback(date => {
1410
- const formatter = new Intl.DateTimeFormat(locale, {
1411
- day: 'numeric'
1412
- });
1413
- return formatter.format(date);
1414
- }, [locale]);
1415
- const preferredWeekStartsOn = weekStartsOn || getStartOfWeek(locale);
1416
- const monthStartDate = startOfMonth(displayDate);
1417
- const monthEndDate = endOfMonth(monthStartDate);
1418
- const startDate = startOfWeek(monthStartDate, {
1419
- weekStartsOn: preferredWeekStartsOn
1420
- });
1421
- const endDate = endOfWeek(monthEndDate, {
1422
- weekStartsOn: preferredWeekStartsOn
1423
- });
1424
- const dayLabels = eachDayOfInterval({
1425
- start: startDate,
1426
- end: addDays(startDate, 6)
1427
- }).map(date => {
1428
- const formattedDayLabel = dayLabelFormatter(date);
1429
- return React__default.createElement(StyledCalendarItem, {
1430
- key: `day-label-${formattedDayLabel}`,
1431
- isCompact: isCompact
1432
- }, React__default.createElement(StyledDayLabel, {
1433
- isCompact: isCompact
1434
- }, formattedDayLabel));
1435
- });
1436
- const items = eachDayOfInterval({
1437
- start: startDate,
1438
- end: endDate
1439
- }).map((date, itemsIndex) => {
1440
- const formattedDayLabel = dayFormatter(date);
1441
- const isCurrentDate = isToday(date);
1442
- const isPreviousMonth = !isSameMonth(date, displayDate);
1443
- if (isPreviousMonth) {
1444
- return React__default.createElement(StyledCalendarItem, {
1445
- key: `day-${itemsIndex}`,
1446
- isCompact: isCompact
1447
- }, React__default.createElement(StyledDay, {
1448
- isCompact: isCompact,
1449
- isPreviousMonth: true,
1450
- isDisabled: true
1451
- }, "\xA0"));
1452
- }
1453
- let isSelected = false;
1454
- if (startValue !== undefined) {
1455
- isSelected = isSameDay(date, startValue);
1456
- }
1457
- if (endValue !== undefined) {
1458
- isSelected = isSelected || isSameDay(date, endValue);
1459
- }
1460
- let isDisabled = false;
1461
- if (minValue !== undefined) {
1462
- isDisabled = isBefore(date, minValue) && !isSameDay(date, minValue);
1463
- }
1464
- if (maxValue !== undefined) {
1465
- isDisabled = isDisabled || isAfter(date, maxValue) && !isSameDay(date, maxValue);
1466
- }
1467
- let isHighlighted = false;
1468
- if (startValue !== undefined && endValue !== undefined) {
1469
- isHighlighted = (isAfter(date, startValue) || isSameDay(date, startValue)) && (isBefore(date, endValue) || isSameDay(date, endValue)) && !isSameDay(startValue, endValue);
1470
- } else if (startValue !== undefined && state.hoverDate !== undefined) {
1471
- isHighlighted = (isAfter(date, startValue) || isSameDay(date, startValue)) && (isBefore(date, state.hoverDate) || isSameDay(date, state.hoverDate));
1472
- }
1473
- const isHighlightStart = isHighlighted && startValue && isSameDay(date, startValue) || false;
1474
- const isHighlightEnd = isHighlighted && endValue && isSameDay(date, endValue) || state.hoverDate && isSameDay(date, state.hoverDate) && !isBefore(date, endValue) || false;
1475
- let isInvalidDateRange = endValue && startValue && compareAsc(endValue, startValue) === -1 || false;
1476
- if (minValue) {
1477
- if (startValue) {
1478
- isInvalidDateRange = isInvalidDateRange || compareAsc(startValue, subDays(minValue, 1)) === -1;
1479
- }
1480
- if (endValue) {
1481
- isInvalidDateRange = isInvalidDateRange || compareAsc(endValue, subDays(minValue, 1)) === -1;
1482
- }
1483
- }
1484
- if (maxValue) {
1485
- if (startValue) {
1486
- isInvalidDateRange = isInvalidDateRange || compareAsc(startValue, maxValue) === 1;
1487
- }
1488
- if (endValue) {
1489
- isInvalidDateRange = isInvalidDateRange || compareAsc(endValue, maxValue) === 1;
1490
- }
1491
- }
1492
- return React__default.createElement(StyledCalendarItem, {
1493
- key: `day-${itemsIndex}`,
1494
- isCompact: isCompact
1495
- }, React__default.createElement(StyledHighlight, {
1496
- isHighlighted: !isInvalidDateRange && isHighlighted && !isDisabled,
1497
- isStart: !isInvalidDateRange && isHighlightStart,
1498
- isEnd: !isInvalidDateRange && isHighlightEnd
1499
- }), React__default.createElement(StyledDay, {
1500
- isToday: isCurrentDate,
1501
- isPreviousMonth: isPreviousMonth,
1502
- isSelected: !isInvalidDateRange && isSelected,
1503
- isDisabled: isDisabled,
1504
- isCompact: isCompact,
1505
- onClick: () => {
1506
- if (!isDisabled) {
1507
- dispatch({
1508
- type: 'CLICK_DATE',
1509
- value: date
1510
- });
1511
- if (onChange) {
1512
- if (state.isStartFocused) {
1513
- if (endValue !== undefined && (isBefore(date, endValue) || isSameDay(date, endValue))) {
1514
- onChange({
1515
- startValue: date,
1516
- endValue
1517
- });
1518
- } else {
1519
- onChange({
1520
- startValue: date,
1521
- endValue: undefined
1522
- });
1523
- }
1524
- } else if (state.isEndFocused) {
1525
- if (startValue !== undefined && (isAfter(date, startValue) || isSameDay(date, startValue))) {
1526
- onChange({
1527
- startValue,
1528
- endValue: date
1529
- });
1530
- } else {
1531
- onChange({
1532
- startValue: date,
1533
- endValue: undefined
1534
- });
1535
- }
1536
- } else if (startValue === undefined) {
1537
- onChange({
1538
- startValue: date,
1539
- endValue: undefined
1540
- });
1541
- } else if (endValue === undefined) {
1542
- if (isBefore(date, startValue)) {
1543
- onChange({
1544
- startValue: date,
1545
- endValue: undefined
1546
- });
1547
- } else {
1548
- onChange({
1549
- startValue,
1550
- endValue: date
1551
- });
1552
- }
1553
- } else {
1554
- onChange({
1555
- startValue: date,
1556
- endValue: undefined
1557
- });
1558
- }
1559
- }
1560
- }
1561
- },
1562
- onMouseEnter: () => {
1563
- if (!isSelected) {
1564
- dispatch({
1565
- type: 'HOVER_DATE',
1566
- value: date
1567
- });
1568
- }
1569
- }
1570
- }, formattedDayLabel));
1571
- });
1572
- return React__default.createElement(StyledDatepicker, {
1573
- ref: ref,
1574
- isCompact: isCompact,
1575
- onMouseDown: e => {
1576
- e.preventDefault();
1577
- }
1578
- }, React__default.createElement(StyledHeader, {
1579
- isCompact: isCompact
1580
- }, React__default.createElement(StyledHeaderPaddle, {
1581
- isCompact: isCompact,
1582
- onClick: () => {
1583
- dispatch({
1584
- type: 'PREVIEW_PREVIOUS_MONTH'
1585
- });
1586
- },
1587
- isHidden: isPreviousHidden
1588
- }, React__default.createElement(SvgChevronLeftStroke, null)), React__default.createElement(StyledHeaderLabel, {
1589
- isCompact: isCompact
1590
- }, headerLabelFormatter(displayDate)), React__default.createElement(StyledHeaderPaddle, {
1591
- isCompact: isCompact,
1592
- isHidden: isNextHidden,
1593
- onClick: () => {
1594
- dispatch({
1595
- type: 'PREVIEW_NEXT_MONTH'
1596
- });
1597
- }
1598
- }, React__default.createElement(SvgChevronRightStroke, null))), React__default.createElement(StyledCalendar, {
1599
- isCompact: isCompact,
1600
- onMouseLeave: () => {
1601
- dispatch({
1602
- type: 'HOVER_DATE',
1603
- value: undefined
1604
- });
1605
- }
1606
- }, dayLabels, items));
1607
- });
1608
- Month.displayName = 'Month';
1609
-
1610
- const Calendar = forwardRef((props, ref) => {
1611
- const {
1612
- state
1613
- } = useDatepickerContext();
1614
- return React__default.createElement(StyledRangeCalendar, _extends({
1615
- ref: ref,
1616
- "data-garden-id": "datepickers.range",
1617
- "data-garden-version": '9.0.0-next.1'
1618
- }, props), React__default.createElement(Month, {
1619
- displayDate: state.previewDate,
1620
- isNextHidden: true
1621
- }), React__default.createElement(Month, {
1622
- displayDate: addMonths(state.previewDate, 1),
1623
- isPreviousHidden: true
1624
- }));
1625
- });
1626
- Calendar.displayName = 'DatepickerRange.Calendar';
1627
-
1628
- const DatepickerRangeComponent = props => {
1629
- const {
1630
- startValue,
1631
- locale,
1632
- weekStartsOn,
1633
- formatDate,
1634
- endValue,
1635
- onChange,
1636
- customParseDate,
1637
- isCompact,
1638
- minValue,
1639
- maxValue,
1640
- children
1641
- } = props;
1642
- const reducer = useCallback(datepickerRangeReducer({
1643
- startValue,
1644
- locale,
1645
- formatDate,
1646
- endValue,
1647
- customParseDate
1648
- }), [startValue, endValue, locale, formatDate, onChange, customParseDate]);
1649
- const [state, dispatch] = useReducer(reducer, retrieveInitialState(props));
1650
- const previousStartValue = useRef(startValue);
1651
- const previousEndValue = useRef(endValue);
1652
- const startInputRef = useRef();
1653
- const endInputRef = useRef();
1654
- useEffect(() => {
1655
- dispatch({
1656
- type: 'CONTROLLED_START_VALUE_CHANGE',
1657
- value: startValue
1658
- });
1659
- if (endInputRef.current && previousStartValue.current !== startValue && startValue !== undefined) {
1660
- endInputRef.current.focus();
1661
- }
1662
- previousStartValue.current = startValue;
1663
- }, [props, startValue]);
1664
- useEffect(() => {
1665
- dispatch({
1666
- type: 'CONTROLLED_END_VALUE_CHANGE',
1667
- value: endValue
1668
- });
1669
- if (startInputRef.current && previousEndValue.current !== endValue && endValue !== undefined) {
1670
- startInputRef.current.focus();
1671
- }
1672
- previousEndValue.current = endValue;
1673
- }, [props, endValue]);
1674
- const value = useMemo(() => ({
1675
- state,
1676
- dispatch,
1677
- isCompact,
1678
- locale,
1679
- weekStartsOn,
1680
- minValue,
1681
- maxValue,
1682
- startValue,
1683
- endValue,
1684
- onChange,
1685
- startInputRef,
1686
- endInputRef,
1687
- customParseDate
1688
- }), [state, dispatch, isCompact, locale, weekStartsOn, minValue, maxValue, startValue, endValue, onChange, startInputRef, endInputRef, customParseDate]);
1689
- return React__default.createElement(DatepickerRangeContext.Provider, {
1690
- value: value
1691
- }, children);
1692
- };
1693
- DatepickerRangeComponent.propTypes = {
1694
- locale: PropTypes.string,
1695
- weekStartsOn: PropTypes.number,
1696
- startValue: PropTypes.instanceOf(Date),
1697
- endValue: PropTypes.instanceOf(Date),
1698
- minValue: PropTypes.instanceOf(Date),
1699
- maxValue: PropTypes.instanceOf(Date),
1700
- onChange: PropTypes.func,
1701
- formatDate: PropTypes.func,
1702
- customParseDate: PropTypes.func,
1703
- isCompact: PropTypes.bool
1704
- };
1705
- DatepickerRangeComponent.defaultProps = {
1706
- locale: 'en-US',
1707
- isCompact: false
1708
- };
1709
- const DatepickerRange = DatepickerRangeComponent;
1710
- DatepickerRange.Calendar = Calendar;
1711
- DatepickerRange.End = End;
1712
- DatepickerRange.Start = Start;
1713
-
1714
- export { Datepicker, DatepickerRange };