UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

206 lines 6.94 kB
import subMonths from 'date-fns/subMonths'; import addMonths from 'date-fns/addMonths'; import isWithinInterval from 'date-fns/isWithinInterval'; import isValid from 'date-fns/isValid'; import isAfter from 'date-fns/isAfter'; import isBefore from 'date-fns/isBefore'; import isSameDay from 'date-fns/isSameDay'; import isToday from 'date-fns/isToday'; import isSameMonth from 'date-fns/isSameMonth'; import addDays from 'date-fns/addDays'; import startOfWeek from 'date-fns/startOfWeek'; import getDay from 'date-fns/getDay'; import startOfMonth from 'date-fns/startOfMonth'; import getDaysInMonth from 'date-fns/getDaysInMonth'; import toDate from 'date-fns/toDate'; import parseISO from 'date-fns/parseISO'; import parse from 'date-fns/parse'; import startOfDay from 'date-fns/startOfDay'; import { warn } from '../../shared/component-helper'; import { LOCALE } from '../../shared/defaults'; 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; } export function convertStringToDate(date, { dateFormat = null } = {}) { if (!date) { return null; } let dateObject; dateObject = typeof date === 'string' ? parseISO(date) : toDate(date); if (typeof date === 'string' && dateFormat && !isValid(dateObject)) { dateFormat = correctV1Format(dateFormat); dateObject = parse(date, dateFormat, new Date()); } if (!isValid(dateObject)) { warn('convertStringToDate got invalid date:', date); return null; } return dateObject; } export function formatDate(dateValue, { locale = LOCALE, variant = 'numeric', formatOptions = undefined } = {}) { const options = formatOptions !== null && formatOptions !== void 0 ? formatOptions : getFormatOptions(variant); const date = convertStringToDate(dateValue); return typeof Intl !== 'undefined' ? new Intl.DateTimeFormat(locale, options).format(date) : date.toLocaleString(locale, options); } export function formatDateRange(dates, { locale = LOCALE, variant = 'numeric', formatOptions = undefined } = {}) { const options = formatOptions !== null && formatOptions !== void 0 ? formatOptions : getFormatOptions(variant); const startDate = convertStringToDate(dates.startDate); const endDate = convertStringToDate(dates.endDate); if (typeof Intl !== 'undefined') { return new Intl.DateTimeFormat(locale, options).formatRange(startDate, endDate); } const startDateString = startDate.toLocaleString(locale, options); const endDateString = endDate.toLocaleString(locale, options); return `${startDateString}-${endDateString}`; } export function getFormatOptions(variant) { if (variant === 'numeric') { return { day: '2-digit', month: '2-digit', year: 'numeric' }; } return { day: 'numeric', month: variant, year: 'numeric' }; } //# sourceMappingURL=DatePickerCalc.js.map