UNPKG

@fremtind/jkl-datepicker-react

Version:
103 lines (102 loc) 2.69 kB
import startOfDay from "date-fns/startOfDay"; import { useState } from "react"; import { composeEventHandlers, subtractMonth, addMonth, isBackDisabled, isForwardDisabled, getCalendars } from "./utils"; function isOffsetControlled(propOffset) { return propOffset !== void 0; } function getOffset(prop, state) { return isOffsetControlled(prop) ? prop : state; } function getDateProps(onDateSelected, { onClick, dateObj }) { return { onClick: composeEventHandlers(onClick, (event) => { onDateSelected(dateObj, event); }), disabled: !dateObj.selectable, "aria-pressed": dateObj.selected, role: "button" }; } function getBackProps({ minDate, offsetMonth, handleOffsetChanged }, { onClick, offset = 1, calendars }) { const label = "G\xE5 tilbake ".concat(offset, " m\xE5ned").concat(offset === 1 ? "" : "er"); return { onClick: composeEventHandlers(onClick, () => { handleOffsetChanged( offsetMonth - subtractMonth({ calendars, offset, minDate }) ); }), disabled: isBackDisabled({ calendars, minDate }), "aria-label": label, title: label }; } function getForwardProps({ maxDate, offsetMonth, handleOffsetChanged }, { onClick, offset = 1, calendars }) { const label = "G\xE5 frem ".concat(offset, " m\xE5ned").concat(offset === 1 ? "" : "er"); return { onClick: composeEventHandlers(onClick, () => { handleOffsetChanged( offsetMonth + addMonth({ calendars, offset, maxDate }) ); }), disabled: isForwardDisabled({ calendars, maxDate }), "aria-label": label, title: label }; } function useCalendar({ date = startOfDay(/* @__PURE__ */ new Date()), maxDate, minDate, monthsToDisplay = 1, firstDayOfWeek = 0, showOutsideDays = true, offset, onDateSelected, onOffsetChanged, selected }) { const [stateOffset, setStateOffset] = useState(0); const offsetMonth = getOffset(offset, stateOffset); function handleOffsetChanged(newOffset) { if (!isOffsetControlled(offset)) { setStateOffset(newOffset); } onOffsetChanged(newOffset); } const calendars = getCalendars({ date, selected, monthsToDisplay, minDate, maxDate, offset: offsetMonth, firstDayOfWeek, showOutsideDays }); return { calendars, getDateProps: getDateProps.bind(null, onDateSelected), getBackProps: getBackProps.bind(null, { minDate, offsetMonth, handleOffsetChanged }), getForwardProps: getForwardProps.bind(null, { maxDate, offsetMonth, handleOffsetChanged }), handleOffsetChanged }; } export { useCalendar }; //# sourceMappingURL=useCalendar.js.map