UNPKG

@yamada-ui/calendar

Version:

Yamada UI calendar component

296 lines (292 loc) • 10.2 kB
"use client" "use strict"; 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/use-month-list.tsx var use_month_list_exports = {}; __export(use_month_list_exports, { useMonthList: () => useMonthList }); module.exports = __toCommonJS(use_month_list_exports); var import_utils3 = require("@yamada-ui/utils"); var import_react2 = require("react"); // src/calendar-utils.ts var import_utils = require("@yamada-ui/utils"); var import_dayjs = __toESM(require("dayjs")); 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 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 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 disableAllTabIndex = (refs) => { for (const ref of refs.current.values()) { if (ref.current) ref.current.tabIndex = -1; } }; // src/use-calendar.ts var import_core = require("@yamada-ui/core"); var import_use_controllable_state = require("@yamada-ui/use-controllable-state"); var import_utils2 = require("@yamada-ui/utils"); var import_dayjs2 = __toESM(require("dayjs")); var import_react = require("react"); var [CalendarProvider, useCalendarContext] = (0, import_utils2.createContext)({ name: "CalendarContext", errorMessage: `useCalendarContext returned is 'undefined'. Seems you forgot to wrap the components in "<Calendar />"` }); // src/use-month-list.tsx var useMonthList = () => { const { locale, maxDate, maxYear, minDate, minYear, month, monthFormat, monthRefs, setMonth, setType, setYear, value: selectedValue, year, yearFormat, __selectType } = useCalendarContext(); const multi = (0, import_utils3.isArray)(selectedValue); const beforeYear = (0, import_react2.useRef)(null); const rangeMonths = getRangeMonths(locale, monthFormat); const label = getFormattedLabel(year, locale, yearFormat); const onFocusPrev = (0, import_react2.useCallback)( (targetMonth) => { if (targetMonth < 0) { if (year <= minYear) return; setYear((prev) => { beforeYear.current = prev; return prev - 1; }); } else { const ref = monthRefs.current.get(targetMonth); if (ref == null ? void 0 : ref.current) { ref.current.focus(); ref.current.tabIndex = 0; } } }, [minYear, monthRefs, setYear, year] ); const onFocusNext = (0, import_react2.useCallback)( (targetMonth) => { if (11 < targetMonth) { if (maxYear <= year) return; setYear((prev) => { beforeYear.current = prev; return prev + 1; }); } else { const ref = monthRefs.current.get(targetMonth); if (ref == null ? void 0 : ref.current) { ref.current.focus(); ref.current.tabIndex = 0; } } }, [maxYear, monthRefs, setYear, year] ); const onKeyDown = (0, import_react2.useCallback)( (ev) => { var _a; const focusedMonth = (_a = getFocused(monthRefs)) != null ? _a : 0; const actions = { ArrowDown: () => focusedMonth + 3 <= 11 ? onFocusNext(focusedMonth + 3) : {}, ArrowLeft: () => onFocusPrev(focusedMonth - 1), ArrowRight: () => onFocusNext(focusedMonth + 1), ArrowUp: () => focusedMonth - 3 >= 0 ? onFocusPrev(focusedMonth - 3) : {}, End: () => onFocusNext(11), Home: () => onFocusPrev(0) }; const action = actions[ev.key]; if (!action) return; ev.preventDefault(); ev.stopPropagation(); disableAllTabIndex(monthRefs); action(ev); }, [monthRefs, onFocusNext, onFocusPrev] ); const onClick = (0, import_react2.useCallback)( (ev, month2) => { ev.preventDefault(); ev.stopPropagation(); if ((0, import_utils3.isDisabled)(ev.target)) return; setMonth(new Date(year, month2, 1)); setType("date", year, month2); }, [year, setMonth, setType] ); const getIsSelectedYear = (0, import_react2.useCallback)(() => { var _a; if (__selectType === "date") { return month.getFullYear() === year; } else { const selectedYear = !multi ? selectedValue == null ? void 0 : selectedValue.getFullYear() : (_a = selectedValue[0]) == null ? void 0 : _a.getFullYear(); return selectedYear === year; } }, [__selectType, multi, month, selectedValue, year]); const getIsSelected = (0, import_react2.useCallback)( (value) => { var _a; if (__selectType === "date") { return month.getMonth() === value; } else { const month2 = !multi ? selectedValue == null ? void 0 : selectedValue.getMonth() : (_a = selectedValue[0]) == null ? void 0 : _a.getMonth(); return month2 === value; } }, [__selectType, multi, month, selectedValue] ); (0, import_utils3.useUpdateEffect)(() => { if (typeof beforeYear.current !== "number") return; onShouldFocus(monthRefs, () => false, beforeYear.current < year); beforeYear.current = null; }, [year]); (0, import_utils3.useUnmountEffect)(() => { monthRefs.current.clear(); }); const getGridProps = (0, import_react2.useCallback)( (props = {}) => ({ "aria-label": label, role: "grid", ...props, onKeyDown: (0, import_utils3.handlerAll)(onKeyDown, props.onKeyDown) }), [label, onKeyDown] ); const getButtonProps = (0, import_react2.useCallback)( ({ value, ...props }, ref = null) => { const controlled = typeof beforeYear.current === "number"; const selectedYear = getIsSelectedYear(); const selected = selectedYear && getIsSelected(value); const disabled = !isMonthInRange({ date: new Date(year, value), maxDate, minDate }); monthRefs.current.set(value, (0, import_react2.createRef)()); let tabIndex = -1; if (controlled) { tabIndex = -1; } else if (!selectedYear && value === 0) { tabIndex = 0; } else if (selected) { tabIndex = 0; } return { ref: (0, import_utils3.mergeRefs)(ref, monthRefs.current.get(value)), disabled, ...props, "aria-disabled": (0, import_utils3.ariaAttr)(disabled), "aria-selected": (0, import_utils3.ariaAttr)(selected), "data-disabled": (0, import_utils3.dataAttr)(disabled), "data-selected": (0, import_utils3.dataAttr)(selected), "data-value": value, tabIndex, onClick: (0, import_utils3.handlerAll)(props.onClick, (ev) => onClick(ev, value)) }; }, [ getIsSelectedYear, getIsSelected, year, minDate, maxDate, monthRefs, onClick ] ); return { label, rangeMonths, getButtonProps, getGridProps }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useMonthList }); //# sourceMappingURL=use-month-list.js.map