UNPKG

@kubit-ui-web/react-components

Version:

Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience

74 lines 3.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Calendar = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const stylesName_1 = require("../../constants/stylesName/stylesName"); const useStyles_1 = require("../../hooks/useStyles/useStyles"); const errorBoundary_1 = require("../../provider/errorBoundary/errorBoundary"); const fallbackComponent_1 = require("../../provider/errorBoundary/fallbackComponent"); const calendarStandAlone_1 = require("./calendarStandAlone"); const variant_1 = require("./types/variant"); const CalendarComponent = react_1.default.forwardRef(({ hasRange = false, variant = variant_1.CalendarVariantType.DEFAULT, maxDate = new Date(), defaultCurrentDate, onSelectedDateChange, ctv, ...props }, ref) => { const styles = (0, useStyles_1.useStyles)(stylesName_1.STYLES_NAME.CALENDAR, variant, ctv); const getCurrentDate = () => { const _defaultCurrentDate = defaultCurrentDate ?? new Date(); if (!hasRange && props.selectedDate) { return props.selectedDate; } if (date[1]) { return date[1]; } else if (date[0]) { return date[0]; } return _defaultCurrentDate; }; const getInitialDate = () => { let initialDate; if (hasRange) { initialDate = [ props.selectedDate ? props.selectedDate : null, props.secondSelectedDate ? props.secondSelectedDate : null, ]; } else if (props.selectedDate) { try { initialDate = [props.selectedDate]; } catch { initialDate = [null]; } } else { initialDate = [null]; } return initialDate; }; react_1.default.useEffect(() => { setDate(getInitialDate()); }, [props.selectedDate]); const [date, setDate] = react_1.default.useState(getInitialDate()); const [currentDate, setCurrentDate] = react_1.default.useState(getCurrentDate()); react_1.default.useEffect(() => { setCurrentDate(getCurrentDate()); }, [defaultCurrentDate]); return ((0, jsx_runtime_1.jsx)(calendarStandAlone_1.CalendarStandAlone, { ...props, ref: ref, currentDate: currentDate, hasRange: hasRange, maxDate: maxDate, selectedDate: date, setCurrentDate: setCurrentDate, setSelectedDate: date => { setDate(date); onSelectedDateChange?.(date); }, styles: styles })); }); CalendarComponent.displayName = 'CalendarComponent'; const CalendarBoundary = (props, ref) => ((0, jsx_runtime_1.jsx)(errorBoundary_1.ErrorBoundary, { fallBackComponent: (0, jsx_runtime_1.jsx)(fallbackComponent_1.FallbackComponent, { children: (0, jsx_runtime_1.jsx)(calendarStandAlone_1.CalendarStandAlone, { ...props, ref: ref }) }), children: (0, jsx_runtime_1.jsx)(CalendarComponent, { ...props, ref: ref }) })); /** * @description * Calendar component is a selector of dates, that includes a month and year selector. * @param {React.PropsWithChildren<ICalendar<V>>} props * @returns {JSX.Element} */ const Calendar = react_1.default.forwardRef(CalendarBoundary); exports.Calendar = Calendar; //# sourceMappingURL=calendar.js.map