UNPKG

@i18n-components/input-number

Version:

Web component to format input number based on locale

2 lines 8.43 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.i18nComponent=e():(t.i18nComponent=t.i18nComponent||{},t.i18nComponent["input-number"]=e())}(self,(function(){return function(){"use strict";var t={921:function(t,e,n){function i(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function o(t,e,n){return e&&i(t.prototype,e),n&&i(t,n),t}function r(t,e){return(r=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function a(t){return(a=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function u(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function l(t){return(l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function c(t,e){return!e||"object"!==l(e)&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function s(t,e,n){return(s=u()?Reflect.construct:function(t,e,n){var i=[null];i.push.apply(i,e);var o=new(Function.bind.apply(t,i));return n&&r(o,n.prototype),o}).apply(null,arguments)}function f(t){var e="function"==typeof Map?new Map:void 0;return(f=function(t){if(null===t||(n=t,-1===Function.toString.call(n).indexOf("[native code]")))return t;var n;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,i)}function i(){return s(t,arguments,a(this).constructor)}return i.prototype=Object.create(t.prototype,{constructor:{value:i,enumerable:!1,writable:!0,configurable:!0}}),r(i,t)})(t)}function h(t){return Math.max(t.selectionStart,t.selectionEnd)}function p(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:".",i=new RegExp("^-?\\d+(?:"+n+"\\d{0,"+(e||-1)+"})?");return Number(Array.isArray(t.toString().match(i))?t.toString().match(i)[0]:0)}n.r(e),n.d(e,{InputNumber:function(){return g}});var g=function(t){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&r(t,e)}(l,t);var e,n,i=(e=l,n=u(),function(){var t,i=a(e);if(n){var o=a(this).constructor;t=Reflect.construct(i,arguments,o)}else t=i.apply(this,arguments);return c(this,t)});function l(){var t;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,l),(t=i.call(this)).intl=t.initializeIntl(),t.numRegx=t.getNumRegx(),t.inputValue=t.value||t.intl.format(0),t.value=t.getFormattedValue(),t}return o(l,null,[{key:"tag",get:function(){return"i18n-input-number"}},{key:"observedAttributes",get:function(){return["locale","decimaldigits","value"]}}]),o(l,[{key:"getNumRegx",value:function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.locale,n="|\\"+this.getDecimalSeparator(e),i=this.getThousandSeparator(e)?"|\\"+this.getThousandSeparator(e):"",o="\\d"+n+i;return t&&(o="[^"+o+"]"),new RegExp(o,"g")}},{key:"getFormattedValue",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.locale,n=void 0===e?this.locale:e,i=t.value,o=void 0===i?this.inputValue:i;return this.intl.format(this.getNumericValue({locale:n,value:o}))}},{key:"getDecimalSeparator",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.locale;return 1.1.toLocaleString(t).substring(1,2)}},{key:"getThousandSeparator",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.locale,e=1e3.toLocaleString(t).substring(1,2);return e.match(/\d/)?"":e}},{key:"isValidInput",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.value,n=void 0===e?this.value:e,i=t.locale,o=void 0===i?this.locale:i,r=this.getNumRegx(!0,o);return!r.exec(n)||!isNaN(Number(n))}},{key:"getNumericValue",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.locale,n=void 0===e?this.locale:e,i=t.value,o=void 0===i?this.inputValue:i;if(!o)return 0;var r=o;return(isNaN(Number(o))||o===this.inputValue&&!isNaN(Number(o))&&"."===this.getThousandSeparator(n))&&(r=r.replace(new RegExp("\\".concat(this.getThousandSeparator(n)),"gi"),"").replace(new RegExp("\\".concat(this.getDecimalSeparator(n)),"gi"),".")),Number(p(parseFloat(r),this.fractionDigits))}},{key:"initializeIntl",value:function(){return new Intl.NumberFormat(this.locale,{maximumFractionDigits:this.fractionDigits,minimumFractionDigits:this.fractionDigits})}},{key:"connectedCallback",value:function(){this.type="tel",this.attachEvents()}},{key:"attributeChangedCallback",value:function(t,e,n){if("locale"!==t&&"decimaldigits"!==t||(this.intl=this.initializeIntl()),"locale"===t)this.isValidInput({locale:e})&&(this.inputValue=this.value=this.getFormattedValue({locale:e}));else{var i="value"===t?n:void 0;this.isValidInput({value:i})?this.inputValue=this.value=this.getFormattedValue({value:i}):(console.info("@i18n-components/input-number:","Invalid attribute "+t+" Value: ",n),"value"===t&&(this.inputValue=this.value=e))}}},{key:"updateCaretPosition",value:function(t){!function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;t.value=t.value,null!==t&&(t.selectionStart||0===t.selectionStart?(t.focus(),t.setSelectionRange(e,e)):t.focus())}(this,t)}},{key:"attachEvents",value:function(){this.addEventListener("change",this.onChange),this.addEventListener("input",this.onChange),this.addEventListener("keydown",this.onKeydown),this.addEventListener("focus",this.onFocus)}},{key:"onFocus",value:function(){this.isValidInput()||(this.inputValue=this.value=this.intl.format(0))}},{key:"onChange",value:function(){this.isValidInput()&&(this.inputValue=this.value);var t=h(this),e=this.getFormattedValue(),n=function(t,e,n){var i,o,r=/\d/g,a=(t.match(r)||[]).join(""),u=(e.match(r)||[]).join("");for(i=0,o=0;o<n;o++){var l=t[o]||"",c=e[i]||"";if((l.match(r)||l===c)&&("0"!==l||!c.match(r)||"0"===c||a.length===u.length)){for(;l!==e[i]&&i<e.length;)i++;i++}}return i}(this.inputValue,this.getFormattedValue(),t);this.inputValue=this.value=e,this.isValidInput()&&this.updateCaretPosition(n)}},{key:"onKeydown",value:function(t){var e=t.key,n=h(this),i=this.getNumRegx(!0);e.match(/^[1-9]$/g)&&0===n?this.updateCaretPosition(1):"Backspace"===e?this.correctCaretPosition(!0):"Delete"===e?this.correctCaretPosition():i.exec(e)&&this.updateCaretPosition(n)}},{key:"correctCaretPosition",value:function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],e=h(this),n=this.fractionDigits?"\\"+this.getDecimalSeparator():"",i=this.getThousandSeparator()?"|\\"+this.getThousandSeparator():"",o=new RegExp("^"+n+i+"$","g"),r=t?this.inputValue[e-1]:this.inputValue[e];if(null==r?void 0:r.match(o)){var a=e+(t?-1:1);this.updateCaretPosition(a)}}},{key:"locale",get:function(){return this.getAttribute("locale")||this.setAttribute("locale",navigator.language),this.getAttribute("locale")}},{key:"formattedValue",get:function(){return this.getFormattedValue()}},{key:"fractionDigits",get:function(){var t=Number(parseInt(this.getAttribute("decimaldigits")));return!isNaN(t)&&t>-1?t:2}},{key:"numericValue",get:function(){return this.getNumericValue()}}]),l}(f(HTMLInputElement));customElements.define(g.tag,g,{extends:"input"})}},e={};function n(i){if(e[i])return e[i].exports;var o=e[i]={exports:{}};return t[i](o,o.exports,n),o.exports}return n.d=function(t,e){for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n(921)}()})); //# sourceMappingURL=index.js.map