@tanstack/form-core 1.0.3 → 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/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 {
@@ -1007,13 +1008,7 @@ export class FieldApi<
1007
1008
  fn: () => {
1008
1009
  const value = this.form.getFieldValue(this.name)
1009
1010
  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: {},
1011
+ ...defaultFieldMeta,
1017
1012
  ...opts.defaultMeta,
1018
1013
  }
1019
1014
 
@@ -1226,7 +1221,14 @@ export class FieldApi<
1226
1221
  pushValue = (
1227
1222
  value: TData extends any[] ? TData[number] : never,
1228
1223
  opts?: UpdateMetaOptions,
1229
- ) => this.form.pushFieldValue(this.name, value as any, opts)
1224
+ ) => {
1225
+ this.form.pushFieldValue(this.name, value as any, opts)
1226
+
1227
+ this.options.listeners?.onChange?.({
1228
+ value: this.state.value,
1229
+ fieldApi: this,
1230
+ })
1231
+ }
1230
1232
 
1231
1233
  /**
1232
1234
  * Inserts a value at the specified index, shifting the subsequent values to the right.
@@ -1235,7 +1237,14 @@ export class FieldApi<
1235
1237
  index: number,
1236
1238
  value: TData extends any[] ? TData[number] : never,
1237
1239
  opts?: UpdateMetaOptions,
1238
- ) => this.form.insertFieldValue(this.name, index, value as any, opts)
1240
+ ) => {
1241
+ this.form.insertFieldValue(this.name, index, value as any, opts)
1242
+
1243
+ this.options.listeners?.onChange?.({
1244
+ value: this.state.value,
1245
+ fieldApi: this,
1246
+ })
1247
+ }
1239
1248
 
1240
1249
  /**
1241
1250
  * Replaces a value at the specified index.
@@ -1244,26 +1253,51 @@ export class FieldApi<
1244
1253
  index: number,
1245
1254
  value: TData extends any[] ? TData[number] : never,
1246
1255
  opts?: UpdateMetaOptions,
1247
- ) => this.form.replaceFieldValue(this.name, index, value as any, opts)
1256
+ ) => {
1257
+ this.form.replaceFieldValue(this.name, index, value as any, opts)
1258
+
1259
+ this.options.listeners?.onChange?.({
1260
+ value: this.state.value,
1261
+ fieldApi: this,
1262
+ })
1263
+ }
1248
1264
 
1249
1265
  /**
1250
1266
  * Removes a value at the specified index.
1251
1267
  */
1252
- removeValue = (index: number, opts?: UpdateMetaOptions) =>
1268
+ removeValue = (index: number, opts?: UpdateMetaOptions) => {
1253
1269
  this.form.removeFieldValue(this.name, index, opts)
1254
1270
 
1271
+ this.options.listeners?.onChange?.({
1272
+ value: this.state.value,
1273
+ fieldApi: this,
1274
+ })
1275
+ }
1276
+
1255
1277
  /**
1256
1278
  * Swaps the values at the specified indices.
1257
1279
  */
1258
- swapValues = (aIndex: number, bIndex: number, opts?: UpdateMetaOptions) =>
1280
+ swapValues = (aIndex: number, bIndex: number, opts?: UpdateMetaOptions) => {
1259
1281
  this.form.swapFieldValues(this.name, aIndex, bIndex, opts)
1260
1282
 
1283
+ this.options.listeners?.onChange?.({
1284
+ value: this.state.value,
1285
+ fieldApi: this,
1286
+ })
1287
+ }
1288
+
1261
1289
  /**
1262
1290
  * Moves the value at the first specified index to the second specified index.
1263
1291
  */
1264
- moveValue = (aIndex: number, bIndex: number, opts?: UpdateMetaOptions) =>
1292
+ moveValue = (aIndex: number, bIndex: number, opts?: UpdateMetaOptions) => {
1265
1293
  this.form.moveFieldValues(this.name, aIndex, bIndex, opts)
1266
1294
 
1295
+ this.options.listeners?.onChange?.({
1296
+ value: this.state.value,
1297
+ fieldApi: this,
1298
+ })
1299
+ }
1300
+
1267
1301
  /**
1268
1302
  * @private
1269
1303
  */
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