index-a7cc295e.js 8.1 KB

1
  1. import{X as ve,Y as ye,i as Ie,Z as Se,R as xe,c as Ce,n as N,z as T,m as ke,e as Ve,G as we,a as Ee,d as U,_ as Me,D as Te,W as Le,k as x,$ as Y,j as Ae,s as Pe,p as R,I as _,r as Re,w as _e}from"./index-e9453d64.js";import{c as Be,C as ze}from"./index-e270b3af.js";import{g as Ne,d as De,n as Oe,r as B,a as C,U as We,w as $e,z as M,o as je,c as d,m as Fe,s as qe}from"./index-ddf66cb5.js";import{u as He}from"./use-scope-id-7611483c.js";let Ke=0;function Ue(){const t=Ne(),{name:r="unknown"}=(t==null?void 0:t.type)||{};return`${r}-${++Ke}`}function X(t){return Array.isArray(t)?!t.length:t===0?!1:!t}function Ye(t,r){if(X(t)){if(r.required)return!1;if(r.validateEmpty===!1)return!0}return!(r.pattern&&!r.pattern.test(String(t)))}function Ge(t,r){return new Promise(l=>{const f=r.validator(t,r);if(Se(f)){f.then(l);return}l(f)})}function G(t,r){const{message:l}=r;return xe(l)?l(t,r):l||""}function Je({target:t}){t.composing=!0}function J({target:t}){t.composing&&(t.composing=!1,t.dispatchEvent(new Event("input")))}function Xe(t,r){const l=ve();t.style.height="auto";let f=t.scrollHeight;if(Ie(r)){const{maxHeight:i,minHeight:s}=r;i!==void 0&&(f=Math.min(f,i)),s!==void 0&&(f=Math.max(f,s))}f&&(t.style.height=`${f}px`,ye(l))}function Ze(t){return t==="number"?{type:"text",inputmode:"decimal"}:t==="digit"?{type:"tel",inputmode:"numeric"}:{type:t}}function v(t){return[...t].length}function z(t,r){return[...t].slice(0,r).join("")}const[Qe,g]=Ce("field"),pe={id:String,name:String,leftIcon:String,rightIcon:String,autofocus:Boolean,clearable:Boolean,maxlength:N,max:Number,min:Number,formatter:Function,clearIcon:T("clear"),modelValue:ke(""),inputAlign:String,placeholder:String,autocomplete:String,autocapitalize:String,autocorrect:String,errorMessage:String,enterkeyhint:String,clearTrigger:T("focus"),formatTrigger:T("onChange"),spellcheck:{type:Boolean,default:null},error:{type:Boolean,default:null},disabled:{type:Boolean,default:null},readonly:{type:Boolean,default:null}},et=Ve({},Be,pe,{rows:N,type:T("text"),rules:Array,autosize:[Boolean,Object],labelWidth:N,labelClass:we,labelAlign:String,showWordLimit:Boolean,errorMessageAlign:String,colon:{type:Boolean,default:null}});var tt=De({name:Qe,props:et,emits:["blur","focus","clear","keypress","clickInput","endValidate","startValidate","clickLeftIcon","clickRightIcon","update:modelValue"],setup(t,{emit:r,slots:l}){const f=Ue(),i=Oe({status:"unvalidated",focused:!1,validateMessage:""}),s=B(),D=B(),L=B(),{parent:m}=Ee(Le),S=()=>{var e;return String((e=t.modelValue)!=null?e:"")},u=e=>{if(x(t[e]))return t[e];if(m&&x(m.props[e]))return m.props[e]},Z=C(()=>{const e=u("readonly");if(t.clearable&&!e){const n=S()!=="",a=t.clearTrigger==="always"||t.clearTrigger==="focus"&&i.focused;return n&&a}return!1}),O=C(()=>L.value&&l.input?L.value():t.modelValue),Q=C(()=>{var e;const n=u("required");return n==="auto"?(e=t.rules)==null?void 0:e.some(a=>a.required):n}),p=e=>e.reduce((n,a)=>n.then(()=>{if(i.status==="failed")return;let{value:o}=O;if(a.formatter&&(o=a.formatter(o,a)),!Ye(o,a)){i.status="failed",i.validateMessage=G(o,a);return}if(a.validator)return X(o)&&a.validateEmpty===!1?void 0:Ge(o,a).then(c=>{c&&typeof c=="string"?(i.status="failed",i.validateMessage=c):c===!1&&(i.status="failed",i.validateMessage=G(o,a))})}),Promise.resolve()),k=()=>{i.status="unvalidated",i.validateMessage=""},W=()=>r("endValidate",{status:i.status,message:i.validateMessage}),$=(e=t.rules)=>new Promise(n=>{k(),e?(r("startValidate"),p(e).then(()=>{i.status==="failed"?(n({name:t.name,message:i.validateMessage}),W()):(i.status="passed",n(),W())})):n()}),A=e=>{if(m&&t.rules){const{validateTrigger:n}=m.props,a=Y(n).includes(e),o=t.rules.filter(c=>c.trigger?Y(c.trigger).includes(e):a);o.length&&$(o)}},ee=e=>{var n;const{maxlength:a}=t;if(x(a)&&v(e)>+a){const o=S();if(o&&v(o)===+a)return o;const c=(n=s.value)==null?void 0:n.selectionEnd;if(i.focused&&c){const y=[...e],I=y.length-+a;return y.splice(c-I,I),y.join("")}return z(e,+a)}return e},V=(e,n="onChange")=>{var a,o;const c=e;e=ee(e);const y=v(c)-v(e);if(t.type==="number"||t.type==="digit"){const h=t.type==="number";e=Ae(e,h,h),n==="onBlur"&&e!==""&&(e=Pe(+e,(a=t.min)!=null?a:-1/0,(o=t.max)!=null?o:1/0).toString())}let I=0;if(t.formatter&&n===t.formatTrigger){const{formatter:h,maxlength:b}=t;if(e=h(e),x(b)&&v(e)>+b&&(e=z(e,+b)),s.value&&i.focused){const{selectionEnd:E}=s.value,K=z(c,E);I=v(h(K))-v(K)}}if(s.value&&s.value.value!==e)if(i.focused){let{selectionStart:h,selectionEnd:b}=s.value;if(s.value.value=e,x(h)&&x(b)){const E=v(e);y?(h-=y,b-=y):I&&(h+=I,b+=I),s.value.setSelectionRange(Math.min(h,E),Math.min(b,E))}}else s.value.value=e;e!==t.modelValue&&r("update:modelValue",e)},te=e=>{e.target.composing||V(e.target.value)},P=()=>{var e;return(e=s.value)==null?void 0:e.blur()},j=()=>{var e;return(e=s.value)==null?void 0:e.focus()},w=()=>{const e=s.value;t.type==="textarea"&&t.autosize&&e&&Xe(e,t.autosize)},ne=e=>{i.focused=!0,r("focus",e),M(w),u("readonly")&&P()},ae=e=>{i.focused=!1,V(S(),"onBlur"),r("blur",e),!u("readonly")&&(A("onBlur"),M(w),Re())},F=e=>r("clickInput",e),re=e=>r("clickLeftIcon",e),ie=e=>r("clickRightIcon",e),le=e=>{R(e),r("update:modelValue",""),r("clear",e)},q=C(()=>{if(typeof t.error=="boolean")return t.error;if(m&&m.props.showError&&i.status==="failed")return!0}),oe=C(()=>{const e=u("labelWidth"),n=u("labelAlign");if(e&&n!=="top")return{width:U(e)}}),se=e=>{e.keyCode===13&&(!(m&&m.props.submitOnEnter)&&t.type!=="textarea"&&R(e),t.type==="search"&&P()),r("keypress",e)},H=()=>t.id||`${f}-input`,ce=()=>i.status,ue=()=>{const e=g("control",[u("inputAlign"),{error:q.value,custom:!!l.input,"min-height":t.type==="textarea"&&!t.autosize}]);if(l.input)return d("div",{class:e,onClick:F},[l.input()]);const n={id:H(),ref:s,name:t.name,rows:t.rows!==void 0?+t.rows:void 0,class:e,disabled:u("disabled"),readonly:u("readonly"),autofocus:t.autofocus,placeholder:t.placeholder,autocomplete:t.autocomplete,autocapitalize:t.autocapitalize,autocorrect:t.autocorrect,enterkeyhint:t.enterkeyhint,spellcheck:t.spellcheck,"aria-labelledby":t.label?`${f}-label`:void 0,onBlur:ae,onFocus:ne,onInput:te,onClick:F,onChange:J,onKeypress:se,onCompositionend:J,onCompositionstart:Je};return t.type==="textarea"?d("textarea",n,null):d("input",Fe(Ze(t.type),n),null)},de=()=>{const e=l["left-icon"];if(t.leftIcon||e)return d("div",{class:g("left-icon"),onClick:re},[e?e():d(_,{name:t.leftIcon,classPrefix:t.iconPrefix},null)])},fe=()=>{const e=l["right-icon"];if(t.rightIcon||e)return d("div",{class:g("right-icon"),onClick:ie},[e?e():d(_,{name:t.rightIcon,classPrefix:t.iconPrefix},null)])},ge=()=>{if(t.showWordLimit&&t.maxlength){const e=v(S());return d("div",{class:g("word-limit")},[d("span",{class:g("word-num")},[e]),qe("/"),t.maxlength])}},me=()=>{if(m&&m.props.showErrorMessage===!1)return;const e=t.errorMessage||i.validateMessage;if(e){const n=l["error-message"],a=u("errorMessageAlign");return d("div",{class:g("error-message",a)},[n?n({message:e}):e])}},he=()=>{const e=u("labelWidth"),n=u("labelAlign"),a=u("colon")?":":"";if(l.label)return[l.label(),a];if(t.label)return d("label",{id:`${f}-label`,for:l.input?void 0:H(),onClick:o=>{R(o),j()},style:n==="top"&&e?{width:U(e)}:void 0},[t.label+a])},be=()=>[d("div",{class:g("body")},[ue(),Z.value&&d(_,{ref:D,name:t.clearIcon,class:g("clear")},null),fe(),l.button&&d("div",{class:g("button")},[l.button()])]),ge(),me()];return He({blur:P,focus:j,validate:$,formValue:O,resetValidation:k,getValidationStatus:ce}),We(Me,{customValue:L,resetValidation:k,validateWithTrigger:A}),$e(()=>t.modelValue,()=>{V(S()),k(),A("onChange"),M(w)}),je(()=>{V(S(),t.formatTrigger),M(w)}),Te("touchstart",le,{target:C(()=>{var e;return(e=D.value)==null?void 0:e.$el})}),()=>{const e=u("disabled"),n=u("labelAlign"),a=de(),o=()=>{const c=he();return n==="top"?[a,c].filter(Boolean):c||[]};return d(ze,{size:t.size,class:g({error:q.value,disabled:e,[`label-${n}`]:n}),center:t.center,border:t.border,isLink:t.isLink,clickable:t.clickable,titleStyle:oe.value,valueClass:g("value"),titleClass:[g("label",[n,{required:Q.value}]),t.labelClass],arrowDirection:t.arrowDirection},{icon:a&&n!=="top"?()=>a:null,title:o,value:be,extra:l.extra})}}});const lt=_e(tt);export{lt as F,Ue as u};