Skip to content

Commit 56f87cc

Browse files
committed
refactor: adjust types for getFormFieldOptions
1 parent 4725028 commit 56f87cc

File tree

3 files changed

+56
-53
lines changed

3 files changed

+56
-53
lines changed

packages/form-core/src/FieldGroupApi.ts

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
FormAsyncValidateOrFn,
77
FormValidateOrFn,
88
} from './FormApi'
9-
import type { AnyFieldMeta, AnyFieldMetaBase } from './FieldApi'
9+
import type { AnyFieldMetaBase, FieldOptions } from './FieldApi'
1010
import type {
1111
DeepKeys,
1212
DeepKeysOfType,
@@ -163,6 +163,53 @@ export class FieldGroupApi<
163163
return concatenatePaths(formMappedPath, restOfPath)
164164
}
165165

166+
/**
167+
* Get the field options with the true form DeepKeys for validators
168+
* @private
169+
*/
170+
getFormFieldOptions = <
171+
TOptions extends FieldOptions<
172+
any,
173+
any,
174+
any,
175+
any,
176+
any,
177+
any,
178+
any,
179+
any,
180+
any,
181+
any
182+
>,
183+
>(
184+
props: TOptions,
185+
): TOptions => {
186+
const newProps = { ...props }
187+
const validators = newProps.validators
188+
189+
if (
190+
validators &&
191+
(validators.onChangeListenTo || validators.onBlurListenTo)
192+
) {
193+
const newValidators = { ...validators }
194+
195+
const remapListenTo = (listenTo: DeepKeys<any>[] | undefined) => {
196+
if (!listenTo) return undefined
197+
return listenTo.map((localFieldName) =>
198+
this.getFormFieldName(localFieldName),
199+
)
200+
}
201+
202+
newValidators.onChangeListenTo = remapListenTo(
203+
validators.onChangeListenTo,
204+
)
205+
newValidators.onBlurListenTo = remapListenTo(validators.onBlurListenTo)
206+
207+
newProps.validators = newValidators
208+
}
209+
210+
return newProps
211+
}
212+
166213
store: Derived<FieldGroupState<TFieldGroupData>>
167214

168215
get state() {
@@ -458,43 +505,4 @@ export class FieldGroupApi<
458505

459506
validateAllFields = (cause: ValidationCause) =>
460507
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-
}
500508
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it, vi } from 'vitest'
22
import { FieldApi, FieldGroupApi, FormApi } from '../src/index'
3-
import { defaultFieldMeta } from '../src/metaHelper'
43

54
describe('field group api', () => {
65
type Person = {
@@ -941,12 +940,13 @@ describe('field group api', () => {
941940
fieldGroupString.mount()
942941

943942
const props1 = {
943+
name: 'confirmPassword',
944944
validators: {
945945
onChangeListenTo: ['password'],
946946
onBlurListenTo: ['confirmPassword'],
947947
},
948948
}
949-
const remappedProps1 = fieldGroupString.remapFieldProps(props1)
949+
const remappedProps1 = fieldGroupString.getFormFieldOptions(props1)
950950
expect(remappedProps1.validators.onChangeListenTo).toEqual([
951951
'account.password',
952952
])
@@ -965,11 +965,12 @@ describe('field group api', () => {
965965
fieldGroupObject.mount()
966966

967967
const props2 = {
968+
name: 'confirmPassword',
968969
validators: {
969970
onChangeListenTo: ['password'],
970971
},
971972
}
972-
const remappedProps2 = fieldGroupObject.remapFieldProps(props2)
973+
const remappedProps2 = fieldGroupObject.getFormFieldOptions(props2)
973974
expect(remappedProps2.validators.onChangeListenTo).toEqual(['userPassword'])
974975
})
975976
})

packages/react-form/src/useFieldGroup.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,15 @@ export function useFieldGroup<
194194
return <form.AppForm {...appFormProps} />
195195
}
196196

197-
extendedApi.AppField = function AppField({ name, ...appFieldProps }) {
197+
extendedApi.AppField = function AppField(props) {
198198
return (
199-
<form.AppField
200-
name={formLensApi.getFormFieldName(name)}
201-
{...formLensApi.remapFieldProps(appFieldProps as any)}
202-
/>
199+
<form.AppField {...(formLensApi.getFormFieldOptions(props) as any)} />
203200
) as never
204201
}
205202

206-
extendedApi.Field = function Field({ name, ...fieldProps }) {
203+
extendedApi.Field = function Field(props) {
207204
return (
208-
<form.Field
209-
name={formLensApi.getFormFieldName(name)}
210-
{...formLensApi.remapFieldProps(fieldProps as any)}
211-
/>
205+
<form.Field {...(formLensApi.getFormFieldOptions(props) as any)} />
212206
) as never
213207
}
214208

0 commit comments

Comments
 (0)