import e,{createRef as t,Component as n}from"react";import{InputText as i}from"primereact/inputtext";import{classNames as r}from"primereact/utils";import{tip as s}from"primereact/tooltip";import{Ripple as o}from"primereact/ripple";function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,i=new Array(t);n0&&t>a){var p=this.isDecimalMode()&&(this.props.minFractionDigits||0)0?r:""):r=i.slice(0,t-1)+i.slice(t)}this.updateValue(e,r,null,"delete-single")}else r=this.deleteRange(i,t,n),this.updateValue(e,r,null,"delete-range");break;case 46:if(e.preventDefault(),t===n){var c=i.charAt(t),h=this.getDecimalCharIndexes(i),f=h.decimalCharIndex,d=h.decimalCharIndexWithoutPrefix;if(this.isNumeralChar(c)){var m=this.getDecimalLength(i);if(this._group.test(c))this._group.lastIndex=0,r=i.slice(0,t)+i.slice(t+2);else if(this._decimal.test(c))this._decimal.lastIndex=0,m?this.$refs.input.$el.setSelectionRange(t+1,t+1):r=i.slice(0,t)+i.slice(t+1);else if(f>0&&t>f){var y=this.isDecimalMode()&&(this.props.minFractionDigits||0)0?r:""):r=i.slice(0,t)+i.slice(t+1)}this.updateValue(e,r,null,"delete-back-single")}else r=this.deleteRange(i,t,n),this.updateValue(e,r,null,"delete-range")}this.props.onKeyDown&&this.props.onKeyDown(e)}}},{key:"onInputKeyPress",value:function(e){if(!this.props.disabled&&!this.props.readOnly){var t=e.which||e.keyCode,n=String.fromCharCode(t),i=this.isDecimalSign(n),r=this.isMinusSign(n);(48<=t&&t<=57||r||i)&&(this.insert(e,n,{isDecimalSign:i,isMinusSign:r}),e.preventDefault())}}},{key:"onPaste",value:function(e){if(e.preventDefault(),!this.props.disabled&&!this.props.readOnly){var t=(e.clipboardData||window.clipboardData).getData("Text");if(t){var n=this.parseValue(t);null!=n&&this.insert(e,n.toString())}}}},{key:"allowMinusSign",value:function(){return null===this.props.min||this.props.min<0}},{key:"isMinusSign",value:function(e){return!(!this._minusSign.test(e)&&"-"!==e||(this._minusSign.lastIndex=0,0))}},{key:"isDecimalSign",value:function(e){return!!this._decimal.test(e)&&(this._decimal.lastIndex=0,!0)}},{key:"isDecimalMode",value:function(){return"decimal"===this.props.mode}},{key:"getDecimalCharIndexes",value:function(e){var t=e.search(this._decimal);this._decimal.lastIndex=0;var n=e.replace(this._prefix,"").trim().replace(/\s/g,"").replace(this._currency,"").search(this._decimal);return this._decimal.lastIndex=0,{decimalCharIndex:t,decimalCharIndexWithoutPrefix:n}}},{key:"getCharIndexes",value:function(e){var t=e.search(this._decimal);this._decimal.lastIndex=0;var n=e.search(this._minusSign);this._minusSign.lastIndex=0;var i=e.search(this._suffix);this._suffix.lastIndex=0;var r=e.search(this._currency);return this._currency.lastIndex=0,{decimalCharIndex:t,minusCharIndex:n,suffixCharIndex:i,currencyCharIndex:r}}},{key:"insert",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{isDecimalSign:!1,isMinusSign:!1},i=t.search(this._minusSign);if(this._minusSign.lastIndex=0,this.allowMinusSign()||-1===i){var r,s=this.inputRef.current.selectionStart,o=this.inputRef.current.selectionEnd,a=this.inputRef.current.value.trim(),u=this.getCharIndexes(a),l=u.decimalCharIndex,p=u.minusCharIndex,c=u.suffixCharIndex,h=u.currencyCharIndex;if(n.isMinusSign)0===s&&(r=a,-1!==p&&0===o||(r=this.insertText(a,t,0,o)),this.updateValue(e,r,t,"insert"));else if(n.isDecimalSign)l>0&&s===l?this.updateValue(e,a,t,"insert"):(l>s&&l0&&s>l){if(s+t.length-(l+1)<=f){var m=h>=s?h-1:c>=s?c:a.length;r=a.slice(0,s)+t+a.slice(s+t.length,m)+a.slice(m),this.updateValue(e,r,t,d)}}else r=this.insertText(a,t,s,o),this.updateValue(e,r,t,d)}}}},{key:"insertText",value:function(e,t,n,i){if(2===("."===t?t:t.split(".")).length){var r=e.slice(n,i).search(this._decimal);return this._decimal.lastIndex=0,r>0?e.slice(0,n)+this.formatValue(t)+e.slice(i):e||this.formatValue(t)}return i-n===e.length?this.formatValue(t):0===n?t+e.slice(i):i===e.length?e.slice(0,n)+t:e.slice(0,n)+t+e.slice(i)}},{key:"deleteRange",value:function(e,t,n){return n-t===e.length?"":0===t?e.slice(n):n===e.length?e.slice(0,t):e.slice(0,t)+e.slice(n)}},{key:"initCursor",value:function(){var e=this.inputRef.current.selectionStart,t=this.inputRef.current.value,n=t.length,i=null,r=(this.prefixChar||"").length,s=(t=t.replace(this._prefix,"")).charAt(e-=r);if(this.isNumeralChar(s))return e+r;for(var o=e-1;o>=0;){if(s=t.charAt(o),this.isNumeralChar(s)){i=o+r;break}o--}if(null!==i)this.inputRef.current.setSelectionRange(i+1,i+1);else{for(o=e;othis.props.max?this.props.max:e}},{key:"updateInput",value:function(e,t,n,i){t=t||"";var r=this.inputRef.current,s=r.value,o=this.formatValue(e),a=s.length;if(o!==i&&(o=this.concatValues(o,i)),0===a){r.value=o,r.setSelectionRange(0,0);var u=this.initCursor()+t.length;r.setSelectionRange(u,u)}else{var l=r.selectionStart,p=r.selectionEnd;r.value=o;var c=o.length;if("range-insert"===n){var h=this.parseValue((s||"").slice(0,l)),f=(null!==h?h.toString():"").split("").join("(".concat(this.groupChar,")?")),d=new RegExp(f,"g");d.test(o);var m=t.split("").join("(".concat(this.groupChar,")?")),y=new RegExp(m,"g");y.test(o.slice(d.lastIndex)),r.setSelectionRange(p=d.lastIndex+y.lastIndex,p)}else if(c===a)"insert"===n||"delete-back-single"===n?r.setSelectionRange(p+1,p+1):"delete-single"===n?r.setSelectionRange(p-1,p-1):"delete-range"!==n&&"spin"!==n||r.setSelectionRange(p,p);else if("delete-back-single"===n){var v=s.charAt(p-1),g=s.charAt(p),b=a-c,x=this._group.test(g);x&&1===b?p+=1:!x&&this.isNumeralChar(v)&&(p+=-1*b+1),this._group.lastIndex=0,r.setSelectionRange(p,p)}else if("-"===s&&"insert"===n){r.setSelectionRange(0,0);var w=this.initCursor()+t.length+1;r.setSelectionRange(w,w)}else r.setSelectionRange(p+=c-a,p)}r.setAttribute("aria-valuenow",e)}},{key:"updateInputValue",value:function(e){e=this.evaluateEmpty(e);var t=this.inputRef.current,n=t.value,i=this.formattedValue(e);n!==i&&(t.value=i,t.setAttribute("aria-valuenow",e))}},{key:"formattedValue",value:function(e){var t=this.evaluateEmpty(e);return this.formatValue(t)}},{key:"concatValues",value:function(e,t){if(e&&t){var n=t.search(this._decimal);return this._decimal.lastIndex=0,-1!==n?e.split(this._decimal)[0]+t.slice(n):e}return e}},{key:"getDecimalLength",value:function(e){if(e){var t=e.split(this._decimal);if(2===t.length)return t[1].replace(this._suffix,"").trim().replace(/\s/g,"").replace(this._currency,"").length}return 0}},{key:"updateModel",value:function(e,t){this.props.onValueChange&&this.props.onValueChange({originalEvent:e,value:t,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:t}})}},{key:"onInputFocus",value:function(e){var t=this;e.persist(),this.setState({focused:!0},(function(){t.props.onFocus&&t.props.onFocus(e)}))}},{key:"onInputBlur",value:function(e){var t=this;e.persist(),this.setState({focused:!1},(function(){var n=t.inputRef.current.value;if(t.isValueChanged(n,t.props.value)){var i=t.validateValue(t.parseValue(n));t.updateInputValue(i),t.updateModel(e,i)}t.props.onBlur&&t.props.onBlur(e)}))}},{key:"clearTimer",value:function(){this.timer&&clearInterval(this.timer)}},{key:"isStacked",value:function(){return this.props.showButtons&&"stacked"===this.props.buttonLayout}},{key:"isHorizontal",value:function(){return this.props.showButtons&&"horizontal"===this.props.buttonLayout}},{key:"isVertical",value:function(){return this.props.showButtons&&"vertical"===this.props.buttonLayout}},{key:"getInputMode",value:function(){return this.props.inputMode||("decimal"!==this.props.mode||this.props.minFractionDigits?"decimal":"numeric")}},{key:"getFormatter",value:function(){return this.numberFormat}},{key:"updateInputRef",value:function(){var e=this.props.inputRef;e&&("function"==typeof e?e(this.inputRef.current):e.current=this.inputRef.current)}},{key:"componentDidMount",value:function(){this.updateInputRef(),this.props.tooltip&&this.renderTooltip();var e=this.validateValue(this.props.value);null!==this.props.value&&this.props.value!==e&&this.updateModel(null,e)}},{key:"componentDidUpdate",value:function(e){e.tooltip===this.props.tooltip&&e.tooltipOptions===this.props.tooltipOptions||(this.tooltip?this.tooltip.update(D({content:this.props.tooltip},this.props.tooltipOptions||{})):this.renderTooltip());var t=this.isOptionChanged(e);if(t&&this.constructParser(),e.value!==this.props.value||t){var n=this.validateValue(this.props.value);this.updateInputValue(n),null!==this.props.value&&this.props.value!==n&&this.updateModel(null,n)}}},{key:"isOptionChanged",value:function(e){var t=this;return["locale","localeMatcher","mode","currency","currencyDisplay","useGrouping","minFractionDigits","maxFractionDigits","suffix","prefix"].some((function(n){return e[n]!==t.props[n]}))}},{key:"componentWillUnmount",value:function(){this.tooltip&&(this.tooltip.destroy(),this.tooltip=null)}},{key:"renderTooltip",value:function(){this.tooltip=s({target:this.element,content:this.props.tooltip,options:this.props.tooltipOptions})}},{key:"renderInputElement",value:function(){var t=r("p-inputnumber-input",this.props.inputClassName),n=this.formattedValue(this.props.value);return e.createElement(i,{ref:this.inputRef,id:this.props.inputId,style:this.props.inputStyle,role:"spinbutton",className:t,defaultValue:n,type:this.props.type,size:this.props.size,tabIndex:this.props.tabIndex,inputMode:this.getInputMode(),maxLength:this.props.maxLength,disabled:this.props.disabled,required:this.props.required,pattern:this.props.pattern,placeholder:this.props.placeholder,readOnly:this.props.readOnly,name:this.props.name,autoFocus:this.props.autoFocus,onKeyDown:this.onInputKeyDown,onKeyPress:this.onInputKeyPress,onInput:this.onInput,onClick:this.onInputClick,onBlur:this.onInputBlur,onFocus:this.onInputFocus,onPaste:this.onPaste,min:this.props.min,max:this.props.max,"aria-valuemin":this.props.min,"aria-valuemax":this.props.max,"aria-valuenow":this.props.value,"aria-labelledby":this.props.ariaLabelledBy})}},{key:"renderUpButton",value:function(){var t=r("p-inputnumber-button p-inputnumber-button-up p-button p-button-icon-only p-component",{"p-disabled":this.props.disabled},this.props.incrementButtonClassName),n=r("p-button-icon",this.props.incrementButtonIcon);return e.createElement("button",{type:"button",className:t,onMouseLeave:this.onUpButtonMouseLeave,onMouseDown:this.onUpButtonMouseDown,onMouseUp:this.onUpButtonMouseUp,onKeyDown:this.onUpButtonKeyDown,onKeyUp:this.onUpButtonKeyUp,disabled:this.props.disabled,tabIndex:-1},e.createElement("span",{className:n}),e.createElement(o,null))}},{key:"renderDownButton",value:function(){var t=r("p-inputnumber-button p-inputnumber-button-down p-button p-button-icon-only p-component",{"p-disabled":this.props.disabled},this.props.decrementButtonClassName),n=r("p-button-icon",this.props.decrementButtonIcon);return e.createElement("button",{type:"button",className:t,onMouseLeave:this.onDownButtonMouseLeave,onMouseDown:this.onDownButtonMouseDown,onMouseUp:this.onDownButtonMouseUp,onKeyDown:this.onDownButtonKeyDown,onKeyUp:this.onDownButtonKeyUp,disabled:this.props.disabled,tabIndex:-1},e.createElement("span",{className:n}),e.createElement(o,null))}},{key:"renderButtonGroup",value:function(){var t=this.props.showButtons&&this.renderUpButton(),n=this.props.showButtons&&this.renderDownButton();return this.isStacked()?e.createElement("span",{className:"p-inputnumber-button-group"},t,n):e.createElement(e.Fragment,null,t,n)}},{key:"render",value:function(){var t=this,n=r("p-inputnumber p-component p-inputwrapper",this.props.className,{"p-inputwrapper-filled":null!=this.props.value&&this.props.value.toString().length>0,"p-inputwrapper-focus":this.state.focused,"p-inputnumber-buttons-stacked":this.isStacked(),"p-inputnumber-buttons-horizontal":this.isHorizontal(),"p-inputnumber-buttons-vertical":this.isVertical()}),i=this.renderInputElement(),s=this.renderButtonGroup();return e.createElement("span",{ref:function(e){return t.element=e},id:this.props.id,className:n,style:this.props.style},i,s)}}],w&&f(x.prototype,w),k&&f(x,k),Object.defineProperty(x,"prototype",{writable:!1}),_}();x(k,"defaultProps",{value:null,inputRef:null,format:!0,showButtons:!1,buttonLayout:"stacked",incrementButtonClassName:null,decrementButtonClassName:null,incrementButtonIcon:"pi pi-angle-up",decrementButtonIcon:"pi pi-angle-down",locale:void 0,localeMatcher:void 0,mode:"decimal",suffix:null,prefix:null,currency:void 0,currencyDisplay:void 0,useGrouping:!0,minFractionDigits:void 0,maxFractionDigits:void 0,id:null,name:null,type:"text",allowEmpty:!0,step:1,min:null,max:null,disabled:!1,required:!1,tabIndex:null,pattern:null,inputMode:null,placeholder:null,readOnly:!1,size:null,style:null,className:null,inputId:null,autoFocus:!1,inputStyle:null,inputClassName:null,tooltip:null,tooltipOptions:null,ariaLabelledBy:null,onValueChange:null,onChange:null,onBlur:null,onFocus:null,onKeyDown:null});export{k as InputNumber};