index-6c6fed8e.js 7.5 KB

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