Skip to content

Commit 4725028

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

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

packages/form-core/src/FieldGroupApi.ts

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

459459
validateAllFields = (cause: ValidationCause) =>
460460
this.form.validateAllFields(cause)
461+
462+
/**
463+
* Remaps field validator listener paths to their full form paths.
464+
*/
465+
remapFieldProps = <TProps extends { validators?: any }>(
466+
props: TProps,
467+
): TProps => {
468+
const newProps = { ...props }
469+
const validators = newProps.validators
470+
471+
if (
472+
validators &&
473+
(validators.onChangeListenTo || validators.onBlurListenTo)
474+
) {
475+
const newValidators = { ...validators }
476+
477+
const remapListenTo = (listenTo: DeepKeys<any>[] | undefined) => {
478+
if (!listenTo) return undefined
479+
return listenTo.map((localFieldName) =>
480+
this.getFormFieldName(localFieldName),
481+
)
482+
}
483+
484+
if (newValidators.onChangeListenTo) {
485+
newValidators.onChangeListenTo = remapListenTo(
486+
newValidators.onChangeListenTo,
487+
)
488+
}
489+
if (newValidators.onBlurListenTo) {
490+
newValidators.onBlurListenTo = remapListenTo(
491+
newValidators.onBlurListenTo,
492+
)
493+
}
494+
495+
newProps.validators = newValidators
496+
}
497+
498+
return newProps
499+
}
461500
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,4 +919,57 @@ 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+
account: {
927+
password: '',
928+
confirmPassword: '',
929+
},
930+
userPassword: '',
931+
userConfirmPassword: '',
932+
},
933+
})
934+
form.mount()
935+
936+
const fieldGroupString = new FieldGroupApi({
937+
form,
938+
fields: 'account',
939+
defaultValues: { password: '', confirmPassword: '' },
940+
})
941+
fieldGroupString.mount()
942+
943+
const props1 = {
944+
validators: {
945+
onChangeListenTo: ['password'],
946+
onBlurListenTo: ['confirmPassword'],
947+
},
948+
}
949+
const remappedProps1 = fieldGroupString.remapFieldProps(props1)
950+
expect(remappedProps1.validators.onChangeListenTo).toEqual([
951+
'account.password',
952+
])
953+
expect(remappedProps1.validators.onBlurListenTo).toEqual([
954+
'account.confirmPassword',
955+
])
956+
957+
const fieldGroupObject = new FieldGroupApi({
958+
form,
959+
fields: {
960+
password: 'userPassword',
961+
confirmPassword: 'userConfirmPassword',
962+
},
963+
defaultValues: { password: '', confirmPassword: '' },
964+
})
965+
fieldGroupObject.mount()
966+
967+
const props2 = {
968+
validators: {
969+
onChangeListenTo: ['password'],
970+
},
971+
}
972+
const remappedProps2 = fieldGroupObject.remapFieldProps(props2)
973+
expect(remappedProps2.validators.onChangeListenTo).toEqual(['userPassword'])
974+
})
922975
})

packages/react-form/src/useFieldGroup.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export function useFieldGroup<
198198
return (
199199
<form.AppField
200200
name={formLensApi.getFormFieldName(name)}
201-
{...(appFieldProps as any)}
201+
{...formLensApi.remapFieldProps(appFieldProps as any)}
202202
/>
203203
) as never
204204
}
@@ -207,7 +207,7 @@ export function useFieldGroup<
207207
return (
208208
<form.Field
209209
name={formLensApi.getFormFieldName(name)}
210-
{...(fieldProps as any)}
210+
{...formLensApi.remapFieldProps(fieldProps as any)}
211211
/>
212212
) as never
213213
}

0 commit comments

Comments
 (0)