@yamada-ui/calendar
Version:
Yamada UI calendar component
435 lines (429 loc) • 13.3 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-header.tsx
var calendar_header_exports = {};
__export(calendar_header_exports, {
CalendarHeader: () => CalendarHeader
});
module.exports = __toCommonJS(calendar_header_exports);
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 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;
};
// 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 isDisabled = (() => {
switch (type) {
case "year":
return minRangeYear <= minYear;
case "month":
return year <= minYear;
default:
return !isMonthInRange({ date: prevMonth, maxDate, minDate });
}
})();
if (!isDisabled) onPrev();
},
ArrowRight: () => {
const isDisabled = (() => {
switch (type) {
case "year":
return maxYear <= maxRangeYear;
case "month":
return maxYear <= year;
default:
return !isMonthInRange({ date: nextMonth, maxDate, minDate });
}
})();
if (!isDisabled) 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";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CalendarHeader
});
//# sourceMappingURL=calendar-header.js.map