@yamada-ui/calendar
Version:
Yamada UI calendar component
726 lines (717 loc) • 23.9 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/year-list.tsx
var year_list_exports = {};
__export(year_list_exports, {
YearList: () => YearList
});
module.exports = __toCommonJS(year_list_exports);
var import_button2 = require("@yamada-ui/button");
var import_core3 = require("@yamada-ui/core");
var import_utils6 = require("@yamada-ui/utils");
// src/calendar-header.tsx
var import_button = require("@yamada-ui/button");
var import_core2 = require("@yamada-ui/core");
var import_icon = require("@yamada-ui/icon");
var import_utils4 = require("@yamada-ui/utils");
// 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");
// src/calendar-utils.ts
var import_utils = require("@yamada-ui/utils");
var import_dayjs = __toESM(require("dayjs"));
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 [CalendarProvider, useCalendarContext] = (0, import_utils2.createContext)({
name: "CalendarContext",
errorMessage: `useCalendarContext returned is 'undefined'. Seems you forgot to wrap the components in "<Calendar />"`
});
// src/use-calendar-header.tsx
var import_utils3 = require("@yamada-ui/utils");
var import_dayjs3 = __toESM(require("dayjs"));
var import_react2 = require("react");
var useCalendarHeader = ({ index }) => {
var _a, _b;
const {
type,
amountOfMonths,
dayRefs,
maxDate,
maxYear,
minDate,
minYear,
month,
nextMonth,
nextRef,
paginateBy,
prevMonth,
prevRef,
rangeYears,
setInternalYear,
setMonth,
setType,
setYear,
typeRef,
year
} = useCalendarContext();
const minRangeYear = (_a = rangeYears[0]) != null ? _a : minYear;
const maxRangeYear = (_b = rangeYears[rangeYears.length - 1]) != null ? _b : maxYear;
const onChangeType = (0, import_react2.useCallback)(() => {
switch (type) {
case "month":
setType("year", year, month.getMonth());
break;
case "date":
setType("month", year, month.getMonth());
break;
default:
break;
}
}, [month, setType, type, year]);
const onPrev = (0, import_react2.useCallback)(() => {
switch (type) {
case "year":
setInternalYear((prev) => prev - 12);
break;
case "month":
setYear((prev) => prev - 1);
break;
default:
dayRefs.current.clear();
setMonth((prev) => (0, import_dayjs3.default)(prev).subtract(paginateBy, "months").toDate());
break;
}
}, [dayRefs, paginateBy, setInternalYear, setMonth, setYear, type]);
const onNext = (0, import_react2.useCallback)(() => {
switch (type) {
case "year":
setInternalYear((prev) => prev + 12);
break;
case "month":
setYear((prev) => prev + 1);
break;
default:
dayRefs.current.clear();
setMonth((prev) => (0, import_dayjs3.default)(prev).add(paginateBy, "months").toDate());
break;
}
}, [dayRefs, paginateBy, setInternalYear, setMonth, setYear, type]);
(0, import_utils3.assignRef)(typeRef, onChangeType);
(0, import_utils3.assignRef)(prevRef, onPrev);
(0, import_utils3.assignRef)(nextRef, onNext);
const onKeyDown = (0, import_react2.useCallback)(
(ev) => {
const actions = {
ArrowDown: onChangeType,
ArrowLeft: () => {
const isDisabled2 = (() => {
switch (type) {
case "year":
return minRangeYear <= minYear;
case "month":
return year <= minYear;
default:
return !isMonthInRange({ date: prevMonth, maxDate, minDate });
}
})();
if (!isDisabled2) onPrev();
},
ArrowRight: () => {
const isDisabled2 = (() => {
switch (type) {
case "year":
return maxYear <= maxRangeYear;
case "month":
return maxYear <= year;
default:
return !isMonthInRange({ date: nextMonth, maxDate, minDate });
}
})();
if (!isDisabled2) onNext();
}
};
const action = actions[ev.key];
if (!action) return;
ev.preventDefault();
ev.stopPropagation();
action(ev);
},
[
onChangeType,
onPrev,
type,
minRangeYear,
minYear,
year,
prevMonth,
maxDate,
minDate,
onNext,
maxYear,
maxRangeYear,
nextMonth
]
);
const getContainerProps = (0, import_react2.useCallback)(
(props = {}) => ({
...props,
onKeyDown: (0, import_utils3.handlerAll)(onKeyDown, props.onKeyDown)
}),
[onKeyDown]
);
const getControlProps = (0, import_react2.useCallback)(
({ operation, ...props }) => {
const isPrev = operation === "prev";
const ariaLabel = `Go to ${isPrev ? "previous" : "next"} ${type === "date" ? "month" : "year"}`;
const isHidden = (() => {
switch (type) {
case "year":
if (isPrev) {
return minRangeYear <= minYear;
} else {
return maxYear <= maxRangeYear;
}
case "month":
if (isPrev) {
return year <= minYear;
} else {
return maxYear <= year;
}
default:
if (typeof index !== "number") return;
if (isPrev) {
return index !== 0 || !isMonthInRange({ date: prevMonth, maxDate, minDate });
} else {
return index + 1 !== amountOfMonths || !isMonthInRange({ date: nextMonth, maxDate, minDate });
}
}
})();
return {
"aria-label": ariaLabel,
...props,
"aria-disabled": (0, import_utils3.ariaAttr)(isHidden),
"data-disabled": (0, import_utils3.dataAttr)(isHidden),
"data-hidden": (0, import_utils3.dataAttr)(isHidden),
tabIndex: -1,
onClick: (0, import_utils3.handlerAll)(isPrev ? onPrev : onNext, props.onClick)
};
},
[
amountOfMonths,
index,
maxDate,
maxRangeYear,
maxYear,
minDate,
minRangeYear,
minYear,
nextMonth,
onNext,
onPrev,
prevMonth,
type,
year
]
);
const getLabelProps = (0, import_react2.useCallback)(
(props = {}) => {
return {
as: type !== "year" ? "button" : "span",
pointerEvents: type !== "year" ? "auto" : "none",
...props,
"aria-live": type !== "year" ? "polite" : void 0,
tabIndex: !!index ? -1 : 0,
onClick: (0, import_utils3.handlerAll)(props.onClick, onChangeType)
};
},
[index, onChangeType, type]
);
return { getContainerProps, getControlProps, getLabelProps };
};
// src/calendar-header.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var CalendarHeader = ({
className,
index,
label,
controlProps,
labelProps,
nextProps,
prevProps,
...rest
}) => {
const { type, styles, withControls, withHeader, withLabel } = useCalendarContext();
const { getContainerProps, getControlProps, getLabelProps } = useCalendarHeader({ index });
const css = {
alignItems: "center",
display: "flex",
w: "100%",
...styles.header
};
const { icon: iconOrProps, ...computedLabelProps } = labelProps != null ? labelProps : {};
return withHeader ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
import_core2.ui.div,
{
className: (0, import_utils4.cx)("ui-calendar__header", className),
__css: css,
...getContainerProps(rest),
children: [
withControls ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
CalendarControlPrev,
{
...getControlProps({
operation: "prev",
...controlProps,
...prevProps
})
}
) : null,
withLabel ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CalendarLabel, { ...getLabelProps({ ...computedLabelProps }), children: [
label,
type !== "year" ? (0, import_utils4.isValidElement)(iconOrProps) ? iconOrProps : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CalendarLabelIcon, { ...iconOrProps }) : null
] }) : null,
withControls ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
CalendarControlNext,
{
...getControlProps({
operation: "next",
...controlProps,
...nextProps
})
}
) : null
]
}
) : null;
};
CalendarHeader.displayName = "CalendarHeader";
CalendarHeader.__ui__ = "CalendarHeader";
var CalendarLabel = ({ className, ...rest }) => {
const { styles } = useCalendarContext();
const css = {
flex: 1,
fontSize: void 0,
fontWeight: "normal",
gap: 1,
h: "auto",
...styles.label
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_button.Button,
{
className: (0, import_utils4.cx)("ui-calendar__header__label", className),
variant: "ghost",
__css: css,
...rest
}
);
};
CalendarLabel.displayName = "CalendarLabel";
CalendarLabel.__ui__ = "CalendarLabel";
var CalendarLabelIcon = ({
className,
...rest
}) => {
const { styles } = useCalendarContext();
const css = {
...styles.labelIcon
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_icon.ChevronIcon,
{
className: (0, import_utils4.cx)("ui-calendar__header__label__icon", className),
__css: css,
...rest
}
);
};
CalendarLabelIcon.displayName = "CalendarLabelIcon";
CalendarLabelIcon.__ui__ = "CalendarLabelIcon";
var CalendarControlPrev = ({
className,
...rest
}) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
CalendarControl,
{
className: (0, import_utils4.cx)("ui-calendar__header__control--prev", className),
icon: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.ChevronIcon, { __css: { transform: "rotate(90deg)" } }),
operation: "prev",
...rest
}
);
};
CalendarControlPrev.displayName = "CalendarControlPrev";
CalendarControlPrev.__ui__ = "CalendarControlPrev";
var CalendarControlNext = ({
className,
...rest
}) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
CalendarControl,
{
className: (0, import_utils4.cx)("ui-calendar__header__control--next", className),
icon: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.ChevronIcon, { __css: { transform: "rotate(-90deg)" } }),
operation: "next",
...rest
}
);
};
CalendarControlNext.displayName = "CalendarControlNext";
CalendarControlNext.__ui__ = "CalendarControlNext";
var CalendarControl = ({
className,
operation,
...rest
}) => {
const { styles } = useCalendarContext();
const css = {
h: "auto",
minW: "auto",
...styles.control,
...styles[operation]
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_button.IconButton,
{
className: (0, import_utils4.cx)("ui-calendar__header__control", className),
variant: "ghost",
__css: css,
...rest
}
);
};
CalendarControl.displayName = "CalendarControl";
CalendarControl.__ui__ = "CalendarControl";
// src/use-year-list.tsx
var import_utils5 = require("@yamada-ui/utils");
var import_react3 = require("react");
var useYearList = () => {
var _a, _b;
const {
internalYear,
locale,
maxYear,
minYear,
month,
rangeYears,
setInternalYear,
setMonth,
setType,
setYear,
value: selectedValue,
year,
yearFormat,
yearRefs,
__selectType
} = useCalendarContext();
const multi = (0, import_utils5.isArray)(selectedValue);
const containerRef = (0, import_react3.useRef)(null);
const beforeInternalYear = (0, import_react3.useRef)(null);
const minRangeYear = (_a = rangeYears[0]) != null ? _a : minYear;
const maxRangeYear = (_b = rangeYears[rangeYears.length - 1]) != null ? _b : maxYear;
const minYearLabel = getFormattedLabel(minRangeYear, locale, yearFormat);
const maxYearLabel = getFormattedLabel(maxRangeYear, locale, yearFormat);
const label = `${minYearLabel} - ${maxYearLabel}`;
const ariaLabel = `From ${minYearLabel} to ${maxYearLabel}`;
const onFocusPrev = (0, import_react3.useCallback)(
(targetIndex) => {
if (targetIndex < 0) {
if (minRangeYear <= minYear) return;
setInternalYear((prev) => {
beforeInternalYear.current = prev;
return prev - 12;
});
} else {
const ref = yearRefs.current.get(targetIndex);
if (ref == null ? void 0 : ref.current) {
ref.current.focus();
ref.current.tabIndex = 0;
}
}
},
[minYear, minRangeYear, setInternalYear, yearRefs]
);
const onFocusNext = (0, import_react3.useCallback)(
(targetIndex) => {
if (11 < targetIndex) {
if (maxYear <= maxRangeYear) return;
setInternalYear((prev) => {
beforeInternalYear.current = prev;
return prev + 12;
});
} else {
const ref = yearRefs.current.get(targetIndex);
if (ref == null ? void 0 : ref.current) {
ref.current.focus();
ref.current.tabIndex = 0;
}
}
},
[maxYear, maxRangeYear, setInternalYear, yearRefs]
);
const onKeyDown = (0, import_react3.useCallback)(
(ev) => {
var _a2;
const focusedIndex = (_a2 = getFocused(yearRefs)) != null ? _a2 : 0;
const actions = {
ArrowDown: () => focusedIndex + 4 <= 11 ? onFocusNext(focusedIndex + 4) : {},
ArrowLeft: () => onFocusPrev(focusedIndex - 1),
ArrowRight: () => onFocusNext(focusedIndex + 1),
ArrowUp: () => focusedIndex - 4 >= 0 ? onFocusPrev(focusedIndex - 4) : {},
End: () => onFocusNext(11),
Home: () => onFocusPrev(0)
};
const action = actions[ev.key];
if (!action) return;
ev.preventDefault();
ev.stopPropagation();
disableAllTabIndex(yearRefs);
action(ev);
},
[onFocusNext, onFocusPrev, yearRefs]
);
const onClick = (0, import_react3.useCallback)(
(ev, year2) => {
ev.preventDefault();
ev.stopPropagation();
if ((0, import_utils5.isDisabled)(ev.target)) return;
setYear(year2);
setMonth((prev) => new Date(prev.setFullYear(year2)));
setType("month", year2, month.getMonth());
},
[month, setMonth, setType, setYear]
);
const getIsSelected = (0, import_react3.useCallback)(
(value) => {
var _a2;
if (__selectType === "date" || __selectType === "month") {
return year === value;
} else {
const year2 = !multi ? selectedValue == null ? void 0 : selectedValue.getFullYear() : (_a2 = selectedValue[0]) == null ? void 0 : _a2.getFullYear();
return year2 === value;
}
},
[__selectType, multi, selectedValue, year]
);
(0, import_utils5.useUpdateEffect)(() => {
if (!(0, import_utils5.isNumber)(beforeInternalYear.current)) return;
onShouldFocus(
yearRefs,
() => false,
beforeInternalYear.current < internalYear
);
beforeInternalYear.current = null;
}, [internalYear]);
(0, import_utils5.useUnmountEffect)(() => {
yearRefs.current.clear();
});
const getGridProps = (0, import_react3.useCallback)(
(props = {}, ref = null) => ({
ref: (0, import_utils5.mergeRefs)(ref, containerRef),
"aria-label": ariaLabel,
role: "grid",
...props,
onKeyDown: (0, import_utils5.handlerAll)(onKeyDown, props.onKeyDown)
}),
[ariaLabel, onKeyDown]
);
const getButtonProps = (0, import_react3.useCallback)(
({ index, value, ...props }, ref = null) => {
const controlled = (0, import_utils5.isNumber)(beforeInternalYear.current);
const selected = getIsSelected(value);
const disabled = value < minYear || value > maxYear;
yearRefs.current.set(index, (0, import_react3.createRef)());
let tabIndex = -1;
if (controlled) {
tabIndex = -1;
} else if (!rangeYears.includes(year) && rangeYears[0] === value) {
tabIndex = 0;
} else if (selected) {
tabIndex = 0;
}
return {
ref: (0, import_utils5.mergeRefs)(ref, yearRefs.current.get(index)),
disabled,
...props,
"aria-disabled": (0, import_utils5.ariaAttr)(disabled),
"aria-selected": (0, import_utils5.ariaAttr)(selected),
"data-disabled": (0, import_utils5.dataAttr)(disabled),
"data-selected": (0, import_utils5.dataAttr)(selected),
"data-value": value,
tabIndex,
onClick: (0, import_utils5.handlerAll)(props.onClick, (ev) => onClick(ev, value))
};
},
[getIsSelected, minYear, maxYear, yearRefs, rangeYears, year, onClick]
);
return { label, rangeYears, getButtonProps, getGridProps };
};
// src/year-list.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
var YearList = ({
className,
controlProps,
headerProps,
labelProps,
nextProps,
prevProps,
yearGridProps,
yearProps,
...rest
}) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
const { locale, styles, yearFormat } = useCalendarContext();
const { label, rangeYears, getButtonProps, getGridProps } = useYearList();
const { component: customYear, ...computedYearProps } = yearProps != null ? yearProps : {};
const w = (_a = rest.w) != null ? _a : rest.width;
const minW = (_b = rest.minW) != null ? _b : rest.minWidth;
const maxW = (_c = rest.maxW) != null ? _c : rest.maxWidth;
const h = (_d = rest.h) != null ? _d : rest.height;
const minH = (_e = rest.minH) != null ? _e : rest.minHeight;
const maxH = (_f = rest.maxH) != null ? _f : rest.maxHeight;
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_core3.ui.div, { __css: { ...styles.content }, ...(0, import_utils6.filterUndefined)(rest), children: [
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
CalendarHeader,
{
...{
...headerProps,
label,
controlProps,
labelProps,
nextProps,
prevProps
}
}
),
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
import_core3.ui.div,
{
className: (0, import_utils6.cx)("ui-calendar__year-list", className),
__css: {
display: "grid",
h: (_i = (_g = styles.content) == null ? void 0 : _g.h) != null ? _i : (_h = styles.content) == null ? void 0 : _h.height,
maxH: (_l = (_j = styles.content) == null ? void 0 : _j.maxH) != null ? _l : (_k = styles.content) == null ? void 0 : _k.maxHeight,
maxW: (_o = (_m = styles.content) == null ? void 0 : _m.maxW) != null ? _o : (_n = styles.content) == null ? void 0 : _n.maxWidth,
minH: (_r = (_p = styles.content) == null ? void 0 : _p.minH) != null ? _r : (_q = styles.content) == null ? void 0 : _q.minHeight,
minW: (_u = (_s = styles.content) == null ? void 0 : _s.minW) != null ? _u : (_t = styles.content) == null ? void 0 : _t.minWidth,
w: (_x = (_v = styles.content) == null ? void 0 : _v.w) != null ? _x : (_w = styles.content) == null ? void 0 : _w.width,
...styles.year
},
...getGridProps({
...(0, import_utils6.filterUndefined)({ h, maxH, maxW, minH, minW, w }),
...yearGridProps
}),
children: rangeYears.map((year, index) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
import_button2.Button,
{
className: "ui-calendar__year-list__button",
variant: "ghost",
__css: {
fontSize: void 0,
fontWeight: "normal",
h: "auto",
minW: "auto",
p: 0,
...styles.button
},
...getButtonProps({ ...computedYearProps, index, value: year }),
children: customYear ? customYear({ index, year }) : getFormattedLabel(year, locale, yearFormat)
},
index
))
}
)
] });
};
YearList.displayName = "YearList";
YearList.__ui__ = "YearList";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
YearList
});
//# sourceMappingURL=year-list.js.map