@tanstack/form-core
Advanced tools
Comparing version 0.41.2 to 0.41.3
import { batch, Store, Derived } from "@tanstack/store"; | ||
import { getSyncValidatorArray, getAsyncValidatorArray, getBy, functionalUpdate, setBy, deleteBy, isNonEmptyArray } from "./utils.js"; | ||
import { shallow, getSyncValidatorArray, getAsyncValidatorArray, getBy, functionalUpdate, setBy, deleteBy, isNonEmptyArray } from "./utils.js"; | ||
import { isStandardSchemaValidator, standardSchemaValidator } from "./standardSchemaValidator.js"; | ||
@@ -50,5 +50,6 @@ function getDefaultFormState(defaultState) { | ||
this.options = options; | ||
const shouldUpdateValues = options.defaultValues && !shallow(options.defaultValues, oldOptions.defaultValues) && !this.state.isTouched; | ||
const shouldUpdateState = !shallow(options.defaultState, oldOptions.defaultState) && !this.state.isTouched; | ||
if (!shouldUpdateValues && !shouldUpdateState) return; | ||
batch(() => { | ||
const shouldUpdateValues = options.defaultValues && options.defaultValues !== oldOptions.defaultValues && !this.state.isTouched; | ||
const shouldUpdateState = options.defaultState !== oldOptions.defaultState && !this.state.isTouched; | ||
this.baseStore.setState( | ||
@@ -528,2 +529,3 @@ () => getDefaultFormState( | ||
const currBaseStore = currDepVals[0]; | ||
let originalMetaCount = 0; | ||
const fieldMeta = {}; | ||
@@ -545,2 +547,3 @@ for (const fieldName of Object.keys( | ||
fieldMeta[fieldName] = prevFieldInfo; | ||
originalMetaCount++; | ||
continue; | ||
@@ -554,2 +557,5 @@ } | ||
} | ||
if (prevVal && originalMetaCount === Object.keys(currBaseStore.fieldMetaBase).length) { | ||
return prevVal; | ||
} | ||
return fieldMeta; | ||
@@ -616,2 +622,5 @@ } | ||
} | ||
if (prevVal && prevBaseStore && prevVal.errorMap === errorMap && prevVal.fieldMeta === this.fieldMetaDerived.state && prevVal.errors === errors && prevVal.isFieldsValidating === isFieldsValidating && prevVal.isFieldsValid === isFieldsValid && prevVal.isFormValid === isFormValid && prevVal.isValid === isValid && prevVal.canSubmit === canSubmit && prevVal.isTouched === isTouched && prevVal.isBlurred === isBlurred && prevVal.isPristine === isPristine && prevVal.isDirty === isDirty && shallow(prevBaseStore, currBaseStore)) { | ||
return prevVal; | ||
} | ||
let state = { | ||
@@ -618,0 +627,0 @@ ...currBaseStore, |
import { FormApi } from "./FormApi.js"; | ||
import { FieldApi } from "./FieldApi.js"; | ||
import { deleteBy, functionalUpdate, getAsyncValidatorArray, getBy, getSyncValidatorArray, isNonEmptyArray, makePathArray, setBy } from "./utils.js"; | ||
import { deleteBy, functionalUpdate, getAsyncValidatorArray, getBy, getSyncValidatorArray, isNonEmptyArray, makePathArray, setBy, shallow } from "./utils.js"; | ||
import { mergeForm, mutateMergeDeep } from "./mergeForm.js"; | ||
@@ -22,4 +22,5 @@ import { formOptions } from "./formOptions.js"; | ||
setBy, | ||
shallow, | ||
standardSchemaValidator | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -63,2 +63,3 @@ import { ValidationCause } from './types.js'; | ||
export declare function getSyncValidatorArray<T>(cause: ValidationCause, options: SyncValidatorArrayPartialOptions<T>): T extends FieldValidators<any, any> ? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit'] | T['onMount']>> : T extends FormValidators<any, any> ? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit'] | T['onMount']>> : never; | ||
export declare function shallow<T>(objA: T, objB: T): boolean; | ||
export {}; |
@@ -180,2 +180,34 @@ function functionalUpdate(updater, input) { | ||
} | ||
function shallow(objA, objB) { | ||
if (Object.is(objA, objB)) { | ||
return true; | ||
} | ||
if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) { | ||
return false; | ||
} | ||
if (objA instanceof Map && objB instanceof Map) { | ||
if (objA.size !== objB.size) return false; | ||
for (const [k, v] of objA) { | ||
if (!objB.has(k) || !Object.is(v, objB.get(k))) return false; | ||
} | ||
return true; | ||
} | ||
if (objA instanceof Set && objB instanceof Set) { | ||
if (objA.size !== objB.size) return false; | ||
for (const v of objA) { | ||
if (!objB.has(v)) return false; | ||
} | ||
return true; | ||
} | ||
const keysA = Object.keys(objA); | ||
if (keysA.length !== Object.keys(objB).length) { | ||
return false; | ||
} | ||
for (let i = 0; i < keysA.length; i++) { | ||
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
export { | ||
@@ -189,4 +221,5 @@ deleteBy, | ||
makePathArray, | ||
setBy | ||
setBy, | ||
shallow | ||
}; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@tanstack/form-core", | ||
"version": "0.41.2", | ||
"version": "0.41.3", | ||
"description": "Powerful, type-safe, framework agnostic forms.", | ||
@@ -5,0 +5,0 @@ "author": "tannerlinsley", |
@@ -10,2 +10,3 @@ import { Derived, Store, batch } from '@tanstack/store' | ||
setBy, | ||
shallow, | ||
} from './utils' | ||
@@ -408,2 +409,4 @@ import { | ||
let originalMetaCount = 0 | ||
const fieldMeta = {} as FormState<TFormData>['fieldMeta'] | ||
@@ -442,2 +445,3 @@ for (const fieldName of Object.keys( | ||
fieldMeta[fieldName] = prevFieldInfo | ||
originalMetaCount++ | ||
continue | ||
@@ -453,2 +457,9 @@ } | ||
if ( | ||
prevVal && | ||
originalMetaCount === Object.keys(currBaseStore.fieldMetaBase).length | ||
) { | ||
return prevVal | ||
} | ||
return fieldMeta | ||
@@ -539,2 +550,22 @@ }, | ||
if ( | ||
prevVal && | ||
prevBaseStore && | ||
prevVal.errorMap === errorMap && | ||
prevVal.fieldMeta === this.fieldMetaDerived.state && | ||
prevVal.errors === errors && | ||
prevVal.isFieldsValidating === isFieldsValidating && | ||
prevVal.isFieldsValid === isFieldsValid && | ||
prevVal.isFormValid === isFormValid && | ||
prevVal.isValid === isValid && | ||
prevVal.canSubmit === canSubmit && | ||
prevVal.isTouched === isTouched && | ||
prevVal.isBlurred === isBlurred && | ||
prevVal.isPristine === isPristine && | ||
prevVal.isDirty === isDirty && | ||
shallow(prevBaseStore, currBaseStore) | ||
) { | ||
return prevVal | ||
} | ||
let state = { | ||
@@ -637,12 +668,14 @@ ...currBaseStore, | ||
batch(() => { | ||
const shouldUpdateValues = | ||
options.defaultValues && | ||
options.defaultValues !== oldOptions.defaultValues && | ||
!this.state.isTouched | ||
const shouldUpdateValues = | ||
options.defaultValues && | ||
!shallow(options.defaultValues, oldOptions.defaultValues) && | ||
!this.state.isTouched | ||
const shouldUpdateState = | ||
options.defaultState !== oldOptions.defaultState && | ||
!this.state.isTouched | ||
const shouldUpdateState = | ||
!shallow(options.defaultState, oldOptions.defaultState) && | ||
!this.state.isTouched | ||
if (!shouldUpdateValues && !shouldUpdateState) return | ||
batch(() => { | ||
this.baseStore.setState(() => | ||
@@ -649,0 +682,0 @@ getDefaultFormState( |
@@ -327,1 +327,47 @@ import type { ValidationCause } from './types' | ||
} | ||
export function shallow<T>(objA: T, objB: T) { | ||
if (Object.is(objA, objB)) { | ||
return true | ||
} | ||
if ( | ||
typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null | ||
) { | ||
return false | ||
} | ||
if (objA instanceof Map && objB instanceof Map) { | ||
if (objA.size !== objB.size) return false | ||
for (const [k, v] of objA) { | ||
if (!objB.has(k) || !Object.is(v, objB.get(k))) return false | ||
} | ||
return true | ||
} | ||
if (objA instanceof Set && objB instanceof Set) { | ||
if (objA.size !== objB.size) return false | ||
for (const v of objA) { | ||
if (!objB.has(v)) return false | ||
} | ||
return true | ||
} | ||
const keysA = Object.keys(objA) | ||
if (keysA.length !== Object.keys(objB).length) { | ||
return false | ||
} | ||
for (let i = 0; i < keysA.length; i++) { | ||
if ( | ||
!Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) || | ||
!Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T]) | ||
) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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
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
534324
6980