@tanstack/form-core 0.23.2 → 0.23.3

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.
@@ -1,13 +1,26 @@
1
1
  type Nullable<T> = T | null;
2
2
  type IsNullable<T> = [null] extends [T] ? true : false;
3
+ /**
4
+ * @private
5
+ */
3
6
  export type RequiredByKey<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
4
7
  type Narrowable = string | number | bigint | boolean;
5
8
  type NarrowRaw<A> = (A extends [] ? [] : never) | (A extends Narrowable ? A : never) | {
6
9
  [K in keyof A]: A[K] extends Function ? A[K] : NarrowRaw<A[K]>;
7
10
  };
11
+ /**
12
+ * @private
13
+ */
8
14
  export type NoInfer<T> = [T][T extends any ? 0 : never];
15
+ /**
16
+ * @private
17
+ */
9
18
  export type Narrow<A> = Try<A, [], NarrowRaw<A>>;
10
19
  type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch;
20
+ /**
21
+ * Hack to get TypeScript to show simplified types in error messages
22
+ * @private
23
+ */
11
24
  export type Pretty<T> = {
12
25
  [K in keyof T]: T[K];
13
26
  } & {};
@@ -26,11 +39,13 @@ type PrefixTupleAccessor<T extends any[], TIndex extends number, TDepth extends
26
39
  type PrefixObjectAccessor<T extends object, TDepth extends any[]> = {
27
40
  [K in keyof T]-?: K extends string | number ? PrefixFromDepth<K, TDepth> | `${PrefixFromDepth<K, TDepth>}${DeepKeys<T[K], [TDepth]>}` : never;
28
41
  }[keyof T];
42
+ /**
43
+ * The keys of an object or array, deeply nested.
44
+ */
29
45
  export type DeepKeys<T, TDepth extends any[] = []> = TDepth['length'] extends 5 ? never : unknown extends T ? PrefixFromDepth<string, TDepth> : T extends readonly any[] & IsTuple<T> ? PrefixTupleAccessor<T, AllowedIndexes<T>, TDepth> : T extends any[] ? PrefixArrayAccessor<T, [...TDepth, any]> : T extends Date ? never : T extends object ? PrefixObjectAccessor<T, TDepth> : T extends string | number | boolean | bigint ? '' : never;
30
46
  type PrefixFromDepth<T extends string | number, TDepth extends any[]> = TDepth['length'] extends 0 ? T : `.${T}`;
31
47
  /**
32
- Infer the type of a deeply nested property within an object or an array.
33
- See the tests in `util-types.test-d.ts` for usage.
34
- */
48
+ * Infer the type of a deeply nested property within an object or an array.
49
+ */
35
50
  export type DeepValue<TValue, TAccessor, TNullable extends boolean = IsNullable<TValue>> = unknown extends TValue ? TValue : TValue extends ReadonlyArray<any> ? TAccessor extends `[${infer TBrackets}].${infer TAfter}` ? DeepValue<DeepValue<TValue, TBrackets>, TAfter> : TAccessor extends `[${infer TBrackets}]` ? DeepValue<TValue, TBrackets> : TAccessor extends keyof TValue ? TValue[TAccessor] : TValue[TAccessor & number] : TValue extends Record<string | number, any> ? TAccessor extends `${infer TBefore}[${infer TEverythingElse}` ? DeepValue<DeepValue<TValue, TBefore>, `[${TEverythingElse}`> : TAccessor extends `[${infer TBrackets}]` ? DeepValue<TValue, TBrackets> : TAccessor extends `${infer TBefore}.${infer TAfter}` ? DeepValue<DeepValue<TValue, TBefore>, TAfter> : TAccessor extends string ? TNullable extends true ? Nullable<TValue[TAccessor]> : TValue[TAccessor] : never : never;
