@yamada-ui/calendar
Version:
Yamada UI calendar component
438 lines (436 loc) • 10.2 kB
JavaScript
"use client"
import {
isAfterDate,
isBeforeDate
} from "./chunk-BPJGE3HG.mjs";
// src/use-calendar-picker.ts
import { layoutStyleProperties, useI18n, useTheme } from "@yamada-ui/core";
import {
formControlProperties,
useFormControlProps
} from "@yamada-ui/form-control";
import { useDisclosure } from "@yamada-ui/use-disclosure";
import { useOutsideClick } from "@yamada-ui/use-outside-click";
import {
dataAttr,
getEventRelatedTarget,
handlerAll,
isContains,
mergeRefs,
pickObject,
splitObject
} from "@yamada-ui/utils";
import dayjs from "dayjs";
import { useCallback, useRef } from "react";
var useCalendarPicker = (props) => {
var _a, _b;
const { locale: defaultLocale } = useI18n();
const { theme } = 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
} = useFormControlProps(props);
const { "aria-readonly": _ariaReadonly, ...formControlProps } = pickObject(
rest,
formControlProperties
);
const [containerProps, inputProps] = splitObject(rest, layoutStyleProperties);
const {
open,
onClose,
onOpen: onInternalOpen
} = useDisclosure({
defaultIsOpen,
defaultOpen,
isOpen,
open: openProp,
onClose: onCloseProp,
onOpen: onOpenProp
});
const containerRef = useRef(null);
const inputRef = useRef(null);
const { disabled, readOnly } = formControlProps;
const stringToDate = useCallback(
(value2) => {
let date = parseDate == null ? void 0 : parseDate(value2);
if (!date && dayjs(value2).isValid())
date = dayjs(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 = 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 dayjs(value2).locale(locale).format(inputFormat);
},
[allowInputBeyond, excludeDate, inputFormat, locale, maxDate, minDate]
);
const onOpen = 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 = useCallback(() => {
var _a2;
if (open) {
if (autoFocus && allowInput) (_a2 = inputRef.current) == null ? void 0 : _a2.focus();
} else {
onOpen();
}
}, [autoFocus, allowInput, open, onOpen]);
const onFocus = useCallback(() => {
if (!open) onOpen();
}, [open, onOpen]);
const onBlur = useCallback(
(ev) => {
const relatedTarget = getEventRelatedTarget(ev);
if (isContains(containerRef.current, relatedTarget)) return;
if (!closeOnBlur) return;
if (open) onClose();
},
[closeOnBlur, open, onClose]
);
const onKeyDown = 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 = 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]
);
useOutsideClick({
ref: containerRef,
enabled: open && closeOnBlur,
handler: onClose
});
const getContainerProps = useCallback(
(props2 = {}, ref = null) => ({
ref: mergeRefs(containerRef, ref),
...containerProps,
...formControlProps,
...props2,
onBlur: handlerAll(
props2.onBlur,
rest.onBlur,
onBlur
),
onClick: handlerAll(props2.onClick, rest.onClick, onClick)
}),
[containerProps, formControlProps, onBlur, onClick, rest]
);
const getPopoverProps = 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 = useCallback(
(props2 = {}, ref = null) => {
return {
"aria-haspopup": "dialog",
"data-active": dataAttr(open),
"data-not-allowed": dataAttr(!readOnly && !disabled && !allowInput),
role: "combobox",
tabIndex: -1,
...formControlProps,
...props2,
ref: mergeRefs(inputRef, ref),
onFocus: handlerAll(props2.onFocus, rest.onFocus, onFocus),
onKeyDown: handlerAll(
props2.onKeyDown,
rest.onKeyDown,
onKeyDown
)
};
},
[
allowInput,
disabled,
readOnly,
formControlProps,
open,
rest,
onFocus,
onKeyDown
]
);
const getCalendarProps = 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 = useCallback(
({ clear, ...props2 }) => ({
...props2,
...formControlProps,
onClick: 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
};
};
export {
useCalendarPicker
};
//# sourceMappingURL=chunk-RUQS2HL6.mjs.map