@gsretail.com/gui-core
Version:
A skeleton to create your own React component library using Rollup, TypeScript, Sass and Storybook
2 lines (1 loc) • 2.07 kB
JavaScript
import t from"../../../@babel/runtime/helpers/esm/extends.js";import*as n from"react";import{useIsDayDisabled as e}from"../internals/hooks/validation/useDateValidation.js";import{useNow as i,useUtils as a}from"../internals/hooks/useUtils.js";const o=(n,e,i)=>(a,o)=>{switch(o.type){case"changeMonth":return t({},a,{slideDirection:o.direction,currentMonth:o.newMonth,isMonthSwitchingAnimating:!n});case"finishMonthSwitchingAnimation":return t({},a,{isMonthSwitchingAnimating:!1});case"changeFocusedDay":{if(null!=a.focusedDay&&null!=o.focusedDay&&i.isSameDay(o.focusedDay,a.focusedDay))return a;const s=null!=o.focusedDay&&!e&&!i.isSameMonth(a.currentMonth,o.focusedDay);return t({},a,{focusedDay:o.focusedDay,isMonthSwitchingAnimating:s&&!n&&!o.withoutMonthSwitchingAnimation,currentMonth:s?i.startOfMonth(o.focusedDay):a.currentMonth,slideDirection:null!=o.focusedDay&&i.isAfterDay(o.focusedDay,a.currentMonth)?"left":"right"})}default:throw new Error("missing support")}},s=({date:s,defaultCalendarMonth:r,disableFuture:h,disablePast:c,disableSwitchToMonthOnDayFocus:u=!1,maxDate:l,minDate:d,onMonthChange:D,reduceAnimations:M,shouldDisableDate:f})=>{var m;const g=i(),y=a(),w=n.useRef(o(Boolean(M),u,y)).current,[b,S]=n.useReducer(w,{isMonthSwitchingAnimating:!1,focusedDay:s||g,currentMonth:y.startOfMonth(null!=(m=null!=s?s:r)?m:g),slideDirection:"left"}),p=n.useCallback((n=>{S(t({type:"changeMonth"},n)),D&&D(n.newMonth)}),[D]),A=n.useCallback((t=>{const n=null!=t?t:g;y.isSameMonth(n,b.currentMonth)||p({newMonth:y.startOfMonth(n),direction:y.isAfterDay(n,b.currentMonth)?"left":"right"})}),[b.currentMonth,p,g,y]),C=e({shouldDisableDate:f,minDate:d,maxDate:l,disableFuture:h,disablePast:c}),k=n.useCallback((()=>{S({type:"finishMonthSwitchingAnimation"})}),[]),F=n.useCallback(((t,n)=>{C(t)||S({type:"changeFocusedDay",focusedDay:t,withoutMonthSwitchingAnimation:n})}),[C]);return{calendarState:b,changeMonth:A,changeFocusedDay:F,isDateDisabled:C,onMonthSwitchingAnimationEnd:k,handleChangeMonth:p}};export{o as createCalendarStateReducer,s as useCalendarState};