Open History to see the last commit
1---
2id: ui-libraries
3title: UI Libraries
4---
5​
6## Usage of TanStack Form with UI Libraries
7​
8TanStack Form is a headless library, offering you complete flexibility to style it as you see fit. It's compatible with a wide range of UI libraries, including `Chakra UI`, `Tailwind`, `Material UI`, `Mantine`, `shadcn/ui`, or even plain CSS.
9​
10This guide focuses on `Chakra UI`, `Material UI`, `Mantine`, and `shadcn/ui`, but the concepts are applicable to any UI library of your choice.
11​
12### Prerequisites
13​
14Before integrating TanStack Form with a UI library, ensure the necessary dependencies are installed in your project:
15​
16- For `Chakra UI`, follow the installation instructions on their [official site](https://chakra-ui.com/docs/get-started/installation)
17- For `Material UI`, follow the installation instructions on their [official site](https://mui.com/material-ui/getting-started/).
18- For `Mantine`, refer to their [documentation](https://mantine.dev/).
19- For `shadcn/ui`, refer to their [official site](https://ui.shadcn.com/).
20​
21Note: While you can mix and match libraries, it's generally advisable to stick with one to maintain consistency and minimize bloat.
22​
23### Example with Mantine
24​
25Here's an example demonstrating the integration of TanStack Form with Mantine components:
26​
27```tsx
28import { TextInput, Checkbox } from '@mantine/core'
29import { useForm } from '@tanstack/react-form'
30​
31export default function App() {
32 const { Field, handleSubmit, state } = useForm({
33 defaultValues: {
34 name: '',
35 isChecked: false,
36 },
37 onSubmit: async ({ value }) => {
38 // Handle form submission
39 console.log(value)
40 },
41 })
42​
43 return (
44 <>
45 <form
46 onSubmit={(e) => {
47 e.preventDefault()
48 handleSubmit()
49 }}
50 >
51 <Field
52 name="name"
53 children={({ state, handleChange, handleBlur }) => (
54 <TextInput
55 defaultValue={state.value}
56 onChange={(e) => handleChange(e.target.value)}
57 onBlur={handleBlur}
58 placeholder="Enter your name"
59 />
60 )}
61 />
62 <Field
63 name="isChecked"
64 children={({ state, handleChange, handleBlur }) => (
65 <Checkbox
66 onChange={(e) => handleChange(e.target.checked)}
67 onBlur={handleBlur}
68 checked={state.value}
69 />
70 )}
71 />
72 </form>
73 <div>
74 <pre>{JSON.stringify(state.values, null, 2)}</pre>
75 </div>
76 </>
77 )
78}
79```
80​
81- Initially, we utilize the `useForm` hook from TanStack and destructure the necessary properties. This step is optional; alternatively, you could use `const form = useForm()` if preferred. TypeScript's type inference ensures a smooth experience regardless of the approach.
82- The `Field` component, derived from `useForm`, accepts several properties, such as `validators`. For this demonstration, we focus on two primary properties: `name` and `children`.
83 - The `name` property identifies each `Field`, for instance, `name` in our example.
84 - The `children` property leverages the concept of render props, allowing us to integrate components without unnecessary abstractions.
85- TanStack's design relies heavily on render props, providing access to `children` within the `Field` component. This approach is entirely type-safe. When integrating with Mantine components, such as `TextInput`, we selectively destructure properties like `state.value`, `handleChange`, and `handleBlur`. This selective approach is due to the slight differences in types between `TextInput` and the `field` we get in the children.
86- By following these steps, you can seamlessly integrate Mantine components with TanStack Form.
87- This methodology is equally applicable to other components, such as `Checkbox`, ensuring consistent integration across different UI elements.
88​
89### Usage with Material UI
90​
91The process for integrating Material UI components is similar. Here's an example using TextField and Checkbox from Material UI:
92​
93```tsx
94<Field
95 name="name"
96 children={({ state, handleChange, handleBlur }) => {
97 return (
98 <TextField
99 id="filled-basic"
100 label="Filled"
101 variant="filled"
102 defaultValue={state.value}
103 onChange={(e) => handleChange(e.target.value)}
104 onBlur={handleBlur}
105 placeholder="Enter your name"
106 />
107 );
108 }}
109/>
110​
111<Field
112 name="isMuiCheckBox"
113 children={({ state, handleChange, handleBlur }) => {
114 return (
115 <MuiCheckbox
116 onChange={(e) => handleChange(e.target.checked)}
117 onBlur={handleBlur}
118 checked={state.value}
119 />
120 );
121 }}
122/>
123​
124```
125​
126- The integration approach is the same as with Mantine.
127- The primary difference lies in the specific Material UI component properties and styling options.
128​
129### Usage with shadcn/ui
130​
131The process for integrating shadcn/ui components is similar. Here's an example using Input and Checkbox from shadcn/ui:
132​
133```tsx
134<Field
135 name="name"
136 children={({ state, handleChange, handleBlur }) => (
137 <Input
138 value={state.value}
139 onChange={(e) => handleChange(e.target.value)}
140 onBlur={handleBlur}
141 placeholder="Enter your name"
142 />
143 )}
144/>
145<Field
146 name="isChecked"
147 children={({ state, handleChange, handleBlur }) => (
148 <Checkbox
149 onCheckedChange={(checked) => handleChange(checked === true)}
150 onBlur={handleBlur}
151 checked={state.value}
152 />
153 )}
154/>
155```
156​
157- The integration approach is the same as with Mantine, Material UI.
158- The primary difference lies in the specific shadcn/ui component properties and styling options.
159- Note the onCheckedChange property of Checkbox instead of onChange.
160​
161The ShadCN library includes a dedicated guide covering common scenarios for integrating TanStack Form with its components: [https://ui.shadcn.com/docs/forms/tanstack-form](https://ui.shadcn.com/docs/forms/tanstack-form)
162​
163### Usage with Chakra UI
164​
165The process for integrating Chakra UI components is similar. Here's an example using Input and Checkbox from Chakra UI:
166​
167```tsx
168<Field
169 name="name"
170 children={({ state, handleChange, handleBlur }) => (
171 <Input
172 value={state.value}
173 onChange={(e) => handleChange(e.target.value)}
174 onBlur={handleBlur}
175 placeholder="Enter your name"
176 />
177 )}
178/>
179<Field
180 name="isChecked"
181 children={({ state, handleChange, handleBlur }) => (
182 <Checkbox.Root
183 checked={state.value}
184 onCheckedChange={(details) => handleChange(!!details.checked)}
185 onBlur={handleBlur}
186 >
187 <Checkbox.HiddenInput />
188 <Checkbox.Control />
189 <Checkbox.Label>Accept terms</Checkbox.Label>
190 </Checkbox.Root>
191 )}
192/>
193```
194​
195- The integration approach is the same as with Mantine, Material UI, and shadcn/ui.
196- Chakra UI exposes its Checkbox as a composable component with separate `Checkbox.Root`, `Checkbox.Control`, `Checkbox.Label`, and `Checkbox.HiddenInput` parts that you wire together.
197- The double negation `!!` is used on `onCheckedChange` to coerce Chakra's `"indeterminate"` state to a boolean, ensuring it matches the form state. See the [Chakra UI Checkbox documentation](https://chakra-ui.com/docs/components/checkbox#indeterminate) for more details.
198- Alternatively, Chakra UI offers a pre-composed Checkbox component that works the same way as their standard examples, without requiring manual composition. You can learn more about this closed component approach in the [Chakra UI Checkbox documentation](https://chakra-ui.com/docs/components/checkbox#closed-component).
199- The TanStack Form integration works identically with either approach—simply attach the `checked`, `onCheckedChange`, and `onBlur` handlers to your chosen component.
200​
201Example using the closed Checkbox component:
202​
203```tsx
204<Field
205 name="isChecked"
206 children={({ state, handleChange, handleBlur }) => (
207 <Checkbox
208 checked={state.value}
209 onCheckedChange={(details) => handleChange(!!details.checked)}
210 onBlur={handleBlur}
211 >
212 Accept terms
213 </Checkbox>
214 )}
215/>
216```