Skip to content

Commit b37628d

Browse files
committed
fix: remove form.Form for headless form.getFormProps()
1 parent 3572353 commit b37628d

File tree

5 files changed

+20
-49
lines changed

5 files changed

+20
-49
lines changed

docs/framework/react/reference/formApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ title: Form API
88
When using `@tanstack/react-form`, the [core form API](../../reference/formApi) is extended with additional methods for React-specific functionality:
99

1010
- ```tsx
11-
Form: FormComponent
11+
getFormProps: () => FormProps
1212
```
13-
- A pre-bound and type-safe form component, specific to this forms instance.
13+
- A function that returns props for the form element.
1414
- ```tsx
1515
Field: FieldComponent<TFormData>
1616
```

docs/framework/react/reference/useForm.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,3 @@ A type representing a form component.
3232

3333
- `(props: FormProps) => any`
3434
- A function that takes `FormProps` as an argument and returns a form component.
35-
36-
### `createFormComponent`
37-
38-
```tsx
39-
export function createFormComponent(formApi: FormApi<any>): FormComponent
40-
```
41-
42-
A function that creates a form component with the provided form API instance.
43-
44-
- `formApi`
45-
- An instance of the `FormApi<any>` class.

examples/react/simple/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default function App() {
4545
<div>
4646
<h1>Simple Form Example</h1>
4747
{/* A pre-bound form component */}
48-
<form.Form>
48+
<form {...form.getFormProps()}>
4949
<div>
5050
{/* A type-safe and pre-bound field component*/}
5151
<form.Field

packages/react-form/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type {
2222

2323
export { FormApi, FieldApi, functionalUpdate } from '@tanstack/form-core'
2424

25-
export type { FormComponent, FormProps } from './useForm'
25+
export type { FormProps } from './useForm'
2626
export { useForm } from './useForm'
2727

2828
export type { UseField, FieldComponent } from './useField'

packages/react-form/src/useForm.tsx

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import React from 'react'
66
import { type UseField, type FieldComponent, Field, useField } from './useField'
77
import { formContext } from './formContext'
88

9+
export type FormSubmitEvent = React.FormEvent<HTMLFormElement>
10+
911
declare module '@tanstack/form-core' {
1012
interface Register {
11-
FormSubmitEvent: React.FormEvent<HTMLFormElement>
13+
FormSubmitEvent: FormSubmitEvent
1214
}
1315

1416
// eslint-disable-next-line no-shadow
1517
interface FormApi<TFormData> {
16-
Form: FormComponent
18+
getFormProps: () => FormProps
1719
Field: FieldComponent<TFormData, TFormData>
1820
useField: UseField<TFormData>
1921
useStore: <TSelected = NoInfer<FormState<TFormData>>>(
@@ -28,12 +30,22 @@ declare module '@tanstack/form-core' {
2830
}
2931
}
3032

33+
export type FormProps = {
34+
onSubmit: (e: FormSubmitEvent) => void
35+
disabled: boolean
36+
}
37+
3138
export function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData> {
3239
const [formApi] = React.useState(() => {
3340
// @ts-ignore
3441
const api = new FormApi<TData>(opts)
3542

36-
api.Form = createFormComponent(api)
43+
api.getFormProps = () => {
44+
return {
45+
onSubmit: formApi.handleSubmit,
46+
disabled: api.state.isSubmitting,
47+
}
48+
}
3749
api.Field = Field as any
3850
api.useField = useField as any
3951
api.useStore = (
@@ -57,38 +69,8 @@ export function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData> {
5769
return api
5870
})
5971

72+
formApi.useStore((state) => state.isSubmitting)
6073
formApi.update(opts)
6174

6275
return formApi as any
6376
}
64-
65-
export type FormProps = React.HTMLProps<HTMLFormElement> & {
66-
children: React.ReactNode
67-
noFormElement?: boolean
68-
}
69-
70-
export type FormComponent = (props: FormProps) => any
71-
72-
function createFormComponent(formApi: FormApi<any>) {
73-
const Form: FormComponent = ({ children, noFormElement, ...rest }) => {
74-
const isSubmitting = formApi.useStore((state) => state.isSubmitting)
75-
76-
return (
77-
<formContext.Provider value={{ formApi }}>
78-
{noFormElement ? (
79-
children
80-
) : (
81-
<form
82-
onSubmit={formApi.handleSubmit}
83-
disabled={isSubmitting}
84-
{...rest}
85-
>
86-
{children}
87-
</form>
88-
)}
89-
</formContext.Provider>
90-
)
91-
}
92-
93-
return Form
94-
}

0 commit comments

Comments
 (0)