@tanstack/form-core 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/FormApi.ts CHANGED
@@ -10,11 +10,12 @@ import {
10
10
  setBy,
11
11
  shallow,
12
12
  } from './utils'
13
+
13
14
  import {
14
15
  isStandardSchemaValidator,
15
16
  standardSchemaValidators,
16
17
  } from './standardSchemaValidator'
17
- import { metaHelper } from './metaHelper'
18
+ import { defaultFieldMeta, metaHelper } from './metaHelper'
18
19
  import type {
19
20
  StandardSchemaV1,
20
21
  StandardSchemaV1Issue,
@@ -1715,21 +1716,16 @@ export class FormApi<
1715
1716
  })
1716
1717
  }
1717
1718
 
1719
+ /**
1720
+ * resets every field's meta
1721
+ */
1718
1722
  resetFieldMeta = <TField extends DeepKeys<TFormData>>(
1719
1723
  fieldMeta: Record<TField, AnyFieldMeta>,
1720
1724
  ): Record<TField, AnyFieldMeta> => {
1721
1725
  return Object.keys(fieldMeta).reduce(
1722
1726
  (acc: Record<TField, AnyFieldMeta>, key) => {
1723
1727
  const fieldKey = key as TField
1724
- acc[fieldKey] = {
1725
- isValidating: false,
1726
- isTouched: false,
1727
- isBlurred: false,
1728
- isDirty: false,
1729
- isPristine: true,
1730
- errors: [],
1731
- errorMap: {},
1732
- }
1728
+ acc[fieldKey] = defaultFieldMeta
1733
1729
  return acc
1734
1730
  },
1735
1731
  {} as Record<TField, AnyFieldMeta>,
@@ -1956,6 +1952,28 @@ export class FormApi<
1956
1952
  this.validateField(`${field}[${index1}]` as DeepKeys<TFormData>, 'change')
1957
1953
  this.validateField(`${field}[${index2}]` as DeepKeys<TFormData>, 'change')
1958
1954
  }
1955
+
1956
+ /**
1957
+ * Resets the field value and meta to default state
1958
+ */
1959
+ resetField = <TField extends DeepKeys<TFormData>>(field: TField) => {
1960
+ this.baseStore.setState((prev) => {
1961
+ return {
1962
+ ...prev,
1963
+ fieldMetaBase: {
1964
+ ...prev.fieldMetaBase,
1965
+ [field]: defaultFieldMeta,
1966
+ },
1967
+ values: {
1968
+ ...prev.values,
1969
+ [field]:
1970
+ this.options.defaultValues &&
1971
+ this.options.defaultValues[field as keyof TFormData],
1972
+ },
1973
+ }
1974
+ })
1975
+ }
1976
+
1959
1977
  /**
1960
1978
  * Updates the form's errorMap
1961
1979
  */
package/src/metaHelper.ts CHANGED
@@ -8,6 +8,16 @@ import type { DeepKeys } from './util-types'
8
8
 
9
9
  type ArrayFieldMode = 'insert' | 'remove' | 'swap' | 'move'
10
10
 
11
+ export const defaultFieldMeta: AnyFieldMeta = {
12
+ isValidating: false,
13
+ isTouched: false,
14
+ isBlurred: false,
15
+ isDirty: false,
16
+ isPristine: true,
17
+ errors: [],
18
+ errorMap: {},
19
+ }
20
+
11
21
  export function metaHelper<
12
22
  TFormData,
13
23
  TOnMount extends undefined | FormValidateOrFn<TFormData>,
@@ -116,15 +126,7 @@ export function metaHelper<
116
126
  })
117
127
  }
118
128
 
119
- const getEmptyFieldMeta = (): AnyFieldMeta => ({
120
- isValidating: false,
121
- isTouched: false,
122
- isBlurred: false,
123
- isDirty: false,
124
- isPristine: true,
125
- errors: [],
126
- errorMap: {},
127
- })
129
+ const getEmptyFieldMeta = (): AnyFieldMeta => defaultFieldMeta
128
130
 
129
131
  const handleInsertMode = (
130
132
  fields: DeepKeys<TFormData>[],
package/src/utils.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { GlobalFormValidationError, ValidationCause } from './types'
2
2
  import type { FormValidators } from './FormApi'
3
- import type { FieldValidators } from './FieldApi'
3
+ import type { AnyFieldMeta, FieldValidators } from './FieldApi'
4
4
 
5
5
  export type UpdaterFn<TInput, TOutput = TInput> = (input: TInput) => TOutput
6
6