36
51
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["import type { ValidationCause } from './types'\nimport type { FormValidators } from './FormApi'\nimport type { FieldValidators } from './FieldApi'\n\nexport type UpdaterFn<TInput, TOutput = TInput> = (input: TInput) => TOutput\n\nexport type Updater<TInput, TOutput = TInput> =\n | TOutput\n | UpdaterFn<TInput, TOutput>\n\nexport function functionalUpdate<TInput, TOutput = TInput>(\n updater: Updater<TInput, TOutput>,\n input: TInput,\n): TOutput {\n return typeof updater === 'function'\n ? (updater as UpdaterFn<TInput, TOutput>)(input)\n : updater\n}\n\n/**\n * Get a value from an object using a path, including dot notation.\n */\nexport function getBy(obj: any, path: any) {\n const pathObj = makePathArray(path)\n return pathObj.reduce((current: any, pathPart: any) => {\n if (current === null) return null\n if (typeof current !== 'undefined') {\n return current[pathPart]\n }\n return undefined\n }, obj)\n}\n\n/**\n * Set a value on an object using a path, including dot notation.\n */\nexport function setBy(obj: any, _path: any, updater: Updater<any>) {\n const path = makePathArray(_path)\n\n function doSet(parent?: any): any {\n if (!path.length) {\n return functionalUpdate(updater, parent)\n }\n\n const key = path.shift()\n\n if (typeof key === 'string') {\n if (typeof parent === 'object') {\n if (parent === null) {\n parent = {}\n }\n return {\n ...parent,\n [key]: doSet(parent[key]),\n }\n }\n return {\n [key]: doSet(),\n }\n }\n\n if (Array.isArray(parent) && key !== undefined) {\n const prefix = parent.slice(0, key)\n return [\n ...(prefix.length ? prefix : new Array(key)),\n doSet(parent[key]),\n ...parent.slice(key + 1),\n ]\n }\n return [...new Array(key), doSet()]\n }\n\n return doSet(obj)\n}\n\n/**\n * Delete a field on an object using a path, including dot notation.\n */\nexport function deleteBy(obj: any, _path: any) {\n const path = makePathArray(_path)\n\n function doDelete(parent: any): any {\n if (!parent) return\n if (path.length === 1) {\n const finalPath = path[0]!\n if (Array.isArray(parent) && typeof finalPath === 'number') {\n return parent.filter((_, i) => i !== finalPath)\n }\n const { [finalPath]: remove, ...rest } = parent\n return rest\n }\n\n const key = path.shift()\n\n if (typeof key === 'string') {\n if (typeof parent === 'object') {\n return {\n ...parent,\n [key]: doDelete(parent[key]),\n }\n }\n }\n\n if (typeof key === 'number') {\n if (Array.isArray(parent)) {\n if (key >= parent.length) {\n return parent\n }\n const prefix = parent.slice(0, key)\n return [\n ...(prefix.length ? prefix : new Array(key)),\n doDelete(parent[key]),\n ...parent.slice(key + 1),\n ]\n }\n }\n\n throw new Error('It seems we have created an infinite loop in deleteBy. ')\n }\n\n return doDelete(obj)\n}\n\nconst reFindNumbers0 = /^(\\d*)$/gm\nconst reFindNumbers1 = /\\.(\\d*)\\./gm\nconst reFindNumbers2 = /^(\\d*)\\./gm\nconst reFindNumbers3 = /\\.(\\d*$)/gm\nconst reFindMultiplePeriods = /\\.{2,}/gm\n\nconst intPrefix = '__int__'\nconst intReplace = `${intPrefix}$1`\n\nexport function makePathArray(str: string) {\n if (typeof str !== 'string') {\n throw new Error('Path must be a string.')\n }\n\n return str\n .replaceAll('[', '.')\n .replaceAll(']', '')\n .replace(reFindNumbers0, intReplace)\n .replace(reFindNumbers1, `.${intReplace}.`)\n .replace(reFindNumbers2, `${intReplace}.`)\n .replace(reFindNumbers3, `.${intReplace}`)\n .replace(reFindMultiplePeriods, '.')\n .split('.')\n .map((d) => {\n if (d.indexOf(intPrefix) === 0) {\n return parseInt(d.substring(intPrefix.length), 10)\n }\n return d\n })\n}\n\nexport function isNonEmptyArray(obj: any) {\n return !(Array.isArray(obj) && obj.length === 0)\n}\n\ninterface AsyncValidatorArrayPartialOptions<T> {\n validators?: T\n asyncDebounceMs?: number\n}\n\nexport interface AsyncValidator<T> {\n cause: ValidationCause\n validate: T\n debounceMs: number\n}\n\nexport function getAsyncValidatorArray<T>(\n cause: ValidationCause,\n options: AsyncValidatorArrayPartialOptions<T>,\n): T extends FieldValidators<any, any>\n ? Array<\n AsyncValidator<T['onChangeAsync'] | T['onBlurAsync'] | T['onSubmitAsync']>\n >\n : T extends FormValidators<any, any>\n ? Array<\n AsyncValidator<\n T['onChangeAsync'] | T['onBlurAsync'] | T['onSubmitAsync']\n >\n >\n : never {\n const { asyncDebounceMs } = options\n const {\n onChangeAsync,\n onBlurAsync,\n onSubmitAsync,\n onBlurAsyncDebounceMs,\n onChangeAsyncDebounceMs,\n } = (options.validators || {}) as\n | FieldValidators<any, any>\n | FormValidators<any, any>\n\n const defaultDebounceMs = asyncDebounceMs ?? 0\n\n const changeValidator = {\n cause: 'change',\n validate: onChangeAsync,\n debounceMs: onChangeAsyncDebounceMs ?? defaultDebounceMs,\n } as const\n\n const blurValidator = {\n cause: 'blur',\n validate: onBlurAsync,\n debounceMs: onBlurAsyncDebounceMs ?? defaultDebounceMs,\n } as const\n\n const submitValidator = {\n cause: 'submit',\n validate: onSubmitAsync,\n debounceMs: 0,\n } as const\n\n const noopValidator = (\n validator:\n | typeof changeValidator\n | typeof blurValidator\n | typeof submitValidator,\n ) => ({ ...validator, debounceMs: 0 }) as const\n\n switch (cause) {\n case 'submit':\n return [\n noopValidator(changeValidator),\n noopValidator(blurValidator),\n submitValidator,\n ] as never\n case 'blur':\n return [blurValidator] as never\n case 'change':\n return [changeValidator] as never\n case 'server':\n default:\n return [] as never\n }\n}\n\ninterface SyncValidatorArrayPartialOptions<T> {\n validators?: T\n}\n\nexport interface SyncValidator<T> {\n cause: ValidationCause\n validate: T\n}\n\nexport function getSyncValidatorArray<T>(\n cause: ValidationCause,\n options: SyncValidatorArrayPartialOptions<T>,\n): T extends FieldValidators<any, any>\n ? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit']>>\n : T extends FormValidators<any, any>\n ? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit']>>\n : never {\n const { onChange, onBlur, onSubmit } = (options.validators || {}) as\n | FieldValidators<any, any>\n | FormValidators<any, any>\n\n const changeValidator = { cause: 'change', validate: onChange } as const\n const blurValidator = { cause: 'blur', validate: onBlur } as const\n const submitValidator = { cause: 'submit', validate: onSubmit } as const\n\n // Allows us to clear onServer errors\n const serverValidator = {\n cause: 'server',\n validate: () => undefined,\n } as const\n\n switch (cause) {\n case 'submit':\n return [\n changeValidator,\n blurValidator,\n submitValidator,\n serverValidator,\n ] as never\n case 'server':\n return [serverValidator] as never\n case 'blur':\n return [blurValidator, serverValidator] as never\n case 'change':\n default:\n return [changeValidator, serverValidator] as never\n }\n}\n"],"names":[],"mappings":";;AAUgB,SAAA,iBACd,SACA,OACS;AACT,SAAO,OAAO,YAAY,aACrB,QAAuC,KAAK,IAC7C;AACN;AAKgB,SAAA,MAAM,KAAU,MAAW;AACnC,QAAA,UAAU,cAAc,IAAI;AAClC,SAAO,QAAQ,OAAO,CAAC,SAAc,aAAkB;AACrD,QAAI,YAAY;AAAa,aAAA;AACzB,QAAA,OAAO,YAAY,a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,SAAS;AAG9D,QAAM,kBAAkB;AAAA,IACtB,OAAO;AAAA,IACP,UAAU,MAAM;AAAA,EAAA;AAGlB,UAAQ,OAAO;AAAA,IACb,KAAK;AACI,aAAA;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MAAA;AAAA,IAEJ,KAAK;AACH,aAAO,CAAC,eAAe;AAAA,IACzB,KAAK;AACI,aAAA,CAAC,eAAe,eAAe;AAAA,IACxC,KAAK;AAAA,IACL;AACS,aAAA,CAAC,iBAAiB,eAAe;AAAA,EAC5C;AACF;;;;;;;;;"}
1
+ {"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["import type { ValidationCause } from './types'\nimport type { FormValidators } from './FormApi'\nimport type { FieldValidators } from './FieldApi'\n\nexport type UpdaterFn<TInput, TOutput = TInput> = (input: TInput) => TOutput\n\nexport type Updater<TInput, TOutput = TInput> =\n | TOutput\n | UpdaterFn<TInput, TOutput>\n\n/**\n * @private\n */\nexport function functionalUpdate<TInput, TOutput = TInput>(\n updater: Updater<TInput, TOutput>,\n input: TInput,\n): TOutput {\n return typeof updater === 'function'\n ? (updater as UpdaterFn<TInput, TOutput>)(input)\n : updater\n}\n\n/**\n * Get a value from an object using a path, including dot notation.\n * @private\n */\nexport function getBy(obj: any, path: any) {\n const pathObj = makePathArray(path)\n return pathObj.reduce((current: any, pathPart: any) => {\n if (current === null) return null\n if (typeof current !== 'undefined') {\n return current[pathPart]\n }\n return undefined\n }, obj)\n}\n\n/**\n * Set a value on an object using a path, including dot notation.\n * @private\n */\nexport function setBy(obj: any, _path: any, updater: Updater<any>) {\n const path = makePathArray(_path)\n\n function doSet(parent?: any): any {\n if (!path.length) {\n return functionalUpdate(updater, parent)\n }\n\n const key = path.shift()\n\n if (typeof key === 'string') {\n if (typeof parent === 'object') {\n if (parent === null) {\n parent = {}\n }\n return {\n ...parent,\n [key]: doSet(parent[key]),\n }\n }\n return {\n [key]: doSet(),\n }\n }\n\n if (Array.isArray(parent) && key !== undefined) {\n const prefix = parent.slice(0, key)\n return [\n ...(prefix.length ? prefix : new Array(key)),\n doSet(parent[key]),\n ...parent.slice(key + 1),\n ]\n }\n return [...new Array(key), doSet()]\n }\n\n return doSet(obj)\n}\n\n/**\n * Delete a field on an object using a path, including dot notation.\n * @private\n */\nexport function deleteBy(obj: any, _path: any) {\n const path = makePathArray(_path)\n\n function doDelete(parent: any): any {\n if (!parent) return\n if (path.length === 1) {\n const finalPath = path[0]!\n if (Array.isArray(parent) && typeof finalPath === 'number') {\n return parent.filter((_, i) => i !== finalPath)\n }\n const { [finalPath]: remove, ...rest } = parent\n return rest\n }\n\n const key = path.shift()\n\n if (typeof key === 'string') {\n if (typeof parent === 'object') {\n return {\n ...parent,\n [key]: doDelete(parent[key]),\n }\n }\n }\n\n if (typeof key === 'number') {\n if (Array.isArray(parent)) {\n if (key >= parent.length) {\n return parent\n }\n const prefix = parent.slice(0, key)\n return [\n ...(prefix.length ? prefix : new Array(key)),\n doDelete(parent[key]),\n ...parent.slice(key + 1),\n ]\n }\n }\n\n throw new Error('It seems we have created an infinite loop in deleteBy. ')\n }\n\n return doDelete(obj)\n}\n\nconst reFindNumbers0 = /^(\\d*)$/gm\nconst reFindNumbers1 = /\\.(\\d*)\\./gm\nconst reFindNumbers2 = /^(\\d*)\\./gm\nconst reFindNumbers3 = /\\.(\\d*$)/gm\nconst reFindMultiplePeriods = /\\.{2,}/gm\n\nconst intPrefix = '__int__'\nconst intReplace = `${intPrefix}$1`\n\n/**\n * @private\n */\nexport function makePathArray(str: string) {\n if (typeof str !== 'string') {\n throw new Error('Path must be a string.')\n }\n\n return str\n .replaceAll('[', '.')\n .replaceAll(']', '')\n .replace(reFindNumbers0, intReplace)\n .replace(reFindNumbers1, `.${intReplace}.`)\n .replace(reFindNumbers2, `${intReplace}.`)\n .replace(reFindNumbers3, `.${intReplace}`)\n .replace(reFindMultiplePeriods, '.')\n .split('.')\n .map((d) => {\n if (d.indexOf(intPrefix) === 0) {\n return parseInt(d.substring(intPrefix.length), 10)\n }\n return d\n })\n}\n\n/**\n * @private\n */\nexport function isNonEmptyArray(obj: any) {\n return !(Array.isArray(obj) && obj.length === 0)\n}\n\ninterface AsyncValidatorArrayPartialOptions<T> {\n validators?: T\n asyncDebounceMs?: number\n}\n\n/**\n * @private\n */\nexport interface AsyncValidator<T> {\n cause: ValidationCause\n validate: T\n debounceMs: number\n}\n\n/**\n * @private\n */\nexport function getAsyncValidatorArray<T>(\n cause: ValidationCause,\n options: AsyncValidatorArrayPartialOptions<T>,\n): T extends FieldValidators<any, any>\n ? Array<\n AsyncValidator<T['onChangeAsync'] | T['onBlurAsync'] | T['onSubmitAsync']>\n >\n : T extends FormValidators<any, any>\n ? Array<\n AsyncValidator<\n T['onChangeAsync'] | T['onBlurAsync'] | T['onSubmitAsync']\n >\n >\n : never {\n const { asyncDebounceMs } = options\n const {\n onChangeAsync,\n onBlurAsync,\n onSubmitAsync,\n onBlurAsyncDebounceMs,\n onChangeAsyncDebounceMs,\n } = (options.validators || {}) as\n | FieldValidators<any, any>\n | FormValidators<any, any>\n\n const defaultDebounceMs = asyncDebounceMs ?? 0\n\n const changeValidator = {\n cause: 'change',\n validate: onChangeAsync,\n debounceMs: onChangeAsyncDebounceMs ?? defaultDebounceMs,\n } as const\n\n const blurValidator = {\n cause: 'blur',\n validate: onBlurAsync,\n debounceMs: onBlurAsyncDebounceMs ?? defaultDebounceMs,\n } as const\n\n const submitValidator = {\n cause: 'submit',\n validate: onSubmitAsync,\n debounceMs: 0,\n } as const\n\n const noopValidator = (\n validator:\n | typeof changeValidator\n | typeof blurValidator\n | typeof submitValidator,\n ) => ({ ...validator, debounceMs: 0 }) as const\n\n switch (cause) {\n case 'submit':\n return [\n noopValidator(changeValidator),\n noopValidator(blurValidator),\n submitValidator,\n ] as never\n case 'blur':\n return [blurValidator] as never\n case 'change':\n return [changeValidator] as never\n case 'server':\n default:\n return [] as never\n }\n}\n\ninterface SyncValidatorArrayPartialOptions<T> {\n validators?: T\n}\n\n/**\n * @private\n */\nexport interface SyncValidator<T> {\n cause: ValidationCause\n validate: T\n}\n\n/**\n * @private\n */\nexport function getSyncValidatorArray<T>(\n cause: ValidationCause,\n options: SyncValidatorArrayPartialOptions<T>,\n): T extends FieldValidators<any, any>\n ? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit']>>\n : T extends FormValidators<any, any>\n ? Array<SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit']>>\n : never {\n const { onChange, onBlur, onSubmit } = (options.validators || {}) as\n | FieldValidators<any, any>\n | FormValidators<any, any>\n\n const changeValidator = { cause: 'change', validate: onChange } as const\n const blurValidator = { cause: 'blur', validate: onBlur } as const\n const submitValidator = { cause: 'submit', validate: onSubmit } as const\n\n // Allows us to clear onServer errors\n const serverValidator = {\n cause: 'server',\n validate: () => undefined,\n } as const\n\n switch (cause) {\n case 'submit':\n return [\n changeValidator,\n blurValidator,\n submitValidator,\n serverValidator,\n ] as never\n case 'server':\n return [serverValidator] as never\n case 'blur':\n return [blurValidator, serverValidator] as never\n case 'change':\n default:\n return [changeValidator, serverValidator] as never\n }\n}\n"],"names":[],"mappings":";;AAagB,SAAA,iBACd,SACA,OACS;AACT,SAAO,OAAO,YAAY,aACrB,QAAuC,KAAK,IAC7C;AACN;AAMgB,SAAA,MAAM,KAAU,MAAW;AACnC,QAAA,UAAU,cAAc,IAAI;AAClC,SAAO,QAAQ,OAAO,CAAC,SAAc,aAAkB;AACrD,QAAI,YAAY;AAAa,aAAA;AACzB,QAAA,OAAO,YAAY,aAAa;AAClC,aAAO,QAAQ,QAAQ;AAAA,IACzB;AACO,WAAA;AAAA,KACN,GAAG;AACR;AAMgB,SAAA,MAAM,KAAU,OAAY,SAAuB;AAC3D,QAAA,OAAO,cAAc,KAAK;AAEhC,WAAS,MAAM,QAAmB;AAC5B,QAAA,CAAC,KAAK,QAAQ;AACT,aAAA,iBAAiB,SAAS,MAAM;AAAA,IACzC;AAEM,UAAA,MAAM,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,eAAe,eAAe;AAAA,IACxC,KAAK;AAAA,IACL;AACS,aAAA,CAAC,iBAAiB,eAAe;AAAA,EAC5C;AACF;;;;;;;;;"}
@@ -4,37 +4,61 @@ import { FieldValidators } from './FieldApi.cjs';
4
4
 
5
5
  export type UpdaterFn<TInput, TOutput = TInput> = (input: TInput) => TOutput;
6
6
  export type Updater<TInput, TOutput = TInput> = TOutput | UpdaterFn<TInput, TOutput>;
7
+ /**
8
+ * @private
9
+ */
7
10
  export declare function functionalUpdate<TInput, TOutput = TInput>(updater: Updater<TInput, TOutput>, input: TInput): TOutput;
8
11
  /**
9
12
  * Get a value from an object using a path, including dot notation.
13
+ * @private
10
14
  */
11
15
  export declare function getBy(obj: any, path: any): any;
12
16
  /**
13
17
  * Set a value on an object using a path, including dot notation.
18
+ * @private
14
19
  */
15
20
  export declare function setBy(obj: any, _path: any, updater: Updater<any>): any;
16
21
  /**
17
22
  * Delete a field on an object using a path, including dot notation.
23
+ * @private
18
24
  */
19
25
  export declare function deleteBy(obj: any, _path: any): any;
26
+ /**
27
+ * @private
28
+ */
20
29
  export declare function makePathArray(str: string): (string | number)[];
30
+ /**
31
+ * @private
32
+ */
21
33
  export declare function isNonEmptyArray(obj: any): boolean;
22
34
  interface AsyncValidatorArrayPartialOptions<T> {
23
35
  validators?: T;
24
36
  asyncDebounceMs?: number;
25
37
  }
38
+ /**
39
+ * @private
40
+ */
26
41
  export interface AsyncValidator<T> {
27
42
  cause: ValidationCause;
28
43
  validate: T;
29
44
  debounceMs: number;
30
45
  }
46
+ /**
47
+ * @private
48
+ */
31
49
  export declare function getAsyncValidatorArray<T>(cause: ValidationCause, options: AsyncValidatorArrayPartialOptions<T>): T extends FieldValidators<any, any> ? Array<AsyncValidator<T['onChangeAsync'] | T['onBlurAsync'] | T['onSubmitAsync']>> : T extends FormValidators<any, any> ? Array<AsyncValidator<T['onChangeAsync'] | T['onBlurAsync'] | T['onSubmitAsync']>> : never;
32
50
  interface SyncValidatorArrayPartialOptions<T> {
33
51
  validators?: T;
34
52
  }
53
+ /**
54
+ * @private
55
+ */
35
56
  export interface SyncValidator<T> {
36
57
  cause: ValidationCause;
37
58
  validate: T;
38
59
  }
60
+ /**
61
+ * @private
62
+ */
39
63
  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;
40
64
  export {};
@@ -4,65 +4,224 @@ import { ValidationCause, ValidationError, ValidationErrorMap, Validator } from
4
4
  import { Updater } from './utils.js';
5
5
  import { DeepKeys, DeepValue, NoInfer } from './util-types.js';
6
6
 
7
+ /**
8
+ * @private
9
+ */
7
10
  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: {
8
11
  value: TData;
9
12
  fieldApi: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
10
13
  }) => ValidationError;
