@tanstack/form-core
Advanced tools
Comparing version 0.36.2 to 0.37.0
@@ -29,2 +29,9 @@ import { Store } from '@tanstack/store'; | ||
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>; | ||
/** | ||
* @private | ||
*/ | ||
export type FieldListenerFn<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: { | ||
value: TData; | ||
fieldApi: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>; | ||
}) => void; | ||
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>> { | ||
@@ -98,2 +105,8 @@ /** | ||
} | ||
export interface FieldListeners<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>> { | ||
onChange?: FieldListenerFn<TParentData, TName, TFieldValidator, TFormValidator, TData>; | ||
onBlur?: FieldListenerFn<TParentData, TName, TFieldValidator, TFormValidator, TData>; | ||
onMount?: FieldListenerFn<TParentData, TName, TFieldValidator, TFormValidator, TData>; | ||
onSubmit?: FieldListenerFn<TParentData, TName, TFieldValidator, TFormValidator, TData>; | ||
} | ||
/** | ||
@@ -131,2 +144,6 @@ * An object type representing the options for a field in a form. | ||
defaultMeta?: Partial<FieldMeta>; | ||
/** | ||
* A list of listeners which attach to the corresponding events | ||
*/ | ||
listeners?: FieldListeners<TParentData, TName, TFieldValidator, TFormValidator, TData>; | ||
} | ||
@@ -133,0 +150,0 @@ /** |
@@ -10,2 +10,3 @@ import { Store } from "@tanstack/store"; | ||
this.mount = () => { | ||
var _a, _b; | ||
const info = this.getInfo(); | ||
@@ -45,2 +46,6 @@ info.instance = this; | ||
} | ||
(_b = (_a = this.options.listeners) == null ? void 0 : _a.onMount) == null ? void 0 : _b.call(_a, { | ||
value: this.state.value, | ||
fieldApi: this | ||
}); | ||
return () => { | ||
@@ -72,3 +77,8 @@ unsubscribe(); | ||
this.setValue = (updater, options) => { | ||
var _a, _b; | ||
this.form.setFieldValue(this.name, updater, options); | ||
(_b = (_a = this.options.listeners) == null ? void 0 : _a.onChange) == null ? void 0 : _b.call(_a, { | ||
value: this.state.value, | ||
fieldApi: this | ||
}); | ||
this.validate("change"); | ||
@@ -304,2 +314,3 @@ }; | ||
this.handleBlur = () => { | ||
var _a, _b; | ||
const prevTouched = this.state.meta.isTouched; | ||
@@ -314,2 +325,6 @@ if (!prevTouched) { | ||
this.validate("blur"); | ||
(_b = (_a = this.options.listeners) == null ? void 0 : _a.onBlur) == null ? void 0 : _b.call(_a, { | ||
value: this.state.value, | ||
fieldApi: this | ||
}); | ||
}; | ||
@@ -316,0 +331,0 @@ this.form = opts.form; |
@@ -352,2 +352,11 @@ import { Store } from "@tanstack/store"; | ||
} | ||
this.store.batch(() => { | ||
void Object.values(this.fieldInfo).forEach((field) => { | ||
var _a3, _b2, _c2; | ||
(_c2 = (_b2 = (_a3 = field.instance) == null ? void 0 : _a3.options.listeners) == null ? void 0 : _b2.onSubmit) == null ? void 0 : _c2.call(_b2, { | ||
value: field.instance.state.value, | ||
fieldApi: field.instance | ||
}); | ||
}); | ||
}); | ||
try { | ||
@@ -354,0 +363,0 @@ await ((_d = (_c = this.options).onSubmit) == null ? void 0 : _d.call(_c, { value: this.state.values, formApi: this })); |
{ | ||
"name": "@tanstack/form-core", | ||
"version": "0.36.2", | ||
"version": "0.37.0", | ||
"description": "Powerful, type-safe, framework agnostic forms.", | ||
@@ -5,0 +5,0 @@ "author": "tannerlinsley", |
@@ -136,2 +136,20 @@ import { Store } from '@tanstack/store' | ||
/** | ||
* @private | ||
*/ | ||
export type FieldListenerFn< | ||
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: { | ||
value: TData | ||
fieldApi: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData> | ||
}) => void | ||
export interface FieldValidators< | ||
@@ -259,2 +277,43 @@ TParentData, | ||
export interface FieldListeners< | ||
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>, | ||
> { | ||
onChange?: FieldListenerFn< | ||
TParentData, | ||
TName, | ||
TFieldValidator, | ||
TFormValidator, | ||
TData | ||
> | ||
onBlur?: FieldListenerFn< | ||
TParentData, | ||
TName, | ||
TFieldValidator, | ||
TFormValidator, | ||
TData | ||
> | ||
onMount?: FieldListenerFn< | ||
TParentData, | ||
TName, | ||
TFieldValidator, | ||
TFormValidator, | ||
TData | ||
> | ||
onSubmit?: FieldListenerFn< | ||
TParentData, | ||
TName, | ||
TFieldValidator, | ||
TFormValidator, | ||
TData | ||
> | ||
} | ||
/** | ||
@@ -308,2 +367,12 @@ * An object type representing the options for a field in a form. | ||
defaultMeta?: Partial<FieldMeta> | ||
/** | ||
* A list of listeners which attach to the corresponding events | ||
*/ | ||
listeners?: FieldListeners< | ||
TParentData, | ||
TName, | ||
TFieldValidator, | ||
TFormValidator, | ||
TData | ||
> | ||
} | ||
@@ -574,2 +643,7 @@ | ||
this.options.listeners?.onMount?.({ | ||
value: this.state.value, | ||
fieldApi: this, | ||
}) | ||
return () => { | ||
@@ -629,2 +703,8 @@ unsubscribe() | ||
this.form.setFieldValue(this.name, updater as never, options) | ||
this.options.listeners?.onChange?.({ | ||
value: this.state.value, | ||
fieldApi: this, | ||
}) | ||
this.validate('change') | ||
@@ -1025,2 +1105,7 @@ } | ||
this.validate('blur') | ||
this.options.listeners?.onBlur?.({ | ||
value: this.state.value, | ||
fieldApi: this, | ||
}) | ||
} | ||
@@ -1027,0 +1112,0 @@ |
@@ -983,2 +983,13 @@ import { Store } from '@tanstack/store' | ||
this.store.batch(() => { | ||
void ( | ||
Object.values(this.fieldInfo) as FieldInfo<TFormData, TFormValidator>[] | ||
).forEach((field) => { | ||
field.instance?.options.listeners?.onSubmit?.({ | ||
value: field.instance.state.value, | ||
fieldApi: field.instance, | ||
}) | ||
}) | ||
}) | ||
try { | ||
@@ -985,0 +996,0 @@ // Run the submit code |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
485458
6398