index-2f2fce5a.js 7.7 KB

1
  1. import{bj as e,ak as t,bk as a,bl as l,b2 as r,x as n,z as o,C as s,D as i,a6 as u,aq as c,d,ae as g,r as f,H as p,ar as m,c as v,y as h,ad as b,au as y,j as k,K as x,o as w,W as S,a as C,bm as V,ai as M,bn as I,bo as B,aF as A,V as z,bp as E,G as T,n as j,bq as P,w as W}from"./index-4563f371.js";import{c as q,C as L}from"./index-4a445746.js";function F(e){return Array.isArray(e)?!e.length:0!==e&&!e}function H(e,t){const{message:a}=t;return r(a)?a(e,t):a||""}function D({target:e}){e.composing=!0}function R({target:e}){e.composing&&(e.composing=!1,e.dispatchEvent(new Event("input")))}function K(e){return[...e].length}function O(e,t){return[...e].slice(0,t).join("")}const[G,$]=n("field"),J={id:String,name:String,leftIcon:String,rightIcon:String,autofocus:Boolean,clearable:Boolean,maxlength:o,formatter:Function,clearIcon:s("clear"),modelValue:i(""),inputAlign:String,placeholder:String,autocomplete:String,autocapitalize:String,autocorrect:String,errorMessage:String,enterkeyhint:String,clearTrigger:s("focus"),formatTrigger:s("onChange"),spellcheck:{type:Boolean,"default":null},error:{type:Boolean,"default":null},disabled:{type:Boolean,"default":null},readonly:{type:Boolean,"default":null}},N=W(d({name:G,props:u({},q,J,{rows:o,type:s("text"),rules:Array,autosize:[Boolean,Object],labelWidth:o,labelClass:c,labelAlign:String,showWordLimit:Boolean,errorMessageAlign:String,colon:{type:Boolean,"default":null}}),emits:["blur","focus","clear","keypress","clickInput","endValidate","startValidate","clickLeftIcon","clickRightIcon","update:modelValue"],setup(r,{emit:n,slots:o}){const s=g(),i=f({status:"unvalidated",focused:!1,validateMessage:""}),u=p(),c=p(),d=p(),{parent:W}=m(V),q=()=>{var e;return String(null!=(e=r.modelValue)?e:"")},G=e=>M(r[e])?r[e]:W&&M(W.props[e])?W.props[e]:void 0,J=v((()=>{const e=G("readonly");if(r.clearable&&!e){const e=""!==q(),t="always"===r.clearTrigger||"focus"===r.clearTrigger&&i.focused;return e&&t}return!1})),N=v((()=>d.value&&o.input?d.value():r.modelValue)),Q=v((()=>{var e;const t=G("required");return"auto"===t?null==(e=r.rules)?void 0:e.some((e=>e.required)):t})),U=()=>{i.status="unvalidated",i.validateMessage=""},X=()=>n("endValidate",{status:i.status,message:i.validateMessage}),Y=(e=r.rules)=>new Promise((t=>{U(),e?(n("startValidate"),(e=>e.reduce(((e,t)=>e.then((()=>{if("failed"===i.status)return;let{value:e}=N;if(t.formatter&&(e=t.formatter(e,t)),!function(e,t){if(F(e)){if(t.required)return!1;if(!1===t.validateEmpty)return!0}return!(t.pattern&&!t.pattern.test(String(e)))}(e,t))return i.status="failed",void(i.validateMessage=H(e,t));if(t.validator){if(F(e)&&!1===t.validateEmpty)return;return(a=e,r=t,new Promise((e=>{const t=r.validator(a,r);l(t)?t.then(e):e(t)}))).then((a=>{a&&"string"==typeof a?(i.status="failed",i.validateMessage=a):!1===a&&(i.status="failed",i.validateMessage=H(e,t))}))}var a,r}))),Promise.resolve()))(e).then((()=>{"failed"===i.status?(t({name:r.name,message:i.validateMessage}),X()):(i.status="passed",t(),X())}))):t()})),Z=e=>{if(W&&r.rules){const{validateTrigger:t}=W.props,a=I(t).includes(e),l=r.rules.filter((t=>t.trigger?I(t.trigger).includes(e):a));l.length&&Y(l)}},_=(e,t="onChange")=>{const a=e;e=(e=>{var t;const{maxlength:a}=r;if(M(a)&&K(e)>+a){const l=q();if(l&&K(l)===+a)return l;const r=null==(t=u.value)?void 0:t.selectionEnd;if(i.focused&&r){const t=[...e],l=t.length-+a;return t.splice(r-l,l),t.join("")}return O(e,+a)}return e})(e);const l=K(a)-K(e);if("number"===r.type||"digit"===r.type){const t="number"===r.type;e=B(e,t,t)}let o=0;if(r.formatter&&t===r.formatTrigger){const{formatter:t,maxlength:l}=r;if(e=t(e),M(l)&&K(e)>+l&&(e=O(e,+l)),u.value&&i.focused){const{selectionEnd:e}=u.value,l=O(a,e);o=K(t(l))-K(l)}}if(u.value&&u.value.value!==e)if(i.focused){let{selectionStart:t,selectionEnd:a}=u.value;if(u.value.value=e,M(t)&&M(a)){const r=K(e);l?(t-=l,a-=l):o&&(t+=o,a+=o),u.value.setSelectionRange(Math.min(t,r),Math.min(a,r))}}else u.value.value=e;e!==r.modelValue&&n("update:modelValue",e)},ee=e=>{e.target.composing||_(e.target.value)},te=()=>{var e;return null==(e=u.value)?void 0:e.blur()},ae=()=>{var e;return null==(e=u.value)?void 0:e.focus()},le=()=>{const l=u.value;"textarea"===r.type&&r.autosize&&l&&function(l,r){const n=e();l.style.height="auto";let o=l.scrollHeight;if(a(r)){const{maxHeight:e,minHeight:t}=r;void 0!==e&&(o=Math.min(o,e)),void 0!==t&&(o=Math.max(o,t))}o&&(l.style.height="".concat(o,"px"),t(n))}(l,r.autosize)},re=e=>{i.focused=!0,n("focus",e),x(le),G("readonly")&&te()},ne=e=>{i.focused=!1,_(q(),"onBlur"),n("blur",e),G("readonly")||(Z("onBlur"),x(le),P())},oe=e=>n("clickInput",e),se=e=>n("clickLeftIcon",e),ie=e=>n("clickRightIcon",e),ue=v((()=>"boolean"==typeof r.error?r.error:!(!W||!W.props.showError||"failed"!==i.status)||void 0)),ce=v((()=>{const e=G("labelWidth"),t=G("labelAlign");if(e&&"top"!==t)return{width:h(e)}})),de=e=>{13===e.keyCode&&(W&&W.props.submitOnEnter||"textarea"===r.type||A(e),"search"===r.type&&te()),n("keypress",e)},ge=()=>r.id||"".concat(s,"-input"),fe=()=>{const e=$("control",[G("inputAlign"),{error:ue.value,custom:!!o.input,"min-height":"textarea"===r.type&&!r.autosize}]);if(o.input)return C("div",{"class":e,onClick:oe},[o.input()]);const t={id:ge(),ref:u,name:r.name,rows:void 0!==r.rows?+r.rows:void 0,"class":e,disabled:G("disabled"),readonly:G("readonly"),autofocus:r.autofocus,placeholder:r.placeholder,autocomplete:r.autocomplete,autocapitalize:r.autocapitalize,autocorrect:r.autocorrect,enterkeyhint:r.enterkeyhint,spellcheck:r.spellcheck,"aria-labelledby":r.label?"".concat(s,"-label"):void 0,onBlur:ne,onFocus:re,onInput:ee,onClick:oe,onChange:R,onKeypress:de,onCompositionend:R,onCompositionstart:D};return"textarea"===r.type?C("textarea",t,null):C("input",T("number"===(a=r.type)?{type:"text",inputmode:"decimal"}:"digit"===a?{type:"tel",inputmode:"numeric"}:{type:a},t),null);var a},pe=()=>{const e=o["right-icon"];if(r.rightIcon||e)return C("div",{"class":$("right-icon"),onClick:ie},[e?e():C(z,{name:r.rightIcon,classPrefix:r.iconPrefix},null)])},me=()=>{if(r.showWordLimit&&r.maxlength){const e=K(q());return C("div",{"class":$("word-limit")},[C("span",{"class":$("word-num")},[e]),j("/"),r.maxlength])}},ve=()=>{if(W&&!1===W.props.showErrorMessage)return;const e=r.errorMessage||i.validateMessage;if(e){const t=o["error-message"],a=G("errorMessageAlign");return C("div",{"class":$("error-message",a)},[t?t({message:e}):e])}},he=()=>[C("div",{"class":$("body")},[fe(),J.value&&C(z,{ref:c,name:r.clearIcon,"class":$("clear")},null),pe(),o.button&&C("div",{"class":$("button")},[o.button()])]),me(),ve()];return b({blur:te,focus:ae,validate:Y,formValue:N,resetValidation:U,getValidationStatus:()=>i.status}),y(E,{customValue:d,resetValidation:U,validateWithTrigger:Z}),k((()=>r.modelValue),(()=>{_(q()),U(),Z("onChange"),x(le)})),w((()=>{_(q(),r.formatTrigger),x(le)})),S("touchstart",(e=>{A(e),n("update:modelValue",""),n("clear",e)}),{target:v((()=>{var e;return null==(e=c.value)?void 0:e.$el}))}),()=>{const e=G("disabled"),t=G("labelAlign"),a=(()=>{const e=o["left-icon"];if(r.leftIcon||e)return C("div",{"class":$("left-icon"),onClick:se},[e?e():C(z,{name:r.leftIcon,classPrefix:r.iconPrefix},null)])})();return C(L,{size:r.size,"class":$({error:ue.value,disabled:e,["label-".concat(t)]:t}),center:r.center,border:r.border,isLink:r.isLink,clickable:r.clickable,titleStyle:ce.value,valueClass:$("value"),titleClass:[$("label",[t,{required:Q.value}]),r.labelClass],arrowDirection:r.arrowDirection},{icon:a&&"top"!==t?()=>a:null,title:()=>{const e=(()=>{const e=G("labelWidth"),t=G("labelAlign"),a=G("colon")?":":"";return o.label?[o.label(),a]:r.label?C("label",{id:"".concat(s,"-label"),"for":o.input?void 0:ge(),onClick:e=>{A(e),ae()},style:"top"===t&&e?{width:h(e)}:void 0},[r.label+a]):void 0})();return"top"===t?[a,e].filter(Boolean):e||[]},value:he,extra:o.extra})}}}));export{N as F,J as f};