Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions docs/framework/vue/guides/listeners.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,64 @@ const form = useForm({
</div>
</template>
```

### Built-in Debouncing
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟑 Minor | ⚑ Quick win

Fix heading levels to follow markdown hierarchy.

Both new section headings use h3 (###) but should use h2 (##) since there are no h2 headings in this document. This violates markdown heading hierarchy rules.

πŸ“ Proposed fix
-### Built-in Debouncing
+## Built-in Debouncing
-### Form listeners
+## Form listeners

Also applies to: 90-90

🧰 Tools
πŸͺ› markdownlint-cli2 (0.22.1)

[warning] 68-68: Heading levels should only increment by one level at a time
Expected: h2; Actual: h3

(MD001, heading-increment)

πŸ€– Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/framework/vue/guides/listeners.md` at line 68, Update the two section
headings currently written as "### Built-in Debouncing" (and the other heading
flagged around the same spot) to use H2 level (prefix with "##" instead of
"###") so the document has proper heading hierarchy; locate the headings by
their literal text ("Built-in Debouncing" and the other flagged heading text)
and change their markdown prefixes accordingly.


If you are making an API request inside a listener, you may want to debounce the calls as it can lead to performance issues.
We enable an easy method for debouncing your listeners by adding a `onChangeDebounceMs` or `onBlurDebounceMs`.

```vue
<form.Field
name="country"
:listeners="{
onChangeDebounceMs: 500,
onChange: ({ value }) => {
console.log(`Country changed to: ${value} without a change within 500ms, resetting province`)
form.setFieldValue('province', '')
},
}"
>
<template v-slot="{ field }">
<!-- ... -->
</template>
</form.Field>
```

### Form listeners

At a higher level, listeners are also available at the form level, allowing you access to the `onMount` and `onSubmit` events, and having `onChange` and `onBlur` propagated to all the form's children. Form-level listeners can also be debounced in the same way as previously discussed.

`onMount` and `onSubmit` listeners have the following parameters:

- `formApi`

`onChange` and `onBlur` listeners have access to:

- `fieldApi`
- `formApi`

```vue
<script setup>
import { useForm } from '@tanstack/vue-form'

const form = useForm({
listeners: {
onMount: ({ formApi }) => {
// custom logging service
loggingService('mount', formApi.state.values)
},

onChange: ({ formApi, fieldApi }) => {
// autosave logic
if (formApi.state.isValid) {
formApi.handleSubmit()
}

// fieldApi represents the field that triggered the event.
console.log(fieldApi.name, fieldApi.state.value)
},
onChangeDebounceMs: 500,
},
})
</script>
```