Skip to content

Commit 3b080ec

Browse files
fix: React adapter behavior has now been fixed (#1903)
* fix: React adapter behavior has now been fixed Closes #1901 * chore: add changeset * ci: apply automated fixes and generate docs * fix: handleSubmit handled too --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 92896df commit 3b080ec

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

.changeset/wicked-ravens-shout.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@tanstack/react-form': patch
3+
'@tanstack/form-core': patch
4+
---
5+
6+
Fix issues with methods not being present in React adapter

packages/form-core/src/FieldApi.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,11 @@ export type AnyFieldApi = FieldApi<
962962
any
963963
>
964964

965+
/**
966+
* We cannot use methods and must use arrow functions. Otherwise, our React adapters
967+
* will break due to loss of the method when using spread.
968+
*/
969+
965970
/**
966971
* A class representing the API for managing a form field.
967972
*
@@ -1908,7 +1913,7 @@ export class FieldApi<
19081913
/**
19091914
* Updates the field's errorMap
19101915
*/
1911-
setErrorMap(
1916+
setErrorMap = (
19121917
errorMap: ValidationErrorMap<
19131918
UnwrapFieldValidateOrFn<TName, TOnMount, TFormOnMount>,
19141919
UnwrapFieldValidateOrFn<TName, TOnChange, TFormOnChange>,
@@ -1920,7 +1925,7 @@ export class FieldApi<
19201925
UnwrapFieldValidateOrFn<TName, TOnDynamic, TFormOnDynamic>,
19211926
UnwrapFieldAsyncValidateOrFn<TName, TOnDynamicAsync, TFormOnDynamicAsync>
19221927
>,
1923-
) {
1928+
) => {
19241929
this.setMeta((prev) => ({
19251930
...prev,
19261931
errorMap: {
@@ -1997,7 +2002,7 @@ export class FieldApi<
19972002
/**
19982003
* @private
19992004
*/
2000-
triggerOnChangeListener() {
2005+
triggerOnChangeListener = () => {
20012006
const formDebounceMs = this.form.options.listeners?.onChangeDebounceMs
20022007
if (formDebounceMs && formDebounceMs > 0) {
20032008
if (this.timeoutIds.formListeners.change) {

packages/form-core/src/FormApi.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,11 @@ export type AnyFormApi = FormApi<
870870
any
871871
>
872872

873+
/**
874+
* We cannot use methods and must use arrow functions. Otherwise, our React adapters
875+
* will break due to loss of the method when using spread.
876+
*/
877+
873878
/**
874879
* A class representing the Form API. It handles the logic and interactions with the form state.
875880
*
@@ -2016,12 +2021,17 @@ export class FormApi<
20162021
return this.validateAsync(cause)
20172022
}
20182023

2024+
// Needs to edgecase in the React adapter specifically to avoid type errors
2025+
handleSubmit(): Promise<void>
2026+
handleSubmit(submitMeta: TSubmitMeta): Promise<void>
2027+
handleSubmit(submitMeta?: TSubmitMeta): Promise<void> {
2028+
return this._handleSubmit(submitMeta)
2029+
}
2030+
20192031
/**
20202032
* Handles the form submission, performs validation, and calls the appropriate onSubmit or onSubmitInvalid callbacks.
20212033
*/
2022-
handleSubmit(): Promise<void>
2023-
handleSubmit(submitMeta: TSubmitMeta): Promise<void>
2024-
async handleSubmit(submitMeta?: TSubmitMeta): Promise<void> {
2034+
_handleSubmit = async (submitMeta?: TSubmitMeta): Promise<void> => {
20252035
this.baseStore.setState((old) => ({
20262036
...old,
20272037
// Submission attempts mark the form as not submitted
@@ -2539,7 +2549,7 @@ export class FormApi<
25392549
/**
25402550
* Updates the form's errorMap
25412551
*/
2542-
setErrorMap(
2552+
setErrorMap = (
25432553
errorMap: FormValidationErrorMap<
25442554
TFormData,
25452555
UnwrapFormValidateOrFn<TOnMount>,
@@ -2553,7 +2563,7 @@ export class FormApi<
25532563
UnwrapFormAsyncValidateOrFn<TOnDynamicAsync>,
25542564
UnwrapFormAsyncValidateOrFn<TOnServer>
25552565
>,
2556-
) {
2566+
) => {
25572567
batch(() => {
25582568
Object.entries(errorMap).forEach(([key, value]) => {
25592569
const errorMapKey = key as ValidationErrorMapKeys

packages/react-form/src/useForm.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ export function useForm<
226226
TSubmitMeta
227227
> = {
228228
...formApi,
229+
handleSubmit: ((...props: never[]) => {
230+
formApi._handleSubmit(...props)
231+
}) as typeof formApi.handleSubmit,
229232
// We must add all `get`ters from `core`'s `FormApi` here, as otherwise the spread operator won't catch those
230233
get formId(): string {
231234
return formApi._formId

0 commit comments

Comments
 (0)