14
+ /**
15
+ * @private
16
+ */
11
17
  export type FieldValidateOrFn<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>> = TFieldValidator extends Validator<TData, infer TFN> ? TFN | FieldValidateFn<TParentData, TName, TFieldValidator, TFormValidator, TData> : TFormValidator extends Validator<TParentData, infer FFN> ? FFN | FieldValidateFn<TParentData, TName, TFieldValidator, TFormValidator, TData> : FieldValidateFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
18
+ /**
19
+ * @private
20
+ */
12
21
  export type FieldValidateAsyncFn<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>> = (options: {
13
22
  value: TData;
14
23
  fieldApi: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
15
24
  signal: AbortSignal;
16
25
  }) => ValidationError | Promise<ValidationError>;
26
+ /**
27
+ * @private
28
+ */
17
29
  export type FieldAsyncValidateOrFn<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>> = TFieldValidator extends Validator<TData, infer TFN> ? TFN | FieldValidateAsyncFn<TParentData, TName, TFieldValidator, TFormValidator, TData> : TFormValidator extends Validator<TParentData, infer FFN> ? FFN | FieldValidateAsyncFn<TParentData, TName, TFieldValidator, TFormValidator, TData> : FieldValidateAsyncFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
18
30
  export interface FieldValidators<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>> {
31
+ /**
32
+ * An optional function that takes a param of `formApi` which is a generic type of `TData` and `TParentData`
33
+ */
19
34
  onMount?: FieldValidateOrFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
35
+ /**
36
+ * An optional property that takes a `ValidateFn` which is a generic of `TData` and `TParentData`.
37
+ * If `validatorAdapter` is passed, this may also accept a property from the respective adapter
38
+ *
39
+ * @example `z.string().min(1)` if `zodAdapter` is passed
40
+ */
20
41
  onChange?: FieldValidateOrFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
42
+ /**
43
+ * An optional property similar to `onChange` but async validation. If `validatorAdapter`
44
+ * is passed, this may also accept a property from the respective adapter
45
+ *
46
+ * @example `z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })` if `zodAdapter` is passed
47
+ */
21
48
  onChangeAsync?: FieldAsyncValidateOrFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
49
+ /**
50
+ * An optional number to represent how long the `onChangeAsync` should wait before running
51
+ *
52
+ * If set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds
53
+ */
22
54
  onChangeAsyncDebounceMs?: number;
55
+ /**
56
+ * An optional list of field names that should trigger this field's `onChange` and `onChangeAsync` events when its value changes
57
+ */
23
58
  onChangeListenTo?: DeepKeys<TParentData>[];
59
+ /**
60
+ * An optional function, that when run when subscribing to blur event of input.
61
+ * If `validatorAdapter` is passed, this may also accept a property from the respective adapter
62
+ *
63
+ * @example `z.string().min(1)` if `zodAdapter` is passed
64
+ */
24
65
  onBlur?: FieldValidateOrFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
66
+ /**
67
+ * An optional property similar to `onBlur` but async validation. If `validatorAdapter`
68
+ * is passed, this may also accept a property from the respective adapter
69
+ *
70
+ * @example `z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })` if `zodAdapter` is passed
71
+ */
25
72
  onBlurAsync?: FieldAsyncValidateOrFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
73
+ /**
74
+ * An optional number to represent how long the `onBlurAsync` should wait before running
75
+ *
76
+ * If set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds
77
+ */
26
78
  onBlurAsyncDebounceMs?: number;
79
+ /**
80
+ * An optional list of field names that should trigger this field's `onBlur` and `onBlurAsync` events when its value changes
81
+ */
27
82
  onBlurListenTo?: DeepKeys<TParentData>[];
83
+ /**
84
+ * An optional function, that when run when subscribing to submit event of input.
85
+ * If `validatorAdapter` is passed, this may also accept a property from the respective adapter
86
+ *
87
+ * @example `z.string().min(1)` if `zodAdapter` is passed
88
+ */
28
89
  onSubmit?: FieldValidateOrFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
90
+ /**
91
+ * An optional property similar to `onSubmit` but async validation. If `validatorAdapter`
92
+ * is passed, this may also accept a property from the respective adapter
93
+ *
94
+ * @example `z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })` if `zodAdapter` is passed
95
+ */
29
96
  onSubmitAsync?: FieldAsyncValidateOrFn<TParentData, TName, TFieldValidator, TFormValidator, TData>;
30
97
  }
