@tanstack/form-core
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -315,4 +315,2 @@ /** | ||
} | ||
console.log(this); | ||
} | ||
@@ -319,0 +317,0 @@ }); |
@@ -308,3 +308,2 @@ /** | ||
this.state = next; | ||
console.log(this.state); | ||
} | ||
@@ -311,0 +310,0 @@ }); |
@@ -375,3 +375,2 @@ /** | ||
this.state = next; | ||
console.log(this.state); | ||
} | ||
@@ -696,4 +695,2 @@ }); | ||
} | ||
console.log(this); | ||
} | ||
@@ -700,0 +697,0 @@ }); |
@@ -11,3 +11,3 @@ { | ||
"name": "node_modules/.pnpm/@tanstack+store@0.0.1-beta.84/node_modules/@tanstack/store/build/esm/index.js", | ||
"uid": "68f1-13" | ||
"uid": "766a-13" | ||
}, | ||
@@ -18,15 +18,15 @@ { | ||
{ | ||
"uid": "68f1-15", | ||
"uid": "766a-15", | ||
"name": "utils.ts" | ||
}, | ||
{ | ||
"uid": "68f1-17", | ||
"uid": "766a-17", | ||
"name": "FormApi.ts" | ||
}, | ||
{ | ||
"uid": "68f1-21", | ||
"uid": "766a-21", | ||
"name": "FieldApi.ts" | ||
}, | ||
{ | ||
"uid": "68f1-23", | ||
"uid": "766a-23", | ||
"name": "index.ts" | ||
@@ -37,3 +37,3 @@ } | ||
{ | ||
"uid": "68f1-19", | ||
"uid": "766a-19", | ||
"name": "\u0000rollupPluginBabelHelpers.js" | ||
@@ -47,44 +47,44 @@ } | ||
"nodeParts": { | ||
"68f1-13": { | ||
"766a-13": { | ||
"renderedLength": 1288, | ||
"gzipLength": 497, | ||
"brotliLength": 0, | ||
"mainUid": "68f1-12" | ||
"mainUid": "766a-12" | ||
}, | ||
"68f1-15": { | ||
"766a-15": { | ||
"renderedLength": 2008, | ||
"gzipLength": 694, | ||
"brotliLength": 0, | ||
"mainUid": "68f1-14" | ||
"mainUid": "766a-14" | ||
}, | ||
"68f1-17": { | ||
"renderedLength": 10332, | ||
"gzipLength": 2324, | ||
"766a-17": { | ||
"renderedLength": 10297, | ||
"gzipLength": 2316, | ||
"brotliLength": 0, | ||
"mainUid": "68f1-16" | ||
"mainUid": "766a-16" | ||
}, | ||
"68f1-19": { | ||
"766a-19": { | ||
"renderedLength": 353, | ||
"gzipLength": 227, | ||
"brotliLength": 0, | ||
"mainUid": "68f1-18" | ||
"mainUid": "766a-18" | ||
}, | ||
"68f1-21": { | ||
"renderedLength": 10652, | ||
"gzipLength": 2529, | ||
"766a-21": { | ||
"renderedLength": 10622, | ||
"gzipLength": 2522, | ||
"brotliLength": 0, | ||
"mainUid": "68f1-20" | ||
"mainUid": "766a-20" | ||
}, | ||
"68f1-23": { | ||
"766a-23": { | ||
"renderedLength": 0, | ||
"gzipLength": 0, | ||
"brotliLength": 0, | ||
"mainUid": "68f1-22" | ||
"mainUid": "766a-22" | ||
} | ||
}, | ||
"nodeMetas": { | ||
"68f1-12": { | ||
"766a-12": { | ||
"id": "/node_modules/.pnpm/@tanstack+store@0.0.1-beta.84/node_modules/@tanstack/store/build/esm/index.js", | ||
"moduleParts": { | ||
"index.production.js": "68f1-13" | ||
"index.production.js": "766a-13" | ||
}, | ||
@@ -94,13 +94,13 @@ "imported": [], | ||
{ | ||
"uid": "68f1-16" | ||
"uid": "766a-16" | ||
}, | ||
{ | ||
"uid": "68f1-20" | ||
"uid": "766a-20" | ||
} | ||
] | ||
}, | ||
"68f1-14": { | ||
"766a-14": { | ||
"id": "/packages/form-core/src/utils.ts", | ||
"moduleParts": { | ||
"index.production.js": "68f1-15" | ||
"index.production.js": "766a-15" | ||
}, | ||
@@ -110,20 +110,20 @@ "imported": [], | ||
{ | ||
"uid": "68f1-22" | ||
"uid": "766a-22" | ||
}, | ||
{ | ||
"uid": "68f1-16" | ||
"uid": "766a-16" | ||
} | ||
] | ||
}, | ||
"68f1-16": { | ||
"766a-16": { | ||
"id": "/packages/form-core/src/FormApi.ts", | ||
"moduleParts": { | ||
"index.production.js": "68f1-17" | ||
"index.production.js": "766a-17" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "68f1-12" | ||
"uid": "766a-12" | ||
}, | ||
{ | ||
"uid": "68f1-14" | ||
"uid": "766a-14" | ||
} | ||
@@ -133,10 +133,10 @@ ], | ||
{ | ||
"uid": "68f1-22" | ||
"uid": "766a-22" | ||
} | ||
] | ||
}, | ||
"68f1-18": { | ||
"766a-18": { | ||
"id": "\u0000rollupPluginBabelHelpers.js", | ||
"moduleParts": { | ||
"index.production.js": "68f1-19" | ||
"index.production.js": "766a-19" | ||
}, | ||
@@ -146,17 +146,17 @@ "imported": [], | ||
{ | ||
"uid": "68f1-20" | ||
"uid": "766a-20" | ||
} | ||
] | ||
}, | ||
"68f1-20": { | ||
"766a-20": { | ||
"id": "/packages/form-core/src/FieldApi.ts", | ||
"moduleParts": { | ||
"index.production.js": "68f1-21" | ||
"index.production.js": "766a-21" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "68f1-18" | ||
"uid": "766a-18" | ||
}, | ||
{ | ||
"uid": "68f1-12" | ||
"uid": "766a-12" | ||
} | ||
@@ -166,20 +166,20 @@ ], | ||
{ | ||
"uid": "68f1-22" | ||
"uid": "766a-22" | ||
} | ||
] | ||
}, | ||
"68f1-22": { | ||
"766a-22": { | ||
"id": "/packages/form-core/src/index.ts", | ||
"moduleParts": { | ||
"index.production.js": "68f1-23" | ||
"index.production.js": "766a-23" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "68f1-16" | ||
"uid": "766a-16" | ||
}, | ||
{ | ||
"uid": "68f1-20" | ||
"uid": "766a-20" | ||
}, | ||
{ | ||
"uid": "68f1-14" | ||
"uid": "766a-14" | ||
} | ||
@@ -186,0 +186,0 @@ ], |
@@ -428,3 +428,2 @@ /** | ||
this.state = next; | ||
console.log(this.state); | ||
} | ||
@@ -749,4 +748,2 @@ }); | ||
} | ||
console.log(this); | ||
} | ||
@@ -753,0 +750,0 @@ }); |
@@ -21,3 +21,3 @@ /** | ||
* @license MIT | ||
*/class e{listeners=new Set;batching=!1;queue=[];constructor(t,e){this.state=t,this.options=e}subscribe=t=>{this.listeners.add(t);const e=this.options?.onSubscribe?.(t,this);return()=>{this.listeners.delete(t),e?.()}};setState=t=>{const e=this.state;this.state=this.options?.updateFn?this.options.updateFn(e)(t):t(e),this.state!==e&&(this.options?.onUpdate?.(this.state,e),this.queue.push((()=>{this.listeners.forEach((t=>t(this.state,e)))})),this.#t())};#t=()=>{this.batching||(this.queue.forEach((t=>t())),this.queue=[])};batch=t=>{this.batching=!0,t(),this.batching=!1,this.#t()}}function i(t,e){return"function"==typeof t?t(e):t}function s(t,e){return c(e).reduce(((t,e)=>{if(void 0!==t)return t[e]}),t)}function a(t,e,s){const a=c(e);return function t(e){if(!a.length)return i(s,e);const n=a.shift();if("string"==typeof n)return"object"==typeof e?{...e,[n]:t(e[n])}:{[n]:t()};if("number"==typeof n){if(Array.isArray(e)){const i=e.slice(0,n);return[...i.length?i:new Array(n),t(e[n]),...e.slice(n+1)]}return[...new Array(n),t()]}throw new Error("Uh oh!")}(t)}const n=/^(\d*)$/gm,o=/\.(\d*)\./gm,l=/^(\d*)\./gm,h=/\.(\d*$)/gm,r=/\.{2,}/gm,u="__int__",d="__int__$1";function c(t){if("string"!=typeof t)throw new Error;return t.replace("[",".").replace("]","").replace(n,d).replace(o,".__int__$1.").replace(l,"__int__$1.").replace(h,".__int__$1").replace(r,".").split(".").map((t=>0===t.indexOf(u)?parseInt(t.substring(u.length),10):t))}function f(t){return{values:{},fieldMeta:{},canSubmit:!0,isFieldsValid:!1,isFieldsValidating:!1,isFormValid:!1,isFormValidating:!1,isSubmitted:!1,isSubmitting:!1,isTouched:!1,isValid:!1,isValidating:!1,submissionAttempts:0,formValidationCount:0,...t}}var v=0;function m(t){return"__private_"+v+++"_"+t}function p(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}let g=0;var V=m("updateStore"),b=m("leaseValidateAsync");class y{constructor(t){var i;this.options={},this.mount=()=>{const t=this.getInfo();t.instances[this.uid]=this;const e=this.form.store.subscribe((()=>{p(this,V)[V]()}));return()=>{e(),delete t.instances[this.uid],Object.keys(t.instances).length||delete this.form.fieldInfo[this.name]}},Object.defineProperty(this,V,{writable:!0,value:()=>{this.store.batch((()=>{const t=this.getValue(),e=this.getMeta();t!==this.state.value&&this.store.setState((e=>({...e,value:t}))),e!==this.state.meta&&this.store.setState((t=>({...t,meta:e})))}))}}),this.update=t=>{var e,i,s,a;this.options={validatePristine:null!=(e=this.form.options.defaultValidatePristine)&&e,validateOn:null!=(i=this.form.options.defaultValidateOn)?i:"change",validateAsyncOn:null!=(s=this.form.options.defaultValidateAsyncOn)?s:"blur",validateAsyncDebounceMs:null!=(a=this.form.options.defaultValidateAsyncDebounceMs)?a:0,...t},void 0===this.state.value&&void 0!==this.options.defaultValue&&this.setValue(this.options.defaultValue),void 0===this.getMeta()&&this.setMeta(this.state.meta)},this.getValue=()=>this.form.getFieldValue(this.name),this.setValue=(t,e)=>this.form.setFieldValue(this.name,t,e),this.getMeta=()=>this.form.getFieldMeta(this.name),this.setMeta=t=>this.form.setFieldMeta(this.name,t),this.getInfo=()=>this.form.getFieldInfo(this.name),this.pushValue=t=>this.form.pushFieldValue(this.name,t),this.insertValue=(t,e)=>this.form.insertFieldValue(this.name,t,e),this.removeValue=t=>this.form.removeFieldValue(this.name,t),this.swapValues=(t,e)=>this.form.swapFieldValues(this.name,t,e),this.getSubField=t=>new y({name:this.name+"."+t,form:this.form}),this.validateSync=async(t=this.state.value)=>{const{validate:e}=this.options;if(!e)return;const i=(this.getInfo().validationCount||0)+1;this.getInfo().validationCount=i;const s=S(e(t,this));this.state.meta.error!==s&&this.setMeta((t=>({...t,error:s}))),this.state.meta.error&&this.cancelValidateAsync()},Object.defineProperty(this,b,{writable:!0,value:()=>{const t=(this.getInfo().validationAsyncCount||0)+1;return this.getInfo().validationAsyncCount=t,t}}),this.cancelValidateAsync=()=>{p(this,b)[b](),this.setMeta((t=>({...t,isValidating:!1})))},this.validateAsync=async(t=this.state.value)=>{const{validateAsync:e,validateAsyncDebounceMs:i}=this.options;if(!e)return;!0!==this.state.meta.isValidating&&this.setMeta((t=>({...t,isValidating:!0})));const s=p(this,b)[b](),a=()=>s===this.getInfo().validationAsyncCount;if(this.getInfo().validationPromise||(this.getInfo().validationPromise=new Promise(((t,e)=>{this.getInfo().validationResolve=t,this.getInfo().validationReject=e}))),i>0&&await new Promise((t=>setTimeout(t,i))),a())try{const i=await e(t,this);if(a()){var n,o;const t=S(i);this.setMeta((e=>({...e,isValidating:!1,error:t}))),null==(n=(o=this.getInfo()).validationResolve)||n.call(o,t)}}catch(t){var l,h;if(a())throw null==(l=(h=this.getInfo()).validationReject)||l.call(h,t),t}finally{a()&&(this.setMeta((t=>({...t,isValidating:!1}))),delete this.getInfo().validationPromise)}return this.getInfo().validationPromise},this.shouldValidate=(t,e)=>{const{validateOn:i,validateAsyncOn:s}=this.options,a=function(t){return t?F[t]:3}(e);return Object.keys(F).some((e=>t?s:i===e&&a>=F[e]))},this.validate=async(t,e)=>{if(this.options.validatePristine||this.state.meta.isTouched)return this.shouldValidate(!1,t)&&this.validateSync(e),this.state.meta.error?this.state.meta.error:this.shouldValidate(!0,t)?this.validateAsync(e):void 0},this.getChangeProps=(t={})=>({...t,value:this.state.value,onChange:e=>{this.setValue(e),null==t.onChange||t.onChange(e)},onBlur:e=>{this.setMeta((t=>({...t,isTouched:!0}))),this.validate("blur"),null==t.onBlur||t.onBlur(e)}}),this.getInputProps=(t={})=>({...t,value:String(this.state.value),onChange:e=>{this.setValue(e.target.value),null==t.onChange||t.onChange(e.target.value)},onBlur:this.getChangeProps(t).onBlur}),this.form=t.form,this.uid=g++,this.name=t.name,this.store=new e({value:this.getValue(),meta:null!=(i=this.getMeta())?i:{isValidating:!1,isTouched:!1,...this.options.defaultMeta}},{onUpdate:t=>{t.meta.touchedError=t.meta.isTouched?t.meta.error:void 0;const e=this.state;this.state=t,t.value!==e.value&&this.validate("change",t.value),console.log(this)}}),this.state=this.store.state,this.update(t)}}const F={change:0,blur:1,submit:2};function S(t){if(t)return"string"!=typeof t?"Invalid Form Values":t}t.FieldApi=y,t.FormApi=class{constructor(t){var n,o;this.options={},this.fieldInfo={},this.validationMeta={},this.update=t=>{t&&(this.store.batch((()=>{t.defaultState&&t.defaultState!==this.options.defaultState&&this.store.setState((e=>({...e,...t.defaultState}))),t.defaultValues!==this.options.defaultValues&&this.store.setState((e=>({...e,values:t.defaultValues})))})),this.options=t)},this.reset=()=>this.store.setState((()=>f(this.options.defaultValues))),this.validateAllFields=async()=>{const t=[];return this.store.batch((()=>{Object.values(this.fieldInfo).forEach((e=>{Object.values(e.instances).forEach((e=>{e.state.meta.isTouched||(e.setMeta((t=>({...t,isTouched:!0}))),e.options.validate&&t.push(e.validate()))}))}))})),Promise.all(t)},this.validateForm=async()=>{const{validate:t}=this.options;if(!t)return;this.store.setState((t=>({...t,isValidating:!0,formValidationCount:t.formValidationCount+1})));const e=this.state.formValidationCount,i=()=>e===this.state.formValidationCount;this.validationMeta.validationPromise||(this.validationMeta.validationPromise=new Promise(((t,e)=>{this.validationMeta.validationResolve=t,this.validationMeta.validationReject=e})));return(async()=>{try{const a=await t(this.state.values,this);var e,s;if(i())this.store.setState((t=>({...t,isValidating:!1,error:a?"string"==typeof a?a:"Invalid Form Values":null}))),null==(e=(s=this.validationMeta).validationResolve)||e.call(s,a)}catch(t){var a,n;if(i())null==(a=(n=this.validationMeta).validationReject)||a.call(n,t)}finally{delete this.validationMeta.validationPromise}})(),this.validationMeta.validationPromise},this.handleSubmit=async t=>{if(t.preventDefault(),t.stopPropagation(),this.store.setState((t=>({...t,isSubmitted:!1,submissionAttempts:t.submissionAttempts+1}))),!this.state.canSubmit)return;this.store.setState((t=>({...t,isSubmitting:!0})));const e=()=>{this.store.setState((t=>({...t,isSubmitting:!1})))};var i,s,a,n;if(await this.validateAllFields(),!this.state.isFieldsValid)return e(),void(null==(i=(s=this.options).onInvalidSubmit)||i.call(s,this.state.values,this));if(await this.validateForm(),!this.state.isValid)return e(),void(null==(a=(n=this.options).onInvalidSubmit)||a.call(n,this.state.values,this));try{var o,l;await(null==(o=(l=this.options).onSubmit)?void 0:o.call(l,this.state.values,this)),this.store.batch((()=>{this.store.setState((t=>({...t,isSubmitted:!0}))),e()}))}catch(t){throw e(),t}},this.getFieldValue=t=>s(this.state.values,t),this.getFieldMeta=t=>this.state.fieldMeta[t],this.getFieldInfo=t=>{var e;return(e=this.fieldInfo)[t]||(e[t]={instances:{}})},this.setFieldMeta=(t,e)=>{this.store.setState((s=>({...s,fieldMeta:{...s.fieldMeta,[t]:i(e,s.fieldMeta[t])}})))},this.setFieldValue=(t,e,i)=>{var s;const n=null==(s=null==i?void 0:i.touch)||s;this.store.batch((()=>{this.store.setState((i=>({...i,values:a(i.values,t,e)}))),n&&this.setFieldMeta(t,(t=>({...t,isTouched:!0})))}))},this.pushFieldValue=(t,e,i)=>this.setFieldValue(t,(t=>[...Array.isArray(t)?t:[],e]),i),this.insertFieldValue=(t,e,i,s)=>{this.setFieldValue(t,(t=>t.map(((t,s)=>s===e?i:t))),s)},this.removeFieldValue=(t,e,i)=>{this.setFieldValue(t,(t=>t.filter(((t,i)=>i!==e))),i)},this.swapFieldValues=(t,e,i)=>{this.setFieldValue(t,(t=>{const s=t[e],n=t[i];return a(a(t,[e],n),[i],s)}))},this.store=new e(f({...null==t?void 0:t.defaultState,values:null!=(n=null==t?void 0:t.defaultValues)?n:null==t||null==(o=t.defaultState)?void 0:o.values,isFormValid:!(null!=t&&t.validate)}),{onUpdate:t=>{const e=Object.values(t.fieldMeta),i=e.some((t=>null==t?void 0:t.isValidating)),s=!e.some((t=>null==t?void 0:t.error)),a=e.some((t=>null==t?void 0:t.isTouched)),n=i||t.isFormValidating,o=!t.formError,l=s&&o,h=0===t.submissionAttempts&&!a||!n&&!t.isSubmitting&&l;t={...t,isFieldsValidating:i,isFieldsValid:s,isFormValid:o,isValid:l,canSubmit:h,isTouched:a},this.store.state=t,this.state=t,console.log(this.state)}}),this.state=this.store.state,this.update(t||{})}},t.functionalUpdate=i,t.getBy=s,t.setBy=a,Object.defineProperty(t,"__esModule",{value:!0})})); | ||
*/class e{listeners=new Set;batching=!1;queue=[];constructor(t,e){this.state=t,this.options=e}subscribe=t=>{this.listeners.add(t);const e=this.options?.onSubscribe?.(t,this);return()=>{this.listeners.delete(t),e?.()}};setState=t=>{const e=this.state;this.state=this.options?.updateFn?this.options.updateFn(e)(t):t(e),this.state!==e&&(this.options?.onUpdate?.(this.state,e),this.queue.push((()=>{this.listeners.forEach((t=>t(this.state,e)))})),this.#t())};#t=()=>{this.batching||(this.queue.forEach((t=>t())),this.queue=[])};batch=t=>{this.batching=!0,t(),this.batching=!1,this.#t()}}function i(t,e){return"function"==typeof t?t(e):t}function s(t,e){return c(e).reduce(((t,e)=>{if(void 0!==t)return t[e]}),t)}function a(t,e,s){const a=c(e);return function t(e){if(!a.length)return i(s,e);const n=a.shift();if("string"==typeof n)return"object"==typeof e?{...e,[n]:t(e[n])}:{[n]:t()};if("number"==typeof n){if(Array.isArray(e)){const i=e.slice(0,n);return[...i.length?i:new Array(n),t(e[n]),...e.slice(n+1)]}return[...new Array(n),t()]}throw new Error("Uh oh!")}(t)}const n=/^(\d*)$/gm,o=/\.(\d*)\./gm,l=/^(\d*)\./gm,h=/\.(\d*$)/gm,r=/\.{2,}/gm,u="__int__",d="__int__$1";function c(t){if("string"!=typeof t)throw new Error;return t.replace("[",".").replace("]","").replace(n,d).replace(o,".__int__$1.").replace(l,"__int__$1.").replace(h,".__int__$1").replace(r,".").split(".").map((t=>0===t.indexOf(u)?parseInt(t.substring(u.length),10):t))}function f(t){return{values:{},fieldMeta:{},canSubmit:!0,isFieldsValid:!1,isFieldsValidating:!1,isFormValid:!1,isFormValidating:!1,isSubmitted:!1,isSubmitting:!1,isTouched:!1,isValid:!1,isValidating:!1,submissionAttempts:0,formValidationCount:0,...t}}var v=0;function m(t){return"__private_"+v+++"_"+t}function p(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}let g=0;var V=m("updateStore"),b=m("leaseValidateAsync");class y{constructor(t){var i;this.options={},this.mount=()=>{const t=this.getInfo();t.instances[this.uid]=this;const e=this.form.store.subscribe((()=>{p(this,V)[V]()}));return()=>{e(),delete t.instances[this.uid],Object.keys(t.instances).length||delete this.form.fieldInfo[this.name]}},Object.defineProperty(this,V,{writable:!0,value:()=>{this.store.batch((()=>{const t=this.getValue(),e=this.getMeta();t!==this.state.value&&this.store.setState((e=>({...e,value:t}))),e!==this.state.meta&&this.store.setState((t=>({...t,meta:e})))}))}}),this.update=t=>{var e,i,s,a;this.options={validatePristine:null!=(e=this.form.options.defaultValidatePristine)&&e,validateOn:null!=(i=this.form.options.defaultValidateOn)?i:"change",validateAsyncOn:null!=(s=this.form.options.defaultValidateAsyncOn)?s:"blur",validateAsyncDebounceMs:null!=(a=this.form.options.defaultValidateAsyncDebounceMs)?a:0,...t},void 0===this.state.value&&void 0!==this.options.defaultValue&&this.setValue(this.options.defaultValue),void 0===this.getMeta()&&this.setMeta(this.state.meta)},this.getValue=()=>this.form.getFieldValue(this.name),this.setValue=(t,e)=>this.form.setFieldValue(this.name,t,e),this.getMeta=()=>this.form.getFieldMeta(this.name),this.setMeta=t=>this.form.setFieldMeta(this.name,t),this.getInfo=()=>this.form.getFieldInfo(this.name),this.pushValue=t=>this.form.pushFieldValue(this.name,t),this.insertValue=(t,e)=>this.form.insertFieldValue(this.name,t,e),this.removeValue=t=>this.form.removeFieldValue(this.name,t),this.swapValues=(t,e)=>this.form.swapFieldValues(this.name,t,e),this.getSubField=t=>new y({name:this.name+"."+t,form:this.form}),this.validateSync=async(t=this.state.value)=>{const{validate:e}=this.options;if(!e)return;const i=(this.getInfo().validationCount||0)+1;this.getInfo().validationCount=i;const s=S(e(t,this));this.state.meta.error!==s&&this.setMeta((t=>({...t,error:s}))),this.state.meta.error&&this.cancelValidateAsync()},Object.defineProperty(this,b,{writable:!0,value:()=>{const t=(this.getInfo().validationAsyncCount||0)+1;return this.getInfo().validationAsyncCount=t,t}}),this.cancelValidateAsync=()=>{p(this,b)[b](),this.setMeta((t=>({...t,isValidating:!1})))},this.validateAsync=async(t=this.state.value)=>{const{validateAsync:e,validateAsyncDebounceMs:i}=this.options;if(!e)return;!0!==this.state.meta.isValidating&&this.setMeta((t=>({...t,isValidating:!0})));const s=p(this,b)[b](),a=()=>s===this.getInfo().validationAsyncCount;if(this.getInfo().validationPromise||(this.getInfo().validationPromise=new Promise(((t,e)=>{this.getInfo().validationResolve=t,this.getInfo().validationReject=e}))),i>0&&await new Promise((t=>setTimeout(t,i))),a())try{const i=await e(t,this);if(a()){var n,o;const t=S(i);this.setMeta((e=>({...e,isValidating:!1,error:t}))),null==(n=(o=this.getInfo()).validationResolve)||n.call(o,t)}}catch(t){var l,h;if(a())throw null==(l=(h=this.getInfo()).validationReject)||l.call(h,t),t}finally{a()&&(this.setMeta((t=>({...t,isValidating:!1}))),delete this.getInfo().validationPromise)}return this.getInfo().validationPromise},this.shouldValidate=(t,e)=>{const{validateOn:i,validateAsyncOn:s}=this.options,a=function(t){return t?F[t]:3}(e);return Object.keys(F).some((e=>t?s:i===e&&a>=F[e]))},this.validate=async(t,e)=>{if(this.options.validatePristine||this.state.meta.isTouched)return this.shouldValidate(!1,t)&&this.validateSync(e),this.state.meta.error?this.state.meta.error:this.shouldValidate(!0,t)?this.validateAsync(e):void 0},this.getChangeProps=(t={})=>({...t,value:this.state.value,onChange:e=>{this.setValue(e),null==t.onChange||t.onChange(e)},onBlur:e=>{this.setMeta((t=>({...t,isTouched:!0}))),this.validate("blur"),null==t.onBlur||t.onBlur(e)}}),this.getInputProps=(t={})=>({...t,value:String(this.state.value),onChange:e=>{this.setValue(e.target.value),null==t.onChange||t.onChange(e.target.value)},onBlur:this.getChangeProps(t).onBlur}),this.form=t.form,this.uid=g++,this.name=t.name,this.store=new e({value:this.getValue(),meta:null!=(i=this.getMeta())?i:{isValidating:!1,isTouched:!1,...this.options.defaultMeta}},{onUpdate:t=>{t.meta.touchedError=t.meta.isTouched?t.meta.error:void 0;const e=this.state;this.state=t,t.value!==e.value&&this.validate("change",t.value)}}),this.state=this.store.state,this.update(t)}}const F={change:0,blur:1,submit:2};function S(t){if(t)return"string"!=typeof t?"Invalid Form Values":t}t.FieldApi=y,t.FormApi=class{constructor(t){var n,o;this.options={},this.fieldInfo={},this.validationMeta={},this.update=t=>{t&&(this.store.batch((()=>{t.defaultState&&t.defaultState!==this.options.defaultState&&this.store.setState((e=>({...e,...t.defaultState}))),t.defaultValues!==this.options.defaultValues&&this.store.setState((e=>({...e,values:t.defaultValues})))})),this.options=t)},this.reset=()=>this.store.setState((()=>f(this.options.defaultValues))),this.validateAllFields=async()=>{const t=[];return this.store.batch((()=>{Object.values(this.fieldInfo).forEach((e=>{Object.values(e.instances).forEach((e=>{e.state.meta.isTouched||(e.setMeta((t=>({...t,isTouched:!0}))),e.options.validate&&t.push(e.validate()))}))}))})),Promise.all(t)},this.validateForm=async()=>{const{validate:t}=this.options;if(!t)return;this.store.setState((t=>({...t,isValidating:!0,formValidationCount:t.formValidationCount+1})));const e=this.state.formValidationCount,i=()=>e===this.state.formValidationCount;this.validationMeta.validationPromise||(this.validationMeta.validationPromise=new Promise(((t,e)=>{this.validationMeta.validationResolve=t,this.validationMeta.validationReject=e})));return(async()=>{try{const a=await t(this.state.values,this);var e,s;if(i())this.store.setState((t=>({...t,isValidating:!1,error:a?"string"==typeof a?a:"Invalid Form Values":null}))),null==(e=(s=this.validationMeta).validationResolve)||e.call(s,a)}catch(t){var a,n;if(i())null==(a=(n=this.validationMeta).validationReject)||a.call(n,t)}finally{delete this.validationMeta.validationPromise}})(),this.validationMeta.validationPromise},this.handleSubmit=async t=>{if(t.preventDefault(),t.stopPropagation(),this.store.setState((t=>({...t,isSubmitted:!1,submissionAttempts:t.submissionAttempts+1}))),!this.state.canSubmit)return;this.store.setState((t=>({...t,isSubmitting:!0})));const e=()=>{this.store.setState((t=>({...t,isSubmitting:!1})))};var i,s,a,n;if(await this.validateAllFields(),!this.state.isFieldsValid)return e(),void(null==(i=(s=this.options).onInvalidSubmit)||i.call(s,this.state.values,this));if(await this.validateForm(),!this.state.isValid)return e(),void(null==(a=(n=this.options).onInvalidSubmit)||a.call(n,this.state.values,this));try{var o,l;await(null==(o=(l=this.options).onSubmit)?void 0:o.call(l,this.state.values,this)),this.store.batch((()=>{this.store.setState((t=>({...t,isSubmitted:!0}))),e()}))}catch(t){throw e(),t}},this.getFieldValue=t=>s(this.state.values,t),this.getFieldMeta=t=>this.state.fieldMeta[t],this.getFieldInfo=t=>{var e;return(e=this.fieldInfo)[t]||(e[t]={instances:{}})},this.setFieldMeta=(t,e)=>{this.store.setState((s=>({...s,fieldMeta:{...s.fieldMeta,[t]:i(e,s.fieldMeta[t])}})))},this.setFieldValue=(t,e,i)=>{var s;const n=null==(s=null==i?void 0:i.touch)||s;this.store.batch((()=>{this.store.setState((i=>({...i,values:a(i.values,t,e)}))),n&&this.setFieldMeta(t,(t=>({...t,isTouched:!0})))}))},this.pushFieldValue=(t,e,i)=>this.setFieldValue(t,(t=>[...Array.isArray(t)?t:[],e]),i),this.insertFieldValue=(t,e,i,s)=>{this.setFieldValue(t,(t=>t.map(((t,s)=>s===e?i:t))),s)},this.removeFieldValue=(t,e,i)=>{this.setFieldValue(t,(t=>t.filter(((t,i)=>i!==e))),i)},this.swapFieldValues=(t,e,i)=>{this.setFieldValue(t,(t=>{const s=t[e],n=t[i];return a(a(t,[e],n),[i],s)}))},this.store=new e(f({...null==t?void 0:t.defaultState,values:null!=(n=null==t?void 0:t.defaultValues)?n:null==t||null==(o=t.defaultState)?void 0:o.values,isFormValid:!(null!=t&&t.validate)}),{onUpdate:t=>{const e=Object.values(t.fieldMeta),i=e.some((t=>null==t?void 0:t.isValidating)),s=!e.some((t=>null==t?void 0:t.error)),a=e.some((t=>null==t?void 0:t.isTouched)),n=i||t.isFormValidating,o=!t.formError,l=s&&o,h=0===t.submissionAttempts&&!a||!n&&!t.isSubmitting&&l;t={...t,isFieldsValidating:i,isFieldsValid:s,isFormValid:o,isValid:l,canSubmit:h,isTouched:a},this.store.state=t,this.state=t}}),this.state=this.store.state,this.update(t||{})}},t.functionalUpdate=i,t.getBy=s,t.setBy=a,Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.production.js.map |
{ | ||
"name": "@tanstack/form-core", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Powerful, type-safe, framework agnostic forms.", | ||
@@ -5,0 +5,0 @@ "author": "tannerlinsley", |
@@ -117,3 +117,2 @@ // | ||
} | ||
console.log(this) | ||
}, | ||
@@ -120,0 +119,0 @@ }, |
@@ -141,3 +141,2 @@ import { Store } from '@tanstack/store' | ||
this.state = next | ||
console.log(this.state) | ||
}, | ||
@@ -144,0 +143,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
433337
3188