rsuite
Version:
A suite of react components
51 lines (42 loc) • 1.94 kB
JavaScript
import { addMonths, getMonth, isSameDay, shouldRenderTime, isSameSecond, startOfMonth, endOfMonth, startOfISOWeek, endOfISOWeek, startOfWeek, endOfWeek } from '../utils/dateUtils';
export function getCalendarDate(_ref) {
var _value;
var value = _ref.value,
_ref$calendarKey = _ref.calendarKey,
calendarKey = _ref$calendarKey === void 0 ? 'start' : _ref$calendarKey;
// Update calendarDate if the value is not null
value = (_value = value) !== null && _value !== void 0 ? _value : [];
if (value[0] && value[1]) {
var startMonth = getMonth(value[0]);
var endMonth = getMonth(value[1]);
if (calendarKey === 'start') {
return [value[0], startMonth >= endMonth ? addMonths(value[0], 1) : value[1]];
} else if (calendarKey === 'end') {
return [startMonth >= endMonth ? addMonths(value[1], -1) : value[0], value[1]];
} // If only the start date
} else if (value[0]) {
return [value[0], addMonths(value[0], 1)];
}
var todayDate = new Date();
return [todayDate, addMonths(todayDate, 1)];
}
export var isSameRange = function isSameRange(source, dest, format) {
// If both are null, reguard as same
if (null === source && null === dest) return true; // If only one is null, regard as different
if (null === source || null === dest) return false;
var result = isSameDay(source[0], dest[0]) && isSameDay(source[1], dest[1]);
if (shouldRenderTime(format)) {
result && (result = isSameSecond(source[0], dest[0]) && isSameSecond(source[1], dest[1]));
}
return result;
};
export var getMonthHoverRange = function getMonthHoverRange(date) {
return [startOfMonth(date), endOfMonth(date)];
};
export var getWeekHoverRange = function getWeekHoverRange(isoWeek, date) {
if (isoWeek) {
// set to the first day of this week according to ISO 8601, 12:00 am
return [startOfISOWeek(date), endOfISOWeek(date)];
}
return [startOfWeek(date), endOfWeek(date)];
};