Skip to content

Commit 50404a6

Browse files
author
Harry Whorlow
committed
chore: some more missing generics
1 parent 9bb39fc commit 50404a6

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

packages/form-core/src/formOptions.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { FormOptions } from './FormApi'
44
export function formOptions<
55
TFormData,
66
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
7-
>(defaultOpts?: FormOptions<TFormData, TFormValidator>) {
7+
TFormSubmitMeta extends object = never,
8+
>(defaultOpts?: FormOptions<TFormData, TFormValidator, TFormSubmitMeta>) {
89
return defaultOpts
910
}

packages/form-core/src/mergeForm.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ export function mutateMergeDeep(target: object, source: object): object {
3636
export function mergeForm<
3737
TFormData,
3838
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
39+
TFormSubmitMeta extends object = never,
3940
>(
40-
baseForm: FormApi<NoInfer<TFormData>, NoInfer<TFormValidator>>,
41-
state: Partial<FormApi<TFormData, TFormValidator>['state']>,
41+
baseForm: FormApi<
42+
NoInfer<TFormData>,
43+
NoInfer<TFormValidator>,
44+
NoInfer<TFormSubmitMeta>
45+
>,
46+
state: Partial<FormApi<TFormData, TFormValidator, TFormSubmitMeta>['state']>,
4247
) {
4348
mutateMergeDeep(baseForm.state, state)
4449
return baseForm

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

+5
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,10 @@ describe('field api', () => {
17441744
defaultValues: {
17451745
name: '',
17461746
},
1747+
onSubmitMeta: {} as { dino: string },
1748+
onSubmit: async ({ meta }) => {
1749+
expect(meta.dino).toEqual('Stegosaurus')
1750+
},
17471751
})
17481752
form.mount()
17491753

@@ -1758,6 +1762,7 @@ describe('field api', () => {
17581762
})
17591763

17601764
nameField.mount()
1765+
17611766
expect(nameField.getMeta().errors).toEqual(['THERE IS AN ERROR'])
17621767
})
17631768
})

packages/react-form/src/useField.tsx

+27-6
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ interface ReactFieldApi<
1111
TFormValidator extends
1212
| Validator<TParentData, unknown>
1313
| undefined = undefined,
14+
TParentMetaExtension extends object = never,
1415
> {
1516
/**
1617
* A pre-bound and type-safe sub-field component using this field as a root.
1718
*/
18-
Field: FieldComponent<TParentData, TFormValidator>
19+
Field: FieldComponent<TParentData, TFormValidator, TParentMetaExtension>
1920
}
2021

2122
/**
@@ -28,6 +29,7 @@ export type UseField<
2829
TFormValidator extends
2930
| Validator<TParentData, unknown>
3031
| undefined = undefined,
32+
TParentMetaExtension extends object = never,
3133
> = <
3234
TName extends DeepKeys<TParentData>,
3335
TFieldValidator extends
@@ -36,10 +38,24 @@ export type UseField<
3638
TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,
3739
>(
3840
opts: Omit<
39-
UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>,
41+
UseFieldOptions<
42+
TParentData,
43+
TName,
44+
TFieldValidator,
45+
TFormValidator,
46+
TData,
47+
TParentMetaExtension
48+
>,
4049
'form'
4150
>,
42-
) => FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>
51+
) => FieldApi<
52+
TParentData,
53+
TName,
54+
TFieldValidator,
55+
TFormValidator,
56+
TData,
57+
TParentMetaExtension
58+
>
4359

4460
/**
4561
* A hook for managing a field in a form.
@@ -57,13 +73,15 @@ export function useField<
5773
| Validator<TParentData, unknown>
5874
| undefined = undefined,
5975
TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,
76+
TParentMetaExtension extends object = never,
6077
>(
6178
opts: UseFieldOptions<
6279
TParentData,
6380
TName,
6481
TFieldValidator,
6582
TFormValidator,
66-
TData
83+
TData,
84+
TParentMetaExtension
6785
>,
6886
) {
6987
const [fieldApi] = useState(() => {
@@ -73,7 +91,8 @@ export function useField<
7391
name: opts.name,
7492
})
7593

76-
const extendedApi: typeof api & ReactFieldApi<TParentData, TFormValidator> =
94+
const extendedApi: typeof api &
95+
ReactFieldApi<TParentData, TFormValidator, TParentMetaExtension> =
7796
api as never
7897

7998
extendedApi.Field = Field as never
@@ -207,4 +226,6 @@ export const Field = (<
207226
[children, fieldApi, fieldApi.state.value, fieldApi.state.meta],
208227
)
209228
return (<>{jsxToDisplay}</>) as never
210-
}) satisfies FunctionComponent<FieldComponentProps<any, any, any, any, any>>
229+
}) satisfies FunctionComponent<
230+
FieldComponentProps<any, any, any, any, any, any>
231+
>

packages/react-form/src/useForm.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import type { FormOptions, FormState, Validator } from '@tanstack/form-core'
1414
export interface ReactFormApi<
1515
TFormData,
1616
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
17-
TParentMetaExtension extends object = never,
17+
TFormSubmitMeta extends object = never,
1818
> {
1919
/**
2020
* A React component to render form fields. With this, you can render and manage individual form fields.
2121
*/
22-
Field: FieldComponent<TFormData, TFormValidator, TParentMetaExtension>
22+
Field: FieldComponent<TFormData, TFormValidator, TFormSubmitMeta>
2323
/**
2424
* A `Subscribe` function that allows you to listen and react to changes in the form's state. It's especially useful when you need to execute side effects or render specific components in response to state updates.
2525
*/

packages/react-form/src/useTransform.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import type { FormApi, FormTransform, Validator } from '@tanstack/form-core'
33
export function useTransform<
44
TFormData,
55
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
6+
TFormSubmitMeta extends object = never,
67
>(
7-
fn: (formBase: FormApi<any, any>) => FormApi<TFormData, TFormValidator>,
8+
fn: (
9+
formBase: FormApi<any, any, any>,
10+
) => FormApi<TFormData, TFormValidator, TFormSubmitMeta>,
811
deps: unknown[],
9-
): FormTransform<TFormData, TFormValidator> {
12+
): FormTransform<TFormData, TFormValidator, TFormSubmitMeta> {
1013
return {
1114
fn,
1215
deps,

0 commit comments

Comments
 (0)