|
| 1 | +--- |
| 2 | +id: ui-libraries |
| 3 | +title: UI Libraries |
| 4 | +--- |
| 5 | + |
| 6 | +# Usage of TanStack Form with UI Libraries |
| 7 | + |
| 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`, or even plain CSS. |
| 9 | + |
| 10 | +This guide focuses on `Material-UI` and `Mantine`, but the concepts are applicable to any UI library of your choice. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +Before integrating TanStack Form with a UI library, ensure the necessary dependencies are installed in your project: |
| 15 | + |
| 16 | +- For `Material-UI`, follow the installation instructions on their [official site](https://mui.com/material-ui/getting-started/). |
| 17 | +- For `Mantine`, refer to their [documentation](https://mantine.dev/). |
| 18 | + |
| 19 | +Note: While you can mix and match libraries, it's generally advisable to stick with one to maintain consistency and minimize bloat. |
| 20 | + |
| 21 | +## Example with Mantine |
| 22 | + |
| 23 | +Here's an example demonstrating the integration of TanStack Form with Mantine components: |
| 24 | + |
| 25 | +```tsx |
| 26 | +import { TextInput, Checkbox } from '@mantine/core' |
| 27 | +import { useForm } from '@tanstack/react-form' |
| 28 | + |
| 29 | +export default function App() { |
| 30 | + const { Provider, Field, handleSubmit, state } = useForm({ |
| 31 | + defaultValues: { |
| 32 | + firstName: '', |
| 33 | + lastName: '', |
| 34 | + isChecked: false, |
| 35 | + }, |
| 36 | + onSubmit: async ({ value }) => { |
| 37 | + // Handle form submission |
| 38 | + console.log(value) |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + return ( |
| 43 | + <> |
| 44 | + <Provider> |
| 45 | + <form |
| 46 | + onSubmit={(e) => { |
| 47 | + e.preventDefault() |
| 48 | + handleSubmit() |
| 49 | + }} |
| 50 | + > |
| 51 | + <Field |
| 52 | + name="firstName" |
| 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 | + </Provider> |
| 74 | + <div> |
| 75 | + <pre>{JSON.stringify(state.values, null, 2)}</pre> |
| 76 | + </div> |
| 77 | + </> |
| 78 | + ) |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +- 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. |
| 83 | +- Next, we encapsulate our form elements within the `Provider` component, a critical step for enabling form functionalities. The `Field` component, derived from `useForm`, accepts several properties, such as `validators`. For this demonstration, we focus on two primary properties: `name` and `children`. |
| 84 | + - The `name` property identifies each `Field`, for instance, `firstName` in our example. |
| 85 | + - The `children` property leverages the concept of render props, allowing us to integrate components without unnecessary abstractions. |
| 86 | +- 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. |
| 87 | +- By following these steps, you can seamlessly integrate Mantine components with TanStack Form. |
| 88 | +- This methodology is equally applicable to other components, such as `Checkbox`, ensuring consistent integration across different UI elements. |
| 89 | + |
| 90 | +## Usage with `Material-UI` |
| 91 | + |
| 92 | +The process for integrating Material-UI components is similar. Here's an example using TextField and Checkbox from Material-UI: |
| 93 | + |
| 94 | +```tsx |
| 95 | + <Field |
| 96 | + name="lastName" |
| 97 | + children={({ state, handleChange, handleBlur }) => { |
| 98 | + return ( |
| 99 | + <TextField |
| 100 | + id="filled-basic" |
| 101 | + label="Filled" |
| 102 | + variant="filled" |
| 103 | + defaultValue={state.value} |
| 104 | + onChange={(e) => handleChange(e.target.value)} |
| 105 | + onBlur={handleBlur} |
| 106 | + placeholder="Enter your last name" |
| 107 | + /> |
| 108 | + ); |
| 109 | + }} |
| 110 | + /> |
| 111 | + |
| 112 | + <Field |
| 113 | + name="isMuiCheckBox" |
| 114 | + children={({ state, handleChange, handleBlur }) => { |
| 115 | + return ( |
| 116 | + <MuiCheckbox |
| 117 | + onChange={(e) => handleChange(e.target.checked)} |
| 118 | + onBlur={handleBlur} |
| 119 | + checked={state.value} |
| 120 | + /> |
| 121 | + ); |
| 122 | + }} |
| 123 | + /> |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +- The integration approach is the same as with Mantine. |
| 128 | +- The primary difference lies in the specific Material-UI component properties and styling options. |
0 commit comments