Skip to content

Commit 888bfcd

Browse files
felipestanzaniautofix-ci[bot]LeCarbonator
authored
docs(react): add shadcn/ui examples to React ui-libraries documentation (#1757)
* Adds shadcn/ui examples to ui-libraries doc * ci: apply automated fixes and generate docs * Update docs/framework/react/guides/ui-libraries.md Changes input defaultValue to value Co-authored-by: LeCarbonator <[email protected]> --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: LeCarbonator <[email protected]>
1 parent b2da22e commit 888bfcd

File tree

1 file changed

+67
-35
lines changed

1 file changed

+67
-35
lines changed

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

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ 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`, 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 `Tailwind`, `Material UI`, `Mantine`, `shadcn/ui`, or even plain CSS.
99

10-
This guide focuses on `Material UI` and `Mantine`, but the concepts are applicable to any UI library of your choice.
10+
This guide focuses on `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

1616
- For `Material UI`, follow the installation instructions on their [official site](https://mui.com/material-ui/getting-started/).
1717
- For `Mantine`, refer to their [documentation](https://mantine.dev/).
18+
- For `shadcn/ui`, refer to their [official site](https://ui.shadcn.com/).
1819

1920
Note: While you can mix and match libraries, it's generally advisable to stick with one to maintain consistency and minimize bloat.
2021

@@ -29,8 +30,7 @@ import { useForm } from '@tanstack/react-form'
2930
export default function App() {
3031
const { Field, handleSubmit, state } = useForm({
3132
defaultValues: {
32-
firstName: '',
33-
lastName: '',
33+
name: '',
3434
isChecked: false,
3535
},
3636
onSubmit: async ({ value }) => {
@@ -48,7 +48,7 @@ export default function App() {
4848
}}
4949
>
5050
<Field
51-
name="firstName"
51+
name="name"
5252
children={({ state, handleChange, handleBlur }) => (
5353
<TextInput
5454
defaultValue={state.value}
@@ -79,7 +79,7 @@ export default function App() {
7979

8080
- 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.
8181
- The `Field` component, derived from `useForm`, accepts several properties, such as `validators`. For this demonstration, we focus on two primary properties: `name` and `children`.
82-
- The `name` property identifies each `Field`, for instance, `firstName` in our example.
82+
- The `name` property identifies each `Field`, for instance, `name` in our example.
8383
- The `children` property leverages the concept of render props, allowing us to integrate components without unnecessary abstractions.
8484
- 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.
8585
- By following these steps, you can seamlessly integrate Mantine components with TanStack Form.
@@ -90,37 +90,69 @@ export default function App() {
9090
The process for integrating Material UI components is similar. Here's an example using TextField and Checkbox from Material UI:
9191

9292
```tsx
93-
<Field
94-
name="lastName"
95-
children={({ state, handleChange, handleBlur }) => {
96-
return (
97-
<TextField
98-
id="filled-basic"
99-
label="Filled"
100-
variant="filled"
101-
defaultValue={state.value}
102-
onChange={(e) => handleChange(e.target.value)}
103-
onBlur={handleBlur}
104-
placeholder="Enter your last name"
105-
/>
106-
);
107-
}}
108-
/>
109-
110-
<Field
111-
name="isMuiCheckBox"
112-
children={({ state, handleChange, handleBlur }) => {
113-
return (
114-
<MuiCheckbox
115-
onChange={(e) => handleChange(e.target.checked)}
116-
onBlur={handleBlur}
117-
checked={state.value}
118-
/>
119-
);
120-
}}
121-
/>
93+
<Field
94+
name="name"
95+
children={({ state, handleChange, handleBlur }) => {
96+
return (
97+
<TextField
98+
id="filled-basic"
99+
label="Filled"
100+
variant="filled"
101+
defaultValue={state.value}
102+
onChange={(e) => handleChange(e.target.value)}
103+
onBlur={handleBlur}
104+
placeholder="Enter your name"
105+
/>
106+
);
107+
}}
108+
/>
109+
110+
<Field
111+
name="isMuiCheckBox"
112+
children={({ state, handleChange, handleBlur }) => {
113+
return (
114+
<MuiCheckbox
115+
onChange={(e) => handleChange(e.target.checked)}
116+
onBlur={handleBlur}
117+
checked={state.value}
118+
/>
119+
);
120+
}}
121+
/>
122122

123123
```
124124

125125
- The integration approach is the same as with Mantine.
126126
- The primary difference lies in the specific Material UI component properties and styling options.
127+
128+
### Usage with shadcn/ui
129+
130+
The process for integrating shadcn/ui components is similar. Here's an example using Input and Checkbox from shadcn/ui:
131+
132+
```tsx
133+
<Field
134+
name="name"
135+
children={({ state, handleChange, handleBlur }) => (
136+
<Input
137+
value={state.value}
138+
onChange={(e) => handleChange(e.target.value)}
139+
onBlur={handleBlur}
140+
placeholder="Enter your name"
141+
/>
142+
)}
143+
/>
144+
<Field
145+
name="isChecked"
146+
children={({ state, handleChange, handleBlur }) => (
147+
<Checkbox
148+
onCheckedChange={(checked) => handleChange(checked === true)}
149+
onBlur={handleBlur}
150+
checked={state.value}
151+
/>
152+
)}
153+
/>
154+
```
155+
156+
- The integration approach is the same as with Mantine and Material UI.
157+
- The primary difference lies in the specific shadcn/ui component properties and styling options.
158+
- Note the onCheckedChange property of Checkbox instead of onChange.

0 commit comments

Comments
 (0)