@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
165 lines • 5.35 kB
JavaScript
import { subMonths, addMonths, isWithinInterval, isValid, isAfter, isBefore, isSameDay, isToday, isSameMonth, addDays, startOfWeek, getDay, startOfMonth, getDaysInMonth, toDate, parseISO, parse, startOfDay } from 'date-fns';
import { warn } from "../../shared/component-helper.js";
const calendarCache = {};
export function makeDayObject(date, {
startDate,
endDate,
hoverDate,
minDate,
maxDate,
month
}) {
const range = getRange(startDate, endDate, hoverDate);
const isLastMonth = isSameMonth(subMonths(date, 1), month);
const isNextMonth = isSameMonth(addMonths(date, 1), month);
const isStartDate = isStartDateCalc(date, range);
const isEndDate = isEndDateCalc(date, range);
const isWithinSelection = isWithinSelectionCalc(date, startDate, endDate);
const isPreview = isPreviewCalc(date, startDate, endDate, hoverDate);
const isDisabled = isDisabledCalc(date, minDate, maxDate);
return {
date,
isToday: isToday(date),
isLastMonth,
isNextMonth,
isStartDate,
isEndDate,
isWithinSelection,
isPreview,
isDisabled,
isSelectable: !isLastMonth && !isNextMonth && !isDisabled,
isInactive: isLastMonth || isNextMonth || isDisabled
};
}
export function getCalendar(month, weekStartsOn = 0, {
onlyMonth = false,
hideNextMonthWeek = false
} = {}) {
const cacheKey = month.toISOString();
const thisMonth = getMonth(month);
if (onlyMonth) {
return calendarCache[cacheKey] = [...thisMonth];
}
if (calendarCache[cacheKey]) {
return calendarCache[cacheKey];
}
const firstDay = (7 + getDay(startOfMonth(month)) - weekStartsOn) % 7;
const lastMonth = getMonth(subMonths(month, 1), getDaysInMonth(subMonths(month, 1)) - firstDay);
let fillCount = -1;
if (hideNextMonthWeek) {
fillCount = 35 - (thisMonth.length + firstDay);
}
if (fillCount < 0) {
fillCount = 42 - (thisMonth.length + firstDay);
}
const nextMonth = getMonth(addMonths(month, 1), 0, fillCount > -1 ? fillCount : 0);
return calendarCache[cacheKey] = [...lastMonth, ...thisMonth, ...nextMonth];
}
export function dayOffset(dayName) {
const week = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
return dayName ? week.indexOf(dayName.toLowerCase()) : 0;
}
export function toRange(startDate, endDate) {
if (isBefore(endDate, startDate)) {
const _startDate = startDate;
startDate = endDate;
endDate = _startDate;
}
return {
startDate,
endDate
};
}
function getRange(startDate, endDate, hoverDate) {
if (startDate && endDate) {
return toRange(startDate, endDate);
} else if (startDate && hoverDate) {
return toRange(startDate, hoverDate);
} else {
return toRange(startDate, startDate);
}
}
export function getWeek(weekStartsOn) {
const startDay = startOfWeek(new Date(), {
weekStartsOn
});
return Array(7).fill(1).map((value, i) => addDays(startDay, i));
}
export function getMonth(month, skip = 0, limit) {
const startDay = startOfMonth(month);
let size = getDaysInMonth(month) - skip;
size = Math.min(Math.max(size, 0), limit > -1 ? limit : size);
size = size < 0 ? 0 : size;
return Array(size).fill(1).map((value, i) => addDays(startDay, i + skip));
}
function isWithinSelectionCalc(date, startDate, endDate) {
const {
startDate: start,
endDate: end
} = toRange(startDate, endDate);
return startDate && endDate ? isValid(start) && isValid(end) && isWithinInterval(date, {
start,
end
}) : false;
}
function isDisabledCalc(date, minDate, maxDate) {
return minDate && isBefore(startOfDay(date), startOfDay(minDate)) || maxDate && isAfter(startOfDay(date), startOfDay(maxDate));
}
export { isDisabledCalc as isDisabled };
function isStartDateCalc(date, range) {
return range.startDate && isSameDay(date, range.startDate);
}
function isEndDateCalc(date, range) {
return range.endDate && isSameDay(date, range.endDate);
}
function isPreviewCalc(date, startDate, endDate, hoverDate) {
const {
startDate: start,
endDate: end
} = toRange(startDate, hoverDate);
return startDate && !endDate && isValid(start) && isValid(end) && isWithinInterval(date, {
start,
end
});
}
export function correctV1Format(date) {
if (/YYYY/.test(date) && /DD/.test(date)) {
warn('You are using "YYYY-MM-DD" as the dateFormat or returnFormat? Please use "yyyy-MM-dd" instead!');
date = date.replace(/DD/, 'dd').replace(/YYYY/, 'yyyy');
}
return date;
}
function parseHumanDate(input, humanDateFormats = ['dd.MM.yyyy', 'dd/MM/yyyy', 'yyyy-MM-dd']) {
for (const format of humanDateFormats) {
const parsed = parse(input, format, new Date());
if (isValid(parsed)) {
return parsed;
}
}
return null;
}
export function convertStringToDate(date, {
dateFormat = null
} = {}) {
if (!date) {
return null;
}
let dateObject;
if (typeof date === 'string') {
dateObject = parseISO(date);
if (!isValid(dateObject)) {
dateObject = parseHumanDate(date);
}
if (dateFormat && !isValid(dateObject)) {
dateObject = parseHumanDate(date, [correctV1Format(dateFormat)]);
}
} else {
dateObject = toDate(date);
}
if (!isValid(dateObject)) {
warn('convertStringToDate got invalid date:', date);
return null;
}
return dateObject;
}
//# sourceMappingURL=DatePickerCalc.js.map