Skip to content

fix(react-form): remap listener paths in withFieldGroup #1654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion packages/form-core/src/FieldGroupApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
FormAsyncValidateOrFn,
FormValidateOrFn,
} from './FormApi'
import type { AnyFieldMeta, AnyFieldMetaBase } from './FieldApi'
import type { AnyFieldMetaBase, FieldOptions } from './FieldApi'
import type {
DeepKeys,
DeepKeysOfType,
Expand Down Expand Up @@ -163,6 +163,55 @@ export class FieldGroupApi<
return concatenatePaths(formMappedPath, restOfPath)
}

/**
* Get the field options with the true form DeepKeys for validators
* @private
*/
getFormFieldOptions = <
TOptions extends FieldOptions<
any,
any,
any,
any,
any,
any,
any,
any,
any,
any
>,
>(
props: TOptions,
): TOptions => {
const newProps = { ...props }
const validators = newProps.validators

newProps.name = this.getFormFieldName(props.name)

if (
validators &&
(validators.onChangeListenTo || validators.onBlurListenTo)
) {
const newValidators = { ...validators }

const remapListenTo = (listenTo: DeepKeys<any>[] | undefined) => {
if (!listenTo) return undefined
return listenTo.map((localFieldName) =>
this.getFormFieldName(localFieldName),
)
}

newValidators.onChangeListenTo = remapListenTo(
validators.onChangeListenTo,
)
newValidators.onBlurListenTo = remapListenTo(validators.onBlurListenTo)

newProps.validators = newValidators
}

return newProps
}

store: Derived<FieldGroupState<TFieldGroupData>>

get state() {
Expand Down
99 changes: 98 additions & 1 deletion packages/form-core/tests/FieldGroupApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { FieldApi, FieldGroupApi, FormApi } from '../src/index'
import { defaultFieldMeta } from '../src/metaHelper'

describe('field group api', () => {
type Person = {
Expand Down Expand Up @@ -919,4 +918,102 @@ describe('field group api', () => {
'complexValue.prop1',
)
})

it('should remap the name of field options correctly', () => {
const form = new FormApi({
defaultValues: {
account: {
password: '',
confirmPassword: '',
},
userPassword: '',
userConfirmPassword: '',
},
})
form.mount()

const fieldGroupString = new FieldGroupApi({
form,
fields: 'account',
defaultValues: { password: '' },
})
fieldGroupString.mount()

const props1 = {
name: 'password',
}
const remappedProps1 = fieldGroupString.getFormFieldOptions(props1)
expect(remappedProps1.name).toBe('account.password')

const fieldGroupObject = new FieldGroupApi({
form,
fields: {
password: 'userPassword',
confirmPassword: 'userConfirmPassword',
},
defaultValues: { password: '' },
})
fieldGroupObject.mount()

const props2 = {
name: 'password',
}
const remappedProps2 = fieldGroupObject.getFormFieldOptions(props2)
expect(remappedProps2.name).toBe('userPassword')
})

it('should remap listener paths with its remapFieldProps method', () => {
const form = new FormApi({
defaultValues: {
account: {
password: '',
confirmPassword: '',
},
userPassword: '',
userConfirmPassword: '',
},
})
form.mount()

const fieldGroupString = new FieldGroupApi({
form,
fields: 'account',
defaultValues: { password: '', confirmPassword: '' },
})
fieldGroupString.mount()

const props1 = {
name: 'confirmPassword',
validators: {
onChangeListenTo: ['password'],
onBlurListenTo: ['confirmPassword'],
},
}
const remappedProps1 = fieldGroupString.getFormFieldOptions(props1)
expect(remappedProps1.validators.onChangeListenTo).toEqual([
'account.password',
])
expect(remappedProps1.validators.onBlurListenTo).toEqual([
'account.confirmPassword',
])

const fieldGroupObject = new FieldGroupApi({
form,
fields: {
password: 'userPassword',
confirmPassword: 'userConfirmPassword',
},
defaultValues: { password: '', confirmPassword: '' },
})
fieldGroupObject.mount()

const props2 = {
name: 'confirmPassword',
validators: {
onChangeListenTo: ['password'],
},
}
const remappedProps2 = fieldGroupObject.getFormFieldOptions(props2)
expect(remappedProps2.validators.onChangeListenTo).toEqual(['userPassword'])
})
})
74 changes: 52 additions & 22 deletions packages/react-form/src/useField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
DeepValue,
FieldAsyncValidateOrFn,
FieldValidateOrFn,
FieldValidators,
FormAsyncValidateOrFn,
FormValidateOrFn,
} from '@tanstack/form-core'
Expand Down Expand Up @@ -446,28 +447,57 @@ export type LensFieldComponent<
>({
children,
...fieldOptions
}: FieldComponentBoundProps<
unknown,
string,
TData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
undefined | FormValidateOrFn<unknown>,
undefined | FormValidateOrFn<unknown>,
undefined | FormAsyncValidateOrFn<unknown>,
undefined | FormValidateOrFn<unknown>,
undefined | FormAsyncValidateOrFn<unknown>,
undefined | FormValidateOrFn<unknown>,
undefined | FormAsyncValidateOrFn<unknown>,
undefined | FormAsyncValidateOrFn<unknown>,
TParentSubmitMeta,
ExtendedApi
> & { name: TName }) => ReactNode
}: Omit<
FieldComponentBoundProps<
unknown,
string,
TData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
undefined | FormValidateOrFn<unknown>,
undefined | FormValidateOrFn<unknown>,
undefined | FormAsyncValidateOrFn<unknown>,
undefined | FormValidateOrFn<unknown>,
undefined | FormAsyncValidateOrFn<unknown>,
undefined | FormValidateOrFn<unknown>,
undefined | FormAsyncValidateOrFn<unknown>,
undefined | FormAsyncValidateOrFn<unknown>,
TParentSubmitMeta,
ExtendedApi
>,
'name' | 'validators'
> & {
name: TName
validators?: Omit<
FieldValidators<
unknown,
string,
TData,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync
>,
'onChangeListenTo' | 'onBlurListenTo'
> & {
/**
* An optional list of field names that should trigger this field's `onChange` and `onChangeAsync` events when its value changes
*/
onChangeListenTo?: DeepKeys<TLensData>[]
/**
* An optional list of field names that should trigger this field's `onBlur` and `onBlurAsync` events when its value changes
*/
onBlurListenTo?: DeepKeys<TLensData>[]
}
}) => ReactNode

