UNPKG

rc-calendar-picker

Version:

<div align="center"> <a href="https://github.com/noxxxxxxxx/calendar" target="_blank"> <img alt="Calendar Logo" width="150" src="https://github.com/noxxxxxxxx/calendar/blob/main/assets/calendar.svg"/> </a> </div>

2 lines (1 loc) 12.3 kB
(function(A,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("react/jsx-runtime"),require("dayjs"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","dayjs","react"],t):(A=typeof globalThis<"u"?globalThis:A||self,t(A["rc-calendar-picker"]={},A.JSX,A.dayjs,A.React))})(this,function(A,t,I,L){"use strict";var O=(e=>(e.Year="year",e.Month="month",e.Day="day",e.Time="time",e))(O||{}),H=(e=>(e.Hour="hour",e.Minute="minute",e.Second="second",e.Millisecond="millisecond",e.Ampm="ampm",e))(H||{});const te={day:"date",month:"month",year:"year"},re={day:"time",month:"day",year:"month"},X={hour:{min:0,max:23,step:1},minute:{min:0,max:59,step:1},second:{min:0,max:59,step:1},millisecond:{min:0,max:999,step:1}},Z="L",R="LTS",$=(e,n)=>{const r={month:2,day:2,hour:2,minute:2,second:2,millisecond:3};let l=n+"";for(;l.length<r[e];)l="0"+l;return l},oe=e=>{if(!e)return{hour:"00",minute:"00",second:"00",millisecond:"00",ampm:"am"};const n=e.hour();return{hour:$("hour",n),minute:$("minute",e.minute()),second:$("second",e.second()),millisecond:$("millisecond",e.millisecond()),ampm:n<12?"am":"pm"}},ce=e=>{const n=X;return Object.keys(X).forEach(r=>{n[r]={...X[r],...(e==null?void 0:e[r])||{}}}),n},se=e=>e.charAt(0).toUpperCase()+e.slice(1),j=e=>typeof e=="boolean"?e?Z:"":e??Z,W=e=>typeof e=="boolean"?e===!0?R:"":e??R,B=(e,n,r,l)=>{if(!e)return;const a=j(r),C=W(l),h=`${a||""} ${C||""}`.trim();return n!==void 0?e.clone().utcOffset(n).format(h):e.clone().format(h)},ae=e=>e!==void 0?I().utcOffset(e):I(),ie=e=>{if(!e)return;const n=e.firstDayOfWeek(),r=[];let l=0;return e.weekdaysMin().forEach(function(a){r[(7+l++-n)%7]=a}),r},J=(e,n)=>e?e(n)!==!1:!0,le=(e,n)=>n<4?e[0]:n<8?e[1]:e[2],ee=(e,n)=>{if(!e)return!1;const r=I(e).format(W(n)).toLowerCase();return r.indexOf("am")!==-1||r.indexOf("pm")!==-1},de=(e,n)=>e?I(e).format(W(n)):"",ue=(e,n)=>{L.useEffect(()=>{const r=l=>{e.current&&!e.current.contains(l.target)&&n()};return document.addEventListener("click",r),()=>{document.removeEventListener("click",r)}},[e,n])},fe=e=>{const{value:n,showInput:r,timezoneOffset:l,dateFormat:a,timeFormat:C,inputProps:h,onInputChange:y,openCalendar:g,closeCalendar:M}=e,[N,S]=L.useState("");if(L.useEffect(()=>{S(B(n,l,a,C)??"")},[n,l,a,C]),!r)return;const m={...h,type:"text",value:N,onFocus:o=>{J(h==null?void 0:h.onFocus,o)&&(g==null||g())},onChange:o=>{if(!J(h==null?void 0:h.onChange,o))return;const p=o.target.value,c=I(p).isValid()?B(I(p),l,a):p;S(p),y==null||y(c??"")},onKeyDown:o=>{J(h==null?void 0:h.onKeyDown,o)&&o.key==="Tab"&&M()},onClick:o=>{J(h==null?void 0:h.onClick,o)&&(g==null||g())}};return t.jsx("input",{...m})},G=({onClickPrev:e,onClickSwitch:n,onClickNext:r,switchContent:l,switchColSpan:a,switchProps:C})=>t.jsxs("tr",{className:"nc-switch-head",children:[t.jsx("th",{className:"nc-prev",onClick:e,children:t.jsx("span",{children:"‹"})}),t.jsx("th",{className:"nc-switch",colSpan:a,onClick:n,...C,children:l}),t.jsx("th",{className:"nc-next",onClick:r,children:t.jsx("span",{children:"›"})})]}),he=e=>{const{viewDate:n,selectedDate:r,isValidDate:l=()=>!0,updateDate:a,renderDay:C,navigate:h,showView:y,timeFormat:g,siblingDate:M,timezoneOffset:N,showTime:S}=e,E=()=>{const o=n==null?void 0:n.localeData();return t.jsx(G,{onClickPrev:()=>h(-1,O.Month),onClickSwitch:()=>y(O.Month),onClickNext:()=>h(1,O.Month),switchContent:`${o==null?void 0:o.months(n)} ${n==null?void 0:n.year()}`,switchColSpan:5,switchProps:{"data-value":n==null?void 0:n.month()}})},w=()=>{var c;const o=n==null?void 0:n.localeData(),p=(c=ie(o))==null?void 0:c.map((u,i)=>t.jsx("th",{className:"dow",children:u},u+i));return t.jsx("tr",{children:p})},s=(o,p,c,u)=>{const i=o.date(),k=o.month(),P=o.year(),V=q=>{a(q)},Y={key:o.format("M_D"),"data-value":i,"data-month":k,"data-year":P,onClick:()=>{},className:""};let D="nc-day";return o.isBefore(p)?D+=" nc-before":o.isAfter(c)&&(D+=" nc-after"),r!=null&&r.isBefore(M,"day")&&o.isBefore(M,"day")&&o.isAfter(r,"day")&&(D+=" in-range"),M&&(r!=null&&r.isAfter(M,"day"))&&M&&o.isAfter(M,"day")&&o.isBefore(r,"day")&&(D+=" in-range"),M!=null&&M.isSame(o,"day")&&(D+=" nc-sibling"),r!=null&&r.isSame(o,"day")&&(D+=" nc-active"),o.isSame(u,"day")&&(D+=" nc-today"),l(o.clone().year(P).month(k).date(i).startOf("day"))?Y.onClick=V:D+=" nc-disabled",Y.className=D,C?C(Y,o.clone().date(),r==null?void 0:r.clone()):L.createElement("td",{...Y,key:Y.key},o.clone().date())},f=()=>{const o=ae(N),p=n,c=p==null?void 0:p.clone().startOf("month"),u=p==null?void 0:p.clone().endOf("month"),i=[[],[],[],[],[],[]];let k=p==null?void 0:p.clone().subtract(1,"month").endOf("month").startOf("week").startOf("day");const P=k==null?void 0:k.clone().add(42,"day");let V=0;for(;k!=null&&k.isBefore(P);){const Y=Math.floor(V/7),D=i[Y>5?5:Y],q=s(k,c,u,o);D.push(q),k=k.add(1,"d"),V+=1}return i.map((Y,D)=>t.jsx("tr",{children:Y},`${P==null?void 0:P.month()}_${D}`))},m=()=>{if(!(!S||!g))return t.jsx("tfoot",{children:t.jsx("tr",{children:t.jsx("td",{onClick:()=>y("time"),colSpan:7,className:"nc-time-toggle",children:n==null?void 0:n.format(g)})})})};return t.jsx("div",{className:"nc-days",children:t.jsxs("table",{children:[t.jsxs("thead",{children:[E(),w()]}),t.jsx("tbody",{children:f()}),m()]})})},me=e=>{const{viewDate:n,updateDate:r,renderMonth:l,isValidDate:a,navigate:C,showView:h,selectedDate:y}=e,g=()=>{const w=n==null?void 0:n.year();return t.jsx(G,{onClickPrev:()=>C(-1,O.Year),onClickSwitch:()=>h(O.Year),onClickNext:()=>C(1,O.Year),switchContent:w,switchColSpan:"5"})},M=w=>{if(!a||!n)return!1;const s=n==null?void 0:n.clone().set("month",w);let f=s.endOf("month").date()+1;for(;f-- >1;)if(a(s.date(f)))return!1;return!0},N=w=>{const s=n==null?void 0:n.localeData().monthsShort(n.month(w));return se(s)},S=w=>{let s="nc-month",f=()=>{};M(w)?s+=" nc-disabled":f=o=>{r(o)},y&&y.year()===(n==null?void 0:n.year())&&y.month()===w&&(s+=" nc-active");const m={key:w,className:s,"data-value":w,onClick:f};return l?l(m,w,y==null?void 0:y.year(),y==null?void 0:y.clone()):L.createElement("td",{...m,key:m.key},N(w))},E=()=>{const w=[[],[],[]];for(let s=0;s<12;s++)le(w,s).push(S(s));return w.map((s,f)=>t.jsx("tr",{children:s},f))};return t.jsxs("div",{className:"nc-months",children:[t.jsx("table",{children:t.jsx("thead",{children:g()})}),t.jsx("table",{children:t.jsx("tbody",{children:E()})})]})},pe=e=>{const{selectedDate:n,viewDate:r,setTime:l,timeFormat:a,dateFormat:C,timeLimit:h,showView:y,timeConstraints:g}=e,M=ce(g),[N,S]=L.useState(oe(r)),E=()=>{const c=[],u=h;return u.toLowerCase().indexOf("h")!==-1&&(c.push(H.Hour),u.indexOf("m")!==-1&&(c.push(H.Minute),u.indexOf("s")!==-1&&(c.push(H.Second),u.indexOf("S")!==-1&&c.push(H.Millisecond)))),ee(r,a)&&c.push(H.Ampm),c},w=()=>{if(!C)return;const c=n||r;return t.jsx("thead",{children:t.jsx("tr",{children:t.jsx("th",{className:"nc-switch",colSpan:4,onClick:()=>y(O.Day),children:c==null?void 0:c.format(j(C))})})})},s=()=>{let c=parseInt(N.hour,10);c>=12?c-=12:c+=12,S({...N,hour:$("hour",c),ampm:N.ampm==="am"?"pm":"am"}),l==null||l("hour",parseInt(String(c),10))},f=c=>{const u=M[c];if(u){let i=parseInt(N[c],10)+u.step;return i>u.max&&(i=u.min+(i-(u.max+1))),$(c,i)}throw new Error("Invalid tc value")},m=c=>{const u=M[c];if(u){let i=parseInt(N[c],10)-u.step;return i<u.min&&(i=u.max+1-(u.min-i)),$(c,i)}throw new Error("Invalid tc value")},o=(c,u,i)=>{if(c.button&&c.button!==0)return;if(i==="ampm")return s();const k=u==="increase"?f(i):m(i);S({...N,[i]:k}),l==null||l(i,parseInt(k,10))},p=(c,u)=>{let i=u;return c==="hour"&&ee(r,a)&&(i=$("hour",(Number(i)-1)%12+1),Number(i)===0&&(i=12)),c==="ampm"&&(de(r,a).indexOf("M")!==-1?i=r==null?void 0:r.format("A"):i=r==null?void 0:r.format("a")),t.jsxs("div",{className:"nc-counter",children:[t.jsx("span",{className:"nc-btn",onMouseDown:k=>o(k,"increase",c),children:"▲"}),t.jsx("div",{className:"nc-count",children:i}),t.jsx("span",{className:"nc-btn",onMouseDown:k=>o(k,"decrease",c),children:"▼"})]})};return t.jsx("div",{className:"nc-time",children:t.jsxs("table",{children:[w(),t.jsx("tbody",{children:t.jsx("tr",{children:t.jsx("td",{children:t.jsx("div",{className:"nc-counters",children:E().map((c,u)=>t.jsx(L.Fragment,{children:p(c,N[c])},`sep${u}`))})})})})]})})},ye=e=>{const{renderYear:n,navigate:r,showView:l,viewDate:a,selectedDate:C,updateDate:h,isValidDate:y}=e,g=s=>{const f={};if(f[s]!==void 0)return f[s];if(!y)return!1;const m=a==null?void 0:a.clone().set("year",s);if(!m)return!1;let o=m.endOf("year").dayOfYear()+1;for(;o-- >1;)if(y(m.dayOfYear(o)))return f[s]=!1,!1;return f[s]=!0,!0},M=s=>{const f=a==null?void 0:a.year();let m="nc-year",o=()=>{};g(s)?m+=" nc-disabled":o=c=>{h(c)},f===s&&(m+=" nc-active");const p={key:s,className:m,"data-value":s,onClick:o};return n?n(p,s,C==null?void 0:C.clone()):L.createElement("td",{...p,key:p.key},s)},N=()=>a&&typeof a.year=="function"?Math.floor(a.year()/10)*10:0,S=()=>{const s=N();return t.jsx(G,{onClickPrev:()=>r(-10,O.Year),onClickSwitch:()=>l(O.Year),onClickNext:()=>r(10,O.Year),switchContent:`${s}-${s+9}`,switchColSpan:"5"})},E=(s,f)=>f<3?s[0]:f<7?s[1]:s[2],w=()=>{const s=N(),f=[[],[],[]];if(s)for(let m=s-1;m<s+11;m++)E(f,m-s).push(M(m));return f.map((m,o)=>t.jsx("tr",{children:m},o))};return t.jsxs("div",{className:"nc-years",children:[t.jsx("table",{children:t.jsx("thead",{children:S()})}),t.jsx("table",{children:t.jsx("tbody",{children:w()})})]})},we=e=>{const{value:n,initialValue:r,initialViewMode:l=O.Day,timezoneOffset:a,closeOnSelect:C=!0,className:h,showInput:y=!0,open:g,inputProps:M,dateFormat:N=!0,timeFormat:S=!0,viewMode:E,closeOnClickOutside:w=!0,timeLimit:s="HH:mm:ss",showTime:f=!1,isEndDate:m,siblingDate:o,isValidDate:p,renderYear:c,renderMonth:u,renderDay:i,onOpen:k,onClose:P,onChange:V,onBeforeNavigate:Y,onNavigate:D,onNavigateForward:q,onNavigateBack:ke,timeConstraints:ve}=e,[v,_]=L.useState({open:g??!1,selectedDate:I(B(n||r,a,N,S)),inputValue:void 0,currentView:E||l,viewDate:I(B(n||r,a,N,S)),ready:!1}),ne=L.useRef(null),K=()=>v.open||g===!1,Ne=()=>{let d="rdt";const b=h;return Array.isArray(b)?d+=" "+b.join(" "):b&&(d+=" "+b),y||(d+=" nc-static"),K()&&(d+=" nc-open"),d},Ce=()=>{K()||(_({...v,open:!0}),k==null||k())},Q=d=>{if(!K())return;const b={...v,...d??{},open:!C};_(b),C&&(P==null||P(b.selectedDate))},Me=(d,b)=>{const x=(b||v.viewDate).clone(),z=(Y==null?void 0:Y(d,v.currentView,x))??d;D==null||D(z),_({...v,currentView:z})},ge=d=>{const b=d.target;if(b){const x=v.currentView;let F;const z=Number(b.dataset.value),xe=Number(b.dataset.year),De=Number(b.dataset.month),T={};if(x===O.Day)F=v.viewDate.clone().year(xe).month(De).date(z).hour(v.viewDate.hour()),T.selectedDate=F.clone();else{const U=te[x];F=v.viewDate[U](z)}if(T.viewDate=F.clone(),V==null||V(T.viewDate),x===E||!E&&x===O.Day){T.selectedDate=F.clone();const U={...v,...T};_(U),Q(U);return}T.currentView=re[x],_({...v,...T})}},be=(d,b)=>{let x=v.viewDate.clone();x=x.add(d,b);const F=d>0?q:ke;F==null||F(d,b),_({...v,viewDate:x})},Oe=(d,b)=>{var F;let x=(F=v.viewDate)==null?void 0:F.clone();x&&(x=x.set(d,b),_({...v,selectedDate:x,viewDate:x.clone()}),V==null||V(x))},Se=()=>{const d={selectedDate:v.selectedDate,viewDate:v.viewDate,updateDate:ge,navigate:be,showView:Me,timezoneOffset:a,isEndDate:m,siblingDate:o,isValidDate:p,renderYear:c,renderMonth:u,renderDay:i,setTime:Oe,dateFormat:j(N),timeFormat:W(S),timeConstraints:ve,timeLimit:s,showTime:f};switch(v.currentView){case O.Year:return t.jsx(ye,{...d});case O.Month:return t.jsx(me,{...d});case O.Time:return t.jsx(pe,{...d});default:return t.jsx(he,{...d})}};return ue(ne,()=>{w&&Q()}),L.useEffect(()=>{if(n){const d=I(B(n,a,N,S));_({...v,selectedDate:d,viewDate:d.clone(),ready:!0})}else _({...v,ready:!0})},[n,a,N,S]),v.ready?t.jsxs("div",{className:Ne(),ref:ne,children:[t.jsx(fe,{showInput:y,value:v.selectedDate,dateFormat:N,timeFormat:S,inputProps:M,openCalendar:Ce,closeCalendar:Q,timezoneOffset:a,onInputChange:d=>V==null?void 0:V(I(d).isValid()?I(d):d)}),t.jsx("div",{className:"nc-picker",onClick:d=>d.stopPropagation(),children:g===!1?null:v.open?Se():null})]}):null};A.TimeConstraintsKeys=H,A.ViewMode=O,A.default=we,Object.defineProperties(A,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});