siegel
Version:
Web application development ecosystem
1 lines • 3.29 kB
JavaScript
import e,{useRef as t,useEffect as l,useLayoutEffect as r,useState as i}from"react";import n from"../../../common/is/exists/index.js";import a from"../_internals/resolve_tag_attributes.js";import s from"../_internals/apply_classname.js";import o from"../_internals/component.js";import u from"../_internals/ref_apply.js";import c from"../_internals/children.js";import m from"../_internals/label.js";import{setCaretPos as d,INPUT_TYPE as f}from"./utils.js";let _="-ui-input",h,g=()=>({isTouched:!1,isFocused:!1}),p=(e={})=>(e.debounceValue=h,e.debounceTimeoutID=h,e),b=o(_,{theme:{root:h,_filled:h,_error:h,_disabled:h,_focused:h,_touched:h,_readonly:h,_textarea:h,children:h,label:h,label_text:h,field:h,error_text:h}},o=>{let _,{value:h="",theme:b,label:v,errorMsg:x,type:T,disabled:E,onBlur:D,rootTagAttributes:j,inputAttributes:y,onChange:F,onFocus:N,payload:R,store:V,autofocus:W,placeholder:A,regexp:B,mask:I,children:M,debounceMs:S,className:$,prefix:C,suffix:L}=o,[O,P]=V||i(g()),{isFocused:q,isTouched:K}=O;if(S){let{debounceTimeoutID:e}=(_=i(p()))[0];r(()=>()=>{clearTimeout(e)},[e])}let k=!E&&!F,w="textarea"==T,z=n(x),G=C||L,H={disabled:E,placeholder:A,type:T,className:b.field,readOnly:k,value:_&&n(_[0].debounceValue)?_[0].debounceValue:h};(W||I||G)&&(H.ref=t(null),W&&l(()=>{E||H.ref.current.focus()},[E]));let J={className:s($,[[b._textarea,w],[b._error,z],[b._filled,h.length>0||n(I?.pattern)],[b._focused,q],[b._touched,K],[b._disabled,E],[b._readonly,k]])};q&&(J.onBlur=e=>{if(D?.(e),!e.defaultPrevented){if(F&&_){let[{debounceTimeoutID:t,debounceValue:l},r]=_;n(l)&&(clearTimeout(t),r(p()),F(l,e,R))}P({isFocused:!1,isTouched:!0})}}),G&&(C&&(H.value=`${C}${H.value}`),L&&(H.value+=`${L}`)),!E&&F&&(q||(J.onFocus=e=>{if(N?.(e),!e.defaultPrevented&&(P({isFocused:!0,isTouched:!0}),G)){let t=e.currentTarget;setTimeout(()=>{let e=t.querySelector("input"),{selectionStart:l,selectionEnd:r,value:i}=e,n=C?.length||0,a=i.length-(L?.length||0),s=Math.max(l,n),o=Math.max(n,Math.min(r,a));(s!=l||o!=r)&&e.setSelectionRange(s,o)})}}),H.onChange=e=>{let t=e.target,{value:l}=t;if(G){let{selectionStart:r}=t,{inputType:i}=e.nativeEvent;if("deleteByDrag"==i||"insertFromDrop"==i)return;let n=e.nativeEvent.data,a=n?.length||0,s=C?.length||0,u=L?.length||0,c=o.value?.length||0,m=i==f.DELETE_BACKWARD||i==f.DELETE_FORWARD;if(m){let e=s+c+u-l.length,t=s>r,i=s+c<r+e;if(t){let t=r+e>s?r:s-e;t>0&&(l=C+l.substring(t)),d(H.ref,s)}else if(i){let e=s+c;l=l.substring(0,r>e?e:e-(e-r))+L,d(H.ref,l.length-u)}}if(C){let e;if(!m){let t=r-a;s>t&&(l=l.substring(0,t)+l.substring(r),e=!0)}l.startsWith(C)&&(l=l.substring(s)),e&&(l=n+l,d(H.ref,s+a))}if(L){let e;if(!m){let t=r-a;s+c<t&&(l=l.substring(0,t-s)+l.substring(r-s),e=!0)}l.endsWith(L)&&(l=l.substring(0,l.length-L.length)),e&&(l+=n,d(H.ref,s+c+a))}}if(!B||B.test(l))if(_){let[{debounceTimeoutID:t},r]=_;clearTimeout(t),r({debounceValue:l,debounceTimeoutID:setTimeout(()=>{r(p({})),F(l,e,R)},S)})}else F(l,e,R)}),u(J,o),J=a(J,j),H=a(H,y),I?.processor(I,H);let Q=w?T:"input",U=e.createElement(Q,{...H});return v&&(U=m(U,{className:b.label},{className:b.label_text,children:v})),e.createElement("div",{...J},U,c(M,b),z&&e.createElement("div",{className:b.error_text,children:x}))});export default b;export{_ as componentID,g as getDefaultState};