@tanstack/form-core 0.0.7 → 0.0.9

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
@@ -7,6 +7,7 @@ export type ValidationCause = 'change' | 'blur' | 'submit'
7
7
 
8
8
  export interface FieldOptions<TData, TFormData> {
9
9
  name: unknown extends TFormData ? string : DeepKeys<TFormData>
10
+ index?: TData extends any[] ? number : never
10
11
  defaultValue?: TData
11
12
  validate?: (
12
13
  value: TData,
@@ -84,12 +85,12 @@ export class FieldApi<TData, TFormData> {
84
85
  this.form = opts.form
85
86
  this.uid = uid++
86
87
  // Support field prefixing from FieldScope
87
- let fieldPrefix = ''
88
- if (this.form.fieldName) {
89
- fieldPrefix = `${this.form.fieldName}.`
90
- }
88
+ // let fieldPrefix = ''
89
+ // if (this.form.fieldName) {
90
+ // fieldPrefix = `${this.form.fieldName}.`
91
+ // }
91
92
 
92
- this.name = (fieldPrefix + opts.name) as any
93
+ this.name = opts.name as any
93
94
 
94
95
  this.store = new Store<FieldState<TData>>(
95
96
  {
@@ -178,7 +179,9 @@ export class FieldApi<TData, TFormData> {
178
179
  }
179
180
  }
180
181
 
181
- getValue = (): TData => this.form.getFieldValue(this.name)
182
+ getValue = (): TData => {
183
+ return this.form.getFieldValue(this.name)
184
+ }
182
185
  setValue = (
183
186
  updater: Updater<TData>,
184
187
  options?: { touch?: boolean; notify?: boolean },
@@ -190,11 +193,11 @@ export class FieldApi<TData, TFormData> {
190
193
 
191
194
  getInfo = () => this.form.getFieldInfo(this.name)
192
195
 
193
- pushValue = (value: TData) =>
196
+ pushValue = (value: TData extends any[] ? TData[number] : never) =>
194
197
  this.form.pushFieldValue(this.name, value as any)
195
198
  insertValue = (index: number, value: TData) =>
196
199
  this.form.insertFieldValue(this.name, index, value as any)
197
- removeValue = (index: number) => this.form.spliceFieldValue(this.name, index)
200
+ removeValue = (index: number) => this.form.removeFieldValue(this.name, index)
198
201
  swapValues = (aIndex: number, bIndex: number) =>
199
202
  this.form.swapFieldValues(this.name, aIndex, bIndex)
200
203
 
package/src/FormApi.ts CHANGED
@@ -147,7 +147,9 @@ export class FormApi<TFormData> {
147
147
  this.update(opts || {})
148
148
  }
149
149
 
150
- update = (options: FormOptions<TFormData>) => {
150
+ update = (options?: FormOptions<TFormData>) => {
151
+ if (!options) return
152
+
151
153
  this.store.batch(() => {
152
154
  if (
153
155
  options.defaultState &&
@@ -402,7 +404,7 @@ export class FormApi<TFormData> {
402
404
  )
403
405
  }
404
406
 
405
- spliceFieldValue = <TField extends DeepKeys<TFormData>>(
407
+ removeFieldValue = <TField extends DeepKeys<TFormData>>(
406
408
  field: TField,
407
409
  index: number,
408
410
  opts?: { touch?: boolean },
package/src/utils.ts CHANGED
@@ -130,7 +130,7 @@ export type DeepKeys<T> = unknown extends T
130
130
  : T extends readonly any[] & IsTuple<T>
131
131
  ? AllowedIndexes<T> | DeepKeysPrefix<T, AllowedIndexes<T>>
132
132
  : T extends any[]
133
- ? never & 'Dynamic length array indexing is not supported'
133
+ ? DeepKeys<T[number]>
134
134
  : T extends Date
135
135
  ? never
136
136
  : T extends object
@@ -146,3 +146,16 @@ export type DeepValue<T, TProp> = T extends Record<string | number, any>
146
146
  ? DeepValue<T[TBranch], TDeepProp>
147
147
  : T[TProp & string]
148
148
  : never
149
+
150
+ type Narrowable = string | number | bigint | boolean
151
+
152
+ type NarrowRaw<A> =
153
+ | (A extends [] ? [] : never)
154
+ | (A extends Narrowable ? A : never)
155
+ | {
156
+ [K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]>
157
+ }
158
+
159
+ export type Narrow<A> = Try<A, [], NarrowRaw<A>>
160
+
161
+ type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch