@yamada-ui/calendar
Version:
Yamada UI calendar component
215 lines (213 loc) • 7.78 kB
JavaScript
"use client"
// src/calendar-utils.ts
import { isActiveElement } from "@yamada-ui/utils";
import dayjs from "dayjs";
var getFirstOfWeek = (date, firstDayOfWeek) => {
const value = new Date(date);
const day = value.getDay() || 7;
const sunday = firstDayOfWeek === "sunday";
const clampToFirstDay = sunday ? day : day - 1;
if (sunday && day !== 0 || day !== 1) value.setHours(-24 * clampToFirstDay);
return value;
};
var getLastOfWeek = (date, firstDayOfWeek) => {
const value = new Date(date);
const day = value.getDay();
const sunday = firstDayOfWeek === "sunday";
const clampToLastDay = 7 - (sunday ? day + 1 : day);
if (sunday && day !== 6 || day !== 0)
value.setDate(value.getDate() + clampToLastDay);
return value;
};
var getWeekdays = (locale, firstDayOfWeek, format = "dd") => {
let weekdays = [];
const date = getFirstOfWeek(/* @__PURE__ */ new Date(), firstDayOfWeek);
for (let i = 0; i < 7; i += 1) {
const weekday = dayjs(date).locale(locale).format(format);
weekdays = [...weekdays, weekday];
date.setDate(date.getDate() + 1);
}
return weekdays;
};
var getMonthDays = (date, firstDayOfWeek) => {
const currentMonth = date.getMonth();
const firstOfMonth = new Date(date.getFullYear(), currentMonth, 1);
const lastOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
const endDate = getLastOfWeek(lastOfMonth, firstDayOfWeek);
const firstDate = getFirstOfWeek(firstOfMonth, firstDayOfWeek);
const weeks = [];
while (firstDate <= endDate) {
const days = [];
for (let i = 0; i < 7; i += 1) {
days.push(new Date(firstDate));
firstDate.setDate(firstDate.getDate() + 1);
}
weeks.push(days);
}
return weeks;
};
var getRangeYears = (year) => {
const rounded = year - 6;
let rangeYears = [];
for (let i = 0; i < 12; i += 1) {
const rangeYear = rounded + i;
rangeYears = [...rangeYears, rangeYear];
}
return rangeYears;
};
var getRangeMonths = (locale, format) => {
const rounded = new Date(1993, 0, 1);
let rangeMonths = [];
for (let i = 0; i < 12; i += 1) {
const rangeMonth = dayjs(rounded).locale(locale).format(format);
rangeMonths = [...rangeMonths, rangeMonth];
rounded.setMonth(rounded.getMonth() + 1);
}
return rangeMonths;
};
var getFormattedLabel = (dateOrYear, locale, format) => {
if (dateOrYear == null || dateOrYear === -1) {
return "";
} else if (dateOrYear instanceof Date) {
return dayjs(dateOrYear).locale(locale).format(format);
} else {
return dayjs(new Date(dateOrYear, 1, 1)).locale(locale).format(format);
}
};
var isSameMonth = (date, comparison) => (date == null ? void 0 : date.getFullYear()) === (comparison == null ? void 0 : comparison.getFullYear()) && (date == null ? void 0 : date.getMonth()) === (comparison == null ? void 0 : comparison.getMonth());
var isSameDate = (date, comparison) => isSameMonth(date, comparison) && (date == null ? void 0 : date.getDate()) === (comparison == null ? void 0 : comparison.getDate());
var getRangeDates = (startDate, endDate) => {
const dates = [];
if (!startDate || !endDate) {
if (startDate) dates.push(startDate);
if (endDate) dates.push(endDate);
} else {
const resolvedStartDate = dayjs(startDate).startOf("day");
const resolvedEndDate = dayjs(endDate).startOf("day");
const n = Math.abs(resolvedStartDate.diff(resolvedEndDate, "day"));
let date = resolvedStartDate;
for (let i = 0; i <= n; i++) {
dates.push(date.add(i, "day").toDate());
}
}
return dates;
};
var isAfterMonth = (value, date) => date instanceof Date && dayjs(value).startOf("month").isAfter(date);
var isBeforeMonth = (value, date) => date instanceof Date && dayjs(value).startOf("month").isBefore(date);
var isInRange = (date, minDate, maxDate) => {
const hasMinDate = minDate instanceof Date;
const hasMaxDate = maxDate instanceof Date;
if (!hasMaxDate && !hasMinDate) return true;
const minInRange = hasMinDate ? isSomeAfterDate(date, minDate) : true;
const maxInRange = hasMaxDate ? isSomeBeforeDate(date, maxDate) : true;
return maxInRange && minInRange;
};
var isMonthInRange = ({
date,
maxDate,
minDate
}) => {
const hasMinDate = minDate instanceof Date;
const hasMaxDate = maxDate instanceof Date;
if (!hasMaxDate && !hasMinDate) return true;
const firstOfMonth = dayjs(date).startOf("month");
const lastOfMonth = dayjs(date).endOf("month");
const minInRange = hasMinDate ? lastOfMonth.isAfter(minDate) : true;
const maxInRange = hasMaxDate ? firstOfMonth.isBefore(maxDate) : true;
return maxInRange && minInRange;
};
var isIncludeDates = (date, dates) => dates.some((d) => dayjs(d).isSame(date, "day"));
var sortDates = (dates, type = "asc") => {
if (type === "asc") {
return dates.sort((a, b) => dayjs(a).isAfter(b, "day") ? 1 : -1);
} else {
return dates.sort((a, b) => dayjs(a).isBefore(b, "day") ? 1 : -1);
}
};
var isSomeAfterDate = (value, date) => (date instanceof Date || date instanceof dayjs) && (dayjs(value).isSame(date) || isAfterDate(value, date));
var isSomeBeforeDate = (value, date) => (date instanceof Date || date instanceof dayjs) && (dayjs(value).isSame(date) || isBeforeDate(value, date));
var isAfterDate = (value, date) => (date instanceof Date || date instanceof dayjs) && dayjs(date).isBefore(value, "day");
var isBeforeDate = (value, date) => (date instanceof Date || date instanceof dayjs) && dayjs(date).isAfter(value, "day");
var onShouldFocus = (refs, validateFunc, isFirst = true) => {
let targetValue;
let targetEl;
for (const value of refs.current.keys()) {
const selected = validateFunc(value);
if (selected) targetValue = value;
}
if (typeof targetValue === "number") {
const ref = refs.current.get(targetValue);
targetEl = ref == null ? void 0 : ref.current;
} else {
const values = [...refs.current.values()];
const firstRef = values[0];
const lastRef = values[values.length - 1];
targetEl = isFirst ? firstRef == null ? void 0 : firstRef.current : lastRef == null ? void 0 : lastRef.current;
}
if (targetEl) {
targetEl.focus();
targetEl.tabIndex = 0;
}
};
var getFocused = (refs) => {
for (const [value, ref] of refs.current.entries()) {
const focused = ref.current ? isActiveElement(ref.current) : false;
if (focused) return value;
}
};
var getRangeFirstDay = (refs) => {
const days = [...refs.current.keys()];
return days[0];
};
var getRangeLastDay = (refs) => {
const days = [...refs.current.keys()];
return days[days.length - 1];
};
var disableAllTabIndex = (refs) => {
for (const ref of refs.current.values()) {
if (ref.current) ref.current.tabIndex = -1;
}
};
var isDisabledDate = ({
disableOutsideDays,
endDate,
excludeDate,
maxDate,
maxTrulySelectStartDate,
maybeEndDate,
maybeStartDate,
minDate,
minTrulySelectStartDate,
outside,
startDate,
value
}) => isAfterDate(value, maxDate) || isBeforeDate(value, minDate) || isAfterDate(value, maybeStartDate) && isBeforeDate(value, maxTrulySelectStartDate) && !endDate || isBeforeDate(value, maybeEndDate) && isAfterDate(value, minTrulySelectStartDate) && !startDate || !!(excludeDate == null ? void 0 : excludeDate(value)) || !!disableOutsideDays && !!outside;
export {
getFirstOfWeek,
getLastOfWeek,
getWeekdays,
getMonthDays,
getRangeYears,
getRangeMonths,
getFormattedLabel,
isSameMonth,
isSameDate,
getRangeDates,
isAfterMonth,
isBeforeMonth,
isInRange,
isMonthInRange,
isIncludeDates,
sortDates,
isSomeAfterDate,
isSomeBeforeDate,
isAfterDate,
isBeforeDate,
onShouldFocus,
getFocused,
getRangeFirstDay,
getRangeLastDay,
disableAllTabIndex,
isDisabledDate
};
//# sourceMappingURL=chunk-BPJGE3HG.mjs.map