Skip to content

Commit ebd3774

Browse files
committed
fix(solid-form): make the field in useFieldContext an accessor.
1 parent a3ea544 commit ebd3774

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

packages/solid-form/src/createFormHook.tsx

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import type {
88
FormOptions,
99
FormValidateOrFn,
1010
} from '@tanstack/form-core'
11-
import type { Accessor, Component, Context, JSX, ParentProps } from 'solid-js'
11+
import type {
12+
Accessor,
13+
Component,
14+
Context,
15+
JSXElement,
16+
ParentProps,
17+
} from 'solid-js'
1218
import type { FieldComponent } from './createField'
1319
import type { SolidFormExtendedApi } from './createForm'
1420

@@ -51,8 +57,8 @@ type UnwrapDefaultOrAny<DefaultT, T> = [DefaultT] extends [T]
5157

5258
export function createFormHookContexts() {
5359
// We should never hit the `null` case here
54-
const fieldContext = createContext<AnyFieldApi>(
55-
null as unknown as AnyFieldApi,
60+
const fieldContext = createContext<Accessor<AnyFieldApi>>(
61+
null as unknown as Accessor<AnyFieldApi>,
5662
)
5763

5864
function useFieldContext<TData>() {
@@ -65,26 +71,28 @@ export function createFormHookContexts() {
6571
)
6672
}
6773

68-
return field as FieldApi<
69-
any,
70-
string,
71-
TData,
72-
any,
73-
any,
74-
any,
75-
any,
76-
any,
77-
any,
78-
any,
79-
any,
80-
any,
81-
any,
82-
any,
83-
any,
84-
any,
85-
any,
86-
any,
87-
any
74+
return field as Accessor<
75+
FieldApi<
76+
any,
77+
string,
78+
TData,
79+
any,
80+
any,
81+
any,
82+
any,
83+
any,
84+
any,
85+
any,
86+
any,
87+
any,
88+
any,
89+
any,
90+
any,
91+
any,
92+
any,
93+
any,
94+
any
95+
>
8896
>
8997
}
9098

@@ -124,7 +132,7 @@ interface CreateFormHookProps<
124132
TFormComponents extends Record<string, Component<any>>,
125133
> {
126134
fieldComponents: TFieldComponents
127-
fieldContext: Context<AnyFieldApi>
135+
fieldContext: Context<Accessor<AnyFieldApi>>
128136
formComponents: TFormComponents
129137
formContext: Context<AnyFormApi>
130138
}
@@ -218,7 +226,7 @@ export interface WithFormProps<
218226
>
219227
}
220228
>,
221-
) => JSX.Element
229+
) => JSXElement
222230
}
223231

224232
export function createFormHook<
@@ -280,7 +288,7 @@ export function createFormHook<
280288
return (
281289
<form.Field {...fieldProps}>
282290
{(field) => (
283-
<opts.fieldContext.Provider value={field()}>
291+
<opts.fieldContext.Provider value={field}>
284292
{childProps.children(Object.assign(field, opts.fieldComponents))}
285293
</opts.fieldContext.Provider>
286294
)}

packages/solid-form/tests/createFormHook.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ function TextField({ label }: { label: string }) {
1212
<label>
1313
<div>{label}</div>
1414
<input
15-
value={field.state.value}
16-
onChange={(e) => field.handleChange(e.target.value)}
15+
value={field().state.value}
16+
onChange={(e) => field().handleChange(e.target.value)}
1717
/>
1818
</label>
1919
)

0 commit comments

Comments
 (0)