/**
* A function component that takes field options and a render function as children and returns a React component.
Expand Down
14 changes: 4 additions & 10 deletions packages/react-form/src/useFieldGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,15 @@ export function useFieldGroup<
return <form.AppForm {...appFormProps} />
}

extendedApi.AppField = function AppField({ name, ...appFieldProps }) {
extendedApi.AppField = function AppField(props) {
return (
<form.AppField
name={formLensApi.getFormFieldName(name)}
{...(appFieldProps as any)}
/>
<form.AppField {...(formLensApi.getFormFieldOptions(props) as any)} />
) as never
}

extendedApi.Field = function Field({ name, ...fieldProps }) {
extendedApi.Field = function Field(props) {
return (
<form.Field
name={formLensApi.getFormFieldName(name)}
{...(fieldProps as any)}
/>
<form.Field {...(formLensApi.getFormFieldOptions(props) as any)} />
) as never
}

Expand Down
84 changes: 84 additions & 0 deletions packages/react-form/tests/createFormHook.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,88 @@ describe('createFormHook', () => {
const inputField1 = getByLabelText('unrelated')
expect(inputField1).toHaveValue('John')
})

it('should remap GroupFieldApi.Field validators to the correct names', () => {
const FieldGroupString = withFieldGroup({
defaultValues: { password: '', confirmPassword: '' },
render: function Render({ group }) {
return (
<group.Field
name="password"
validators={{
onChange: () => null,
onChangeListenTo: ['password'],
onBlur: () => null,
onBlurListenTo: ['confirmPassword'],
}}
>
{(field) => {
expect(field.options.validators?.onChangeListenTo).toStrictEqual([
'account.password',
])
expect(field.options.validators?.onBlurListenTo).toStrictEqual([
'account.confirmPassword',
])
return <></>
}}
</group.Field>
)
},
})

const FieldGroupObject = withFieldGroup({
defaultValues: { password: '', confirmPassword: '' },
render: function Render({ group }) {
return (
<group.Field
name="password"
validators={{
onChange: () => null,
onChangeListenTo: ['password'],
onBlur: () => null,
onBlurListenTo: ['confirmPassword'],
}}
>
{(field) => {
expect(field.options.validators?.onChangeListenTo).toStrictEqual([
'userPassword',
])
expect(field.options.validators?.onBlurListenTo).toStrictEqual([
'userConfirmPassword',
])
return <></>
}}
</group.Field>
)
},
})

const Parent = () => {
const form = useAppForm({
defaultValues: {
account: {
password: '',
confirmPassword: '',
},
userPassword: '',
userConfirmPassword: '',
},
})

return (
<>
<FieldGroupString form={form} fields="account" />
<FieldGroupObject
form={form}
fields={{
password: 'userPassword',
confirmPassword: 'userConfirmPassword',
}}
/>
</>
)
}

render(<Parent />)
})
})