UNPKG

2.16 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5require('./_commonjsHelpers-72d386ba.js');
6var defineProperty$1 = require('./defineProperty-0921a47c.js');
7var dayjs_min = require('./dayjs.min-aa59a48e.js');
8
9function handleDateSelect(_ref) {
10 var date = _ref.date,
11 startDate = _ref.startDate,
12 endDate = _ref.endDate;
13
14 // clicking on start date resets it, so it can be re-picked
15 if (startDate && dayjs_min.dayjs(date).isSame(startDate, 'day')) {
16 return {
17 startDate: null
18 };
19 } // clicking on end date resets it, so it can be re-picked
20
21
22 if (endDate && dayjs_min.dayjs(date).isSame(endDate, 'day')) {
23 return {
24 endDate: null
25 };
26 }
27
28 var isValidStartDate = !endDate || !dayjs_min.dayjs(date).isAfter(endDate);
29 var isValidEndDate = !startDate || !dayjs_min.dayjs(date).isBefore(startDate); // if we have startDate, then `date` is the end date
30
31 var isValidDate = startDate ? isValidEndDate : isValidStartDate; // both dates are selected - if a date is clicked and it's before
32 // start date, it should be the new start date
33 // (this way the start date does not have to be reset before setting)
34 // (the converse case for end date is handled implicitly below, because
35 // after both dates are selected, any selected date will be candidate for
36 // new end date)
37
38 if (startDate && endDate && isValidStartDate && dayjs_min.dayjs(date).isBefore(startDate, 'day')) {
39 return {
40 startDate: date
41 };
42 } // one date is selected, but the clicked date is not corresponding
43 // (start selected, but clicked a date before start, and vice-versa)
44 // in this case, reverse the range
45
46
47 if (!startDate && endDate && dayjs_min.dayjs(date).isAfter(endDate)) {
48 return {
49 startDate: endDate,
50 endDate: date
51 };
52 }
53
54 if (!endDate && startDate && dayjs_min.dayjs(date).isBefore(startDate)) {
55 return {
56 startDate: date,
57 endDate: startDate
58 };
59 }
60
61 if (isValidDate) {
62 return defineProperty$1._defineProperty({}, startDate ? 'endDate' : 'startDate', date);
63 }
64}
65
66exports.handleDateSelect = handleDateSelect;
67//# sourceMappingURL=utils.js.map