@tanstack/form-core
Advanced tools
Comparing version 0.19.0 to 0.19.1
@@ -106,2 +106,3 @@ import { Store } from '@tanstack/store'; | ||
setFieldMeta: <TField extends DeepKeys<TFormData>>(field: TField, updater: Updater<FieldMeta>) => void; | ||
resetFieldMeta: <TField extends DeepKeys<TFormData>>(fieldMeta: Record<TField, FieldMeta>) => Record<TField, FieldMeta>; | ||
setFieldValue: <TField extends DeepKeys<TFormData>>(field: TField, updater: Updater<DeepValue<TFormData, TField>>, opts?: { | ||
@@ -108,0 +109,0 @@ touch?: boolean; |
@@ -78,11 +78,16 @@ import { Store } from "@tanstack/store"; | ||
}; | ||
this.reset = () => this.store.setState( | ||
() => { | ||
var _a2; | ||
return getDefaultFormState({ | ||
...this.options.defaultState, | ||
values: this.options.defaultValues ?? ((_a2 = this.options.defaultState) == null ? void 0 : _a2.values) | ||
}); | ||
} | ||
); | ||
this.reset = () => { | ||
const { fieldMeta: currentFieldMeta } = this.state; | ||
const fieldMeta = this.resetFieldMeta(currentFieldMeta); | ||
this.store.setState( | ||
() => { | ||
var _a2; | ||
return getDefaultFormState({ | ||
...this.options.defaultState, | ||
values: this.options.defaultValues ?? ((_a2 = this.options.defaultState) == null ? void 0 : _a2.values), | ||
fieldMeta | ||
}); | ||
} | ||
); | ||
}; | ||
this.validateAllFields = async (cause) => { | ||
@@ -295,2 +300,20 @@ const fieldValidationPromises = []; | ||
}; | ||
this.resetFieldMeta = (fieldMeta) => { | ||
return Object.keys(fieldMeta).reduce( | ||
(acc, key) => { | ||
const fieldKey = key; | ||
acc[fieldKey] = { | ||
isValidating: false, | ||
isTouched: false, | ||
isDirty: false, | ||
isPristine: true, | ||
touchedErrors: [], | ||
errors: [], | ||
errorMap: {} | ||
}; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
}; | ||
this.setFieldValue = (field, updater, opts2) => { | ||
@@ -297,0 +320,0 @@ const touch = opts2 == null ? void 0 : opts2.touch; |
{ | ||
"name": "@tanstack/form-core", | ||
"version": "0.19.0", | ||
"version": "0.19.1", | ||
"description": "Powerful, type-safe, framework agnostic forms.", | ||
@@ -5,0 +5,0 @@ "author": "tannerlinsley", |
@@ -338,3 +338,5 @@ import { Store } from '@tanstack/store' | ||
reset = () => | ||
reset = () => { | ||
const { fieldMeta: currentFieldMeta } = this.state | ||
const fieldMeta = this.resetFieldMeta(currentFieldMeta) | ||
this.store.setState(() => | ||
@@ -344,4 +346,6 @@ getDefaultFormState({ | ||
values: this.options.defaultValues ?? this.options.defaultState?.values, | ||
fieldMeta, | ||
}), | ||
) | ||
} | ||
@@ -627,2 +631,23 @@ validateAllFields = async (cause: ValidationCause) => { | ||
resetFieldMeta = <TField extends DeepKeys<TFormData>>( | ||
fieldMeta: Record<TField, FieldMeta>, | ||
): Record<TField, FieldMeta> => { | ||
return Object.keys(fieldMeta).reduce( | ||
(acc: Record<TField, FieldMeta>, key) => { | ||
const fieldKey = key as TField | ||
acc[fieldKey] = { | ||
isValidating: false, | ||
isTouched: false, | ||
isDirty: false, | ||
isPristine: true, | ||
touchedErrors: [], | ||
errors: [], | ||
errorMap: {}, | ||
} | ||
return acc | ||
}, | ||
{} as Record<TField, FieldMeta>, | ||
) | ||
} | ||
setFieldValue = <TField extends DeepKeys<TFormData>>( | ||
@@ -629,0 +654,0 @@ field: TField, |
@@ -195,2 +195,66 @@ import { describe, expect, it, vi } from 'vitest' | ||
it('should not wipe validators when resetting', () => { | ||
const form = new FormApi({ | ||
defaultValues: { | ||
name: 'test', | ||
}, | ||
}) | ||
const field = new FieldApi({ | ||
form, | ||
name: 'name', | ||
validators: { | ||
onChange: ({ value }) => (value.length > 0 ? undefined : 'required'), | ||
}, | ||
}) | ||
form.mount() | ||
field.mount() | ||
field.handleChange('') | ||
expect(form.state.isFieldsValid).toEqual(false) | ||
expect(form.state.canSubmit).toEqual(false) | ||
form.reset() | ||
expect(form.state).toEqual({ | ||
values: { name: 'test' }, | ||
errors: [], | ||
errorMap: {}, | ||
fieldMeta: { | ||
name: { | ||
isValidating: false, | ||
isTouched: false, | ||
isDirty: false, | ||
isPristine: true, | ||
touchedErrors: [], | ||
errors: [], | ||
errorMap: {}, | ||
}, | ||
}, | ||
canSubmit: true, | ||
isFieldsValid: true, | ||
isFieldsValidating: false, | ||
isFormValid: true, | ||
isFormValidating: false, | ||
isSubmitted: false, | ||
isSubmitting: false, | ||
isTouched: false, | ||
isPristine: true, | ||
isDirty: false, | ||
isValid: true, | ||
isValidating: false, | ||
submissionAttempts: 0, | ||
validationMetaMap: { | ||
onChange: undefined, | ||
onBlur: undefined, | ||
onSubmit: undefined, | ||
onMount: undefined, | ||
onServer: undefined, | ||
}, | ||
}) | ||
}) | ||
it("should get a field's value", () => { | ||
@@ -197,0 +261,0 @@ const form = new FormApi({ |
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
380569
6351