98
+ /**
99
+ * An object type representing the options for a field in a form.
100
+ */
31
101
  export interface FieldOptions<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>> {
102
+ /**
103
+ * The field name. The type will be `DeepKeys<TParentData>` to ensure your name is a deep key of the parent dataset.
104
+ */
32
105
  name: TName;
106
+ /**
107
+ * An optional default value for the field.
108
+ */
33
109
  defaultValue?: NoInfer<TData>;
110
+ /**
111
+ * The default time to debounce async validation if there is not a more specific debounce time passed.
112
+ */
34
113
  asyncDebounceMs?: number;
114
+ /**
115
+ * If `true`, always run async validation, even if there are errors emitted during synchronous validation.
116
+ */
35
117
  asyncAlways?: boolean;
36
118
  preserveValue?: boolean;
119
+ /**
120
+ * A validator provided by an extension, like `yupValidator` from `@tanstack/yup-form-adapter`
121
+ */
37
122
  validatorAdapter?: TFieldValidator;
123
+ /**
124
+ * A list of validators to pass to the field
125
+ */
38
126
  validators?: FieldValidators<TParentData, TName, TFieldValidator, TFormValidator, TData>;
127
+ /**
128
+ * An optional object with default metadata for the field.
129
+ */
39
130
  defaultMeta?: Partial<FieldMeta>;
40
131
  }
