-
-
Notifications
You must be signed in to change notification settings - Fork 578
docs(react-form): add Chakra UI integration guide #1909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Adebesin-Cell
wants to merge
6
commits into
TanStack:main
Choose a base branch
from
Adebesin-Cell:docs/chakra-ui-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+60
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eef7fae
docs(react-form): add Chakra UI integration guide to ui-libraries doc…
Adebesin-Cell 1d172f4
Merge branch 'main' into docs/chakra-ui-integration
Adebesin-Cell 785c70d
Merge branch 'main' into docs/chakra-ui-integration
Adebesin-Cell 210f925
Merge branch 'main' into docs/chakra-ui-integration
Adebesin-Cell e6a7908
docs: Update Chakra UI Checkbox example in UI libraries guide
Adebesin-Cell 987ae17
Merge branch 'docs/chakra-ui-integration' of https://github.com/Adebe…
Adebesin-Cell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,14 +5,15 @@ title: UI Libraries | |
|
|
||
| ## Usage of TanStack Form with UI Libraries | ||
|
|
||
| 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. | ||
| 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. | ||
|
|
||
| This guide focuses on `Material UI`, `Mantine`, and `shadcn/ui`, but the concepts are applicable to any UI library of your choice. | ||
| This guide focuses on `Chakra UI`, `Material UI`, `Mantine`, and `shadcn/ui`, but the concepts are applicable to any UI library of your choice. | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| Before integrating TanStack Form with a UI library, ensure the necessary dependencies are installed in your project: | ||
|
|
||
| - For `Chakra UI`, follow the installation instructions on their [official site](https://chakra-ui.com/docs/get-started/installation) | ||
| - For `Material UI`, follow the installation instructions on their [official site](https://mui.com/material-ui/getting-started/). | ||
| - For `Mantine`, refer to their [documentation](https://mantine.dev/). | ||
| - 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 | |
| /> | ||
| ``` | ||
|
|
||
| - The integration approach is the same as with Mantine and Material UI. | ||
| - The integration approach is the same as with Mantine, Material UI. | ||
| - The primary difference lies in the specific shadcn/ui component properties and styling options. | ||
| - Note the onCheckedChange property of Checkbox instead of onChange. | ||
|
|
||
| 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 | ||
| 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) | ||
|
|
||
| ### Usage with Chakra UI | ||
|
|
||
| The process for integrating Chakra UI components is similar. Here's an example using Input and Checkbox from Chakra UI: | ||
|
|
||
| ```tsx | ||
| <Field | ||
| name="name" | ||
| children={({ state, handleChange, handleBlur }) => ( | ||
| <Input | ||
| value={state.value} | ||
| onChange={(e) => handleChange(e.target.value)} | ||
| onBlur={handleBlur} | ||
| placeholder="Enter your name" | ||
| /> | ||
| )} | ||
| /> | ||
| <Field | ||
| name="isChecked" | ||
| children={({ state, handleChange, handleBlur }) => ( | ||
| <Checkbox.Root | ||
| checked={state.value} | ||
| onCheckedChange={(details) => handleChange(!!details.checked)} | ||
| onBlur={handleBlur} | ||
| > | ||
| <Checkbox.HiddenInput /> | ||
| <Checkbox.Control /> | ||
| <Checkbox.Label>Accept terms</Checkbox.Label> | ||
| </Checkbox.Root> | ||
| )} | ||
| /> | ||
| ``` | ||
|
|
||
| - The integration approach is the same as with Mantine, Material UI, and shadcn/ui. | ||
| - 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. | ||
| - 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. | ||
| - 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). | ||
| - The TanStack Form integration works identically with either approach—simply attach the `checked`, `onCheckedChange`, and `onBlur` handlers to your chosen component. | ||
|
|
||
| Example using the closed Checkbox component: | ||
|
|
||
| ```tsx | ||
| <Field | ||
| name="isChecked" | ||
| children={({ state, handleChange, handleBlur }) => ( | ||
| <Checkbox | ||
| checked={state.value} | ||
| onCheckedChange={(details) => handleChange(!!details.checked)} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
| onBlur={handleBlur} | ||
| > | ||
| Accept terms | ||
| </Checkbox> | ||
| )} | ||
| /> | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This double negation seems weird for a (seemingly) boolean property. Is this accidental?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @LeCarbonator, thanks for pointing that out!
The double negation (
!!) here is intentional. In Chakra v3, theCheckbox’sonCheckedChangeprovides aCheckedChangeDetailsobject wherecheckedis typed as:Our form state (
state.value) expects a strict boolean, so passingdetails.checkeddirectly would cause this TypeScript error:Using
!!details.checkedensures the value is always a boolean (truefor checked,falsefor unchecked or indeterminate), which satisfies the type checker and avoids runtime issues.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see ... Is this common knowledge for Chakra users, or do you think a comment explaining this would help at that line?
// coerce 'indeterminate' to falseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea 👍
This behavior is documented in the Chakra docs and shown in examples for controlled state and indeterminate checkboxes:
So it should be familiar to Chakra users, but I agree it’s not immediately obvious why the coercion is needed when reading the snippet.
Adding a small comment like you mentioned:
would help clarify the intent. I’m happy to add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @LeCarbonator, I added a line in docs, I hope that helps:
e6a7908(#1909)