Skip to content

Commit

Permalink
feat: add listeners react documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Whorlow committed Nov 22, 2024
1 parent 79d1dad commit 50c19b5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
"label": "Linked Fields",
"to": "framework/react/guides/linked-fields"
},
{
"label": "Listeners",
"to": "framework/react/guides/listeners"
},
{
"label": "UI Libraries",
"to": "framework/react/guides/ui-libraries"
Expand Down
17 changes: 17 additions & 0 deletions docs/framework/react/guides/basic-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ const firstName = form.useStore((state) => state.values.firstName)

Note: The usage of the `form.useField` hook to achieve reactivity is discouraged since it is designed to be used thoughtfully within the `form.Field` component. You might want to use `form.useStore` instead.

## Listeners

`@tanstack/react-form` allows you to react to specific triggers and "listen" to them to dispatch side effects.

Example:

```tsx
<form.Field
name="country"
listener={
onChange: ({ value }) => {
form.reset({county: ''})
},
}
/>
```

## Array Fields

Array fields allow you to manage a list of values within a form, such as a list of hobbies. You can create an array field using the `form.Field` component with the `mode="array"` prop.
Expand Down
64 changes: 64 additions & 0 deletions docs/framework/react/guides/listeners.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
id: listeners
title: Side effects for event triggers
---

For situations where you want to "affect" or "react" to triggers, there's the listener API. For example, if you, as the developer, want to reset a form field as a result of another field changing, you would use the listener API.

Imagine the following user flow:

- User selects a country from a drop-down.
- User then selects a county from another drop-down.
- User changes the selected country to a different one.

In this example, when the user changes the country, the selected county needs to be reset as it's no longer valid. With the listener API, we can subscribe to the onChange event and dispatch a reset to the field "county" when the listener is fired.

Other events that can be "listened" to are:

- onChange
- onBlur
- onMount
- onSubmit

```tsx
function App() {
const form = useForm({
defaultValues: {
country: '',
county: '',
},
// ...
})

return (
<div>
<form.Field name="country">
{(field) => (
<label>
<div>Country</div>
<input
value={field.state.value}
onChange={(e) => field.handleChange(e.target.value)}
listener={onChange: ({ value }) => {
form.reset({county: ''})
}}
/>
</label>
)}
</form.Field>

<form.Field name="county">
{(field) => (
<label>
<div>County</div>
<input
value={field.state.value}
onChange={(e) => field.handleChange(e.target.value)}
/>
</label>
)}
</form.Field>
</div>
)
}
```

0 comments on commit 50c19b5

Please sign in to comment.