132
+ /**
133
+ * An object type representing the required options for the FieldApi class.
134
+ */
41
135
  export interface FieldApiOptions<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>> extends FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> {
42
136
  form: FormApi<TParentData, TFormValidator>;
43
137
  }
138
+ /**
139
+ * An object type representing the metadata of a field in a form.
140
+ */
44
141
  export type FieldMeta = {
142
+ /**
143
+ * A flag indicating whether the field has been touched.
144
+ */
45
145
  isTouched: boolean;
146
+ /**
147
+ * A flag that is `true` if the field's value has not been modified by the user. Opposite of `isDirty`.
148
+ */
46
149
  isPristine: boolean;
150
+ /**
151
+ * A flag that is `true` if the field's value has been modified by the user. Opposite of `isPristine`.
152
+ */
47
153
  isDirty: boolean;
154
+ /**
155
+ * An array of errors related to the touched state of the field.
156
+ */
48
157
  touchedErrors: ValidationError[];
158
+ /**
159
+ * An array of errors related to the field value.
160
+ */
49
161
  errors: ValidationError[];
162
+ /**
163
+ * A map of errors related to the field value.
164
+ */
50
165
  errorMap: ValidationErrorMap;
166
+ /**
167
+ * A flag indicating whether the field is currently being validated.
168
+ */
51
169
  isValidating: boolean;
52
170
  };
