UNPKG

@bothub-chat/ui

Version:

Bothub UI Components

2 lines (1 loc) 2.54 kB
import{jsxs as e,jsx as n}from"react/jsx-runtime";import{useState as t,useRef as r,useEffect as l}from"react";import{useInfoDate as o}from"../../hooks/useInfoDate.js";import{Wrapper as a,Header as i,PrevMonth as s,PreArrowIcon as c,Title as u,NextMonth as h,NextArrowIcon as d,DateGrid as m,DateGridItem as y,DayOfWeek as D,DateGridInteractiveItem as A,DayOfMonth as w,Footer as g,SaveButton as p,CancelButton as f}from"./styled.js";import{getMonthName as v,isEqualDaysOfDate as k,resetTimeInDate as C,subMonth as b,addMont as j}from"../../utils/helpers.js";import"../../../../theme/index.js";import{useTheme as x}from"../../../../theme/hook.js";const S=e=>{let n=new Date;return e&&!Array.isArray(e)?(n=new Date(e),"Invalid Date"===n.toString()&&(n=new Date)):Array.isArray(e)&&(n=new Date(e[0]),"Invalid Date"===n.toString()&&(n=new Date)),C(n)},M=({value:M,closeFn:$,onChange:I,locale:T="en-US",range:B,buttonSaveText:E="Save",buttonCloseText:L="Close"})=>{const F=x(),[R,U]=t(S(M)),[Y,q]=t((e=>Array.isArray(e)?[C(new Date(e[0])),e[1]?C(new Date(e[1])):null]:e&&!Array.isArray(e)?C(new Date(e)):null)(M)),[z,G]=t(null),{monthDays:H,weekdays:J}=o(R.getTime(),T),K=r(null);l(()=>{U(S(M))},[M]),l(()=>{const e=e=>{const n=K.current;null!==n&&(n.contains(e.target)||null==$||$())};return document.addEventListener("click",e),()=>{document.removeEventListener("click",e)}},[$]);const N=Array.isArray(Y);return e(a,{ref:K,children:[e(i,{children:[n(s,{type:"button",onClick:()=>{U(b(R))},children:n(c,{fill:F.colors.base.white})}),e(u,{children:[v(new Date(R).getMonth(),T)," ",new Date(R).getFullYear()]}),n(h,{type:"button",onClick:()=>{U(j(R))},children:n(d,{fill:F.colors.base.white})})]}),e(m,{children:[J.map(e=>n(y,{children:n(D,{children:e})},e)),H.map(e=>{let t=!1;N?t=k(Y[0],e)||k(Y[1],e):Y&&(t=k(Y,e));let r=!1;return N&&(Y[1]?r=e>Y[0]&&e<Y[1]:z&&(r=e>Y[0]&&e<z)),n(A,{tabIndex:-1,$isBetweenSelected:r,$active:t,$outRange:e.getMonth()!==R.getMonth(),$startDate:Boolean(N&&(z&&z>Y[0]||Y[1])&&k(Y[0],e)),$finishDate:Boolean(N&&k(Y[1],e)),onMouseEnter:()=>G(e),onMouseLeave:()=>G(null),onClick:()=>(e=>{const n=Array.isArray(Y);B?n?Y[1]||e<Y[0]?q([e,null]):k(e,Y[0])?q(null):q([Y[0],e]):q([e,null]):n||Y&&k(e,Y)?q(null):q(e)})(e),children:n(w,{children:e.getDate()})},e.getTime())})]}),e(g,{children:[n(p,{onClick:()=>{B&&Array.isArray(Y)?I([Y[0],Y[1]||null]):B||Array.isArray(Y)||!Y?I(null):I(Y),null==$||$()},children:E}),n(f,{variant:"secondary",onClick:()=>{U(S(M)),null==$||$()},children:L})]})]})};export{M as DatepickerCalendar};