Skip to content

Commit 1d5ce01

Browse files
committed
fix(react-form): remap listener paths in withFieldGroup
1 parent 1e58e57 commit 1d5ce01

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

packages/form-core/src/FieldGroupApi.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,43 @@ export class FieldGroupApi<
472472

473473
validateAllFields = (cause: ValidationCause) =>
474474
this.form.validateAllFields(cause)
475+
476+
/**
477+
* Remaps field validator listener paths to their full form paths.
478+
*/
479+
remapFieldProps = <TProps extends { validators?: any }>(
480+
props: TProps,
481+
): TProps => {
482+
const newProps = { ...props }
483+
const validators = newProps.validators
484+
485+
if (
486+
validators &&
487+
(validators.onChangeListenTo || validators.onBlurListenTo)
488+
) {
489+
const newValidators = { ...validators }
490+
491+
const remapListenTo = (listenTo: DeepKeys<any>[] | undefined) => {
492+
if (!listenTo) return undefined
493+
return listenTo.map((localFieldName) =>
494+
this.getFormFieldName(localFieldName),
495+
)
496+
}
497+
498+
if (newValidators.onChangeListenTo) {
499+
newValidators.onChangeListenTo = remapListenTo(
500+
newValidators.onChangeListenTo,
501+
)
502+
}
503+
if (newValidators.onBlurListenTo) {
504+
newValidators.onBlurListenTo = remapListenTo(
505+
newValidators.onBlurListenTo,
506+
)
507+
}
508+
509+
newProps.validators = newValidators
510+
}
511+
512+
return newProps
513+
}
475514
}

packages/form-core/tests/FieldGroupApi.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,4 +919,79 @@ describe('field group api', () => {
919919
'complexValue.prop1',
920920
)
921921
})
922+
923+
it('should remap listener paths with its remapFieldProps method', () => {
924+
const form = new FormApi({
925+
defaultValues: {
926+
user: {
927+
profile: {
928+
personal: {
929+
firstName: '',
930+
lastName: '',
931+
email: '',
932+
},
933+
preferences: {
934+
theme: 'light',
935+
notifications: true,
936+
},
937+
},
938+
settings: {
939+
privacy: {
940+
shareData: false,
941+
allowMarketing: true,
942+
},
943+
},
944+
},
945+
alternateProfile: {
946+
firstName: '',
947+
lastName: '',
948+
email: '',
949+
},
950+
},
951+
})
952+
form.mount()
953+
954+
const fieldGroupString = new FieldGroupApi({
955+
form,
956+
fields: 'user.profile.personal',
957+
defaultValues: { firstName: '', lastName: '', email: '' },
958+
})
959+
fieldGroupString.mount()
960+
961+
const props1 = {
962+
validators: {
963+
onChangeListenTo: ['firstName'],
964+
onBlurListenTo: ['email'],
965+
},
966+
}
967+
const remappedProps1 = fieldGroupString.remapFieldProps(props1)
968+
expect(remappedProps1.validators.onChangeListenTo).toEqual([
969+
'user.profile.personal.firstName',
970+
])
971+
expect(remappedProps1.validators.onBlurListenTo).toEqual([
972+
'user.profile.personal.email',
973+
])
974+
975+
const fieldGroupObject = new FieldGroupApi({
976+
form,
977+
fields: {
978+
firstName: 'alternateProfile.firstName',
979+
lastName: 'alternateProfile.lastName',
980+
email: 'alternateProfile.email',
981+
},
982+
defaultValues: { firstName: '', lastName: '', email: '' },
983+
})
984+
fieldGroupObject.mount()
985+
986+
const props2 = {
987+
validators: {
988+
onChangeListenTo: ['firstName', 'lastName'],
989+
},
990+
}
991+
const remappedProps2 = fieldGroupObject.remapFieldProps(props2)
992+
expect(remappedProps2.validators.onChangeListenTo).toEqual([
993+
'alternateProfile.firstName',
994+
'alternateProfile.lastName',
995+
])
996+
})
922997
})

packages/react-form/src/useFieldGroup.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,17 @@ export function useFieldGroup<
214214
return (
215215
<form.AppField
216216
name={formLensApi.getFormFieldName(name)}
217-
{...(appFieldProps as any)}
217+
{...formLensApi.remapFieldProps(appFieldProps as any)}
218218
/>
219219
) as never
220220
}
221221

222222
extendedApi.Field = function Field({ name, ...fieldProps }) {
223+
console.log({ fieldProps })
223224
return (
224225
<form.Field
225226
name={formLensApi.getFormFieldName(name)}
226-
{...(fieldProps as any)}
227+
{...formLensApi.remapFieldProps(fieldProps as any)}
227228
/>
228229
) as never
229230
}

0 commit comments

Comments
 (0)