171
+ /**
172
+ * An object type representing the state of a field.
173
+ */
53
174
  export type FieldState<TData> = {
175
+ /**
176
+ * The current value of the field.
177
+ */
54
178
  value: TData;
179
+ /**
180
+ * The current metadata of the field.
181
+ */
55
182
  meta: FieldMeta;
56
183
  };
57
- export type ResolveName<TParentData> = unknown extends TParentData ? string : DeepKeys<TParentData>;
184
+ /**
185
+ * A class representing the API for managing a form field.
186
+ *
187
+ * Normally, you will not need to create a new `FieldApi` instance directly.
188
+ * Instead, you will use a framework hook/function like `useField` or `createField`
189
+ * to create a new instance for you that uses your framework's reactivity model.
190
+ * However, if you need to create a new instance manually, you can do so by calling
191
+ * the `new FieldApi` constructor.
192
+ */
58
193
  export declare class FieldApi<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>> {
194
+ /**
195
+ * A reference to the form API instance.
196
+ */
59
197
  form: FieldApiOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>['form'];
198
+ /**
199
+ * The field name.
200
+ */
60
201
  name: DeepKeys<TParentData>;
202
+ /**
203
+ * The field options.
204
+ */
61
205
  options: FieldApiOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>;
206
+ /**
207
+ * The field state store.
208
+ */
62
209
  store: Store<FieldState<TData>>;
210
+ /**
211
+ * The current field state.
212
+ */
63
213
  state: FieldState<TData>;
214
+ /**
215
+ * @private
216
+ */
64
217
  prevState: FieldState<TData>;
218
+ /**
219
+ * Initializes a new `FieldApi` instance.
220
+ */
65
221
  constructor(opts: FieldApiOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>);
222
+ /**
223
+ * @private
224
+ */
66
225
  runValidator<TValue extends {
67
226
  value: TData;
68
227
  fieldApi: FieldApi<any, any, any, any>;
@@ -71,41 +230,101 @@ export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>,
71
230
  value: TValue;
72
231
  type: TType;
73
232
  }): ReturnType<ReturnType<Validator<any>>[TType]>;
