UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

46 lines (45 loc) • 2.45 kB
import dayjs from "dayjs"; //#region src/base-ui/DatePicker/calendar.ts const DEFAULT_FORMAT = { date: "MMM D, YYYY", month: "MMMM YYYY", year: "YYYY" }; const getWeekStart = () => { return dayjs.Ls[dayjs.locale()]?.weekStart ?? 0; }; const offsetFromWeekStart = (date, weekStart) => (dayjs(date).day() - weekStart + 7) % 7; const buildMonthGrid = (month, weekStart = getWeekStart()) => { const first = dayjs(month).startOf("month"); const start = first.subtract(offsetFromWeekStart(first.toDate(), weekStart), "day"); return Array.from({ length: 42 }, (_, index) => start.add(index, "day").toDate()); }; const getWeekdayLabels = (weekStart = getWeekStart()) => Array.from({ length: 7 }, (_, index) => dayjs().day((weekStart + index) % 7).format("dd")); const isSameDay = (a, b) => !!a && !!b && dayjs(a).isSame(b, "day"); const isDayDisabled = (date, { disabledDate, max, min }) => !!min && dayjs(date).isBefore(min, "day") || !!max && dayjs(date).isAfter(max, "day") || !!disabledDate?.(date); const isMonthDisabled = (month, { max, min }) => !!min && dayjs(month).isBefore(min, "month") || !!max && dayjs(month).isAfter(max, "month"); const isYearDisabled = (year, { max, min }) => !!min && dayjs(year).isBefore(min, "year") || !!max && dayjs(year).isAfter(max, "year"); const moveDayFocus = (date, key, weekStart = getWeekStart()) => { const current = dayjs(date); const offset = offsetFromWeekStart(date, weekStart); switch (key) { case "ArrowLeft": return current.subtract(1, "day").toDate(); case "ArrowRight": return current.add(1, "day").toDate(); case "ArrowUp": return current.subtract(7, "day").toDate(); case "ArrowDown": return current.add(7, "day").toDate(); case "PageUp": return current.subtract(1, "month").toDate(); case "PageDown": return current.add(1, "month").toDate(); case "Home": return current.subtract(offset, "day").toDate(); case "End": return current.add(6 - offset, "day").toDate(); default: return null; } }; const orderRange = (a, b) => dayjs(a).isAfter(b, "day") ? [b, a] : [a, b]; const clampToBounds = (date, { max, min }) => { if (min && dayjs(date).isBefore(min, "day")) return min; if (max && dayjs(date).isAfter(max, "day")) return max; return date; }; //#endregion export { DEFAULT_FORMAT, buildMonthGrid, clampToBounds, getWeekStart, getWeekdayLabels, isDayDisabled, isMonthDisabled, isSameDay, isYearDisabled, moveDayFocus, orderRange }; //# sourceMappingURL=calendar.mjs.map