react-box-tools
Version:
Box tools react components, utils and hooks
2 lines (1 loc) • 8.92 kB
JavaScript
"use strict";const t=require("react/jsx-runtime"),A=require("./clsx-CeOg4f1g.cjs"),k=require("react"),re=require("./dropdown-CkR4kz4M.cjs"),H=require("./right-CS_95Vci.cjs"),B=u=>{const{month:w}=u,{daysOfWeek:y,isRefillDay:l,refilldays:s=!1,getClassDay:i,event:p}=k.useContext(P);return t.jsxs("table",{className:"month",children:[t.jsx("thead",{className:"week-days",children:t.jsx("tr",{children:y.map((g,h)=>t.jsx("th",{children:g.substring(0,2)},h))})}),t.jsx("tbody",{className:"month-days",children:w.map((g,h)=>t.jsx("tr",{children:g.map((r,c)=>t.jsx("td",{onClick:()=>p.onSelect(r),className:A.clsx(`day ${i(r)}`),onMouseDown:()=>p.onRangeStart(r),onMouseEnter:()=>p.onRangeHover(r),onMouseUp:()=>p.onRangeEnd(r),children:l(r)&&!s?"":r.key},c))},h))})]})};function ce(u,w,y="es"){const s=new Intl.DateTimeFormat(y,{weekday:"short",day:"numeric",month:"short",year:"numeric"}).formatToParts(u),i=w.replace(/(ddd|dd|mmm|mm|yyyy)/g,p=>{var g,h,r,c,S;switch(p){case"ddd":return((g=s.find(D=>D.type==="weekday"))==null?void 0:g.value)??"";case"dd":return((r=(h=s.find(D=>D.type==="day"))==null?void 0:h.value)==null?void 0:r.padStart(2,"0"))??"";case"mmm":return ae(((c=s.find(D=>D.type==="month"))==null?void 0:c.value)??"");case"mm":return(u.getMonth()+1).toString().padStart(2,"0");case"yyyy":return((S=s.find(D=>D.type==="year"))==null?void 0:S.value)??"";default:return p}});return ae(i)}function ae(u){return u.charAt(0).toUpperCase()+u.slice(1)}const P=k.createContext({culture:"es",date:{today:0,instance:new Date,month:{key:0,name:"",year:0},year:0,surroundingYear:0,manyYears:!1},monthList:[],monthRows:[],daysOfWeek:[],prevMonth:()=>{},nextMonth:()=>{},selectMonth:()=>{},goToMonth:()=>{},setYear:()=>{},goToYear:()=>{},setSurroundingYear:()=>{},yearList:[],isRefillDay:()=>!1,getClassDay:()=>"",event:{onSelect:()=>{},onRangeStart:()=>{},onRangeHover:()=>{},onRangeEnd:()=>{}}}),le=u=>{const{children:w,culture:y="en",monthsToShow:l,refilldays:s=!1,range:i=!1,todayText:p,onSelected:g,onSelectedRange:h,onLoad:r,value:c,minDate:S,maxDate:D}=u,R=ie({culture:y,monthsToShow:l,range:i,onSelected:g,onSelectedRange:h,onLoad:r,value:c,minDate:S,maxDate:D}),j={monthsToShow:l,refilldays:s,todayText:p,...R};return t.jsx(P.Provider,{value:j,children:w})},be=({monthDropDown:u,yearDropDown:w,icons:y={}})=>{const{date:l,monthRows:s,monthList:i,prevMonth:p,selectMonth:g,setSurroundingYear:h,yearList:r,monthsToShow:c=1,goToYear:S,nextMonth:D}=k.useContext(P),R=l.month,j=l.year,N=l.manyYears,d=()=>{const Y=s[s.length-1],a=Y[Y.length-1][0].find(O=>O.type==="month");let F=R;return i.map(O=>O.map(E=>(E.key===(a==null?void 0:a.month)&&(F=E),!0))),{month:c>1?`${R.name} - ${F.name}`:R.name,year:N?`${j} - ${a==null?void 0:a.year}`:j}};return t.jsxs("div",{className:"dp-controls",children:[t.jsx("button",{className:"control prev-month",onClick:p,children:y.prev??t.jsx(H.ChevronLeftIcon,{})}),t.jsx(re.DropDown,{mode:"click",width:"auto",...u,target:t.jsx("button",{className:"control current-month",children:d().month}),className:`month-list ${u.className}`,children:t.jsx("table",{className:"months",children:t.jsx("tbody",{children:i.map((Y,C)=>t.jsx("tr",{children:Y.map((a,F)=>t.jsx("td",{className:A.clsx(`${a.type==="dsbl"?"disabled":"drp-item"} ${a.key===R.key&&"current-month"}`),onClick:()=>{a.type!=="dsbl"&&g(a)},children:`${a.name}`},F))},C))})})}),t.jsx(re.DropDown,{mode:"click",width:"auto",...w,target:t.jsx("button",{className:"control current-year",children:d().year}),className:`year-list ${u.className}`,children:t.jsxs("div",{className:"controls",children:[t.jsx("button",{className:"prev-year",onClick:()=>{h(l.surroundingYear-3)},children:y.prev??t.jsx(H.ChevronLeftIcon,{})}),t.jsx("table",{className:"years",children:t.jsx("tbody",{children:r.map((Y,C)=>t.jsx("tr",{children:Y.map((a,F)=>t.jsx("td",{className:A.clsx(`${a.type==="dsbl"?"disabled":"drp-item"} ${a.key===j&&"current-year"}`),onClick:()=>{a.type!=="dsbl"&&S(a.key)},children:a.key},F))},C))})}),t.jsx("button",{className:"next-year",onClick:()=>{h(l.surroundingYear+3)},children:y.next??t.jsx(H.ChevronRightIcon,{})})]})}),t.jsx("button",{className:"control next-month",onClick:D,children:y.next??t.jsx(H.ChevronRightIcon,{})})]})},Se=()=>{const{monthsToShow:u=1,monthRows:w,culture:y,date:l,todayText:s={}}=k.useContext(P),{show:i=!0,format:p="ddd, dd mmm yyyy"}=s;return t.jsxs(t.Fragment,{children:[u===1?t.jsx(t.Fragment,{children:w.map(g=>g.map((h,r)=>t.jsx(B,{month:h},r)))}):t.jsx("table",{className:"calendar",children:t.jsx("tbody",{children:w.map((g,h)=>t.jsx("tr",{className:"items",children:g.map((r,c)=>t.jsx("td",{children:t.jsx(B,{month:r})},c))},h))})}),i&&t.jsx("span",{className:"today-txt",children:ce(l.instance,p,y)})]})},Re=Object.assign(le,{Months:Se,Controls:be}),ie=u=>{const{culture:w="es",monthsToShow:y=1,range:l=!1,minDate:s,maxDate:i,onSelected:p=()=>{},onSelectedRange:g=()=>{},onLoad:h=()=>{},value:r}=u,c=new Date,S=c.getFullYear(),D=c.getDate(),R=c.getMonth(),j=s==null?void 0:s.getTime(),N=i==null?void 0:i.getTime(),[d,Y]=k.useState({}),C=e=>new Date(e.year,e.month,e.key),a=e=>e.type==="prev"||e.type==="next",F=e=>{a(e)||l||(d.day=e,p(C(e)))},O=e=>{if(!(a(e)||!l)){var n=C(e),o=d.from??new Date,m=d.to??new Date;if(m.getTime()>o.getTime()&&d.range){G(e);return}Y({day:e,from:n,to:n,range:!0})}},E=e=>{a(e)||!l||d.range&&Y({...d,to:C(e)})},G=e=>{var n=d.from??new Date,o=d.to??new Date;a(e)||!l||n.getTime()===o.getTime()||(Y({...d,to:C(e),range:!1}),g({from:n,to:o}))},me=e=>{var n=a(e);const o=e.key===D&&e.month===R&&e.year===S;var m=d.day;const b=m&&m.key===e.key&&m.month===e.month&&m.year===e.year;let v,f="";if(v=n?"refill":o?"today":b?"selected":"",!n&&l&&d.from&&d.to&&e.type==="month"){const{from:$,to:L}=d,I=C(e);f=I>=$&&I<=L?"range":""}return`${v} ${f}`},J=new Intl.DateTimeFormat(w,{year:"numeric",month:"long",day:"numeric",weekday:"long"}),he=()=>{const e=[];for(let n=0;n<7;n++){const o=new Date(2e3,0,n+3),m=J.format(o);e.push(m)}return e},K=e=>{var n;return((n=J.formatToParts(e).find(o=>o.type==="month"))==null?void 0:n.value)??""},[M,de]=k.useState({key:c.getMonth(),name:K(c),year:c.getFullYear()}),W=e=>{c.setMonth(e);const n=new Date(M.year,e,1);de({key:e,name:K(n),year:c.getFullYear()})},ue=()=>{const e=new Intl.DateTimeFormat(w,{month:"long"}),n=[];for(let f=0;f<12;f++){var o=new Date(x,f,j?s==null?void 0:s.getDate():1),m=N?new Date(x,f).getTime():0,b=!0;j&&j>o.getTime()&&(b=!1),N&&N<m&&(b=!1),n.push({key:f,name:e.format(o),year:x,type:b?"":"dsbl"})}const v=[];for(;n.length>0;)v.push(n.splice(0,2));return v},[x,Q]=k.useState(c.getFullYear()),[_,ye]=k.useState(x),U=e=>{Q(e),c.setFullYear(e)},ge=()=>{const e=[];for(let m=_-7;m<=_+4;m++){let b=(s==null?void 0:s.getFullYear())??0,v=(i==null?void 0:i.getFullYear())??0,f=!0;j&&b>m&&(f=!1),N&&v<m&&(f=!1);var n={key:m,type:f?"":"dsbl"};e.push(n)}const o=[];for(;e.length>0;)o.push(e.splice(0,3));return o},[q,z]=k.useState(M.key),fe=()=>{if(i){var e=new Date(x,q),n=new Date(i.getFullYear(),i.getMonth());if(e.getTime()===n.getTime())return}let o=q+1;o===12&&(o=0,U(x+1)),z(o),W(o)},pe=()=>{if(s){var e=new Date(x,q),n=new Date(s.getFullYear(),s.getMonth());if(e.getTime()===n.getTime())return}let o=q-1;o<0&&(o=11,U(x-1)),z(o),W(o)},xe=e=>{z(e.key),W(e.key)},ve=(e=1)=>{const n=[];for(let v=1;v<=e;v++){const f=new Date(x,M.key+v,0),$=f.getMonth(),L=f.getFullYear(),I=f.getDate(),V=new Date(x,v===1?M.key:M.key+(v-1),1).getDay(),Ye=M.key===11?0:M.key+v,Te=M.key===11?x+1:x,X=new Date(Te,Ye-1,0).getDate(),Z=V===0?6:V-1,Ce=42-(I+Z),ee=[];for(let T=X-Z+1;T<=X;T++)ee.push({key:T,month:$,year:L,type:"prev"});const te=[];for(let T=1;T<=I;T++){var o=new Date(L,$,T).getTime(),m=!0;j&&j>o&&(m=!1),N&&N<o&&(m=!1),te.push({key:T,month:$,year:L,type:m?"month":"next"})}const ne=[];for(let T=1;T<=Ce;T++)ne.push({key:T,month:$,year:L,type:"next"});const oe=[],se=[...ee,...te,...ne];for(;se.length>0;)oe.push(se.splice(0,7));M.key+v,n.push(oe)}const b=[];for(;n.length>0;)b.push(n.splice(0,3));return b},we=y>1&&M.key===11,ke=k.useMemo(()=>ve(y),[y,x,M]),De=k.useMemo(()=>ue(),[x,M]),Me=k.useMemo(()=>ge(),[_]),je={onSelect:F,onRangeStart:O,onRangeHover:E,onRangeEnd:G};return k.useLayoutEffect(()=>{if(r&&r instanceof Date&&!l){var e=r,n={key:e.getDate(),month:e.getMonth(),year:e.getFullYear(),type:"month"};Y({...d,day:n}),h({date:e})}},[]),k.useLayoutEffect(()=>{if(r&&!(r instanceof Date)&&l){var e=r;Y({...d,from:e.from,to:e.to,range:!1}),h({dateRange:{from:e.from,to:e.to}})}},[]),{culture:w,date:{today:c.getDate(),instance:c,month:M,year:x,surroundingYear:_,manyYears:we},monthRows:ke,monthList:De,daysOfWeek:he(),prevMonth:pe,nextMonth:fe,selectMonth:xe,goToMonth:W,setYear:Q,goToYear:U,yearList:Me,setSurroundingYear:ye,isRefillDay:a,getClassDay:me,event:je}};exports.Calendar=Re;exports.CalendarContext=P;exports.CalendarProvider=le;exports.MonthOfYear=B;exports.dateFormat=ce;exports.useCalendar=ie;