UNPKG

dgz-ui-shared

Version:

Custom ui library using React.js, Shadcn/ui, TailwindCSS, Typescript, dgz-ui library

3 lines (2 loc) 3.22 kB
"use strict";const e=require("react/jsx-runtime"),L=require("dgz-ui/button"),u=require("dgz-ui/calendar"),w=require("dgz-ui/form"),h=require("dgz-ui/popover"),a=require("dgz-ui/utils"),j=require("lucide-react"),c=require("react"),P=require("react-i18next"),A=({className:O,format:f=u.DATE,selected:s,timezone:y,onRangeSelected:i=()=>{},placeholder:T,defaultPresets:b,disabled:l,error:q,inputProps:N,...D})=>{const{t:r}=P.useTranslation(),[C,p]=c.useState(!1),[t,m]=c.useState();c.useEffect(()=>{m(s)},[s]);const k=c.useMemo(()=>{const o=a.dayjs().endOf("day");return(b??[{from:o.startOf("week").toDate(),to:o.endOf("week").toDate(),label:r("This week")},{from:o.startOf("month").toDate(),to:o.endOf("month").toDate(),label:r("This month")},{from:o.startOf("year").toDate(),to:o.endOf("year").toDate(),label:r("This year")},{from:o.subtract(7,"day").startOf("day").toDate(),to:o.toDate(),label:r("Last 7 days")},{from:o.subtract(30,"day").startOf("day").toDate(),to:o.toDate(),label:r("Last 30 days")},{from:o.subtract(3,"month").startOf("day").toDate(),to:o.toDate(),label:r("Last 3 months")},{from:o.subtract(6,"month").startOf("day").toDate(),to:o.toDate(),label:r("Last 6 months")},{from:o.subtract(12,"month").startOf("day").toDate(),to:o.toDate(),label:r("Last 12 months")}]).map(n=>{var x,v;return{...n,isActive:((x=s==null?void 0:s.from)==null?void 0:x.getTime())===n.from.getTime()&&((v=s==null?void 0:s.to)==null?void 0:v.getTime())===n.to.getTime()}})},[r,s,b]),d=o=>{m(o),o!=null&&o.from&&(o!=null&&o.to)&&(i(o),p(!1))};return e.jsxs(h.Popover,{open:C,onOpenChange:p,children:[e.jsx(h.PopoverTrigger,{asChild:!0,className:"m-0!",disabled:l,children:e.jsxs("div",{className:"relative",children:[e.jsx(w.Input,{variant:q?"failure":"default",...N,readOnly:!0,value:t!=null&&t.from?t.to?`${a.dayjs(t.from).format(f)}-${a.dayjs(t.to).format(f)}`:a.dayjs(t.from).format(f):"",placeholder:T,disabled:l,className:a.cn(O)}),t&&e.jsx(j.XIcon,{onClick:()=>{m(void 0),i==null||i(void 0)},className:a.cn("text-secondary absolute top-3 right-8 size-4 cursor-pointer",l&&"pointer-events-none opacity-50")}),e.jsx(j.Calendar1,{className:a.cn("text-secondary absolute top-2.5 right-2 size-5",l&&"pointer-events-none opacity-50")})]})}),e.jsxs(h.PopoverContent,{className:"flex w-auto p-0",align:"end",side:"bottom",children:[e.jsx("div",{className:"border-border-alpha-light flex flex-col space-y-1 border-e p-2",children:k.map((o,n)=>e.jsx(L.Button,{size:"xs",variant:o.isActive?"default":"ghost",className:"justify-start",onClick:()=>d(o),children:o.label},n))}),e.jsx(u.Calendar,{...D,className:"border-border-alpha-light border-e",mode:"single",endMonth:t==null?void 0:t.to,selected:t==null?void 0:t.from,selectedToDate:t==null?void 0:t.to,selectedFromDate:t==null?void 0:t.from,timeZone:y,disabled:t!=null&&t.to?{after:t.to}:void 0,onSelect:o=>{m({...t,from:o})}}),e.jsx(u.Calendar,{...D,mode:"single",startMonth:t==null?void 0:t.from,selected:t==null?void 0:t.to,timeZone:y,disabled:t!=null&&t.from?{before:t.from}:void 0,selectedToDate:t==null?void 0:t.to,selectedFromDate:t==null?void 0:t.from,onSelect:o=>{t&&d({...t,to:o})}})]})]})};exports.DateRangePicker=A; //# sourceMappingURL=DateRangePicker-VYbOLs-w.cjs.js.map