UNPKG

@mobx-sentinel/form

Version:

MobX library for non-intrusive class-based model enhancement. Acting as a sentinel, it provides change detection, reactive validation, and form integration capabilities without contamination.

2 lines 6.79 kB
"use strict";var F=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var R=Object.prototype.hasOwnProperty;var O=(o,e)=>{for(var n in e)F(o,n,{get:e[n],enumerable:!0})},j=(o,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of E(e))!R.call(o,t)&&t!==n&&F(o,t,{get:()=>e[t],enumerable:!(i=x(e,t))||i.enumerable});return o};var D=o=>j(F({},"__esModule",{value:!0}),o),s=(o,e,n,i)=>{for(var t=i>1?void 0:i?x(e,n):e,g=o.length-1,f;g>=0;g--)(f=o[g])&&(t=(i?f(e,n,t):f(t))||t);return i&&t&&F(e,n,t),t};var H={};O(H,{Form:()=>b,FormField:()=>a,configureForm:()=>I});module.exports=D(H);var d=require("mobx"),M=require("uuid"),m=require("@mobx-sentinel/core");var r=require("mobx"),w=require("uuid"),B=require("@mobx-sentinel/core");var A=Symbol("formField.internal"),a=class{id=(0,w.v4)();fieldName;validator;#t;#e=r.observable.box(!1);#i=r.observable.box(null);#n=r.observable.box(!1);#o=r.observable.box(!1);#r=null;constructor(e){(0,r.makeObservable)(this),this.fieldName=e.fieldName,this.validator=e.validator,this.#t=e.getFinalizationDelayMs,(0,r.reaction)(()=>[this.#n.get(),this.validator.isValidating],([n,i])=>{i||this.#o.set(n)},{equals:r.comparer.shallow})}get isTouched(){return this.#e.get()}get isIntermediate(){return this.#i.get()==="intermediate"}get isChanged(){return!!this.#i.get()}get isErrorReported(){if(this.#o.get())return this.hasErrors}get errors(){return this.validator.getErrorMessages(B.KeyPath.build(this.fieldName))}get hasErrors(){return this.validator.hasErrors(B.KeyPath.build(this.fieldName))}reset(){this.#i.set(null),this.#e.set(!1),this.#n.set(!1),this.#s()}markAsTouched(){this.#e.set(!0)}markAsChanged(e="final"){switch(this.#i.set(e),e){case"final":{this.#s(),this.reportError();break}case"intermediate":{this.#a();break}}}reportError(){this.#n.set(!0)}finalizeChangeIfNeeded(){this.#s(),this.isIntermediate&&this.markAsChanged("final")}#a(){this.#s(),this.#r=+setTimeout(()=>{this.finalizeChangeIfNeeded()},this.#t())}#s(){this.#r&&(clearTimeout(this.#r),this.#r=null)}[A](){return{isReported:this.#n}}};s([r.computed],a.prototype,"isErrorReported",1),s([r.computed.struct],a.prototype,"errors",1),s([r.computed],a.prototype,"hasErrors",1),s([r.action],a.prototype,"reset",1),s([r.action],a.prototype,"markAsTouched",1),s([r.action],a.prototype,"markAsChanged",1),s([r.action],a.prototype,"reportError",1);var v=require("uuid");function p(o){let e=S.get(o);return e||(e=`${o.name}--${(0,v.v4)()}`,S.set(o,e)),e}var S=new WeakMap;var h=require("mobx"),N=Object.freeze({autoFinalizationDelayMs:3e3,allowSubmitNonDirty:!1,allowSubmitInvalid:!1}),c=h.observable.object(N);function I(o){return(0,h.runInAction)(()=>{o===!0?Object.assign(c,N):Object.assign(c,o)}),c}var u=require("mobx"),y=class{#t=u.observable.box(!1);#e={willSubmit:new Set,submit:new Set,didSubmit:new Set};#i=null;get isRunning(){return this.#t.get()}addHandler(e,n){return this.#e[e].add(n),()=>void this.#e[e].delete(n)}async exec(){this.#i?.abort();let e=new AbortController;this.#i=e;let n=!0;(0,u.runInAction)(()=>{this.#t.set(!0)});try{for(let i of this.#e.willSubmit)if(!await i(e.signal)){n=!1;break}}catch(i){n=!1,console.error(i)}if(n)try{for(let i of this.#e.submit)if(!await i(e.signal)){n=!1;break}}catch(i){n=!1,console.error(i)}return this.#i=null,(0,u.runInAction)(()=>{this.#t.set(!1);try{for(let i of this.#e.didSubmit)i(n)}catch(i){console.warn(i)}}),n}};var C=new WeakMap,V=Symbol("form.defaultFormKey"),T=Symbol("form.internalToken"),l=class l{id=(0,M.v4)();#t;watcher;validator;#e;#i=new y;#n=new Map;#o=new Map;#r=d.observable.box({});static get(e,n){let i=this.getSafe(e,n);if(!i)throw new TypeError("subject: Expected an object");return i}static getSafe(e,n){if(!e||typeof e!="object")return null;n??=V;let i=C.get(e);i||(i=new Map,C.set(e,i));let t=i.get(n);return t||(t=new this(T,{subject:e,formKey:n}),i.set(n,t)),t}static dispose(e,n){let i=C.get(e);i&&(n?i.delete(n):i.clear())}constructor(e,n){if(e!==T)throw new Error("private constructor");this.#t=n.formKey,this.watcher=m.Watcher.get(n.subject),this.validator=m.Validator.get(n.subject),this.#e=new m.StandardNestedFetcher(n.subject,i=>l.getSafe(i.data,this.#t)),(0,d.makeObservable)(this),this.#i.addHandler("didSubmit",i=>{i&&this.reset()})}get config(){return{...c,...this.#r.get()}}configure=e=>{typeof e=="object"?Object.assign(this.#r.get(),e):this.#r.set({})};get isDirty(){return this.watcher.changed}get isValid(){return this.validator.isValid}get invalidFieldCount(){return this.validator.invalidKeyCount}get invalidFieldPathCount(){return this.validator.invalidKeyPathCount}get isValidating(){return this.validator.isValidating}get isSubmitting(){return this.#i.isRunning}get isBusy(){return this.isSubmitting||this.isValidating}get canSubmit(){return!this.isBusy&&(this.config.allowSubmitInvalid||this.isValid)&&(this.config.allowSubmitNonDirty||this.isDirty)}get subForms(){return this.#e.dataMap}reportError(){for(let e of this.#n.values())e.reportError();for(let e of this.#e)e.data.reportError()}reset(){this.watcher.reset();for(let e of this.#n.values())e.reset();for(let e of this.#e)e.data.reset()}markAsDirty(){this.watcher.assumeChanged()}async submit(e){return!e?.force&&!this.canSubmit?!1:this.#i.exec()}addHandler=(...e)=>this.#i.addHandler(...e);getField(e){let n=this.#n.get(e);return n||(n=new a({fieldName:String(e),validator:this.validator,getFinalizationDelayMs:()=>this.config.autoFinalizationDelayMs}),this.#n.set(e,n)),n}#a(e,n){let i=this.#o.get(e);return i||(i=n(),this.#o.set(e,i)),i}#s=(e,n)=>{let i=`${p(e)}:${n?.cacheKey}`,t=this.#a(i,()=>new e(this,n));return t.config=n,t.props};#d=(e,n,i)=>{let t=`${e}@${p(n)}:${i?.cacheKey}`,g=this.#a(t,()=>{let f=this.getField(e);return new n(f,i)});return g.config=i,g.props};#l=(e,n,i)=>{let t=`${e.join(",")}@${p(n)}:${i?.cacheKey}`,g=this.#a(t,()=>{let f=e.map(k=>this.getField(k));return new n(f,i)});return g.config=i,g.props};bind=(...e)=>typeof e[0]=="string"?this.#d(e[0],e[1],e[2]):Array.isArray(e[0])?this.#l(e[0],e[1],e[2]):this.#s(e[0],e[1]);getErrors(e,n=!1){let i=this.getField(e);return!n&&!i.isErrorReported?new Set:i.errors}getAllErrors(e){return this.validator.getErrorMessages(e?m.KeyPath.build(e):m.KeyPath.Self,!0)}get firstErrorMessage(){return this.validator.firstErrorMessage}[T](){return{fields:this.#n,bindings:this.#o,submission:this.#i}}};s([d.computed.struct],l.prototype,"config",1),s([d.action.bound],l.prototype,"configure",2),s([d.computed],l.prototype,"isBusy",1),s([d.computed],l.prototype,"canSubmit",1),s([d.action],l.prototype,"reportError",1),s([d.action],l.prototype,"reset",1),s([d.computed],l.prototype,"firstErrorMessage",1);var b=l;0&&(module.exports={Form,FormField,configureForm}); //# sourceMappingURL=index.js.map