@yamada-ui/calendar
Version:
Yamada UI calendar component
273 lines (272 loc) • 10.8 kB
JavaScript
"use client"
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/calendar-utils.ts
var calendar_utils_exports = {};
__export(calendar_utils_exports, {
disableAllTabIndex: () => disableAllTabIndex,
getFirstOfWeek: () => getFirstOfWeek,
getFocused: () => getFocused,
getFormattedLabel: () => getFormattedLabel,
getLastOfWeek: () => getLastOfWeek,
getMonthDays: () => getMonthDays,
getRangeDates: () => getRangeDates,
getRangeFirstDay: () => getRangeFirstDay,
getRangeLastDay: () => getRangeLastDay,
getRangeMonths: () => getRangeMonths,
getRangeYears: () => getRangeYears,
getWeekdays: () => getWeekdays,
isAfterDate: () => isAfterDate,
isAfterMonth: () => isAfterMonth,
isBeforeDate: () => isBeforeDate,
isBeforeMonth: () => isBeforeMonth,
isDisabledDate: () => isDisabledDate,
isInRange: () => isInRange,
isIncludeDates: () => isIncludeDates,
isMonthInRange: () => isMonthInRange,
isSameDate: () => isSameDate,
isSameMonth: () => isSameMonth,
isSomeAfterDate: () => isSomeAfterDate,
isSomeBeforeDate: () => isSomeBeforeDate,
onShouldFocus: () => onShouldFocus,
sortDates: () => sortDates
});
module.exports = __toCommonJS(calendar_utils_exports);
var import_utils = require("@yamada-ui/utils");
var import_dayjs = __toESM(require("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 = (0, import_dayjs.default)(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 = (0, import_dayjs.default)(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 (0, import_dayjs.default)(dateOrYear).locale(locale).format(format);
} else {
return (0, import_dayjs.default)(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 = (0, import_dayjs.default)(startDate).startOf("day");
const resolvedEndDate = (0, import_dayjs.default)(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 && (0, import_dayjs.default)(value).startOf("month").isAfter(date);
var isBeforeMonth = (value, date) => date instanceof Date && (0, import_dayjs.default)(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 = (0, import_dayjs.default)(date).startOf("month");
const lastOfMonth = (0, import_dayjs.default)(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) => (0, import_dayjs.default)(d).isSame(date, "day"));
var sortDates = (dates, type = "asc") => {
if (type === "asc") {
return dates.sort((a, b) => (0, import_dayjs.default)(a).isAfter(b, "day") ? 1 : -1);
} else {
return dates.sort((a, b) => (0, import_dayjs.default)(a).isBefore(b, "day") ? 1 : -1);
}
};
var isSomeAfterDate = (value, date) => (date instanceof Date || date instanceof import_dayjs.default) && ((0, import_dayjs.default)(value).isSame(date) || isAfterDate(value, date));
var isSomeBeforeDate = (value, date) => (date instanceof Date || date instanceof import_dayjs.default) && ((0, import_dayjs.default)(value).isSame(date) || isBeforeDate(value, date));
var isAfterDate = (value, date) => (date instanceof Date || date instanceof import_dayjs.default) && (0, import_dayjs.default)(date).isBefore(value, "day");
var isBeforeDate = (value, date) => (date instanceof Date || date instanceof import_dayjs.default) && (0, import_dayjs.default)(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 ? (0, import_utils.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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
disableAllTabIndex,
getFirstOfWeek,
getFocused,
getFormattedLabel,
getLastOfWeek,
getMonthDays,
getRangeDates,
getRangeFirstDay,
getRangeLastDay,
getRangeMonths,
getRangeYears,
getWeekdays,
isAfterDate,
isAfterMonth,
isBeforeDate,
isBeforeMonth,
isDisabledDate,
isInRange,
isIncludeDates,
isMonthInRange,
isSameDate,
isSameMonth,
isSomeAfterDate,
isSomeBeforeDate,
onShouldFocus,
sortDates
});
//# sourceMappingURL=calendar-utils.js.map