233
+ /**
234
+ * Mounts the field instance to the form.
235
+ */
74
236
  mount: () => () => void;
237
+ /**
238
+ * Updates the field instance with new options.
239
+ */
75
240
  update: (opts: FieldApiOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>) => void;
241
+ /**
242
+ * Gets the current field value.
243
+ */
76
244
  getValue: () => TData;
245
+ /**
246
+ * Sets the field value and run the `change` validator.
247
+ */
77
248
  setValue: (updater: Updater<TData>, options?: {
78
249
  touch?: boolean;
79
250
  notify?: boolean;
80
251
  }) => void;
252
+ /**
253
+ * @private
254
+ */
81
255
  _getMeta: () => FieldMeta | undefined;
256
+ /**
257
+ * Gets the current field metadata.
258
+ */
82
259
  getMeta: () => FieldMeta;
260
+ /**
261
+ * Sets the field metadata.
262
+ */
83
263
  setMeta: (updater: Updater<FieldMeta>) => void;
264
+ /**
265
+ * Gets the field information object.
266
+ */
84
267
  getInfo: () => FieldInfo<TParentData, TFormValidator>;
268
+ /**
269
+ * Pushes a new value to the field.
270
+ */
85
271
  pushValue: (value: TData extends any[] ? TData[number] : never, opts?: {
86
272
  touch?: boolean;
87
273
  }) => void;
