@zendeskgarden/react-datepickers 9.0.0-next.7 → 9.0.0-next.8

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