@spaced-out/ui-design-system 0.1.109 → 0.1.111
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.
- package/CHANGELOG.md +19 -0
- package/lib/components/DateRangePicker/Calendar.js +4 -2
- package/lib/components/DateRangePicker/Calendar.js.flow +11 -6
- package/lib/components/DateRangePicker/DateRangePicker.js +47 -35
- package/lib/components/DateRangePicker/DateRangePicker.js.flow +71 -70
- package/lib/components/DateRangePicker/DateRangeWrapper.js +35 -8
- package/lib/components/DateRangePicker/DateRangeWrapper.js.flow +98 -74
- package/lib/components/DateRangePicker/index.js.flow +0 -1
- package/lib/components/Input/Input.js +15 -2
- package/lib/components/Input/Input.js.flow +20 -0
- package/lib/components/Menu/Menu.js +6 -5
- package/lib/components/Menu/Menu.js.flow +15 -5
- package/lib/components/Menu/Menu.module.css +1 -1
- package/lib/types/date-range-picker.js +3 -0
- package/lib/types/date-range-picker.js.flow +18 -0
- package/lib/types/index.js +11 -0
- package/lib/types/index.js.flow +1 -0
- package/lib/types/menu.js.flow +2 -0
- package/lib/utils/date-range-picker/date-range-picker.js +306 -0
- package/lib/utils/date-range-picker/date-range-picker.js.flow +335 -0
- package/lib/utils/date-range-picker/index.js +27 -0
- package/lib/utils/date-range-picker/index.js.flow +4 -0
- package/lib/utils/index.js +11 -0
- package/lib/utils/index.js.flow +1 -0
- package/package.json +1 -1
- package/lib/components/DateRangePicker/utils.js +0 -236
- package/lib/components/DateRangePicker/utils.js.flow +0 -245
- /package/lib/{components/DateRangePicker → utils/date-range-picker}/timezones.js +0 -0
- /package/lib/{components/DateRangePicker → utils/date-range-picker}/timezones.js.flow +0 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.wrangleMoment = exports.isStartOfRange = exports.isStartDateEndDateSame = exports.isEndOfRange = exports.inDateRange = exports.getValidDates = exports.getTimezones = exports.getSubtractedDate = exports.getMonthEndDate = exports.getDaysInMonth = exports.getAvailableMonths = exports.getAddedDate = exports.generateAvailableYears = exports.formatIsoDate = exports.checkRangeValidity = exports.addTimezone = exports.WEEKDAYS = exports.NAVIGATION_ACTION = exports.MONTHS = exports.MARKERS = exports.DATE_RANGE_PICKER_ERRORS = void 0;
|
|
7
|
+
var _formatISO = _interopRequireDefault(require("date-fns/formatISO"));
|
|
8
|
+
var _parseISO = _interopRequireDefault(require("date-fns/parseISO"));
|
|
9
|
+
var _invariant = _interopRequireDefault(require("invariant"));
|
|
10
|
+
var _lodash = require("lodash");
|
|
11
|
+
var _momentTimezone = _interopRequireDefault(require("moment-timezone"));
|
|
12
|
+
var _timezones = require("./timezones");
|
|
13
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
+
|
|
15
|
+
// $FlowFixMe - strict types for date-fns
|
|
16
|
+
|
|
17
|
+
// $FlowFixMe - strict types for date-fns
|
|
18
|
+
|
|
19
|
+
// $FlowFixMe[untyped-import]
|
|
20
|
+
|
|
21
|
+
const NAVIGATION_ACTION = Object.freeze({
|
|
22
|
+
NEXT: 'next',
|
|
23
|
+
PREV: 'prev'
|
|
24
|
+
});
|
|
25
|
+
exports.NAVIGATION_ACTION = NAVIGATION_ACTION;
|
|
26
|
+
const MARKERS = Object.freeze({
|
|
27
|
+
DATE_RANGE_START: 'FIRST',
|
|
28
|
+
DATE_RANGE_END: 'SECOND'
|
|
29
|
+
});
|
|
30
|
+
exports.MARKERS = MARKERS;
|
|
31
|
+
const WEEKDAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
|
|
32
|
+
exports.WEEKDAYS = WEEKDAYS;
|
|
33
|
+
const MONTHS = [{
|
|
34
|
+
key: '0',
|
|
35
|
+
label: 'Jan'
|
|
36
|
+
}, {
|
|
37
|
+
key: '1',
|
|
38
|
+
label: 'Feb'
|
|
39
|
+
}, {
|
|
40
|
+
key: '2',
|
|
41
|
+
label: 'Mar'
|
|
42
|
+
}, {
|
|
43
|
+
key: '3',
|
|
44
|
+
label: 'Apr'
|
|
45
|
+
}, {
|
|
46
|
+
key: '4',
|
|
47
|
+
label: 'May'
|
|
48
|
+
}, {
|
|
49
|
+
key: '5',
|
|
50
|
+
label: 'Jun'
|
|
51
|
+
}, {
|
|
52
|
+
key: '6',
|
|
53
|
+
label: 'Jul'
|
|
54
|
+
}, {
|
|
55
|
+
key: '7',
|
|
56
|
+
label: 'Aug'
|
|
57
|
+
}, {
|
|
58
|
+
key: '8',
|
|
59
|
+
label: 'Sep'
|
|
60
|
+
}, {
|
|
61
|
+
key: '9',
|
|
62
|
+
label: 'Oct'
|
|
63
|
+
}, {
|
|
64
|
+
key: '10',
|
|
65
|
+
label: 'Nov'
|
|
66
|
+
}, {
|
|
67
|
+
key: '11',
|
|
68
|
+
label: 'Dec'
|
|
69
|
+
}];
|
|
70
|
+
exports.MONTHS = MONTHS;
|
|
71
|
+
const DATE_RANGE_PICKER_ERRORS = Object.freeze({
|
|
72
|
+
MIN_MAX_INVALID: {
|
|
73
|
+
type: 'MIN_MAX_INVALID',
|
|
74
|
+
description: 'Given minDate and maxDate are invalid.'
|
|
75
|
+
},
|
|
76
|
+
START_DATE_EARLY: {
|
|
77
|
+
type: 'START_DATE_EARLY',
|
|
78
|
+
description: 'Given startDate can not come before minDate.'
|
|
79
|
+
},
|
|
80
|
+
START_DATE_LATE: {
|
|
81
|
+
type: 'START_DATE_LATE',
|
|
82
|
+
description: 'Given startDate can not come after endDate.'
|
|
83
|
+
},
|
|
84
|
+
END_DATE_LATE: {
|
|
85
|
+
type: 'END_DATE_LATE',
|
|
86
|
+
description: 'Given endDate can not come after maxDate.'
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
exports.DATE_RANGE_PICKER_ERRORS = DATE_RANGE_PICKER_ERRORS;
|
|
90
|
+
const checkRangeValidity = (rangeStart, rangeEnd, errorBody, onError) => {
|
|
91
|
+
const isRangeStartValid = (0, _momentTimezone.default)(rangeStart).isValid();
|
|
92
|
+
const isRangeEndValid = isRangeStartValid && (0, _momentTimezone.default)(rangeEnd).isValid();
|
|
93
|
+
const isRangeValid = isRangeEndValid && (0, _momentTimezone.default)(rangeStart).isSameOrBefore(rangeEnd);
|
|
94
|
+
(0, _invariant.default)(isRangeValid, JSON.stringify(errorBody));
|
|
95
|
+
if (!isRangeValid) {
|
|
96
|
+
onError?.(errorBody);
|
|
97
|
+
}
|
|
98
|
+
return isRangeValid;
|
|
99
|
+
};
|
|
100
|
+
exports.checkRangeValidity = checkRangeValidity;
|
|
101
|
+
const wrangleMoment = date => {
|
|
102
|
+
if (date instanceof Date) {
|
|
103
|
+
return date;
|
|
104
|
+
} else if (!date) {
|
|
105
|
+
return new Date();
|
|
106
|
+
}
|
|
107
|
+
return date instanceof _momentTimezone.default ? date.toDate() : (0, _parseISO.default)(date);
|
|
108
|
+
};
|
|
109
|
+
exports.wrangleMoment = wrangleMoment;
|
|
110
|
+
const formatIsoDate = date => (0, _formatISO.default)(wrangleMoment(date), {
|
|
111
|
+
representation: 'date'
|
|
112
|
+
});
|
|
113
|
+
exports.formatIsoDate = formatIsoDate;
|
|
114
|
+
const isStartOfRange = (_ref, date) => {
|
|
115
|
+
let {
|
|
116
|
+
startDate
|
|
117
|
+
} = _ref;
|
|
118
|
+
return Boolean(startDate) && (0, _momentTimezone.default)(date).isSame((0, _momentTimezone.default)(startDate), 'd');
|
|
119
|
+
};
|
|
120
|
+
exports.isStartOfRange = isStartOfRange;
|
|
121
|
+
const isEndOfRange = (_ref2, date) => {
|
|
122
|
+
let {
|
|
123
|
+
endDate
|
|
124
|
+
} = _ref2;
|
|
125
|
+
return Boolean(endDate) && (0, _momentTimezone.default)(date).isSame((0, _momentTimezone.default)(endDate), 'd');
|
|
126
|
+
};
|
|
127
|
+
exports.isEndOfRange = isEndOfRange;
|
|
128
|
+
const inDateRange = (_ref3, date) => {
|
|
129
|
+
let {
|
|
130
|
+
startDate,
|
|
131
|
+
endDate
|
|
132
|
+
} = _ref3;
|
|
133
|
+
if (startDate && endDate) {
|
|
134
|
+
const momentDay = (0, _momentTimezone.default)(date);
|
|
135
|
+
const momentStartDate = (0, _momentTimezone.default)(startDate);
|
|
136
|
+
const momentEndDate = (0, _momentTimezone.default)(endDate);
|
|
137
|
+
return momentDay.isBetween(momentStartDate, momentEndDate, null, '[]');
|
|
138
|
+
}
|
|
139
|
+
return false;
|
|
140
|
+
};
|
|
141
|
+
exports.inDateRange = inDateRange;
|
|
142
|
+
const isStartDateEndDateSame = _ref4 => {
|
|
143
|
+
let {
|
|
144
|
+
startDate,
|
|
145
|
+
endDate
|
|
146
|
+
} = _ref4;
|
|
147
|
+
if (startDate && endDate) {
|
|
148
|
+
return (0, _momentTimezone.default)(startDate).isSame((0, _momentTimezone.default)(endDate), 'd');
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
};
|
|
152
|
+
exports.isStartDateEndDateSame = isStartDateEndDateSame;
|
|
153
|
+
const getMonthAndYear = date => {
|
|
154
|
+
const momentDate = date ? (0, _momentTimezone.default)(date) : (0, _momentTimezone.default)();
|
|
155
|
+
return [momentDate.month(), momentDate.year()];
|
|
156
|
+
};
|
|
157
|
+
const getDaysInMonth = date => {
|
|
158
|
+
const startWeek = (0, _momentTimezone.default)(date).startOf('month').startOf('week');
|
|
159
|
+
const endWeek = (0, _momentTimezone.default)(date).endOf('month').endOf('week');
|
|
160
|
+
const days = [],
|
|
161
|
+
current = startWeek;
|
|
162
|
+
while (current.isBefore(endWeek)) {
|
|
163
|
+
days.push(current.clone().format('YYYY-MM-DD'));
|
|
164
|
+
current.add(1, 'd');
|
|
165
|
+
}
|
|
166
|
+
const daysInChunks = (0, _lodash.chunk)(days, 7);
|
|
167
|
+
|
|
168
|
+
// if total rows in calendar are 5 add one more week to the calendar
|
|
169
|
+
if (daysInChunks.length === 5) {
|
|
170
|
+
const nextWeek = getAddedDate(endWeek, WEEKDAYS.length, 'd');
|
|
171
|
+
const extraDays = [];
|
|
172
|
+
while (current.isSameOrBefore(nextWeek)) {
|
|
173
|
+
extraDays.push(current.clone().format('YYYY-MM-DD'));
|
|
174
|
+
current.add(1, 'd');
|
|
175
|
+
}
|
|
176
|
+
daysInChunks.push(extraDays);
|
|
177
|
+
}
|
|
178
|
+
return daysInChunks;
|
|
179
|
+
};
|
|
180
|
+
exports.getDaysInMonth = getDaysInMonth;
|
|
181
|
+
const getAddedDate = (date, addCount, timeUnit) => formatIsoDate((0, _momentTimezone.default)(date).add(addCount, timeUnit));
|
|
182
|
+
exports.getAddedDate = getAddedDate;
|
|
183
|
+
const getSubtractedDate = (date, subtractCount, timeUnit) => formatIsoDate((0, _momentTimezone.default)(date).subtract(subtractCount, timeUnit));
|
|
184
|
+
exports.getSubtractedDate = getSubtractedDate;
|
|
185
|
+
const getMonthEndDate = date => formatIsoDate((0, _momentTimezone.default)(date).endOf('M'));
|
|
186
|
+
exports.getMonthEndDate = getMonthEndDate;
|
|
187
|
+
const addTimezone = (date, timezone) => _momentTimezone.default.tz(date, timezone).startOf('d').utc().toISOString();
|
|
188
|
+
exports.addTimezone = addTimezone;
|
|
189
|
+
const getTimezones = () => Object.keys(_timezones.TIMEZONES).reduce((menuOptions, key) => {
|
|
190
|
+
menuOptions.push({
|
|
191
|
+
key,
|
|
192
|
+
label: _timezones.TIMEZONES[key]
|
|
193
|
+
});
|
|
194
|
+
return menuOptions;
|
|
195
|
+
}, []);
|
|
196
|
+
exports.getTimezones = getTimezones;
|
|
197
|
+
const generateAvailableYears = _ref5 => {
|
|
198
|
+
let {
|
|
199
|
+
marker,
|
|
200
|
+
minDate,
|
|
201
|
+
maxDate,
|
|
202
|
+
rangeStartMonth,
|
|
203
|
+
rangeEndMonth
|
|
204
|
+
} = _ref5;
|
|
205
|
+
const rangeStartYear = (0, _momentTimezone.default)(rangeStartMonth).year();
|
|
206
|
+
const rangeEndYear = (0, _momentTimezone.default)(rangeEndMonth).year();
|
|
207
|
+
const isWithinRange = year => marker === MARKERS.DATE_RANGE_START ? year <= rangeEndYear : year >= rangeStartYear;
|
|
208
|
+
return (0, _lodash.range)((0, _momentTimezone.default)(minDate).year(), (0, _momentTimezone.default)(maxDate).year() + 1).filter(year => isWithinRange(year)).map(year => ({
|
|
209
|
+
key: year.toString(),
|
|
210
|
+
label: year.toString()
|
|
211
|
+
}));
|
|
212
|
+
};
|
|
213
|
+
exports.generateAvailableYears = generateAvailableYears;
|
|
214
|
+
const getAvailableMonths = _ref6 => {
|
|
215
|
+
let {
|
|
216
|
+
marker,
|
|
217
|
+
minDate,
|
|
218
|
+
maxDate,
|
|
219
|
+
rangeStartMonth,
|
|
220
|
+
rangeEndMonth
|
|
221
|
+
} = _ref6;
|
|
222
|
+
const [rangeStartMonthKey, rangeStartYear] = getMonthAndYear(rangeStartMonth);
|
|
223
|
+
const [rangeEndMonthKey, rangeEndYear] = getMonthAndYear(rangeEndMonth);
|
|
224
|
+
const [minDateMonth, minDateYear] = getMonthAndYear(minDate);
|
|
225
|
+
const [maxDateMonth, maxDateYear] = getMonthAndYear(maxDate);
|
|
226
|
+
return MONTHS.filter(month => {
|
|
227
|
+
const isSameYear = rangeStartYear === rangeEndYear;
|
|
228
|
+
const isFirstAndMinDateYearSame = rangeStartYear === minDateYear;
|
|
229
|
+
const isSecondAndMaxDateYearSame = rangeEndYear === maxDateYear;
|
|
230
|
+
if (marker === MARKERS.DATE_RANGE_START) {
|
|
231
|
+
if (isSameYear && month.key >= rangeEndMonthKey) {
|
|
232
|
+
return false;
|
|
233
|
+
} else if (isFirstAndMinDateYearSame && month.key < minDateMonth) {
|
|
234
|
+
return false;
|
|
235
|
+
} else {
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
if (isSameYear && month.key <= rangeStartMonthKey) {
|
|
240
|
+
return false;
|
|
241
|
+
} else if (isSecondAndMaxDateYearSame && month.key > maxDateMonth) {
|
|
242
|
+
return false;
|
|
243
|
+
} else {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
exports.getAvailableMonths = getAvailableMonths;
|
|
250
|
+
const getValidDates = (selectedDateRange, minDate, maxDate, onError) => {
|
|
251
|
+
const today = formatIsoDate();
|
|
252
|
+
const localTimezone = _momentTimezone.default.tz.guess();
|
|
253
|
+
const {
|
|
254
|
+
startDate,
|
|
255
|
+
endDate,
|
|
256
|
+
timezone = ''
|
|
257
|
+
} = selectedDateRange;
|
|
258
|
+
const validTimezone = (0, _lodash.isEmpty)(timezone) ? localTimezone : timezone;
|
|
259
|
+
const validMaxDate = maxDate && !(0, _lodash.isEmpty)(maxDate) ? maxDate : today;
|
|
260
|
+
const validMinDate = minDate && !(0, _lodash.isEmpty)(minDate) ? minDate : getSubtractedDate(today, 8, 'y');
|
|
261
|
+
const isRangeValid = (min, max, errorMessage) => checkRangeValidity(min, max, errorMessage, onError);
|
|
262
|
+
|
|
263
|
+
// minDate should be after maxDate
|
|
264
|
+
const isMinMaxRangeInvalid = !isRangeValid(validMinDate, validMaxDate, DATE_RANGE_PICKER_ERRORS.MIN_MAX_INVALID);
|
|
265
|
+
|
|
266
|
+
// if startDate is defined and then it should be after minDate
|
|
267
|
+
const isStartDateInvalid = isMinMaxRangeInvalid || (0, _lodash.isEmpty)(startDate) || !isRangeValid(minDate, startDate, DATE_RANGE_PICKER_ERRORS.START_DATE_EARLY);
|
|
268
|
+
if (isMinMaxRangeInvalid || isStartDateInvalid) {
|
|
269
|
+
return {
|
|
270
|
+
validTimezone,
|
|
271
|
+
validDateRange: {
|
|
272
|
+
startDate: null,
|
|
273
|
+
endDate: null
|
|
274
|
+
},
|
|
275
|
+
validMinDate,
|
|
276
|
+
validMaxDate
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// if endDate is defined then it should be before maxDate
|
|
281
|
+
const isEndDateInvalid = (0, _lodash.isEmpty)(endDate) || !isRangeValid(endDate, maxDate, DATE_RANGE_PICKER_ERRORS.END_DATE_LATE);
|
|
282
|
+
|
|
283
|
+
// startDate should be before endDate
|
|
284
|
+
const isStartEndRangeInvalid = isEndDateInvalid || !isRangeValid(startDate, endDate, DATE_RANGE_PICKER_ERRORS.START_DATE_LATE);
|
|
285
|
+
if (isEndDateInvalid || isStartEndRangeInvalid) {
|
|
286
|
+
return {
|
|
287
|
+
validTimezone,
|
|
288
|
+
validDateRange: {
|
|
289
|
+
startDate,
|
|
290
|
+
endDate: null
|
|
291
|
+
},
|
|
292
|
+
validMinDate,
|
|
293
|
+
validMaxDate
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
validTimezone,
|
|
298
|
+
validDateRange: {
|
|
299
|
+
startDate: (0, _lodash.isEmpty)(startDate) ? null : startDate,
|
|
300
|
+
endDate: (0, _lodash.isEmpty)(endDate) ? null : endDate
|
|
301
|
+
},
|
|
302
|
+
validMinDate,
|
|
303
|
+
validMaxDate
|
|
304
|
+
};
|
|
305
|
+
};
|
|
306
|
+
exports.getValidDates = getValidDates;
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
// $FlowFixMe - strict types for date-fns
|
|
4
|
+
import formatISO from 'date-fns/formatISO';
|
|
5
|
+
// $FlowFixMe - strict types for date-fns
|
|
6
|
+
import parseISO from 'date-fns/parseISO';
|
|
7
|
+
import invariant from 'invariant';
|
|
8
|
+
import {chunk, isEmpty, range} from 'lodash';
|
|
9
|
+
// $FlowFixMe[untyped-import]
|
|
10
|
+
import moment from 'moment-timezone';
|
|
11
|
+
import type {
|
|
12
|
+
DateRange,
|
|
13
|
+
DateRangePickerError,
|
|
14
|
+
DateRangeWithTimezone,
|
|
15
|
+
TimeUnit,
|
|
16
|
+
} from 'src/types/date-range-picker';
|
|
17
|
+
|
|
18
|
+
import type {MenuOption} from '../../components/Menu';
|
|
19
|
+
|
|
20
|
+
import {TIMEZONES} from './timezones';
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export const NAVIGATION_ACTION = Object.freeze({
|
|
24
|
+
NEXT: 'next',
|
|
25
|
+
PREV: 'prev',
|
|
26
|
+
});
|
|
27
|
+
export const MARKERS = Object.freeze({
|
|
28
|
+
DATE_RANGE_START: 'FIRST',
|
|
29
|
+
DATE_RANGE_END: 'SECOND',
|
|
30
|
+
});
|
|
31
|
+
export const WEEKDAYS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
|
|
32
|
+
export const MONTHS = [
|
|
33
|
+
{key: '0', label: 'Jan'},
|
|
34
|
+
{key: '1', label: 'Feb'},
|
|
35
|
+
{key: '2', label: 'Mar'},
|
|
36
|
+
{key: '3', label: 'Apr'},
|
|
37
|
+
{key: '4', label: 'May'},
|
|
38
|
+
{key: '5', label: 'Jun'},
|
|
39
|
+
{key: '6', label: 'Jul'},
|
|
40
|
+
{key: '7', label: 'Aug'},
|
|
41
|
+
{key: '8', label: 'Sep'},
|
|
42
|
+
{key: '9', label: 'Oct'},
|
|
43
|
+
{key: '10', label: 'Nov'},
|
|
44
|
+
{key: '11', label: 'Dec'},
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
export const DATE_RANGE_PICKER_ERRORS = Object.freeze({
|
|
48
|
+
MIN_MAX_INVALID: {
|
|
49
|
+
type: 'MIN_MAX_INVALID',
|
|
50
|
+
description: 'Given minDate and maxDate are invalid.',
|
|
51
|
+
},
|
|
52
|
+
START_DATE_EARLY: {
|
|
53
|
+
type: 'START_DATE_EARLY',
|
|
54
|
+
description: 'Given startDate can not come before minDate.',
|
|
55
|
+
},
|
|
56
|
+
START_DATE_LATE: {
|
|
57
|
+
type: 'START_DATE_LATE',
|
|
58
|
+
description: 'Given startDate can not come after endDate.',
|
|
59
|
+
},
|
|
60
|
+
END_DATE_LATE: {
|
|
61
|
+
type: 'END_DATE_LATE',
|
|
62
|
+
description: 'Given endDate can not come after maxDate.',
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
export const checkRangeValidity = (
|
|
67
|
+
rangeStart?: ?string,
|
|
68
|
+
rangeEnd?: ?string,
|
|
69
|
+
errorBody: DateRangePickerError,
|
|
70
|
+
onError?: (DateRangePickerError) => void,
|
|
71
|
+
): boolean => {
|
|
72
|
+
const isRangeStartValid = moment(rangeStart).isValid();
|
|
73
|
+
const isRangeEndValid = isRangeStartValid && moment(rangeEnd).isValid();
|
|
74
|
+
const isRangeValid =
|
|
75
|
+
isRangeEndValid && moment(rangeStart).isSameOrBefore(rangeEnd);
|
|
76
|
+
|
|
77
|
+
invariant(isRangeValid, JSON.stringify(errorBody));
|
|
78
|
+
if (!isRangeValid) {
|
|
79
|
+
onError?.(errorBody);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return isRangeValid;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const wrangleMoment = (date?: string | Date): Date => {
|
|
86
|
+
if (date instanceof Date) {
|
|
87
|
+
return date;
|
|
88
|
+
} else if (!date) {
|
|
89
|
+
return new Date();
|
|
90
|
+
}
|
|
91
|
+
return date instanceof moment ? date.toDate() : parseISO(date);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const formatIsoDate = (date?: string | Date): string =>
|
|
95
|
+
formatISO(wrangleMoment(date), {
|
|
96
|
+
representation: 'date',
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
export const isStartOfRange = ({startDate}: DateRange, date: string): boolean =>
|
|
100
|
+
Boolean(startDate) && moment(date).isSame(moment(startDate), 'd');
|
|
101
|
+
|
|
102
|
+
export const isEndOfRange = ({endDate}: DateRange, date: string): boolean =>
|
|
103
|
+
Boolean(endDate) && moment(date).isSame(moment(endDate), 'd');
|
|
104
|
+
|
|
105
|
+
export const inDateRange = (
|
|
106
|
+
{startDate, endDate}: DateRange,
|
|
107
|
+
date: string,
|
|
108
|
+
): boolean => {
|
|
109
|
+
if (startDate && endDate) {
|
|
110
|
+
const momentDay = moment(date);
|
|
111
|
+
const momentStartDate = moment(startDate);
|
|
112
|
+
const momentEndDate = moment(endDate);
|
|
113
|
+
return momentDay.isBetween(momentStartDate, momentEndDate, null, '[]');
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const isStartDateEndDateSame = ({
|
|
119
|
+
startDate,
|
|
120
|
+
endDate,
|
|
121
|
+
}: DateRange): boolean => {
|
|
122
|
+
if (startDate && endDate) {
|
|
123
|
+
return moment(startDate).isSame(moment(endDate), 'd');
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const getMonthAndYear = (date) => {
|
|
129
|
+
const momentDate = date ? moment(date) : moment();
|
|
130
|
+
return [momentDate.month(), momentDate.year()];
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const getDaysInMonth = (date: string): Array<Array<string>> => {
|
|
134
|
+
const startWeek = moment(date).startOf('month').startOf('week');
|
|
135
|
+
const endWeek = moment(date).endOf('month').endOf('week');
|
|
136
|
+
const days = [],
|
|
137
|
+
current = startWeek;
|
|
138
|
+
|
|
139
|
+
while (current.isBefore(endWeek)) {
|
|
140
|
+
days.push(current.clone().format('YYYY-MM-DD'));
|
|
141
|
+
current.add(1, 'd');
|
|
142
|
+
}
|
|
143
|
+
const daysInChunks = chunk(days, 7);
|
|
144
|
+
|
|
145
|
+
// if total rows in calendar are 5 add one more week to the calendar
|
|
146
|
+
if (daysInChunks.length === 5) {
|
|
147
|
+
const nextWeek = getAddedDate(endWeek, WEEKDAYS.length, 'd');
|
|
148
|
+
const extraDays = [];
|
|
149
|
+
while (current.isSameOrBefore(nextWeek)) {
|
|
150
|
+
extraDays.push(current.clone().format('YYYY-MM-DD'));
|
|
151
|
+
current.add(1, 'd');
|
|
152
|
+
}
|
|
153
|
+
daysInChunks.push(extraDays);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return daysInChunks;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const getAddedDate = (
|
|
160
|
+
date: string,
|
|
161
|
+
addCount: number,
|
|
162
|
+
timeUnit: TimeUnit,
|
|
163
|
+
): string => formatIsoDate(moment(date).add(addCount, timeUnit));
|
|
164
|
+
|
|
165
|
+
export const getSubtractedDate = (
|
|
166
|
+
date: string,
|
|
167
|
+
subtractCount: number,
|
|
168
|
+
timeUnit: TimeUnit,
|
|
169
|
+
): string => formatIsoDate(moment(date).subtract(subtractCount, timeUnit));
|
|
170
|
+
|
|
171
|
+
export const getMonthEndDate = (date: string): string =>
|
|
172
|
+
formatIsoDate(moment(date).endOf('M'));
|
|
173
|
+
|
|
174
|
+
export const addTimezone = (date: string, timezone: string): string =>
|
|
175
|
+
moment.tz(date, timezone).startOf('d').utc().toISOString();
|
|
176
|
+
|
|
177
|
+
export const getTimezones = (): Array<MenuOption> =>
|
|
178
|
+
Object.keys(TIMEZONES).reduce((menuOptions, key) => {
|
|
179
|
+
menuOptions.push({
|
|
180
|
+
key,
|
|
181
|
+
label: TIMEZONES[key],
|
|
182
|
+
});
|
|
183
|
+
return menuOptions;
|
|
184
|
+
}, []);
|
|
185
|
+
|
|
186
|
+
export const generateAvailableYears = ({
|
|
187
|
+
marker,
|
|
188
|
+
minDate,
|
|
189
|
+
maxDate,
|
|
190
|
+
rangeStartMonth,
|
|
191
|
+
rangeEndMonth,
|
|
192
|
+
}: {
|
|
193
|
+
marker: $Values<typeof MARKERS>,
|
|
194
|
+
minDate: string,
|
|
195
|
+
maxDate: string,
|
|
196
|
+
rangeStartMonth: string,
|
|
197
|
+
rangeEndMonth: string,
|
|
198
|
+
}): Array<MenuOption> => {
|
|
199
|
+
const rangeStartYear = moment(rangeStartMonth).year();
|
|
200
|
+
const rangeEndYear = moment(rangeEndMonth).year();
|
|
201
|
+
|
|
202
|
+
const isWithinRange = (year: number) =>
|
|
203
|
+
marker === MARKERS.DATE_RANGE_START
|
|
204
|
+
? year <= rangeEndYear
|
|
205
|
+
: year >= rangeStartYear;
|
|
206
|
+
|
|
207
|
+
return range(moment(minDate).year(), moment(maxDate).year() + 1)
|
|
208
|
+
.filter((year) => isWithinRange(year))
|
|
209
|
+
.map((year) => ({
|
|
210
|
+
key: year.toString(),
|
|
211
|
+
label: year.toString(),
|
|
212
|
+
}));
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export const getAvailableMonths = ({
|
|
216
|
+
marker,
|
|
217
|
+
minDate,
|
|
218
|
+
maxDate,
|
|
219
|
+
rangeStartMonth,
|
|
220
|
+
rangeEndMonth,
|
|
221
|
+
}: {
|
|
222
|
+
marker: $Values<typeof MARKERS>,
|
|
223
|
+
minDate: string,
|
|
224
|
+
maxDate: string,
|
|
225
|
+
rangeStartMonth: string,
|
|
226
|
+
rangeEndMonth: string,
|
|
227
|
+
}): Array<MenuOption> => {
|
|
228
|
+
const [rangeStartMonthKey, rangeStartYear] = getMonthAndYear(rangeStartMonth);
|
|
229
|
+
const [rangeEndMonthKey, rangeEndYear] = getMonthAndYear(rangeEndMonth);
|
|
230
|
+
const [minDateMonth, minDateYear] = getMonthAndYear(minDate);
|
|
231
|
+
const [maxDateMonth, maxDateYear] = getMonthAndYear(maxDate);
|
|
232
|
+
|
|
233
|
+
return MONTHS.filter((month: MenuOption) => {
|
|
234
|
+
const isSameYear = rangeStartYear === rangeEndYear;
|
|
235
|
+
const isFirstAndMinDateYearSame = rangeStartYear === minDateYear;
|
|
236
|
+
const isSecondAndMaxDateYearSame = rangeEndYear === maxDateYear;
|
|
237
|
+
|
|
238
|
+
if (marker === MARKERS.DATE_RANGE_START) {
|
|
239
|
+
if (isSameYear && month.key >= rangeEndMonthKey) {
|
|
240
|
+
return false;
|
|
241
|
+
} else if (isFirstAndMinDateYearSame && month.key < minDateMonth) {
|
|
242
|
+
return false;
|
|
243
|
+
} else {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
if (isSameYear && month.key <= rangeStartMonthKey) {
|
|
248
|
+
return false;
|
|
249
|
+
} else if (isSecondAndMaxDateYearSame && month.key > maxDateMonth) {
|
|
250
|
+
return false;
|
|
251
|
+
} else {
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
export const getValidDates = (
|
|
259
|
+
selectedDateRange: DateRangeWithTimezone,
|
|
260
|
+
minDate?: ?string,
|
|
261
|
+
maxDate?: ?string,
|
|
262
|
+
onError?: (DateRangePickerError) => void,
|
|
263
|
+
): {
|
|
264
|
+
validTimezone: string,
|
|
265
|
+
validMinDate: string,
|
|
266
|
+
validMaxDate: string,
|
|
267
|
+
validDateRange: DateRange,
|
|
268
|
+
} => {
|
|
269
|
+
const today = formatIsoDate();
|
|
270
|
+
const localTimezone = moment.tz.guess();
|
|
271
|
+
const {startDate, endDate, timezone = ''} = selectedDateRange;
|
|
272
|
+
|
|
273
|
+
const validTimezone = isEmpty(timezone) ? localTimezone : timezone;
|
|
274
|
+
const validMaxDate = maxDate && !isEmpty(maxDate) ? maxDate : today;
|
|
275
|
+
const validMinDate =
|
|
276
|
+
minDate && !isEmpty(minDate) ? minDate : getSubtractedDate(today, 8, 'y');
|
|
277
|
+
|
|
278
|
+
const isRangeValid = (min, max, errorMessage) =>
|
|
279
|
+
checkRangeValidity(min, max, errorMessage, onError);
|
|
280
|
+
|
|
281
|
+
// minDate should be after maxDate
|
|
282
|
+
const isMinMaxRangeInvalid = !isRangeValid(
|
|
283
|
+
validMinDate,
|
|
284
|
+
validMaxDate,
|
|
285
|
+
DATE_RANGE_PICKER_ERRORS.MIN_MAX_INVALID,
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
// if startDate is defined and then it should be after minDate
|
|
289
|
+
const isStartDateInvalid =
|
|
290
|
+
isMinMaxRangeInvalid ||
|
|
291
|
+
isEmpty(startDate) ||
|
|
292
|
+
!isRangeValid(
|
|
293
|
+
minDate,
|
|
294
|
+
startDate,
|
|
295
|
+
DATE_RANGE_PICKER_ERRORS.START_DATE_EARLY,
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
if (isMinMaxRangeInvalid || isStartDateInvalid) {
|
|
299
|
+
return {
|
|
300
|
+
validTimezone,
|
|
301
|
+
validDateRange: {startDate: null, endDate: null},
|
|
302
|
+
validMinDate,
|
|
303
|
+
validMaxDate,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// if endDate is defined then it should be before maxDate
|
|
308
|
+
const isEndDateInvalid =
|
|
309
|
+
isEmpty(endDate) ||
|
|
310
|
+
!isRangeValid(endDate, maxDate, DATE_RANGE_PICKER_ERRORS.END_DATE_LATE);
|
|
311
|
+
|
|
312
|
+
// startDate should be before endDate
|
|
313
|
+
const isStartEndRangeInvalid =
|
|
314
|
+
isEndDateInvalid ||
|
|
315
|
+
!isRangeValid(startDate, endDate, DATE_RANGE_PICKER_ERRORS.START_DATE_LATE);
|
|
316
|
+
|
|
317
|
+
if (isEndDateInvalid || isStartEndRangeInvalid) {
|
|
318
|
+
return {
|
|
319
|
+
validTimezone,
|
|
320
|
+
validDateRange: {startDate, endDate: null},
|
|
321
|
+
validMinDate,
|
|
322
|
+
validMaxDate,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
validTimezone,
|
|
328
|
+
validDateRange: {
|
|
329
|
+
startDate: isEmpty(startDate) ? null : startDate,
|
|
330
|
+
endDate: isEmpty(endDate) ? null : endDate,
|
|
331
|
+
},
|
|
332
|
+
validMinDate,
|
|
333
|
+
validMaxDate,
|
|
334
|
+
};
|
|
335
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _dateRangePicker = require("./date-range-picker");
|
|
7
|
+
Object.keys(_dateRangePicker).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _dateRangePicker[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _dateRangePicker[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _timezones = require("./timezones");
|
|
18
|
+
Object.keys(_timezones).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _timezones[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _timezones[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
package/lib/utils/index.js
CHANGED
|
@@ -47,6 +47,17 @@ Object.keys(_clickAway).forEach(function (key) {
|
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
|
+
var _dateRangePicker = require("./date-range-picker");
|
|
51
|
+
Object.keys(_dateRangePicker).forEach(function (key) {
|
|
52
|
+
if (key === "default" || key === "__esModule") return;
|
|
53
|
+
if (key in exports && exports[key] === _dateRangePicker[key]) return;
|
|
54
|
+
Object.defineProperty(exports, key, {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () {
|
|
57
|
+
return _dateRangePicker[key];
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
50
61
|
var _dom = require("./dom");
|
|
51
62
|
Object.keys(_dom).forEach(function (key) {
|
|
52
63
|
if (key === "default" || key === "__esModule") return;
|
package/lib/utils/index.js.flow
CHANGED