@tanstack/form-core
Advanced tools
Comparing version 0.13.7 to 0.14.0
import { Store } from '@tanstack/store'; | ||
import type { FormApi } from './FormApi.js'; | ||
import type { ValidationCause, ValidationError, ValidationErrorMap, Validator } from './types.js'; | ||
import type { DeepKeys, DeepValue, Updater } from './utils.js'; | ||
import type { Updater } from './utils.js'; | ||
import type { DeepKeys, DeepValue } from './util-types.js'; | ||
export type FieldValidateFn<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = (props: { | ||
@@ -85,3 +86,2 @@ value: TData; | ||
swapValues: (aIndex: number, bIndex: number) => void; | ||
getSubField: <TSubName extends DeepKeys<TData>, TSubData extends DeepValue<TData, TSubName> = DeepValue<TData, TSubName>>(name: TSubName) => FieldApi<TData, TSubName, Validator<TSubData, unknown> | undefined, Validator<TData, unknown> | undefined, TSubData>; | ||
validateSync: (value: TData | undefined, cause: ValidationCause) => { | ||
@@ -88,0 +88,0 @@ hasErrored: boolean; |
@@ -85,6 +85,2 @@ import { Store } from "@tanstack/store"; | ||
this.swapValues = (aIndex, bIndex) => this.form.swapFieldValues(this.name, aIndex, bIndex); | ||
this.getSubField = (name) => new FieldApi({ | ||
name: `${this.name}.${name}`, | ||
form: this.form | ||
}); | ||
this.validateSync = (value = this.state.value, cause) => { | ||
@@ -91,0 +87,0 @@ const validates = getSyncValidatorArray(cause, this.options); |
import { Store } from '@tanstack/store'; | ||
import type { DeepKeys, DeepValue, Updater } from './utils.js'; | ||
import type { Updater } from './utils.js'; | ||
import type { DeepKeys, DeepValue } from './util-types.js'; | ||
import type { FieldApi, FieldMeta } from './FieldApi.js'; | ||
@@ -4,0 +5,0 @@ import type { ValidationCause, ValidationError, ValidationErrorMap, ValidationErrorMapKeys, Validator } from './types.js'; |
export * from './FormApi.js'; | ||
export * from './FieldApi.js'; | ||
export * from './utils.js'; | ||
export * from './util-types.js'; | ||
export * from './types.js'; | ||
export * from './mergeForm.js'; |
@@ -38,21 +38,2 @@ import type { ValidationCause } from './types.js'; | ||
export declare function getSyncValidatorArray<T>(cause: ValidationCause, options: SyncValidatorArrayPartialOptions<T>): T extends FieldValidators<any, any> ? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit']>> : T extends FormValidators<any, any> ? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit']>> : never; | ||
export type RequiredByKey<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>; | ||
type ComputeRange<N extends number, Result extends Array<unknown> = []> = Result['length'] extends N ? Result : ComputeRange<N, [...Result, Result['length']]>; | ||
type Index40 = ComputeRange<40>[number]; | ||
type IsTuple<T> = T extends readonly any[] & { | ||
length: infer Length; | ||
} ? Length extends Index40 ? T : never : never; | ||
type AllowedIndexes<Tuple extends ReadonlyArray<any>, Keys extends number = never> = Tuple extends readonly [] ? Keys : Tuple extends readonly [infer _, ...infer Tail] ? AllowedIndexes<Tail, Keys | Tail['length']> : Keys; | ||
export type DeepKeys<T, TDepth extends any[] = []> = TDepth['length'] extends 5 ? never : unknown extends T ? string : T extends readonly any[] & IsTuple<T> ? AllowedIndexes<T> | DeepKeysPrefix<T, AllowedIndexes<T>, TDepth> : T extends any[] ? DeepKeysPrefix<T, number, TDepth> : T extends Date ? never : T extends object ? (keyof T & string) | DeepKeysPrefix<T, keyof T, TDepth> : never; | ||
type DeepKeysPrefix<T, TPrefix, TDepth extends any[]> = TPrefix extends keyof T & (number | string) ? `${TPrefix}.${DeepKeys<T[TPrefix], [...TDepth, any]> & string}` : never; | ||
export type DeepValue<T, TProp> = T extends Record<string | number, any> ? TProp extends `${infer TBranch}.${infer TDeepProp}` ? DeepValue<T[TBranch], TDeepProp> : T[TProp & string] : never; | ||
type Narrowable = string | number | bigint | boolean; | ||
type NarrowRaw<A> = (A extends [] ? [] : never) | (A extends Narrowable ? A : never) | { | ||
[K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]>; | ||
}; | ||
export type Narrow<A> = Try<A, [], NarrowRaw<A>>; | ||
type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch; | ||
export type Pretty<T> = { | ||
[K in keyof T]: T[K]; | ||
} & {}; | ||
export {}; |
{ | ||
"name": "@tanstack/form-core", | ||
"version": "0.13.7", | ||
"version": "0.14.0", | ||
"description": "Powerful, type-safe, framework agnostic forms.", | ||
@@ -5,0 +5,0 @@ "author": "tannerlinsley", |
@@ -10,3 +10,4 @@ import { Store } from '@tanstack/store' | ||
} from './types' | ||
import type { DeepKeys, DeepValue, Updater } from './utils' | ||
import type { Updater } from './utils' | ||
import type { DeepKeys, DeepValue } from './util-types' | ||
@@ -299,2 +300,3 @@ export type FieldValidateFn< | ||
value: this.getValue(), | ||
meta: this._getMeta() ?? { | ||
@@ -413,2 +415,3 @@ isValidating: false, | ||
// Default Value | ||
if (this.state.value === undefined) { | ||
@@ -475,19 +478,2 @@ const formDefault = | ||
getSubField = < | ||
TSubName extends DeepKeys<TData>, | ||
TSubData extends DeepValue<TData, TSubName> = DeepValue<TData, TSubName>, | ||
>( | ||
name: TSubName, | ||
): FieldApi< | ||
TData, | ||
TSubName, | ||
Validator<TSubData, unknown> | undefined, | ||
Validator<TData, unknown> | undefined, | ||
TSubData | ||
> => | ||
new FieldApi({ | ||
name: `${this.name}.${name}` as never, | ||
form: this.form, | ||
}) as any | ||
validateSync = (value = this.state.value, cause: ValidationCause) => { | ||
@@ -494,0 +480,0 @@ const validates = getSyncValidatorArray(cause, this.options) |
@@ -11,3 +11,4 @@ import { Store } from '@tanstack/store' | ||
} from './utils' | ||
import type { DeepKeys, DeepValue, Updater } from './utils' | ||
import type { Updater } from './utils' | ||
import type { DeepKeys, DeepValue } from './util-types' | ||
import type { FieldApi, FieldMeta } from './FieldApi' | ||
@@ -315,3 +316,5 @@ import type { | ||
this.state as any, | ||
shouldUpdateState ? options.defaultState : {}, | ||
shouldUpdateValues | ||
@@ -318,0 +321,0 @@ ? { |
export * from './FormApi' | ||
export * from './FieldApi' | ||
export * from './utils' | ||
export * from './util-types' | ||
export * from './types' | ||
export * from './mergeForm' |
@@ -1,2 +0,2 @@ | ||
import { expect, vitest } from 'vitest' | ||
import { describe, expect, it, vi } from 'vitest' | ||
@@ -157,22 +157,2 @@ import { FormApi } from '../FormApi' | ||
it('should get a subfield properly', () => { | ||
const form = new FormApi({ | ||
defaultValues: { | ||
names: { | ||
first: 'one', | ||
second: 'two', | ||
}, | ||
}, | ||
}) | ||
const field = new FieldApi({ | ||
form, | ||
name: 'names', | ||
}) | ||
const subfield = field.getSubField('first') | ||
expect(subfield.getValue()).toBe('one') | ||
}) | ||
it('should not throw errors when no meta info is stored on a field and a form re-renders', async () => { | ||
@@ -623,3 +603,3 @@ const form = new FormApi({ | ||
const unmount = field.mount() | ||
const callback = vitest.fn() | ||
const callback = vi.fn() | ||
const subscription = form.store.subscribe(callback) | ||
@@ -626,0 +606,0 @@ unmount() |
@@ -1,2 +0,2 @@ | ||
import { assertType } from 'vitest' | ||
import { assertType, it } from 'vitest' | ||
import { FormApi } from '../FormApi' | ||
@@ -3,0 +3,0 @@ import { FieldApi } from '../FieldApi' |
@@ -1,2 +0,2 @@ | ||
import { expect } from 'vitest' | ||
import { describe, expect, it, vi } from 'vitest' | ||
@@ -275,3 +275,3 @@ import { FormApi } from '../FormApi' | ||
form, | ||
name: `employees.${0}.firstName`, | ||
name: `employees[0].firstName`, | ||
defaultValue: 'Darcy', | ||
@@ -304,7 +304,7 @@ }) | ||
form, | ||
name: `employees.${0}.firstName`, | ||
name: `employees[0].firstName`, | ||
defaultValue: 'Darcy', | ||
}) | ||
fieldInArray.mount() | ||
form.deleteField(`employees.${0}.firstName`) | ||
form.deleteField(`employees[0].firstName`) | ||
expect(field.state.value.length).toBe(1) | ||
@@ -311,0 +311,0 @@ expect(Object.keys(field.state.value[0]!).length).toBe(0) |
@@ -1,2 +0,2 @@ | ||
import { describe } from 'vitest' | ||
import { describe, expect, test } from 'vitest' | ||
import { mutateMergeDeep } from '../mergeForm' | ||
@@ -3,0 +3,0 @@ |
@@ -22,4 +22,4 @@ import { describe, expect, it } from 'vitest' | ||
it('should get array subfields by path', () => { | ||
expect(getBy(structure, 'kids.0.name')).toBe(structure.kids[0]!.name) | ||
expect(getBy(structure, 'kids.0.age')).toBe(structure.kids[0]!.age) | ||
expect(getBy(structure, 'kids[0].name')).toBe(structure.kids[0]!.name) | ||
expect(getBy(structure, 'kids[0].age')).toBe(structure.kids[0]!.age) | ||
}) | ||
@@ -46,6 +46,6 @@ }) | ||
it('should set array subfields by path', () => { | ||
expect(setBy(structure, 'kids.0.name', 'Taylor').kids[0].name).toBe( | ||
expect(setBy(structure, 'kids[0].name', 'Taylor').kids[0].name).toBe( | ||
'Taylor', | ||
) | ||
expect(setBy(structure, 'kids.0.age', 20).kids[0].age).toBe(20) | ||
expect(setBy(structure, 'kids[0].age', 20).kids[0].age).toBe(20) | ||
}) | ||
@@ -72,4 +72,4 @@ }) | ||
it('should delete array subfields by path', () => { | ||
expect(deleteBy(structure, 'kids.0.name').kids[0].name).not.toBeDefined() | ||
expect(deleteBy(structure, 'kids.0.age').kids[0].age).not.toBeDefined() | ||
expect(deleteBy(structure, 'kids[0].name').kids[0].name).not.toBeDefined() | ||
expect(deleteBy(structure, 'kids[0].age').kids[0].age).not.toBeDefined() | ||
}) | ||
@@ -80,5 +80,5 @@ | ||
expect(deleteBy(structure, 'nonexistent.nonexistent')).toEqual(structure) | ||
expect(deleteBy(structure, 'kids.3.name')).toEqual(structure) | ||
expect(deleteBy(structure, 'nonexistent.3.nonexistent')).toEqual(structure) | ||
expect(deleteBy(structure, 'kids[3].name')).toEqual(structure) | ||
expect(deleteBy(structure, 'nonexistent[3].nonexistent')).toEqual(structure) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
import type { ValidationCause, Validator } from './types' | ||
import type { ValidationCause } from './types' | ||
import type { FormValidators } from './FormApi' | ||
@@ -270,73 +270,1 @@ import type { FieldValidators } from './FieldApi' | ||
} | ||
export type RequiredByKey<T, K extends keyof T> = Omit<T, K> & | ||
Required<Pick<T, K>> | ||
type ComputeRange< | ||
N extends number, | ||
Result extends Array<unknown> = [], | ||
> = Result['length'] extends N | ||
? Result | ||
: ComputeRange<N, [...Result, Result['length']]> | ||
type Index40 = ComputeRange<40>[number] | ||
// Is this type a tuple? | ||
type IsTuple<T> = T extends readonly any[] & { length: infer Length } | ||
? Length extends Index40 | ||
? T | ||
: never | ||
: never | ||
// If this type is a tuple, what indices are allowed? | ||
type AllowedIndexes< | ||
Tuple extends ReadonlyArray<any>, | ||
Keys extends number = never, | ||
> = Tuple extends readonly [] | ||
? Keys | ||
: Tuple extends readonly [infer _, ...infer Tail] | ||
? AllowedIndexes<Tail, Keys | Tail['length']> | ||
: Keys | ||
export type DeepKeys<T, TDepth extends any[] = []> = TDepth['length'] extends 5 | ||
? never | ||
: unknown extends T | ||
? string | ||
: T extends readonly any[] & IsTuple<T> | ||
? AllowedIndexes<T> | DeepKeysPrefix<T, AllowedIndexes<T>, TDepth> | ||
: T extends any[] | ||
? DeepKeysPrefix<T, number, TDepth> | ||
: T extends Date | ||
? never | ||
: T extends object | ||
? (keyof T & string) | DeepKeysPrefix<T, keyof T, TDepth> | ||
: never | ||
type DeepKeysPrefix< | ||
T, | ||
TPrefix, | ||
TDepth extends any[], | ||
> = TPrefix extends keyof T & (number | string) | ||
? `${TPrefix}.${DeepKeys<T[TPrefix], [...TDepth, any]> & string}` | ||
: never | ||
export type DeepValue<T, TProp> = T extends Record<string | number, any> | ||
? TProp extends `${infer TBranch}.${infer TDeepProp}` | ||
? DeepValue<T[TBranch], TDeepProp> | ||
: T[TProp & string] | ||
: never | ||
type Narrowable = string | number | bigint | boolean | ||
type NarrowRaw<A> = | ||
| (A extends [] ? [] : never) | ||
| (A extends Narrowable ? A : never) | ||
| { | ||
[K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]> | ||
} | ||
export type Narrow<A> = Try<A, [], NarrowRaw<A>> | ||
type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch | ||
// Hack to get TypeScript to show simplified types in error messages | ||
export type Pretty<T> = { [K in keyof T]: T[K] } & {} |
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
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
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
50
5569
338376