index.17d70acf.js 6.5 KB

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