input-soft-limit
Version:
A simple JavaScript module that applies a soft character limit to inputs and textareas.
1 lines • 3.14 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("inputSoftLimit",[],e):"object"==typeof exports?exports.inputSoftLimit=e():t.inputSoftLimit=e()}(window,(function(){return function(t){var e={};function n(i){if(e[i])return e[i].exports;var r=e[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(i,r,function(e){return t[e]}.bind(null,r));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e){function n(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}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)}}var r=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t);this.settings=Object.assign({parent:document,inputsSelector:".input-soft-limit",submitButton:null,inputInvalidClass:"is-invalid"},e)}var e,r,o;return e=t,(r=[{key:"init",value:function(){var t=this,e=t.settings.parent.querySelectorAll(t.settings.inputsSelector);0!==e.length&&(t.elems=n(e),t.elems.map((function(e){var n=e.getAttribute("data-input-soft-limit");if(n){var i=document.querySelector(e.getAttribute("data-input-soft-limit-counter"));i&&(i.innerHTML=n),e.setAttribute("data-input-soft-limit-status","valid"),e.addEventListener("input",(function(r){var o=e.value.length,u=n-o;i&&(i.innerHTML=u),u<0?(e.classList.add(t.settings.inputInvalidClass),e.setAttribute("data-input-soft-limit-status","invalid")):(e.classList.remove(t.settings.inputInvalidClass),e.setAttribute("data-input-soft-limit-status","valid")),t.settings.submitButton&&t.checkAll()}))}})))}},{key:"checkAll",value:function(){var t=this.settings.parent.querySelector(this.settings.submitButton);this.elems.filter((function(t){return"invalid"===t.getAttribute("data-input-soft-limit-status")})).length<1?t.disabled=!1:t.disabled=!0}}])&&i(e.prototype,r),o&&i(e,o),t}();t.exports=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return new r(t)}}])}));