Skip to content

Commit 9724dbe

Browse files
committed
fix(react-form): remap listener paths in withFieldGroup
1 parent d8053b0 commit 9724dbe

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

packages/react-form/src/useFieldGroup.tsx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FieldGroupApi, functionalUpdate } from '@tanstack/form-core'
44
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
55
import type {
66
AnyFieldGroupApi,
7+
DeepKeys,
78
DeepKeysOfType,
89
FieldGroupState,
910
FieldsMap,
@@ -194,11 +195,46 @@ export function useFieldGroup<
194195
return <form.AppForm {...appFormProps} />
195196
}
196197

198+
const remapProps = (fieldProps: Record<string, any>) => {
199+
const newProps = { ...fieldProps }
200+
const validators = newProps.validators
201+
202+
if (
203+
validators &&
204+
(validators.onChangeListenTo || validators.onBlurListenTo)
205+
) {
206+
const newValidators = { ...validators }
207+
208+
const remapListenTo = (listenTo: DeepKeys<any>[] | undefined) => {
209+
if (!listenTo) return undefined
210+
return listenTo.map((localFieldName) =>
211+
formLensApi.getFormFieldName(localFieldName),
212+
)
213+
}
214+
215+
if (newValidators.onChangeListenTo) {
216+
newValidators.onChangeListenTo = remapListenTo(
217+
newValidators.onChangeListenTo,
218+
)
219+
}
220+
221+
if (newValidators.onBlurListenTo) {
222+
newValidators.onBlurListenTo = remapListenTo(
223+
newValidators.onBlurListenTo,
224+
)
225+
}
226+
227+
newProps.validators = newValidators
228+
}
229+
230+
return newProps
231+
}
232+
197233
extendedApi.AppField = function AppField({ name, ...appFieldProps }) {
198234
return (
199235
<form.AppField
200236
name={formLensApi.getFormFieldName(name)}
201-
{...(appFieldProps as any)}
237+
{...(remapProps(appFieldProps) as any)}
202238
/>
203239
) as never
204240
}
@@ -207,7 +243,7 @@ export function useFieldGroup<
207243
return (
208244
<form.Field
209245
name={formLensApi.getFormFieldName(name)}
210-
{...(fieldProps as any)}
246+
{...(remapProps(fieldProps) as any)}
211247
/>
212248
) as never
213249
}

0 commit comments

Comments
 (0)