import{defineComponent as W,computed as A,shallowRef as d,getCurrentInstance as X,watch as Y,watchEffect as Z,createVNode as C,nextTick as N}from"vue";import _ from"../_util/classNames.mjs";import ee from"../_util/omit.mjs";import{getMergedStatus as te,getStatusClassNames as ae}from"../_util/statusUtils.mjs";import{useInjectDisabled as ne}from"../config-provider/DisabledContext.mjs";import le from"../config-provider/hooks/useConfigInject.mjs";import{useInjectFormItemContext as ue,FormItemInputContext as oe}from"../form/FormItemContext.mjs";import{fixControlledValue as se,triggerFocus as re,resolveOnChange as j}from"../vc-input/utils/commonUtils.mjs";import ie from"./ClearableLabeledInput.mjs";import ce from"./ResizableTextArea.mjs";import{textAreaProps as ve}from"./inputProps.mjs";import de from"./style/index.mjs";function B(t,o){return[...t||""].slice(0,o).join("")}function M(t,o,c,u){let r=c;return t?r=B(c,u):[...o||""].length<c.length&&[...c||""].length>u&&(r=o),r}const $e=W({compatConfig:{MODE:3},name:"ATextarea",inheritAttrs:!1,props:ve(),setup(t,{attrs:o,expose:c,emit:u}){const r=ue(),f=oe.useInject(),D=A(()=>te(f.status,t.status)),m=d(t.value??t.defaultValue),i=d(),s=d(""),{prefixCls:n,size:F,direction:z}=le("input",t),[L,I]=de(n),E=ne(),v=A(()=>t.showCount===""||t.showCount||!1),g=A(()=>Number(t.maxlength)>0),b=d(!1),w=d(),y=d(0),P=e=>{b.value=!0,w.value=s.value,y.value=e.currentTarget.selectionStart,u("compositionstart",e)},K=e=>{b.value=!1;let a=e.currentTarget.value;if(g.value){const l=y.value>=t.maxlength+1||y.value===w.value?.length;a=M(l,w.value,a,t.maxlength)}a!==s.value&&(S(a),j(e.currentTarget,e,T,a)),u("compositionend",e)},O=X();Y(()=>t.value,()=>{"value"in O.vnode.props,m.value=t.value??""});const V=e=>{re(i.value?.textArea,e)},p=()=>{i.value?.textArea?.blur()},S=(e,a)=>{m.value!==e&&(t.value===void 0?m.value=e:N(()=>{i.value.textArea.value!==s.value&&i.value?.instance.update?.()}),N(()=>{a&&a()}))},q=e=>{e.keyCode===13&&u("pressEnter",e),u("keydown",e)},G=e=>{const{onBlur:a}=t;a?.(e),r.onFieldBlur()},T=e=>{u("update:value",e.target.value),u("change",e),u("input",e),r.onFieldChange()},H=e=>{j(i.value.textArea,e,T),S("",()=>{V()})},R=e=>{let a=e.target.value;if(m.value!==a){if(g.value){const l=e.target,h=l.selectionStart>=t.maxlength+1||l.selectionStart===a.length||!l.selectionStart;a=M(h,s.value,a,t.maxlength)}j(e.currentTarget,e,T,a),S(a)}},J=()=>{const{class:e}=o,{bordered:a=!0}=t,l={...ee(t,["allowClear"]),...o,class:[{[`${n.value}-borderless`]:!a,[`${e}`]:e&&!v.value,[`${n.value}-sm`]:F.value==="small",[`${n.value}-lg`]:F.value==="large"},ae(n.value,D.value),I.value],disabled:E.value,showCount:null,prefixCls:n.value,onInput:R,onChange:R,onBlur:G,onKeydown:q,onCompositionstart:P,onCompositionend:K};return t.valueModifiers?.lazy&&delete l.onInput,C(ce,{...l,id:l?.id??r.id.value,ref:i,maxlength:t.maxlength,lazy:t.lazy},null)};return c({focus:V,blur:p,resizableTextArea:i}),Z(()=>{let e=se(m.value);!b.value&&g.value&&(t.value===null||t.value===void 0)&&(e=B(e,t.maxlength)),s.value=e}),()=>{const{maxlength:e,bordered:a=!0,hidden:l}=t,{style:h,class:Q}=o,U={...t,...o,prefixCls:n.value,inputType:"text",handleReset:H,direction:z.value,bordered:a,style:v.value?void 0:h,hashId:I.value,disabled:t.disabled??E.value};let $=C(ie,{...U,value:s.value,status:t.status},{element:J});if(v.value||f.hasFeedback){const k=[...s.value].length;let x="";typeof v.value=="object"?x=v.value.formatter({value:s.value,count:k,maxlength:e}):x=`${k}${g.value?` / ${e}`:""}`,$=C("div",{hidden:l,class:_(`${n.value}-textarea`,{[`${n.value}-textarea-rtl`]:z.value==="rtl",[`${n.value}-textarea-show-count`]:v.value,[`${n.value}-textarea-in-form-item`]:f.isFormItemInput},`${n.value}-textarea-show-count`,Q,I.value),style:h,"data-count":typeof x!="object"?x:void 0},[$,f.hasFeedback&&C("span",{class:`${n.value}-textarea-suffix`},[f.feedbackIcon])])}return L($)}}});export{$e as default};
