UNPKG

tiny-mobx-form

Version:

Tiny (~2KB) tree-shakable MobX form library

3 lines (2 loc) 6.12 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx");function r(e,t){for(var r=0;r<t.length;r++){var i=t[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function i(e,t,i){return t&&r(e.prototype,t),i&&r(e,i),e}function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e}).apply(this,arguments)}var n=function(e,t){return void 0===t&&(t=""),e?t:void 0},a=function(e){return"Please choose one of these choices: "+e.join(", ")},l=function(e,t){return n(!e.value,t||"This field is required.")},s=function(e,t,r){return n(!(e.value&&e.form.fields[r]),t||"The "+e.form.fields[r].label+" field is required since the "+e.label+" field is filled out.")},u=function(e,t,r,i){return void 0===r&&(r=0),void 0===i&&(i=Infinity),n(!(e.value&&e.value.length>=Number(r)&&e.value.length<=Number(i)),t||function(e,t){return"Must be at least "+e+" and no more than "+t+" characters"}(r.toString(10),i.toString(10)))},d=function(e,t,r){return n(e.value!==e.form.fields[r].value,t||"The "+e.label+" and "+e.form.fields[r].label+" do not match.")},c=/^[a-zA-Z\s]*$/,f=function(e,t){var r=e.value;return n(!!r&&!c.test(r),t||"Please enter letters only.")},v=/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/,h=function(e,t){var r=e.value;return n(!!r&&!v.test(r),t||"Please enter a valid email address.")},p=/^(\()?[2-9]\d{2}(-|.|\))?\d{3}(-|.)?\d{4}$/,m=function(e,t){var r=e.value;return n(!!r&&!p.test(r),t||"Please enter a valid phone number.")},y=/^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$/,_=function(e,t){var r=e.value;return n(!!r&&!y.test(r),t||"Please enter a valid postal number.")},b=/^\d+$/,g=function(e,t){var r=e.value;return n(!!r&&!b.test(r),t||"Please enter a valid number.")},V=/^\w+$/,w=function(e,t){var r=e.value;return n(!!r&&!V.test(r),t||"Please do not use special characters.")},N=function(e,t,r,i){var o=e.value;return void 0===r&&(r=0),void 0===i&&(i=Infinity),n(!(!o||Number(o)>=r&&Number(o)<=i),t||function(e,t){return"Please enter a number between "+e+" and "+t+"."}(r.toString(10),i.toString(10)))},k=function(e,t){for(var r=e.value,i=arguments.length,o=new Array(i>2?i-2:0),l=2;l<i;l++)o[l-2]=arguments[l];return n(!!r&&!o.includes(r),t||a(o))},T=function(){function e(e,t,r){var i=t.name,o=t.label,n=void 0===o?"":o,a=t.placeholder,l=void 0===a?"":a,s=t.validation,u=void 0===s?"":s,d=t.initialValue,c=void 0===d?"":d,f=t.validationMessages,v=void 0===f?{}:f,h=r.additionalValidators;this.form=e,this.isTouched=!1,this.isFocused=!1,this.name=i,this.label=n,this.placeholder=l,this.initialValue=c,this._value=c,this.validation=u,this.validators=h,this.validationMessages=v}return e.prototype.reset=function(){this._value=this.initialValue,this.isTouched=!1,this.isFocused=!1},i(e,[{key:"value",get:function(){return this._value},set:function(e){this._value=e,this.isTouched=!0}},{key:"isValid",get:function(){return 0===this.errors.length}},{key:"isDirty",get:function(){return this.initialValue!==this.value}},{key:"errors",get:function(){return this.validators?(t=this.validationMessages,r=this.validators,(e=this).validation?e.validation.split("|").filter(Boolean).map((function(i){var o=i.split(":"),n=o[0],a=o[1],l=[];if(a&&(l=a.split(",")),!(n in r))throw new Error("There is no validator with the name of "+n);return r[n].apply(r,[e,t&&n in t?t[n]:""].concat(l))})).filter(Boolean):[]):[];var e,t,r}},{key:"hasErrors",get:function(){return this.errors.length>0}}]),e}();e.__decorate([t.observable],T.prototype,"initialValue",void 0),e.__decorate([t.observable],T.prototype,"isTouched",void 0),e.__decorate([t.observable],T.prototype,"isFocused",void 0),e.__decorate([t.observable],T.prototype,"validation",void 0),e.__decorate([t.observable],T.prototype,"_value",void 0),e.__decorate([t.computed],T.prototype,"value",null),e.__decorate([t.computed],T.prototype,"isValid",null),e.__decorate([t.computed],T.prototype,"isDirty",null),e.__decorate([t.computed],T.prototype,"errors",null),e.__decorate([t.computed],T.prototype,"hasErrors",null),e.__decorate([t.action("TinyMobxForm | Field | reset")],T.prototype,"reset",null);var P=function(){function e(e,t,r){var i=this;void 0===t&&(t={}),void 0===r&&(r={}),this.fields={},this.showErrors=function(){var e=!1;i.fieldNames.forEach((function(t){i.fields[t].isTouched=!0,i.fields[t].isValid||e||(i.fields[t].isFocused=!0,e=!0)}))},this.reset=function(){i.fieldNames.forEach((function(e){return i.fields[e].reset()}))},e.forEach((function(e){i.fieldNames.push(e.name);var n,a=o({},e,{initialValue:t[e.name]||e.initialValue});i.fields[a.name]=new T(i,a,{additionalValidators:(n=r.additionalValidators||{},void 0===n&&(n={}),o({required:l,"required-if":s,length:u,letters:f,email:h,match:d,numbers:g,phone:m,postal:_,alpha:w,size:N,"one-of":k},n))})}))}return i(e,[{key:"fieldNames",get:function(){return Object.keys(this.fields)}},{key:"isValid",get:function(){var e=this;return 0===this.fieldNames.filter((function(t){return!e.fields[t].isValid})).length}},{key:"errors",get:function(){var e=this;return this.fieldNames.flatMap((function(t){return e.fields[t].errors}))}},{key:"isDirty",get:function(){var e=this;return this.fieldNames.some((function(t){return e.fields[t].isDirty}))}},{key:"values",get:function(){var e=this;return this.fieldNames.reduce((function(t,r){var i;return o({},t,((i={})[r]=e.fields[r].value,i))}),{})}}]),e}();e.__decorate([t.observable.struct],P.prototype,"fields",void 0),e.__decorate([t.computed],P.prototype,"fieldNames",null),e.__decorate([t.computed],P.prototype,"isValid",null),e.__decorate([t.computed],P.prototype,"errors",null),e.__decorate([t.computed],P.prototype,"isDirty",null),e.__decorate([t.computed],P.prototype,"values",null),e.__decorate([t.action("TinyMobXForm | showErrors")],P.prototype,"showErrors",void 0),e.__decorate([t.action("TinyMobXForm | reset")],P.prototype,"reset",void 0),exports.Field=T,exports.Form=P; //# sourceMappingURL=tiny-mobx-form.cjs.production.min.js.map