Typesafety in FormAPI and FieldAPI for Shadcn Form Components
Hi @crutchcorn
Just following up from our convo — I’m the one working on the TanStack Forms + shadcn/ui integration. I’m mainly stuck with getting things fully type-safe, especially around FormAPI and FieldAPI.
example code:
Github URL: https://github.com/kulterryan/shadcn-tanstack-form/blob/main/src/components/ui/tanstack-form.tsx
GitHub
shadcn-tanstack-form/src/components/ui/tanstack-form.tsx at main ·...
Contribute to kulterryan/shadcn-tanstack-form development by creating an account on GitHub.

8 Replies
genetic-orange•7mo ago
it's generally discouraged to manually create the formApi parameters. There's helper functions such as
withForm in Form Composition, but that depends on what exactly you're trying to achieve.
If it's a form component, then a form context is the recommended way (also in the link)deep-jadeOP•7mo ago
Ok let me check that.
I tried, but not able to understand the docs properly.
genetic-orange•7mo ago
I'm not a maintainer, but I can help out if you have questions about form composition. So it depends on how the implementation is supposed to be done (following general guidelines for everyone vs. custom implementation)
deep-jadeOP•7mo ago
I'm trying to make the whole forms component composable like shadcn has done with react hook forms, I'm almost there but not able to figure out the type safety for making all the fields composable.
genetic-orange•7mo ago
you're likely used to a system like image 1.
Summarized, Tanstack Form works with the system in the second image.
If you want it to be as composable as possible, you'd provide the user with the field and form components. They may then decide which ones to pass onto
createFormHook() and which ones to discard. You will also want to generate a fieldContext and formContext for them to use in the hook, as you'll use the respective useFieldContext()| useFormContext() hooks in your shadcn components

genetic-orange•7mo ago
so the usage would look less like:
And more like
That‘s still assuming you‘re planning to follow these general guidelines. It‘s possible to create a structure like the former code snippet, with some more effort.
stormy-gold•7mo ago
Adding onto this. Looks like a few others have done something like this. Here is an example one (Not saying it is perfect but a starting point):
https://github.com/FatahChan/shadcn-tanstack-form/blob/main/src/registry/new-york/tanstack-form/tanstack-form.tsx
GitHub
shadcn-tanstack-form/src/registry/new-york/tanstack-form/tanstack-f...
Contribute to FatahChan/shadcn-tanstack-form development by creating an account on GitHub.
deep-jadeOP•7mo ago
More likely yes, I'm trying it like the first image only, I'll study more about
createFormHook() and try to implement with the context. This explainer looks quite good to me. Let me give it a shot.
Thanks 🙌