UNPKG

@snowball-tech/fractal

Version:

Fractal's (Snowball's design system) React component library based on RadixUI and PandaCSS

3 lines 5.52 kB
'use client'; import{a as n}from"./chunk-4U6EAEUD.js";import{a as M}from"./chunk-FA4DQLON.js";import{c as te}from"./chunk-LYTTDLE2.js";import{a as K}from"./chunk-AOLTZ2WJ.js";import{b as t}from"./chunk-CMFNILWJ.js";import{a as $,b as re}from"./chunk-XYM7TA65.js";import{Label as ye}from"@radix-ui/react-label";import{UilCheckCircle as he,UilExclamationCircle as U}from"@tooni/iconscout-unicons-react";import{forwardRef as le,useEffect as pe,useId as $e,useImperativeHandle as be,useRef as G,useState as ne}from"react";import X from"lodash/fp/isError";import s from"lodash/fp/isFunction";import k from"lodash/fp/isInteger";import T from"lodash/fp/isNil";import ae from"lodash/fp/isString";import j from"lodash/fp/omit";import{jsx as m,jsxs as oe}from"react/jsx-runtime";function A(F,r,l){if(!k(r)||r==="")return!1;switch(F){case"day":return r>=1&&r<=31;case"month":return r>=1&&r<=12;case"year":return l===void 0||!k(l)?r>=1900:r>=1900&&r<=l;default:return!1}}var se=le(({autoFocus:F=!1,defaultValue:r,descriptions:l,disabled:I=!1,error:c,id:ie,label:b,labelElement:me,maxYear:N=2099,name:H,onBlur:w,onChange:B,onFieldChange:J,onFocus:g,onKeyDown:O,onKeyUp:_,placeholders:S,readOnly:x=!1,required:D=!1,success:C,value:o,...R},ce)=>{let Q=$e(),p=(ie??Q)||Q,P=!1,L=G(null),d=G(null),u=G(null);be(ce,()=>({get day(){return L?.current??null},get month(){return d?.current??null},get year(){return u?.current??null}}));let[i,W]=ne({day:!1,month:!1,year:!1}),[fe,Z]=ne(!0),de=X(c)?c.message:c,Y=c!==!0&&!!de,v=C!==!0&&!!C,y=Y||c===!0||X(c),E=(v||C===!0)&&!y;pe(()=>{let e={...i},a=o?.day||r?.day;e.day=!T(a)&&(!k(a)||!A("day",a));let h=o?.month||r?.month;e.month=!T(h)&&(!k(h)||!A("month",h));let f=o?.year||r?.year;e.year=!T(f)&&(!k(f)||!A("year",f,N)),(e.day!==i.day||e.month!==i.month||e.year!==i.year)&&W(e)},[r?.day,r?.month,r?.year,o?.day,o?.month,o?.year,i,W,N]);let V=(e,a,h)=>{let f=Number.parseInt(a,10);if(s(B)&&B(e,{...r,...o,[h]:f}),s(J)&&J(e,h,f),P){P=!1;return}if(!T(f)&&k(f)&&A(h,f,N)&&fe)switch(h){case"day":{a.length===2&&d?.current&&d.current.focus();break}case"month":{a.length===2&&u?.current&&u.current.focus();break}case"year":{a.length===4&&u?.current&&u.current.blur();break}default:break}},q=(e,a)=>{switch(s(O)&&O(e,a),e.key){case"ArrowDown":case"ArrowUp":{P=!0;break}case"ArrowLeft":{switch(a){case"month":{L?.current&&(L.current.focus(),e.preventDefault());break}case"year":{d?.current&&(d.current.focus(),e.preventDefault());break}default:break}break}case"ArrowRight":{switch(a){case"day":{d?.current&&(d.current.focus(),e.preventDefault());break}case"month":{u?.current&&(u.current.focus(),e.preventDefault());break}default:break}break}default:break}e.key==="ArrowUp"||e.key==="ArrowDown"?Z(!1):Z(!0)},ee=!I&&!x,z=`${t}-${n}__field !max-w-[100px] w-fit`,ue=ae(b)?b:te(b);return oe("div",{className:re(`${t}-${n}`,"flex w-fit max-w-full flex-col gap-1",`${t}-${n}--${ee?"":"not-"}writable`,I?`${t}-${n}--disabled`:"",y?`${t}-${n}--with-error`:"",x?`${t}-${n}--readonly`:"",D?`${t}-${n}--required`:"",E?`${t}-${n}--with-success`:"",R.className),title:ue,children:[b?m(ye,{asChild:!0,className:$(`${t}-${n}__label`,ee?"cursor-pointer":"cursor-default",D?`${t}-${n}__label--required after:text-feedback-danger-50 after:content-required`:""),htmlFor:`${p}-day`,children:m(K,{element:me||(ae(b)?"label":"div"),children:b})}):!1,oe("div",{className:$(`${t}-${n}__fields`,"flex w-fit gap-1"),children:[m(M,{id:`${p}-day`,ref:L,autoFocus:F,className:$(`${t}-${n}__field__day`,z,"[&_input]:min-w-9"),description:l?.day??"",disabled:I,error:i.day||y,inputMode:"numeric",max:31,maxLength:2,min:1,name:`${H||p}-day`,pattern:"[0-9]*",placeholder:S?.day??"",readOnly:x,required:D,size:2,success:E,suffix:i.day?m(U,{}):void 0,type:"number",...o?.day===void 0?{}:{value:o.day},...r?.day===void 0?{}:{defaultValue:r.day},onChange:(e,a)=>V(e,a,"day"),onKeyDown:e=>q(e,"day"),...s(w)?{onBlur:e=>w(e,"day")}:{},...s(g)?{onFocus:e=>g(e,"year")}:{},...s(_)?{onKeyUp:e=>_(e,"day")}:{},...j(["className"],R)}),m(M,{id:`${p}-month`,ref:d,className:$(`${t}-${n}__field__month`,z,"[&_input]:min-w-9"),description:l?.month??"",disabled:I,error:i.month||y,inputMode:"numeric",max:12,maxLength:2,min:1,name:`${H||p}-month`,pattern:"[0-9]*",placeholder:S?.month??"",readOnly:x,required:D,size:2,success:E,suffix:i.month?m(U,{}):void 0,type:"number",...o?.month===void 0?{}:{value:o.month},...r?.month===void 0?{}:{defaultValue:r.month},onChange:(e,a)=>V(e,a,"month"),onKeyDown:e=>q(e,"month"),...s(w)?{onBlur:e=>w(e,"month")}:{},...s(g)?{onFocus:e=>g(e,"year")}:{},...s(_)?{onKeyUp:e=>_(e,"month")}:{},...j(["className"],R)}),m(M,{id:`${p}-year`,ref:u,className:$(`${t}-${n}__field__year`,z,"[&_input]:!w-unset [&_input]:min-w-11"),description:l?.year??"",disabled:I,error:i.year||y,inputMode:"numeric",max:N,maxLength:4,min:1900,name:`${H||p}-year`,pattern:"[0-9]*",placeholder:S?.year??"",readOnly:x,required:D,size:4,success:E,suffix:i.year||y?m(U,{}):E?m(he,{}):void 0,type:"number",...o?.year===void 0?{}:{value:o.year},...r?.year===void 0?{}:{defaultValue:r.year},onKeyDown:e=>q(e,"year"),...s(w)?{onBlur:e=>w(e,"year")}:{},...s(g)?{onFocus:e=>g(e,"year")}:{},...s(_)?{onKeyUp:e=>_(e,"year")}:{},...j(["className"],R)})]}),(Y||v)&&m(K,{className:$(`${t}-${n}__message ${t}-${n}__message--${y?"error":"success"}`,"cursor-default text-dark"),element:"div",variant:"caption-median",children:y?X(c)?c.message:c:C})]})});se.displayName="InputDate";var He=se;export{se as a,He as b}; //# sourceMappingURL=chunk-CEE7JGB7.js.map