Skip to content

Commit 16dca26

Browse files
docs(react-form): add Chakra UI integration guide (#1909)
* docs(react-form): add Chakra UI integration guide to ui-libraries documentation Add comprehensive Chakra UI integration examples to the UI libraries guide, covering both composable (Checkbox.Root) and pre-composed Checkbox components. Includes code examples demonstrating how to integrate Chakra UI's Input and Checkbox components with TanStack Form's Field component, maintaining consistency with existing Mantine, Material UI, and shadcn/ui examples. * docs: Update Chakra UI Checkbox example in UI libraries guide
1 parent 169ebeb commit 16dca26

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

docs/framework/react/guides/ui-libraries.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ title: UI Libraries
55

66
## Usage of TanStack Form with UI Libraries
77

8-
TanStack 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 `Tailwind`, `Material UI`, `Mantine`, `shadcn/ui`, or even plain CSS.
8+
TanStack 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.
99

10-
This guide focuses on `Material UI`, `Mantine`, and `shadcn/ui`, but the concepts are applicable to any UI library of your choice.
10+
This guide focuses on `Chakra UI`, `Material UI`, `Mantine`, and `shadcn/ui`, but the concepts are applicable to any UI library of your choice.
1111

1212
### Prerequisites
1313

1414
Before integrating TanStack Form with a UI library, ensure the necessary dependencies are installed in your project:
1515

16+
- For `Chakra UI`, follow the installation instructions on their [official site](https://chakra-ui.com/docs/get-started/installation)
1617
- For `Material UI`, follow the installation instructions on their [official site](https://mui.com/material-ui/getting-started/).
1718
- For `Mantine`, refer to their [documentation](https://mantine.dev/).
1819
- For `shadcn/ui`, refer to their [official site](https://ui.shadcn.com/).
@@ -153,8 +154,63 @@ The process for integrating shadcn/ui components is similar. Here's an example u
153154
/>
154155
```
155156

156-
- The integration approach is the same as with Mantine and Material UI.
157+
- The integration approach is the same as with Mantine, Material UI.
157158
- The primary difference lies in the specific shadcn/ui component properties and styling options.
158159
- Note the onCheckedChange property of Checkbox instead of onChange.
159160

160-
The ShadCN library includes a dedicated guide covering common scenarios for integrating TanStack Form with its components: https://ui.shadcn.com/docs/forms/tanstack-form
161+
The 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+
165+
The 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+
201+
Example 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+
```

0 commit comments

Comments
 (0)