UNPKG

mobx-react-form

Version:
3 lines (2 loc) 47.6 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("mobx"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","mobx","lodash"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).MobxReactForm={},e.mobx,e._)}(this,function(e,t,i){"use strict";class s{_isArrayMap=!0;_entries=t.observable.array([]);constructor(e){e&&e.length>0&&this._entries.replace(e)}get size(){return this._entries.length}get(e){const t=this._entries.find(([t])=>t===e);return t?t[1]:void 0}set(e,t){const i=this._entries.find(([t])=>t===e);return i?i[1]=t:this._entries.push([e,t]),this}has(e){return this._entries.some(([t])=>t===e)}delete(e){const t=this._entries.findIndex(([t])=>t===e);return-1!==t&&(this._entries.splice(t,1),!0)}clear(){this._entries.clear()}keys(){return this._entries.map(([e])=>e)[Symbol.iterator]()}values(){return this._entries.map(([,e])=>e)[Symbol.iterator]()}entries(){return this._entries[Symbol.iterator]()}forEach(e,t){this._entries.forEach(([i,s])=>{e.call(t,s,i,this)})}toJSON(){const e={};return this._entries.forEach(([t,i])=>{e[String(t)]=i}),e}toString(){return"[object ArrayMap]"}merge(e){return e&&"object"==typeof e&&(e.forEach&&"function"==typeof e.forEach?e.forEach((e,t)=>{this.set(t,e)}):Array.isArray(e)?e.forEach(([e,t])=>{this.set(e,t)}):Object.keys(e).forEach(t=>{this.set(t,e[t])})),this}replace(e){return this.clear(),this.merge(e)}get intercept(){return this._entries.intercept}[Symbol.iterator](){return this._entries[Symbol.iterator]()}move(e,i){t.runInAction(()=>{if(e<0||e>=this._entries.length)return;if(i<0||i>=this._entries.length)return;if(e===i)return;const[t]=this._entries.splice(e,1);this._entries.splice(i,0,t)})}toArray(){return this._entries}}var r,a,l,n;!function(e){e.key="key",e.id="id",e.path="path",e.name="name",e.fields="fields",e.ref="ref",e.type="type",e.computed="computed",e.value="value",e.initial="initial",e.default="default",e.checked="checked",e.label="label",e.placeholder="placeholder",e.error="error",e.validatedWith="validatedWith",e.validators="validators",e.rules="rules",e.related="related",e.options="options",e.extra="extra",e.bindings="bindings",e.hooks="hooks",e.handlers="handlers",e.converter="converter",e.input="input",e.output="output",e.interceptors="interceptors",e.observers="observers",e.disabled="disabled",e.deleted="deleted",e.blurred="blurred",e.validating="validating",e.submitting="submitting",e.clearing="clearing",e.resetting="resetting",e.changed="changed",e.touched="touched",e.focused="focused",e.isEmpty="isEmpty",e.isDefault="isDefault",e.isPristine="isPristine",e.isDirty="isDirty",e.isValid="isValid",e.hasError="hasError",e.onInit="onInit",e.onSync="onSync",e.onChange="onChange",e.onBlur="onBlur",e.onFocus="onFocus",e.onToggle="onToggle",e.onDrop="onDrop",e.onSubmit="onSubmit",e.onReset="onReset",e.onClear="onClear",e.onAdd="onAdd",e.onDel="onDel",e.autoFocus="autoFocus",e.inputMode="inputMode",e.onKeyDown="onKeyDown",e.onKeyUp="onKeyUp",e.class="class",e.nullable="nullable",e.autoComplete="autoComplete"}(r||(r={})),function(e){e.computed="computed",e.observable="observable",e.editable="editable",e.all="all"}(a||(a={})),function(e){e.some="some",e.every="every"}(l||(l={})),function(e){e.computed="computed",e.values="values",e.labels="labels",e.placeholders="placeholders",e.defaults="defaults",e.initials="initials",e.disabled="disabled",e.deleted="deleted",e.types="types",e.related="related",e.rules="rules",e.options="options",e.bindings="bindings",e.extra="extra",e.hooks="hooks",e.handlers="handlers",e.validatedWith="validatedWith",e.validators="validators",e.observers="observers",e.interceptors="interceptors",e.converters="converters",e.input="input",e.output="output",e.autoFocus="autoFocus",e.inputMode="inputMode",e.refs="refs",e.classes="classes",e.nullable="nullable",e.autoComplete="autoComplete"}(n||(n={}));const o={editable:[r.type,r.value,r.initial,r.default,r.label,r.placeholder,r.related,r.options,r.extra,r.bindings,r.hooks,r.handlers,r.deleted,r.disabled,r.autoFocus,r.inputMode,r.ref,r.nullable,r.autoComplete],handlers:[r.onChange,r.onToggle,r.onFocus,r.onBlur,r.onDrop,r.onSubmit,r.onReset,r.onClear,r.onAdd,r.onDel],computed:[r.error,r.hasError,r.isValid,r.isDirty,r.isPristine,r.isDefault,r.isEmpty,r.focused,r.touched,r.changed,r.validating,r.submitting,r.resetting,r.clearing,r.blurred,r.deleted,r.disabled],separated:[n.computed,n.values,n.labels,n.placeholders,n.defaults,n.initials,n.disabled,n.deleted,n.types,n.related,n.rules,n.options,n.bindings,n.extra,n.hooks,n.handlers,n.validatedWith,n.validators,n.observers,n.interceptors,n.converters,n.input,n.output,n.autoFocus,n.inputMode,n.refs,n.classes,n.nullable,n.autoComplete],functions:[r.computed,r.observers,r.interceptors,r.converter,r.input,r.output],validation:[r.rules,r.validators,r.validatedWith],occurrences:{isDirty:l.some,isPristine:l.every,isDefault:l.every,isValid:l.every,isEmpty:l.every,hasError:l.some,focused:l.some,blurred:l.some,touched:l.some,deleted:l.every,disabled:l.every,clearing:l.every,resetting:l.every}},d=e=>{if(e&&e._isArrayMap){const t=[];return e.forEach(e=>t.push(e)),t}return t.values(e)},h=({type:e,data:t})=>{let i;switch(e){case l.some:i=e=>e.some(Boolean);break;case l.every:i=e=>e.every(Boolean);break;default:throw new Error("Occurrence not found for specified prop")}return i(t)},u=(e,t)=>{let i;switch(e){case a.computed:i=o.computed;break;case a.observable:i=[r.fields,...o.computed,...o.editable];break;case a.editable:i=[...o.editable,...o.validation,...o.functions,...o.handlers];break;case a.all:i=[r.id,r.key,r.name,r.path,...o.computed,...o.editable,...o.validation,...o.functions,...o.handlers];break;default:i=null}return t.filter(e=>i.includes(e)).length>0},c=(e,t)=>{if(u(e,t))return;throw new Error(`The selected property is not allowed (${JSON.stringify(t)})`)},p=(e,t,i=null)=>{if(null!=t)return;throw new Error(`${null==i?"The selected field is not defined":i} (${e})`)},v=e=>{let t;return t=e.replace(/\.\d+($|\.)/g,"[]."),t=t.replace("..","."),t=t.replace(/^\.+|\.+$/g,""),t},f=(e,t)=>t.some(t=>i.has(e,t)),b=e=>i.isEmpty(e)&&Array.isArray(e),g=e=>Array.isArray(e)&&e.every(e=>"string"==typeof e),m=e=>Array.isArray(e)&&e.every(e=>i.isPlainObject(e)),y=({fields:e})=>!g({fields:e})&&u(a.editable,(e=>e?[...new Set(Object.values(e).flatMap(e=>e?Object.keys(e):[]))]:[])(e)),$=e=>Array.from((e=>e&&e._isArrayMap?Array.from(e.keys()):t.keys(e))(e)).map(Number),O=e=>$(e).every(e=>Number.isInteger(e)),E=e=>{const t=$(e),i=t.length?Math.max(...t):void 0;return void 0===i?0:i+1};let k=0;const V=e=>`${[e.path.replace(/\./g,"-"),"--"].join("")}${++k}`,F=e=>null!=e&&"undefined"!=typeof Event&&(e instanceof Event||null!=e.target),A=(...e)=>{for(const t of e)if(void 0!==t)return t},w=({type:e,value:t,nullable:i,isEmptyArray:s=!1,fallbackValueOption:r=""})=>Array.isArray(t)||s?[]:i||t instanceof Date||"date"===e||"datetime-local"===e?null:"number"==typeof t||"number"===e?0:"boolean"!=typeof t&&"checkbox"!==e&&("string"==typeof t||"file"===e?"":r),D=e=>{let t=String(e??"");return t=t.replace(/\[/g,"."),t=t.replace(/\]/g,""),t},S=(e,{fallbackValueOption:t="",type:i,isEmptyArray:s,separated:r,unified:a,fallback:l})=>e(A(r,a,l,w({fallbackValueOption:t,type:i,isEmptyArray:s}))),x=(e,t,s,a)=>e.incremental&&null!==t&&"object"==typeof t&&i.isEmpty(t)?[]:e.hasIncrementalKeys?((e,t,i)=>{const s=Object.values(e),a=[r.value,r.initial,r.default].includes(t);return i&&a?s.filter(e=>null!=e&&""!==e):s})(t,s,a):t,C=(e,t,s=!1)=>{if(t===r.value||t.startsWith("value.")){const a=e.$output?e.$output(e[r.value]):e[r.value],l=t.startsWith("value.")?i.get(a,t.substring(6)):a;return"string"==typeof l&&i.isEmpty(l)&&s?null:l}return e[t]},I=(e,t=!1)=>{let s=e;return g(s)&&(s=i.transform(s,(e,s)=>{const r=s.split(".");return r.length?i.merge(e,((e,t=!1)=>e.reduceRight((i,s)=>{const r={};if(s.endsWith("[]")){const e=t?[i]:[];return r[s.replace(/\[\]$/,"")]=e,r}const a=e[e.indexOf(s)-1],l=!!a&&a.endsWith("[]")&&e[e.length-1]===s;return!t&&l||(r[s]=i),r},{}))(r,t)):Object.assign(e,{[s]:""})},{})),s},_=(e,t=!0)=>i.transform(e,(e,s,a)=>((e,t)=>null!==e&&"object"==typeof e&&!(e instanceof Date)&&!i.has(e,r.fields)&&!i.has(e,r.class)&&(!f(e,[...o.editable,...o.handlers,...o.validation,...o.functions])||t))(s,t)?Object.assign(e,{[a]:{fields:b(s)?[]:_(s)}}):Object.assign(e,{[a]:s}),{}),j=(e,t,s)=>{if(!i.has(t,n.values))return e;let{values:r}=t;return y({fields:t.fields})&&(r=(e=>i.transform(e,(e,t,s)=>{return Object.assign(e,{[s]:{value:t,fields:(r=t,i.isPlainObject(r)?Object.entries(r).map(([e,t])=>({value:t,name:e})):void 0)}});var r},{}))(r)),i.merge(e,i.transform(r,(t,i,r)=>{Array.isArray(e[r])&&(t[r]=i),r in e||isNaN(Number(r))&&!s||(t[r]=i)},{}))},N=(e,t=!0,s=!0)=>{let a=i.merge(I(e.fields,!1),I(e.struct,!1));return a=(e=>{let t=e;return m(t)&&(t=i.transform(t,(e,t)=>{if(!y({fields:{field:t}})||i.has(t,r.name))return Object.assign(e,{[t.name]:t})},{})),t})(a),a=j(a,e,s),a=_(a,t),a},M=(e,t,s=0,a=!1)=>{const l=(Array.isArray(e)?e:Object.values(e)).filter(e=>"string"==typeof e),n=v(t),o=l.filter(e=>e.startsWith(n)),d=I(o,a),h=n.replace(/\[\]/g,`[${s}]`),u=_(i.get(d,h));return l.length&&l.filter(e=>e.startsWith(t+"[]")).map(e=>e.substring((t+"[].").length)).filter(e=>e.endsWith("[]")).map(e=>e.substring(0,e.length-2)).forEach(e=>{const t=e.split(".");let i=u[0]?.fields;for(let e=0;e<t.length&&(i=i?.[t[e]]?.[r.fields],i);e++);i&&delete i[0]}),u};var P,B,W;!function(e){e.uniqueId="uniqueId",e.fallback="fallback",e.fallbackValue="fallbackValue",e.defaultGenericError="defaultGenericError",e.submitThrowsError="submitThrowsError",e.showErrorsOnInit="showErrorsOnInit",e.showErrorsOnSubmit="showErrorsOnSubmit",e.showErrorsOnBlur="showErrorsOnBlur",e.showErrorsOnChange="showErrorsOnChange",e.showErrorsOnClear="showErrorsOnClear",e.showErrorsOnReset="showErrorsOnReset",e.validateOnInit="validateOnInit",e.validateOnSubmit="validateOnSubmit",e.validateOnBlur="validateOnBlur",e.validateOnChange="validateOnChange",e.validateOnChangeAfterInitialBlur="validateOnChangeAfterInitialBlur",e.validateOnChangeAfterSubmit="validateOnChangeAfterSubmit",e.validateDisabledFields="validateDisabledFields",e.validateDeletedFields="validateDeletedFields",e.validatePristineFields="validatePristineFields",e.validateTrimmedValue="validateTrimmedValue",e.validateOnClear="validateOnClear",e.validateOnReset="validateOnReset",e.strictSet="strictSet",e.strictUpdate="strictUpdate",e.strictDelete="strictDelete",e.strictSelect="strictSelect",e.softDelete="softDelete",e.retrieveOnlyDirtyFieldsValues="retrieveOnlyDirtyFieldsValues",e.retrieveOnlyEnabledFieldsValues="retrieveOnlyEnabledFieldsValues",e.retrieveOnlyEnabledFieldsErrors="retrieveOnlyEnabledFieldsErrors",e.retrieveNullifiedEmptyStrings="retrieveNullifiedEmptyStrings",e.removeNullishValuesInArrays="removeNullishValuesInArrays",e.preserveDeletedFieldsValues="preserveDeletedFieldsValues",e.autoTrimValue="autoTrimValue",e.autoParseNumbers="autoParseNumbers",e.validationDebounceWait="validationDebounceWait",e.validationDebounceOptions="validationDebounceOptions",e.stopValidationOnError="stopValidationOnError",e.validationPluginsOrder="validationPluginsOrder",e.resetValidationBeforeValidate="resetValidationBeforeValidate",e.applyInputConverterOnInit="applyInputConverterOnInit",e.applyInputConverterOnSet="applyInputConverterOnSet",e.applyInputConverterOnUpdate="applyInputConverterOnUpdate",e.bubbleUpErrorMessages="bubbleUpErrorMessages"}(P||(P={})),function(e){e.onSuccess="onSuccess",e.onError="onError"}(B||(B={}));class H{noop=()=>{};state;fields=new s;path;$submitted=0;$submitting=!1;$validated=0;$validating=!1;$clearing=!1;$resetting=!1;$touched=!1;$changed=0;$hooks={};$handlers={};constructor(){t.makeObservable(this,{$submitted:t.observable,$submitting:t.observable,$validated:t.observable,$validating:t.observable,$clearing:t.observable,$resetting:t.observable,$touched:t.observable,$changed:t.observable,$hooks:t.observable,$handlers:t.observable,changed:t.computed,submitted:t.computed,submitting:t.computed,validated:t.computed,validating:t.computed,clearing:t.computed,resetting:t.computed,hasIncrementalKeys:t.computed,hasNestedFields:t.computed,size:t.computed,initField:t.action,submit:t.action,deepUpdate:t.action,set:t.action,add:t.action,del:t.action})}execHook=(e,t={})=>A(t[e],this.$hooks[e],this.noop).apply(this,[this]);execHandler=(e,t,i=void 0,s=null,r=!0)=>[A(this.$handlers[e]&&this.$handlers[e].apply(this,[this]),i,this.noop).apply(this,[...t]),r&&this.execHook(s||e)];get resetting(){return this.hasNestedFields?this.check(r.resetting,!0):this.$resetting}get clearing(){return this.hasNestedFields?this.check(r.clearing,!0):this.$clearing}get submitted(){return t.toJS(this.$submitted)}get submitting(){return t.toJS(this.$submitting)}get validated(){return t.toJS(this.$validated)}get validating(){return t.toJS(this.$validating)}get hasIncrementalKeys(){return!!this.fields.size&&O(this.fields)}get hasNestedFields(){return 0!==this.fields.size}get size(){return this.fields.size}get changed(){return null!=this.path&&this.hasNestedFields?this.reduce((e,t)=>e+t.changed,0)+this.$changed:this.$changed}intercept=e=>this.MOBXEvent("function"==typeof e?{type:"interceptor",call:e}:{type:"interceptor",...e});observe=e=>this.MOBXEvent("function"==typeof e?{type:"observer",call:e}:{type:"observer",...e});onClear=(...e)=>this.execHandler(r.onClear,e,e=>{F(e)&&e.preventDefault(),this.clear(!0,!1)});onReset=(...e)=>this.execHandler(r.onReset,e,e=>{F(e)&&e.preventDefault(),this.reset(!0,!1)});onSubmit=(...e)=>this.execHandler(r.onSubmit,e,(e,t={})=>{F(e)&&e.preventDefault(),this.submit(t)},null,!1);onAdd=(...e)=>this.execHandler(r.onAdd,e,(e,t)=>{F(e)&&e.preventDefault(),this.add(F(t)?null:t,!1)});onDel=(...e)=>this.execHandler(r.onDel,e,(e,t)=>{F(e)&&e.preventDefault(),this.del(F(t)?this.path:t,!1)});initFields(e,t=!1){const s=this.state.options.get(P.fallback),r=e=>[this.path,e].join(".").replace(/^\.+/,"");let a;a=N(e,this.state.strict,s),a=((e,t)=>{if(t){const s=i.get(t.plugins,"svk.config.schema");i.isEmpty(e)&&s&&s.properties&&i.each(s.properties,(t,s)=>{i.set(e,s,{value:t.default,label:t.title})})}return e})(a,this.validator),i.forIn(a,(e,i)=>{const a=r(i);if(null==this.select(a,null,!1))if(s)this.initField(i,a,e,t);else{const s=v(a);this.state.struct().filter(e=>e.startsWith(s)).find(e=>"."===e.charAt(s.length)||"[]"===e.substring(s.length,s.length+2)||e===s)&&this.initField(i,a,e,t)}})}initField(e,t,s,a=!1){const l=this.state.get("current","props"),o=v(t),d=e=>{const t=i.get(l[e],o);if(![r.input,r.output,r.converter].includes(e)||"function"==typeof t)return t},h={$value:i.get(l[n.values],t),$computed:d(n.computed),$label:d(n.labels),$placeholder:d(n.placeholders),$default:d(n.defaults),$initial:d(n.initials),$disabled:d(n.disabled),$deleted:d(n.deleted),$type:d(n.types),$related:d(n.related),$rules:d(n.rules),$options:d(n.options),$bindings:d(n.bindings),$extra:d(n.extra),$hooks:d(n.hooks),$handlers:d(n.handlers),$validatedWith:d(n.validatedWith),$validators:d(n.validators),$observers:d(n.observers),$interceptors:d(n.interceptors),$converters:d(n.converters),$input:d(n.input),$output:d(n.output),$autoFocus:d(n.autoFocus),$ref:d(n.refs),$nullable:d(n.nullable),$autoComplete:d(n.autoComplete)},u=this.state.form.makeField({key:e,path:t,struct:o,data:s,props:h,update:a,state:this.state},s&&s[r.class]||d(n.classes));return this.fields.merge({[e]:u}),u}validate(e,t){const s=i.merge(e,{path:this.path});return this.state.form.validator.validate(s,t)}submit(e={},{execOnSubmitHook:i=!0,execValidationHooks:s=!0,validate:a=!0}={}){const l=i?(()=>this.execHook(r.onSubmit,e))():void 0;if(this.$submitting=!0,this.$submitted+=1,!a||!this.state.options.get(P.validateOnSubmit,this))return Promise.resolve(l).then(t.action(()=>this.$submitting=!1)).catch(t.action(e=>{throw this.$submitting=!1,e})).then(()=>this);const n=t=>t?this.execHook(B.onSuccess,e):this.execHook(B.onError,e);return this.validate({showErrors:this.state.options.get(P.showErrorsOnSubmit,this)}).then(({isValid:e})=>{const t=s?n(e):void 0;if(e)return Promise.all([l,t]);const i=this.state.options.get(P.defaultGenericError,this);return this.state.options.get(P.submitThrowsError,this)&&i&&this.invalidate(),Promise.all([l,t])}).then(t.action(()=>this.$submitting=!1)).catch(t.action(e=>{throw this.$submitting=!1,e})).then(()=>this)}check(e,t=!1){return c(a.computed,[e]),t?h({type:o.occurrences[e],data:this.deepCheck(o.occurrences[e],e,this.fields)}):this[e]}deepCheck(e,t,s){const r=d(s);return i.transform(r,(i,s)=>(s.fields.size&&Array.isArray(s.initial)||i.push(s[t]),i.push(h({data:this.deepCheck(e,t,s.fields),type:e})),i),[])}firstError(){if(!this.state.options.get(P.bubbleUpErrorMessages,this))return null;for(const e of d(this.fields)){if(e.error)return e.error;if(e.fields.size){const t=e.firstError();if(t)return t}}return null}update(e){if(!i.isPlainObject(e))throw new Error("The update() method accepts only plain objects.");this.deepUpdate(N({fields:e},this.state.strict),void 0,void 0,e)}deepUpdate(e,t="",s=!0,a){i.each(e,(e,l)=>{const n=i.has(e,r.name)?e.name:l,o=`${t}.${n}`.replace(/^\.+/,""),h=this.state.options.get(P.strictUpdate,this),u=this.select(o,null,h),c=this.select(t,null,!1)||this.state.form.select(this.path??"",null,!1),p=this.state.options.get(P.applyInputConverterOnUpdate,this);if(null!=u&&void 0!==e){if(Array.isArray(u.values())){const t=Math.max(-1,...Object.keys(e.fields||{}).map(Number));d(u.fields).forEach(e=>{Number(e.name)>t&&(u.$changed++,u.state.form.$changed++,u.fields.delete(e.name))})}if(e?.fields){const e=this.state.options.get(P.fallback),t=this.state.struct().findIndex(e=>e.startsWith(u.path.replace(/\.\d+\./,"[].")+"[]"));if(!e&&0===u.fields.size&&t<0)return void(u.value=S(p?u.$input:e=>e,{fallbackValueOption:this.state.options.get(P.fallbackValue,this),separated:i.get(a,o)}))}if(null===e||null==e.fields)return void(u.value=S(p?u.$input:e=>e,{fallbackValueOption:this.state.options.get(P.fallbackValue,this),separated:e}))}if(null!=c&&null==u){const t=[this.path,o].join(".").replace(/^\.+/,"");c.$changed++,c.state.form.$changed++,c.initField(n,t,e,!0)}else if(s)if(i.has(e,r.fields)&&null!=e.fields)this.deepUpdate(e.fields,o);else{const e=M(this.state.struct(),o);this.deepUpdate(e,o,!1)}})}get(e=null,t=!0){if(null==e)return this.deepGet([...o.computed,...o.editable,...o.validation],this.fields,t);if(c(a.all,Array.isArray(e)?e:[e]),"string"==typeof e){if([r.hooks,r.handlers].includes(e))return this[`$${e}`];if(t&&0===this.fields.size){const i=this.state.options.get(P.retrieveNullifiedEmptyStrings,this);return C(this,e,!!t&&i)}const i=this.deepGet(e,this.fields,t),s=this.state.options.get(P.removeNullishValuesInArrays,this);return x(this,i,e,!!t&&s)}return this.deepGet(e,this.fields,t)}deepGet(e,t,s=!0){const a=d(t),l=i.transform(a,(t,a)=>{if(Object.assign(t,{[a.key]:{fields:(t=>0!==t.size?this.deepGet(e,t,s):void 0)(a.fields)}}),"string"==typeof e){const i=this.state.options,l=i.get(P.retrieveOnlyDirtyFieldsValues,this)&&e===r.value&&a.isPristine||i.get(P.retrieveOnlyEnabledFieldsValues,this)&&e===r.value&&a.disabled||i.get(P.retrieveOnlyEnabledFieldsErrors,this)&&e===r.error&&a.disabled&&a.isValid&&(!a.error||!a.hasError)||i.get(P.softDelete,this)&&e===r.value&&a.deleted;if(0===a.fields.size){if(delete t[a.key],l)return t;const i=this.state.options.get(P.retrieveNullifiedEmptyStrings,this);return Object.assign(t,{[a.key]:C(a,e,!!s&&i)})}let n=this.deepGet(e,a.fields,s);if(e===r.value&&(n=a.$output(n)),delete t[a.key],l)return t;const o=this.state.options.get(P.removeNullishValuesInArrays,this);return Object.assign(t,{[a.key]:x(a,n,e,!!s&&o)})}return i.each(e,e=>Object.assign(t[a.key],{[e]:a[e]})),t},{});if("string"==typeof e&&a.length>0&&O(t)){const e=new Set(Object.keys(l));return a.filter(t=>e.has(String(t.key))).map(e=>l[String(e.key)])}return l}set(e,t){if("string"==typeof e&&void 0!==t){c(a.editable,[e]);const s=[r.hooks,r.handlers].includes(e);if((null!==t&&"object"==typeof t&&e===r.value||i.isPlainObject(t)&&!s)&&this.hasNestedFields)return this.deepSet(e,t,"",!0);if(e===r.value){const e=this.state.options.get(P.applyInputConverterOnSet,this);this.value=S(e?this.$input:e=>e,{fallbackValueOption:this.state.options.get(P.fallbackValue,this),separated:t})}else s?Object.assign(this[`$${e}`],t):i.set(this,`$${e}`,t);return}null==t&&(this.hasNestedFields?this.deepSet(r.value,e,"",!0):this.set(r.value,e))}deepSet(e,t,s="",r=!1){const a=this.state.options.get(P.strictSet,this);null!=t?i.each(t,(t,i)=>{const l=`${s}.${i}`.replace(/^\.+/,""),n=this.select(l,null,a);a&&p(l,n,"You are updating a not existent field:"),void 0!==n&&(void 0!==t&&n.set(e,t,r),n.fields.size&&null!==t&&"object"==typeof t&&this.deepSet(e,t,l,r))}):this.each(e=>e.$value=w({fallbackValueOption:this.state.options.get(P.fallbackValue,this),value:e.$value,nullable:e.$nullable,type:e.type}))}add(e,t=!0){if(m(e))return i.each(e,e=>this.update({[E(this.fields)]:e})),this.$changed++,this.state.form.$changed++,t&&this.execHook(r.onAdd),this;let s;i.has(e,r.key)&&(s=e.key),i.has(e,r.name)&&(s=e.name),s||(s=E(this.fields));const a=M(this.state.struct(),this.path??"",0,!0),l=this.initField(s,(e=>[this.path,e].join(".").replace(/^\.+/,""))(s),i.merge(a[0],e));if(!(i.has(e,r.value)||i.has(e,r.fields))&&!this.state.options.get(P.preserveDeletedFieldsValues,this)){const e=this.state.options.get(P.fallbackValue,this);l.$value=w({fallbackValueOption:e,value:l.$value,nullable:l.$nullable,type:l.type}),l.each(t=>t.$value=w({fallbackValueOption:e,value:t.$value,nullable:t.$nullable,type:t.type}))}return this.$changed++,this.state.form.$changed++,t&&this.execHook(r.onAdd),l}del(e=null,t=!0){const i=this.state.options.get(P.strictDelete,this),s=D(e??this.path??""),a=[this.path??"",s??""].join(".").replace(/^\.+|\.+$/g,""),l=this.container(e),n=s.split("."),o=n[n.length-1];if(i&&!l.fields.has(o)){p(a,null,`Key "${o}" not found when trying to delete field`)}return l.$changed++,l.state.form.$changed++,this.state.options.get(P.softDelete,this)?this.select(a).set(r.deleted,!0):(l.each(e=>e.debouncedValidation.cancel()),t&&this.execHook(r.onDel),l.fields.delete(o))}MOBXEvent({prop:e=r.value,key:s=null,path:l=null,call:n,type:o}){let d=s||e;c(a.observable,[d]);const h=this.select(l||this.path,null,!1)||this,u=e=>n.apply(null,[{change:e,form:this.state.form,path:h.path||null,field:h.path?h:null}]);let p,v;"observer"===o&&(p=t.observe,v=e=>t.observe(h.fields.toArray(),e)),"interceptor"===o&&(d=`$${e}`,p=t.intercept,v=h.fields.intercept);const f=h.path?`${d}@${h.path}`:d;i.merge(this.state.disposers[o],{[f]:d===r.fields?v.apply(e=>u(e)):p(h,d,e=>u(e))})}dispose(e=null){return this.path&&e?this.disposeSingle(e):this.disposeAll()}disposeAll(){const e=e=>e.apply();i.each(this.state.disposers.interceptor,e),i.each(this.state.disposers.observer,e),this.state.disposers={interceptor:{},observer:{}}}disposeSingle({type:e,key:t=r.value,path:i=null}){const s=D(i??this.path??"");"interceptor"===e&&(t=`$${t}`);const a=this.state.disposers[e];a[`${t}@${s}`].apply(),delete a[`${t}@${s}`]}select(e,t=null,s=!0){const r=D(String(e)).split("."),a=r[0];r.shift();let l=null==t?this.fields.get(a):t.get(a),n=!1;return i.each(r,e=>{n||(null==l?(l=void 0,n=!0):l=l.fields.get(e))}),s&&this.state.options.get(P.strictSelect,this)&&p(String(e),l),l}container(e){const t=D(e??this.path??"").replace(/[^./]+$/,"").replace(/^\.+|\.+$/g,"");return this.path&&null==e?""!==t?this.state.form.select(t,null,!1):this.state.form:""!==t?this.select(t,null,!1):this}has(e){return this.fields.has(e)}map(e){return d(this.fields).map(e)}each(e,t=null,i=0){const s=t||this.fields;d(s).forEach((t,s)=>{e(t,s,i),0!==t.fields.size&&this.each(e,t.fields,i+1)})}reduce(e,t){return d(this.fields).reduce(e,t)}move(e,t){this.fields.move(e,t)}$(e){return this.select(e)}values(){return this.get(r.value)}errors(){return this.get(r.error)}labels(){return this.get(r.label)}placeholders(){return this.get(r.placeholder)}defaults(){return this.get(r.default)}initials(){return this.get(r.initial)}types(){return this.get(r.type)}}class R{promises=[];form=null;drivers={};plugins={vjf:void 0,dvr:void 0,svk:void 0,yup:void 0,zod:void 0,joi:void 0};error=null;constructor(e){t.makeObservable(this,{error:t.observable,validate:t.action,validateField:t.action}),this.form=e.form,Object.assign(this.plugins,e.plugins),this.initDrivers()}initDrivers(){Object.entries(this.plugins).forEach(([e,t])=>{this.drivers[e]=t&&t.class&&new t.class({config:t.config,state:this.form.state,promises:this.promises})})}validate(e,i){const s=A(e?.path,e),r=A(e?.field,this.form.select(s,null,!1),this.form),a=A(e?.related,i?.related,!0),l=A(e?.showErrors,i?.showErrors,!1);return r.$validating=!0,r.$validated+=1,this.error=null,new Promise(e=>{(r.path||"string"==typeof s)&&this.validateField({field:r,showErrors:l,related:a,path:s}),r.each(e=>this.validateField({path:e.path,field:e,showErrors:l,related:a})),e(Promise.all(this.promises))}).then(t.action(()=>{r.$validating=!1,r.$clearing=!1,r.$resetting=!1})).catch(t.action(e=>{throw r.$validating=!1,r.$clearing=!1,r.$resetting=!1,e})).then(()=>r)}validateField({showErrors:e=!1,related:t=!1,field:s=null,path:r}){const a=s||this.form.select(r),{options:l}=this.form.state;if(!a.path)throw new Error("Validation Error: Invalid Field Instance");if(a.deleted&&!l.get(P.validateDeletedFields,a))return;if(a.disabled&&!l.get(P.validateDisabledFields,a))return;if(a.isPristine&&!l.get(P.validatePristineFields,a))return;l.get(P.resetValidationBeforeValidate,a)&&a.resetValidation(),l.get(P.validateTrimmedValue,a)&&a.trim();const n=l.get(P.stopValidationOnError,a),o=l.get(P.validationPluginsOrder,a),d=o?o.map(e=>this.drivers[e]):this.drivers;i.each(d,e=>{e&&e.validate(a),!n||a.hasError}),a.showErrors(e,!1),t&&this.validateRelatedFields(a,e)}validateRelatedFields(e,t){e.related&&e.related.length&&e.related.map(e=>this.validateField({related:!1,showErrors:t,path:e}))}}class U{options={uniqueId:V,fallback:!0,fallbackValue:"",defaultGenericError:null,submitThrowsError:!0,showErrorsOnInit:!1,showErrorsOnSubmit:!0,showErrorsOnBlur:!0,showErrorsOnChange:!0,showErrorsOnClear:!1,showErrorsOnReset:!0,validateOnInit:!0,validateOnSubmit:!0,validateOnBlur:!0,validateOnChange:!1,validateOnChangeAfterInitialBlur:!1,validateOnChangeAfterSubmit:!1,validateOnClear:!1,validateOnReset:!1,validateDisabledFields:!1,validateDeletedFields:!1,validatePristineFields:!0,strictSet:!1,strictUpdate:!1,strictDelete:!0,strictSelect:!0,softDelete:!1,retrieveOnlyDirtyFieldsValues:!1,retrieveOnlyEnabledFieldsValues:!1,retrieveOnlyEnabledFieldsErrors:!1,retrieveNullifiedEmptyStrings:!1,autoTrimValue:!1,autoParseNumbers:!1,removeNullishValuesInArrays:!1,preserveDeletedFieldsValues:!1,validationDebounceWait:250,validationDebounceOptions:{leading:!1,trailing:!0},stopValidationOnError:!1,validationPluginsOrder:void 0,resetValidationBeforeValidate:!0,validateTrimmedValue:!1,applyInputConverterOnInit:!0,applyInputConverterOnSet:!0,applyInputConverterOnUpdate:!0,bubbleUpErrorMessages:!1};constructor(){t.makeObservable(this,{options:t.observable,set:t.action})}get(e,s=null){return i.has(s,"path")&&i.has(s.$options,e)?s.$options[e]:e?i.get(this.options,e):t.toJS(this.options)}set(e){t.set?t.set(this.options,e):t.extendObservable(this.options,e)}}class T{templates={};rewriters={default:{id:r.id,name:r.name,type:r.type,value:r.value,checked:r.checked,label:r.label,placeholder:r.placeholder,disabled:r.disabled,autoComplete:r.autoComplete,onChange:r.onChange,onBlur:r.onBlur,onFocus:r.onFocus,autoFocus:r.autoFocus,inputMode:r.inputMode,onKeyUp:r.onKeyUp,onKeyDown:r.onKeyDown}};register(e){return i.each(e,(e,t)=>{"function"==typeof e&&i.merge(this.templates,{[t]:e}),i.isPlainObject(e)&&i.merge(this.rewriters,{[t]:e})}),this}load(e,t=r.default,s){const a={keys:i.get(this.rewriters,r.default),form:e.state.form,field:e,props:s,$try:A};if(i.has(this.templates,r.default))return i.get(this.templates,t)(a);if(i.has(this.rewriters,t)){const r={};return i.each(i.get(this.rewriters,t),(t,a)=>i.merge(r,{[t]:A(s[a],e[a])})),r}return i.get(this.templates,t)(a)}}!function(e){e.mixed="mixed",e.unified="unified",e.separated="separated"}(W||(W={}));class K{mode=W.mixed;strict=!1;form;options;bindings;$extra;$struct=[];disposers={interceptor:{},observer:{}};initial={props:{},fields:{}};current={props:{},fields:{}};constructor({form:e,initial:t,options:i,bindings:s}){this.set("form",e),this.initProps(t),this.options=new U,this.options.set(i),this.bindings=new T,this.bindings.register(s),this.observeOptions()}initProps(e={}){const t=[...o.editable,...o.separated,...o.validation,...o.functions,...o.handlers],i=Object.fromEntries(Object.entries(e).filter(([e])=>t.includes(e)));this.set("initial","props",i);const s=y(e),r=(e=>f(e,o.separated)||f(e,o.validation))(e);if((r||g(e.fields))&&!s){const t=A(e.struct,e.fields);return this.struct(t),this.strict=!0,void(this.mode=W.separated)}this.struct(e.struct),this.mode=W.unified}struct(e=null){return e&&(this.$struct=e),this.$struct}get(e,t){return this[e][t]}set(e,t,i=null){"form"===e&&(this.form=t),"initial"===e&&(Object.assign(this.initial[t],i),Object.assign(this.current[t],i)),"current"===e&&Object.assign(this.current[t],i)}extra(e=null){return"string"==typeof e?i.get(this.$extra,e):null===e?this.$extra:(this.$extra=e,null)}observeOptions(){var e;t.observe(this.options.options,(e=[{type:"update",key:P.validateOnChange,to:!0,exec:()=>this.form.each(e=>e.observeValidationOnChange())},{type:"update",key:P.validateOnChange,to:!1,exec:()=>this.form.each(e=>e.disposeValidationOnChange())},{type:"update",key:P.validateOnBlur,to:!0,exec:()=>this.form.each(e=>e.observeValidationOnBlur())},{type:"update",key:P.validateOnBlur,to:!1,exec:()=>this.form.each(e=>e.disposeValidationOnBlur())}],t=>e.map((e=>({key:t,to:i,type:s,exec:r})=>e.type===s&&e.name===t&&e.newValue===i&&r.apply(e,[e]))(t))))}}const z=(e,t)=>"function"!=typeof t?t:t({field:e,form:e.state.form}),G=e=>"function"==typeof e?e:void 0,J=(e,t)=>"function"==typeof e[`_${t}`]?e[`_${t}`].apply(e,[{form:e.state.form,field:e}]):e[`$${t}`],q=(e,t,i)=>Object.assign(e,{_label:G(t.$label||i?.label),_placeholder:G(t.$placeholder||i?.placeholder),_disabled:G(t.$disabled||i?.disabled),_rules:G(t.$rules||i?.rules),_related:G(t.$related||i?.related),_deleted:G(t.$deleted||i?.deleted),_validators:G(t.$validators||i?.validators),_validatedWith:G(t.$validatedWith||i?.validatedWith),_bindings:G(t.$bindings||i?.bindings),_extra:G(t.$extra||i?.extra),_options:G(t.$options||i?.options),_autoFocus:G(t.$autoFocus||i?.autoFocus),_inputMode:G(t.$inputMode||i?.inputMode),$label:z(e,t.$label||i?.label||""),$placeholder:z(e,t.$placeholder||i?.placeholder||""),$disabled:z(e,t.$disabled||i?.disabled||!1),$rules:z(e,t.$rules||i?.rules||null),$related:z(e,t.$related||i?.related||[]),$deleted:z(e,t.$deleted||i?.deleted||!1),$validatedWith:z(e,t.$validatedWith||i?.validatedWith||r.value),$bindings:z(e,t.$bindings||i?.bindings||r.default),$extra:z(e,t.$extra||i?.extra||null),$options:z(e,t.$options||i?.options||{}),$autoFocus:z(e,t.$autoFocus||i?.autoFocus||!1),$inputMode:z(e,t.$inputMode||i?.inputMode||void 0),$validators:z(e,t.$validators||i?.validators||null),$hooks:t.$hooks||i?.hooks||{},$handlers:t.$handlers||i?.handlers||{},$observers:t.$observers||i?.observers||null,$interceptors:t.$interceptors||i?.interceptors||null,$ref:t.$ref||i?.ref||void 0,$nullable:t.$nullable||i?.nullable||!1,$autoComplete:t.$autoComplete||i?.autoComplete||void 0}),X=(e,t,i,s,{isEmptyArray:r,fallbackValueOption:a})=>S(e=>e,{isEmptyArray:r,type:e.type,unified:s?w({fallbackValueOption:a,type:e.type,value:e.value}):t?.default,separated:i.$default,fallback:e.$initial});class Y extends H{hasInitialNestedFields=!1;incremental=!1;id;key;name;$observers;$interceptors;$converter=e=>e;$input=e=>e;$output=e=>e;_value;_label;_placeholder;_disabled;_rules;_related;_deleted;_validatedWith;_validators;_bindings;_extra;_options;_autoFocus;_inputMode;$options=void 0;$value=void 0;$type=void 0;$label=void 0;$placeholder=void 0;$default=void 0;$initial=void 0;$bindings=void 0;$extra=void 0;$related=void 0;$validatedWith=void 0;$validators=void 0;$rules=void 0;$disabled=!1;$focused=!1;$blurred=!1;$deleted=!1;$autoFocus=!1;$inputMode=void 0;$ref=void 0;$nullable=!1;$autoComplete=void 0;showError=!1;errorSync=null;errorAsync=null;validationErrorStack=[];validationFunctionsData=[];validationAsyncData={valid:!0,message:null};debouncedValidation;disposeValidationOnBlur;disposeValidationOnChange;files=void 0;constructor({key:e,path:s,struct:a,data:l={},props:n={},update:o=!1,state:d}){super(),t.makeObservable(this,{$options:t.observable,$value:t.observable,$type:t.observable,$label:t.observable,$placeholder:t.observable,$default:t.observable,$initial:t.observable,$bindings:t.observable,$extra:t.observable,$related:t.observable,$validatedWith:t.observable,$validators:t.observable,$rules:t.observable,$disabled:t.observable,$focused:t.observable,$blurred:t.observable,$deleted:t.observable,showError:t.observable,errorSync:t.observable,errorAsync:t.observable,validationErrorStack:t.observable,validationFunctionsData:t.observable,validationAsyncData:t.observable,files:t.observable,autoFocus:t.computed,inputMode:t.computed,ref:t.computed,checkValidationErrors:t.computed,checked:t.computed,value:t.computed,initial:t.computed,default:t.computed,actionRunning:t.computed,type:t.computed,label:t.computed,placeholder:t.computed,extra:t.computed,options:t.computed,bindings:t.computed,related:t.computed,disabled:t.computed,rules:t.computed,validators:t.computed,validatedValue:t.computed,error:t.computed,hasError:t.computed,isValid:t.computed,isDefault:t.computed,isDirty:t.computed,isPristine:t.computed,isEmpty:t.computed,blurred:t.computed,touched:t.computed,deleted:t.computed,setupField:t.action,initNestedFields:t.action,invalidate:t.action,setValidationAsyncData:t.action,resetValidation:t.action,clear:t.action,reset:t.action,focus:t.action,blur:t.action,showErrors:t.action,update:t.action}),this.state=d,this.setupField(e,s,a,l,n,o),this.initNestedFields(l,o),this.incremental=this.hasIncrementalKeys,this.debouncedValidation=i.debounce(this.validate,this.state.options.get(P.validationDebounceWait,this),this.state.options.get(P.validationDebounceOptions,this)),this.observeValidationOnBlur(),this.observeValidationOnChange(),this.initMOBXEvent(r.observers),this.initMOBXEvent(r.interceptors),t.runInAction(()=>Object.assign(this.$hooks,this.hooks?.apply(this,[this]))),t.runInAction(()=>Object.assign(this.$handlers,this.handlers?.apply(this,[this]))),this.execHook(r.onInit),t.autorun(()=>this.changed&&this.execHook(r.onChange))}get checkValidationErrors(){return!this.validationAsyncData.valid||!i.isEmpty(this.validationErrorStack)||"string"==typeof this.errorAsync||"string"==typeof this.errorSync}set value(e){let t=e;"string"==typeof t&&this.state.options.get(P.autoTrimValue,this)&&(t=t.trim()),this.$value!==t&&(this.handleSetNumberValue(t)||(this.$value=this.$converter(t),this.$changed++,this.actionRunning||this.state.form.$changed++))}handleSetNumberValue(e){return!!this.state.options.get(P.autoParseNumbers,this)&&("number"!=typeof this.$initial&&"number"!=this.type||!new RegExp("^-?\\d+(,\\d+)*(\\.\\d+([eE]\\d+)?)?$","g").exec(e)?void 0:(this.$value=this.$converter(Number(e)),this.$changed++,this.actionRunning||this.state.form.$changed++,!0))}get actionRunning(){return this.submitting||this.clearing||this.resetting}get checked(){return"checkbox"===this.type?this.value:void 0}get value(){return"function"!=typeof this._value||this.hasNestedFields?this.getComputedProp(r.value):J(this,r.value)}get initial(){return this.$initial?t.toJS(this.$initial):this.getComputedProp(r.initial)}get default(){return this.$default?t.toJS(this.$default):this.getComputedProp(r.default)}set initial(e){this.$initial=e}set default(e){this.$default=e}get nullable(){return J(this,r.nullable)}get autoComplete(){return J(this,r.autoComplete)}get ref(){return J(this,r.ref)}get extra(){return J(this,r.extra)}get autoFocus(){return J(this,r.autoFocus)}get inputMode(){return J(this,r.inputMode)}get type(){return J(this,r.type)}get label(){return J(this,r.label)}get placeholder(){return J(this,r.placeholder)}get options(){return J(this,r.options)}get bindings(){return J(this,r.bindings)}get related(){return J(this,r.related)}get disabled(){return J(this,r.disabled)}get rules(){return J(this,r.rules)}get validators(){return J(this,r.validators)}get validatedWith(){return J(this,r.validatedWith)}get validatedValue(){return C(this,this.validatedWith)}get error(){return!1===this.showError?null:this.errorAsync||this.errorSync||this.firstError()||null}get hasError(){return this.checkValidationErrors||this.check(r.hasError,!0)}get isValid(){return!this.checkValidationErrors&&this.check(r.isValid,!0)}get isDefault(){return!i.isNil(this.default)&&i.isEqual(this.default,this.value)}get isDirty(){const e=this.changed?this.value:this.initial;return!i.isEqual(this.initial,e)}get isPristine(){const e=this.changed?this.value:this.initial;return i.isEqual(this.initial,e)}get isEmpty(){return this.hasNestedFields?this.check(r.isEmpty,!0):"boolean"==typeof this.value?!!this.$value:"number"!=typeof this.value&&(!(this.value instanceof Date)&&(null!==this.value&&i.isEmpty(this.value)))}get focused(){return this.hasNestedFields?this.check(r.focused,!0):this.$focused}get blurred(){return this.hasNestedFields?this.check(r.blurred,!0):this.$blurred}get touched(){return this.hasNestedFields?this.check(r.touched,!0):this.$touched}get deleted(){return this.hasNestedFields?this.check(r.deleted,!0):this.$deleted}sync=t.action((e,t=null)=>{const s=e=>((e,t)=>"boolean"==typeof t&&"boolean"==typeof e.target.checked)(e,this.value)?e.target.checked:e.target.value;if(i.isNil(e)||i.isNil(e.target))return i.isNil(t)||i.isNil(t.target)||(t=s(t)),void(this.value=A(e,t));i.isNil(e.target)?this.value=e:this.value=s(e)});onSync=(...e)=>"file"===this.type?this.onDrop(...e):this.execHandler(r.onChange,e,this.sync,r.onSync);onChange=this.onSync;onToggle=(...e)=>this.execHandler(r.onToggle,e,this.sync);onBlur=(...e)=>this.execHandler(r.onBlur,e,t.action(()=>{this.$focused=!1,this.$blurred=!0}));onFocus=(...e)=>this.execHandler(r.onFocus,e,t.action(()=>{this.$focused=!0,this.$touched=!0}));onDrop=(...e)=>this.execHandler(r.onDrop,e,t.action(()=>{const t=e[0];let i=null;var s;F(t)&&((s=t).target.files&&0!==s.target.files.length)&&(i=Array.from(t.target.files)),this.files=[...this.files||[],...i||e]}));onKeyDown=(...e)=>this.execHandler(r.onKeyDown,e);onKeyUp=(...e)=>this.execHandler(r.onKeyUp,e);setupField(e,t,s,r,a,l){this.key=e,this.path=t,this.id=this.state.options.get(P.uniqueId)?.apply(this,[this]);const n=this.state.options.get(P.fallbackValue,this),o=this.state.options.get(P.applyInputConverterOnInit,this),d=((e,t)=>!!g(e)&&(!!e.filter(e=>e.startsWith(t)).find(e=>"[]"===e.substring(t.length))||(e?.find(e=>e===t)?.endsWith("[]")??!1)))(this.state.struct(),v(this.path??"")),{$type:h,$input:u,$output:c,$converter:p,$converters:f,$computed:b}=a;if(i.isPlainObject(r)){const{type:t,input:i,output:s,converter:v,converters:g,computed:m}=r;this.name=r.name??String(e),this.$type=h||t||"text",this.$converter=A(p,f,v,g,this.$converter),this.$input=A(u,i,this.$input),this.$output=A(c,s,this.$output);const y=S(o?this.$input:e=>e,{fallbackValueOption:n,isEmptyArray:d,type:this.type,unified:m||r.value,separated:b||a.$value,fallback:a.$initial});return this._value=G(y),this.$value="function"==typeof this._value?z(this,y):y,this.$initial=S(e=>e,{fallbackValueOption:n,isEmptyArray:d,type:this.type,unified:r.initial,separated:a.$initial,fallback:this.$value}),this.$default=X(this,r,a,l,{fallbackValueOption:n,isEmptyArray:d}),void q(this,a,r)}this.name=String(e),this.$type=h||"text",this.$converter=A(p,f,this.$converter),this.$input=A(u,this.$input),this.$output=A(c,this.$output);const m=S(o?this.$input:e=>e,{fallbackValueOption:n,isEmptyArray:d,type:this.type,unified:b||r,separated:b||a.$value});this._value=G(m),this.$value="function"==typeof this._value?z(this,m):m,this.$initial=S(e=>e,{fallbackValueOption:n,isEmptyArray:d,type:this.type,unified:r,separated:a.$initial,fallback:this.$value}),this.$default=X(this,r,a,l,{fallbackValueOption:n,isEmptyArray:d}),q(this,a,r)}getComputedProp(e){if(this.incremental||this.hasNestedFields)return e===r.value?this.get(e,!1):t.untracked(()=>this.get(e,!1));const i=this[`$${e}`];return Array.isArray(i)||t.isObservableArray(i)?[].slice.call(i):t.toJS(i)}initNestedFields(e,t){const s=i.isNil(e)?null:e.fields;Array.isArray(s)&&!i.isEmpty(s)&&(this.hasInitialNestedFields=!0),this.initFields({fields:s},t),!t&&Array.isArray(s)&&i.isEmpty(s)&&Array.isArray(this.value)&&!i.isEmpty(this.value)&&(this.hasInitialNestedFields=!0,this.initFields({fields:s,values:this.value},t))}invalidate(e,t=!0,i=!1){return!0===i?(this.errorAsync=e,void this.showErrors(!0,t)):Array.isArray(e)?(this.validationErrorStack=e,void this.showErrors(!0,t)):(this.validationErrorStack.unshift(e),void this.showErrors(!0,t))}setValidationAsyncData(e=!1,t=null){this.validationAsyncData={valid:e,message:t}}resetValidation(e=!1){this.showError=!1,this.errorSync=null,this.errorAsync=null,this.validationAsyncData={valid:!0,message:null},this.validationFunctionsData=[],this.validationErrorStack=[],Promise.resolve().then(t.action(()=>{this.$resetting=!1,this.$clearing=!1})),e&&this.each(t=>t.resetValidation(e))}clear(e=!0,t=!0){t&&this.execHook(r.onClear),this.$clearing=!0,this.$touched=!1,this.$blurred=!1,this.$changed=0,this.files=void 0,this.$value=w({fallbackValueOption:this.state.options.get(P.fallbackValue),value:this.$value,nullable:this.$nullable,type:this.type}),e&&this.each(t=>t.clear(e)),this.state.options.get(P.validateOnClear,this)?this.validate({showErrors:this.state.options.get(P.showErrorsOnClear,this)}):this.resetValidation(e)}reset(e=!0,t=!0){t&&this.execHook(r.onReset),this.$resetting=!0,this.$touched=!1,this.$blurred=!1,this.$changed=0,this.files=void 0;const i=this.$default!==this.$initial;i&&(this.value=this.$default),i||(this.value=this.$initial),e&&this.each(t=>t.reset(e)),this.state.options.get(P.validateOnReset,this)?this.validate({showErrors:this.state.options.get(P.showErrorsOnReset,this)}):this.resetValidation(e)}focus(){this.ref&&!this.focused&&this.ref.focus(),this.$focused=!0,this.$touched=!0}blur(){this.ref&&this.focused&&this.ref.blur(),this.$focused=!1,this.$blurred=!0}trim(){"string"==typeof this.value&&(this.$value=this.value.trim())}showErrors(e=!0,t=!0){this.showError=e,this.errorSync=this.validationErrorStack.length?this.validationErrorStack[0]:null,this.errorAsync=this.validationAsyncData.valid?null:this.validationAsyncData.message,t&&this.each(i=>i.showErrors(e,t))}observeValidationOnBlur(){const e=this.state.options;e.get(P.validateOnBlur,this)&&(this.disposeValidationOnBlur=t.observe(this,"$focused",t=>!1===t.newValue&&this.debouncedValidation({showErrors:e.get(P.showErrorsOnBlur,this)})))}observeValidationOnChange(){const e=this.state.options;e.get(P.validateOnChange,this)?this.disposeValidationOnChange=t.observe(this,"$value",()=>!this.actionRunning&&this.debouncedValidation({showErrors:e.get(P.showErrorsOnChange,this)})):(e.get(P.validateOnChangeAfterInitialBlur,this)||e.get(P.validateOnChangeAfterSubmit,this))&&(this.disposeValidationOnChange=t.observe(this,"$value",()=>!this.actionRunning&&(e.get(P.validateOnChangeAfterInitialBlur,this)&&this.blurred||e.get(P.validateOnChangeAfterSubmit,this)&&this.state.form.submitted)&&this.debouncedValidation({showErrors:e.get(P.showErrorsOnChange,this)})))}initMOBXEvent(e){const t=this[`$${e}`];if(!Array.isArray(t))return;let s;e===r.observers&&(s=this.observe),e===r.interceptors&&(s=this.intercept),t.map(e=>s(i.omit(e,r.path)))}bind(e={}){return{...this.state.bindings.load(this,this.bindings,e),ref:e=>this.$ref=e}}update(e){if(!i.isPlainObject(e))throw new Error("The update() method accepts only plain objects.");const t=this.state.options.get(P.fallback,this),s=this.state.options.get(P.applyInputConverterOnUpdate,this),r=this.state.struct().findIndex(e=>e.startsWith((this.path??"").replace(/\.\d+\./,"[].")+"[]"));!t&&0===this.fields.size&&r<0?this.value=S(s?this.$input:e=>e,{fallbackValueOption:this.state.options.get(P.fallbackValue,this),separated:e}):super.update(e)}}class L extends H{name;path=null;extra={};validator;debouncedValidation=null;constructor(e={},{name:s="",options:a={},plugins:l={},bindings:n={},hooks:o={},handlers:d={},extra:h={}}={}){super(),t.makeObservable(this,{extra:t.observable,fields:t.observable,flatMapValues:t.computed,validatedValues:t.computed,error:t.computed,hasError:t.computed,isValid:t.computed,isPristine:t.computed,isDirty:t.computed,isDefault:t.computed,isEmpty:t.computed,focused:t.computed,touched:t.computed,disabled:t.computed,invalidate:t.action,clear:t.action,reset:t.action,resetValidation:t.action}),this.name=s,this.extra=h,t.runInAction(()=>this.$hooks=o),t.runInAction(()=>this.$handlers=d);const u=i.each({setup:e,options:a,plugins:l,bindings:n},(e,t)=>"function"==typeof this[t]?i.merge(e,this[t].apply(this,[this])):e);t.runInAction(()=>Object.assign(this.$hooks,this.hooks?.apply(this,[this]))),t.runInAction(()=>Object.assign(this.$handlers,this.handlers?.apply(this,[this]))),this.state=new K({form:this,initial:u.setup,options:u.options,bindings:u.bindings}),this.validator=new R({form:this,plugins:u.plugins}),this.initFields(u.setup),this.debouncedValidation=i.debounce(this.validate,this.state.options.get(P.validationDebounceWait),this.state.options.get(P.validationDebounceOptions)),this.state.options.get(P.validateOnInit)&&this.validator.validate({showErrors:this.state.options.get(P.showErrorsOnInit)}),this.execHook(r.onInit),t.autorun(()=>this.$changed&&this.execHook(r.onChange))}get validatedValues(){return console.warn("validatedValues is deprecated, use flatMapValues instead."),this.flatMapValues}get flatMapValues(){const e={};return this.each(t=>e[t.path]=t.validatedValue),e}get error(){return this.validator.error||this.firstError()||null}get hasError(){return!!this.validator.error||this.check(r.hasError,!0)}get isValid(){return!this.validator.error&&this.check(r.isValid,!0)}get isPristine(){return this.check(r.isPristine,!0)}get isDirty(){return this.check(r.isDirty,!0)}get isDefault(){return this.check(r.isDefault,!0)}get isEmpty(){return this.check(r.isEmpty,!0)}get focused(){return this.check(r.focused,!0)}get touched(){return this.check(r.touched,!0)}get disabled(){return this.check(r.disabled,!0)}makeField(e,t=Y){return new t(e)}$(e){return super.$(e)}select(e,t=null,i=!0){return super.select(e,t,i)}values(){return super.values()}invalidate(e=null,t=!0){this.debouncedValidation.cancel(),this.validator.error=e||this.state.options.get(P.defaultGenericError)||!0,t&&this.each(e=>e.debouncedValidation.cancel())}showErrors(e=!0){this.each(t=>t.showErrors(e))}resetValidation(e=!0){this.validator.error=null,e&&this.each(t=>t.resetValidation(e))}clear(e=!0,t=!0){t&&this.execHook(r.onClear),this.$touched=!1,this.$changed=0,e&&this.each(t=>t.clear(e))}reset(e=!0,t=!0){t&&this.execHook(r.onReset),this.$touched=!1,this.$changed=0,e&&this.each(t=>t.reset(e))}}e.ArrayMap=s,e.Field=Y,e.Form=L,e.default=L,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=MobxReactForm.umd.min.js.map