|
| 1 | +--- |
| 2 | +id: listeners |
| 3 | +title: Side effects for event triggers |
| 4 | +--- |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +Imagine the following user flow: |
| 9 | + |
| 10 | +- User selects a country from a drop-down. |
| 11 | +- User then selects a province from another drop-down. |
| 12 | +- User changes the selected country to a different one. |
| 13 | + |
| 14 | +In this example, when the user changes the country, the selected province 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 "province" field when the listener is fired. |
| 15 | + |
| 16 | +Events that can be "listened" to are: |
| 17 | + |
| 18 | +- `onChange` |
| 19 | +- `onBlur` |
| 20 | +- `onMount` |
| 21 | +- `onSubmit` |
| 22 | +- `onUnmount` |
| 23 | + |
| 24 | +```ts |
| 25 | +${this.#form.field( |
| 26 | + { |
| 27 | + name: 'country', |
| 28 | + listeners: { |
| 29 | + onChange: ({ value }) => { |
| 30 | + console.log(`Country changed to: ${value}, resetting province`) |
| 31 | + this.#form.api.setFieldValue('province', '') |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + (field) => { |
| 36 | + return html` |
| 37 | + <label> |
| 38 | + <div>Country</div> |
| 39 | + <input |
| 40 | + .value="${field.state.value}" |
| 41 | + @input="${(e: Event) => { |
| 42 | + const target = e.target as HTMLInputElement |
| 43 | + field.handleChange(target.value) |
| 44 | + }}" |
| 45 | + /> |
| 46 | + </label> |
| 47 | + ` |
| 48 | + }, |
| 49 | +)} |
| 50 | + |
| 51 | +${this.#form.field( |
| 52 | + { name: 'province' }, |
| 53 | + (field) => { |
| 54 | + return html` |
| 55 | + <label> |
| 56 | + <div>Province</div> |
| 57 | + <input |
| 58 | + .value="${field.state.value}" |
| 59 | + @input="${(e: Event) => { |
| 60 | + const target = e.target as HTMLInputElement |
| 61 | + field.handleChange(target.value) |
| 62 | + }}" |
| 63 | + /> |
| 64 | + </label> |
| 65 | + ` |
| 66 | + }, |
| 67 | +)} |
| 68 | +``` |
| 69 | + |
| 70 | +## Built-in Debouncing |
| 71 | + |
| 72 | +If you are making an API request inside a listener, you may want to debounce the calls as it can lead to performance issues. |
| 73 | +We enable an easy method for debouncing your listeners by adding a `onChangeDebounceMs` or `onBlurDebounceMs`. |
| 74 | + |
| 75 | +```ts |
| 76 | +${this.#form.field( |
| 77 | + { |
| 78 | + name: 'country', |
| 79 | + listeners: { |
| 80 | + onChangeDebounceMs: 500, // 500ms debounce |
| 81 | + onChange: ({ value }) => { |
| 82 | + console.log(`Country changed to: ${value} without a change within 500ms, resetting province`) |
| 83 | + this.#form.api.setFieldValue('province', '') |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + (field) => { |
| 88 | + return html`<!-- ... -->` |
| 89 | + }, |
| 90 | +)} |
| 91 | +``` |
| 92 | + |
| 93 | +## Form listeners |
| 94 | + |
| 95 | +At a higher level, listeners are also available at the form level, allowing you access to the `onMount` and `onSubmit` events, and having `onChange`, `onBlur`, and `onUnmount` propagated to all the form's children. Form-level listeners can also be debounced in the same way as previously discussed. |
| 96 | + |
| 97 | +`onMount` and `onSubmit` listeners have the following parameters: |
| 98 | + |
| 99 | +- `formApi` |
| 100 | + |
| 101 | +`onChange`, `onBlur`, and `onUnmount` listeners have access to: |
| 102 | + |
| 103 | +- `fieldApi` |
| 104 | +- `formApi` |
| 105 | + |
| 106 | +```ts |
| 107 | +#form = new TanStackFormController(this, { |
| 108 | + listeners: { |
| 109 | + onMount: ({ formApi }) => { |
| 110 | + // custom logging service |
| 111 | + loggingService('mount', formApi.state.values) |
| 112 | + }, |
| 113 | + |
| 114 | + onChange: ({ formApi, fieldApi }) => { |
| 115 | + // autosave logic |
| 116 | + if (formApi.state.isValid) { |
| 117 | + formApi.handleSubmit() |
| 118 | + } |
| 119 | + |
| 120 | + // fieldApi represents the field that triggered the event. |
| 121 | + console.log(fieldApi.name, fieldApi.state.value) |
| 122 | + }, |
| 123 | + onChangeDebounceMs: 500, |
| 124 | + }, |
| 125 | +}) |
| 126 | +``` |
0 commit comments