274
+ /**
275
+ * Inserts a value at the specified index, shifting the subsequent values to the right.
276
+ */
88
277
  insertValue: (index: number, value: TData extends any[] ? TData[number] : never, opts?: {
89
278
  touch?: boolean;
90
279
  }) => Promise<void>;
280
+ /**
281
+ * Replaces a value at the specified index.
282
+ */
91
283
  replaceValue: (index: number, value: TData extends any[] ? TData[number] : never, opts?: {
92
284
  touch?: boolean;
93
285
  }) => Promise<void>;
286
+ /**
287
+ * Removes a value at the specified index.
288
+ */
94
289
  removeValue: (index: number, opts?: {
95
290
  touch: boolean;
96
291
  }) => Promise<void>;
292
+ /**
293
+ * Swaps the values at the specified indices.
294
+ */
97
295
  swapValues: (aIndex: number, bIndex: number, opts?: {
98
296
  touch?: boolean;
99
297
  }) => void;
298
+ /**
299
+ * Moves the value at the first specified index to the second specified index.
300
+ */
100
301
  moveValue: (aIndex: number, bIndex: number, opts?: {
101
302
  touch?: boolean;
102
303
  }) => void;
304
+ /**
305
+ * @private
306
+ */
103
307
  getLinkedFields: (cause: ValidationCause) => FieldApi<any, any, any, any, any>[];
308
+ /**
309
+ * @private
310
+ */
104
311
  validateSync: (cause: ValidationCause) => {
105
312
  hasErrored: boolean;
106
313
  };
314
+ /**
315
+ * @private
316
+ */
107
317
  validateAsync: (cause: ValidationCause) => Promise<ValidationError[]>;
318
+ /**
319
+ * Validates the field value.
320
+ */
108
321
  validate: (cause: ValidationCause) => ValidationError[] | Promise<ValidationError[]>;
322
+ /**
323
+ * Handles the change event.
324
+ */
109
325
  handleChange: (updater: Updater<TData>) => void;
326
+ /**
327
+ * Handles the blur event.
328
+ */
110
329
  handleBlur: () => void;
111
330
  }
@@ -1,6 +1,9 @@
1
1
  import { Store } from "@tanstack/store";
2
2
  import { getBy, getSyncValidatorArray, getAsyncValidatorArray } from "./utils.js";
3
3
  class FieldApi {
4
+ /**
5
+ * Initializes a new `FieldApi` instance.
6
+ */
4
7
  constructor(opts) {
5
8
  this.options = {};
6
9
  this.mount = () => {
@@ -57,6 +60,9 @@ class FieldApi {
57
60
  if (this._getMeta() === void 0) {
58
61
  this.setMeta(this.state.meta);
59
62
  }
63
+ if (opts2.defaultValue === void 0 && this.form._tempDefaultValue !== void 0 && this.form._tempDefaultValue.field === this.name) {
64
+ opts2.defaultValue = this.form._tempDefaultValue.value;
65
+ }
60
66
  this.options = opts2;
61
67
  };
62
68
  this.getValue = () => {
@@ -284,6 +290,9 @@ class FieldApi {
284
290
  };
285
291
  this.form = opts.form;
286
292
  this.name = opts.name;
293
+ if (opts.defaultValue === void 0 && this.form._tempDefaultValue !== void 0 && this.form._tempDefaultValue.field === this.name) {
294
+ opts.defaultValue = this.form._tempDefaultValue.value;
295
+ }
287
296
  if (opts.defaultValue !== void 0) {
288
297
  this.form.setFieldValue(this.name, opts.defaultValue);
289
298
  }
@@ -311,6 +320,11 @@ class FieldApi {
311
320
  state.meta.isPristine = !state.meta.isDirty;
312
321
  this.prevState = state;
313
322
  this.state = state;
323
+ setTimeout(() => {
324
+ if (this.form._tempDefaultValue !== void 0 && this.form._tempDefaultValue.field === this.name) {
325
+ this.form._tempDefaultValue = void 0;
326
+ }
327
+ }, 0);
314
328
  }
315
329
  }
316
330
  );
@@ -318,6 +332,9 @@ class FieldApi {
318
332
  this.prevState = this.state;
319
333
  this.options = opts;
320
334
  }
335
+ /**
336
+ * @private
337
+ */
321
338
  runValidator(props) {
322
339
  const adapters = [
323
340
  this.form.options.validatorAdapter,