@tanstack/form-core 1.0.5 → 1.1.2

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/FieldApi.ts CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  isStandardSchemaValidator,
4
4
  standardSchemaValidators,
5
5
  } from './standardSchemaValidator'
6
+ import { defaultFieldMeta } from './metaHelper'
6
7
  import { getAsyncValidatorArray, getBy, getSyncValidatorArray } from './utils'
7
8
  import type { DeepKeys, DeepValue, UnwrapOneLevelOfArray } from './util-types'
8
9
  import type {
@@ -996,24 +997,13 @@ export class FieldApi<
996
997
  this.form = opts.form as never
997
998
  this.name = opts.name as never
998
999
  this.timeoutIds = {} as Record<ValidationCause, never>
999
- if (opts.defaultValue !== undefined) {
1000
- this.form.setFieldValue(this.name, opts.defaultValue as never, {
1001
- dontUpdateMeta: true,
1002
- })
1003
- }
1004
1000
 
1005
1001
  this.store = new Derived({
1006
1002
  deps: [this.form.store],
1007
1003
  fn: () => {
1008
1004
  const value = this.form.getFieldValue(this.name)
1009
1005
  const meta = this.form.getFieldMeta(this.name) ?? {
1010
- isValidating: false,
1011
- isTouched: false,
1012
- isBlurred: false,
1013
- isDirty: false,
1014
- isPristine: true,
1015
- errors: [],
1016
- errorMap: {},
1006
+ ...defaultFieldMeta,
1017
1007
  ...opts.defaultMeta,
1018
1008
  }
1019
1009
 
@@ -1077,6 +1067,12 @@ export class FieldApi<
1077
1067
  mount = () => {
1078
1068
  const cleanup = this.store.mount()
1079
1069
 
1070
+ if (this.options.defaultValue !== undefined) {
1071
+ this.form.setFieldValue(this.name, this.options.defaultValue as never, {
1072
+ dontUpdateMeta: true,
1073
+ })
1074
+ }
1075
+
1080
1076
  const info = this.getInfo()
1081
1077
  info.instance = this as never
1082
1078
 
@@ -1558,7 +1554,6 @@ export class FieldApi<
1558
1554
 
1559
1555
  // TODO: Dedupe this logic to reduce bundle size
1560
1556
  for (const validateObj of validates) {
1561
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1562
1557
  if (!validateObj.validate) continue
1563
1558
  validateFieldAsyncFn(this, validateObj, validatesPromises)
1564
1559
  }
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