@yamada-ui/calendar
Version:
Yamada UI calendar component
464 lines (461 loc) • 12.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/use-calendar-picker.ts
var use_calendar_picker_exports = {};
__export(use_calendar_picker_exports, {
useCalendarPicker: () => useCalendarPicker
});
module.exports = __toCommonJS(use_calendar_picker_exports);
var import_core = require("@yamada-ui/core");
var import_form_control = require("@yamada-ui/form-control");
var import_use_disclosure = require("@yamada-ui/use-disclosure");
var import_use_outside_click = require("@yamada-ui/use-outside-click");
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 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");
// src/use-calendar-picker.ts
var useCalendarPicker = (props) => {
var _a, _b;
const { locale: defaultLocale } = (0, import_core.useI18n)();
const { theme } = (0, import_core.useTheme)();
const themeLocale = (_b = (_a = theme.__config) == null ? void 0 : _a.date) == null ? void 0 : _b.locale;
const {
type,
allowInput = true,
allowInputBeyond = false,
amountOfMonths,
animation,
autoFocus = true,
boundary,
calendarColorScheme,
calendarSize,
calendarVariant,
closeDelay,
closeOnBlur = true,
closeOnEsc = true,
dateFormat,
defaultIsOpen,
defaultMonth,
defaultOpen,
defaultType,
defaultValue,
disableOutsideDays,
duration = 0.2,
enableMultiple,
enableRange,
eventListeners,
excludeDate,
firstDayOfWeek,
flip,
gutter,
hiddenOutsideDays,
holidays,
inputFormat = "YYYY/MM/DD",
isLazy,
isOpen,
lazy = isLazy,
lazyBehavior,
locale = themeLocale != null ? themeLocale : defaultLocale,
matchWidth,
maxDate,
maxSelectValues,
minDate,
minSelectValues,
modifiers,
month,
monthFormat,
offset,
open: openProp,
openDelay,
paginateBy,
parseDate,
pattern = /[^0-9\-\/]/g,
placement = "bottom-start",
preventOverflow,
strategy,
today,
value,
weekdayFormat,
weekendDays,
withControls,
withHeader,
withLabel,
withWeekdays,
yearFormat,
calendarProps,
onChange,
onChangeMonth,
onChangeType,
onClear: onClearProp,
onClose: onCloseProp,
onDelete,
onEnter,
onOpen: onOpenProp,
__selectType,
...rest
} = (0, import_form_control.useFormControlProps)(props);
const { "aria-readonly": _ariaReadonly, ...formControlProps } = (0, import_utils2.pickObject)(
rest,
import_form_control.formControlProperties
);
const [containerProps, inputProps] = (0, import_utils2.splitObject)(rest, import_core.layoutStyleProperties);
const {
open,
onClose,
onOpen: onInternalOpen
} = (0, import_use_disclosure.useDisclosure)({
defaultIsOpen,
defaultOpen,
isOpen,
open: openProp,
onClose: onCloseProp,
onOpen: onOpenProp
});
const containerRef = (0, import_react.useRef)(null);
const inputRef = (0, import_react.useRef)(null);
const { disabled, readOnly } = formControlProps;
const stringToDate = (0, import_react.useCallback)(
(value2) => {
let date = parseDate == null ? void 0 : parseDate(value2);
if (!date && (0, import_dayjs2.default)(value2).isValid())
date = (0, import_dayjs2.default)(value2, inputFormat, locale).toDate();
if (date == null) return void 0;
if (excludeDate == null ? void 0 : excludeDate(date)) return void 0;
if (!allowInputBeyond) {
if (maxDate && isAfterDate(date, maxDate)) date = maxDate;
if (minDate && isBeforeDate(date, minDate)) date = minDate;
}
return date;
},
[
allowInputBeyond,
excludeDate,
inputFormat,
locale,
maxDate,
minDate,
parseDate
]
);
const dateToString = (0, import_react.useCallback)(
(value2) => {
if (value2 == null) return void 0;
if (excludeDate == null ? void 0 : excludeDate(value2)) return void 0;
if (!allowInputBeyond) {
if (maxDate && isAfterDate(value2, maxDate)) value2 = maxDate;
if (minDate && isBeforeDate(value2, minDate)) value2 = minDate;
}
return (0, import_dayjs2.default)(value2).locale(locale).format(inputFormat);
},
[allowInputBeyond, excludeDate, inputFormat, locale, maxDate, minDate]
);
const onOpen = (0, import_react.useCallback)(() => {
var _a2;
if (disabled || readOnly) return;
onInternalOpen();
if (autoFocus && allowInput) (_a2 = inputRef.current) == null ? void 0 : _a2.focus();
}, [autoFocus, allowInput, disabled, readOnly, onInternalOpen]);
const onClick = (0, import_react.useCallback)(() => {
var _a2;
if (open) {
if (autoFocus && allowInput) (_a2 = inputRef.current) == null ? void 0 : _a2.focus();
} else {
onOpen();
}
}, [autoFocus, allowInput, open, onOpen]);
const onFocus = (0, import_react.useCallback)(() => {
if (!open) onOpen();
}, [open, onOpen]);
const onBlur = (0, import_react.useCallback)(
(ev) => {
const relatedTarget = (0, import_utils2.getEventRelatedTarget)(ev);
if ((0, import_utils2.isContains)(containerRef.current, relatedTarget)) return;
if (!closeOnBlur) return;
if (open) onClose();
},
[closeOnBlur, open, onClose]
);
const onKeyDown = (0, import_react.useCallback)(
(ev) => {
if (ev.key === " ") ev.key = ev.code;
if (disabled || readOnly) return;
const actions = {
Backspace: onDelete,
Enter: !open ? (ev2) => {
ev2.preventDefault();
ev2.stopPropagation();
onOpen();
} : onEnter,
Escape: closeOnEsc ? (ev2) => {
ev2.preventDefault();
ev2.stopPropagation();
onClose();
} : void 0,
Space: !open ? (ev2) => {
ev2.preventDefault();
ev2.stopPropagation();
onOpen();
} : void 0
};
const action = actions[ev.key];
if (!action) return;
action(ev);
},
[closeOnEsc, disabled, readOnly, open, onClose, onEnter, onDelete, onOpen]
);
const onClear = (0, import_react.useCallback)(
(ev) => {
var _a2;
ev.stopPropagation();
onClearProp == null ? void 0 : onClearProp();
if (autoFocus && allowInput && open) (_a2 = inputRef.current) == null ? void 0 : _a2.focus();
},
[autoFocus, allowInput, open, onClearProp]
);
(0, import_use_outside_click.useOutsideClick)({
ref: containerRef,
enabled: open && closeOnBlur,
handler: onClose
});
const getContainerProps = (0, import_react.useCallback)(
(props2 = {}, ref = null) => ({
ref: (0, import_utils2.mergeRefs)(containerRef, ref),
...containerProps,
...formControlProps,
...props2,
onBlur: (0, import_utils2.handlerAll)(
props2.onBlur,
rest.onBlur,
onBlur
),
onClick: (0, import_utils2.handlerAll)(props2.onClick, rest.onClick, onClick)
}),
[containerProps, formControlProps, onBlur, onClick, rest]
);
const getPopoverProps = (0, import_react.useCallback)(
(props2) => ({
animation,
boundary,
closeDelay,
duration,
eventListeners,
flip,
gutter,
lazy,
lazyBehavior,
matchWidth,
modifiers,
offset,
openDelay,
placement,
preventOverflow,
strategy,
...props2,
closeOnBlur: false,
closeOnButton: false,
isOpen: open,
trigger: "never",
onClose,
onOpen
}),
[
animation,
boundary,
closeDelay,
duration,
eventListeners,
flip,
gutter,
open,
lazy,
lazyBehavior,
matchWidth,
modifiers,
offset,
onClose,
onOpen,
openDelay,
placement,
preventOverflow,
strategy
]
);
const getFieldProps = (0, import_react.useCallback)(
(props2 = {}, ref = null) => {
return {
"aria-haspopup": "dialog",
"data-active": (0, import_utils2.dataAttr)(open),
"data-not-allowed": (0, import_utils2.dataAttr)(!readOnly && !disabled && !allowInput),
role: "combobox",
tabIndex: -1,
...formControlProps,
...props2,
ref: (0, import_utils2.mergeRefs)(inputRef, ref),
onFocus: (0, import_utils2.handlerAll)(props2.onFocus, rest.onFocus, onFocus),
onKeyDown: (0, import_utils2.handlerAll)(
props2.onKeyDown,
rest.onKeyDown,
onKeyDown
)
};
},
[
allowInput,
disabled,
readOnly,
formControlProps,
open,
rest,
onFocus,
onKeyDown
]
);
const getCalendarProps = (0, import_react.useCallback)(
(props2) => ({
...props2,
type,
colorScheme: calendarColorScheme,
size: calendarSize,
variant: calendarVariant,
amountOfMonths,
dateFormat,
defaultMonth,
defaultType,
disableOutsideDays,
firstDayOfWeek,
hiddenOutsideDays,
holidays,
maxSelectValues,
minSelectValues,
month,
monthFormat,
paginateBy,
today,
weekdayFormat,
weekendDays,
withControls,
withHeader,
withLabel,
withWeekdays,
yearFormat,
onChangeMonth,
onChangeType,
...calendarProps,
defaultValue,
enableMultiple,
enableRange,
excludeDate,
locale,
maxDate,
minDate,
value,
onChange,
__selectType
}),
[
hiddenOutsideDays,
maxSelectValues,
minSelectValues,
enableMultiple,
enableRange,
amountOfMonths,
calendarColorScheme,
calendarProps,
calendarSize,
calendarVariant,
dateFormat,
defaultMonth,
defaultType,
defaultValue,
disableOutsideDays,
excludeDate,
firstDayOfWeek,
holidays,
locale,
maxDate,
minDate,
month,
monthFormat,
onChange,
onChangeMonth,
onChangeType,
paginateBy,
today,
type,
value,
weekdayFormat,
weekendDays,
withControls,
withHeader,
withLabel,
withWeekdays,
yearFormat,
__selectType
]
);
const getIconProps = (0, import_react.useCallback)(
({ clear, ...props2 }) => ({
...props2,
...formControlProps,
onClick: (0, import_utils2.handlerAll)(props2.onClick, clear ? onClear : void 0)
}),
[formControlProps, onClear]
);
return {
allowInput,
containerRef,
dateToString,
inputFormat,
inputRef,
locale,
open,
pattern,
stringToDate,
formControlProps,
getCalendarProps,
getContainerProps,
getFieldProps,
getIconProps,
getPopoverProps,
inputProps,
onClose,
onOpen
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useCalendarPicker
});
//# sourceMappingURL=use-calendar-picker.js.map