Skip to content

Commit 4005416

Browse files
committed
chore: clean up and docs
1 parent 5d52666 commit 4005416

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

docs/framework/react/guides/listeners.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,34 @@ We enable an easy method for debouncing your listeners by adding a `onChangeDebo
8989
)}
9090
</form.Field>
9191
```
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` and `onBlur` propagated to all the form's children.
96+
97+
`onMount` and `onSubmit` listeners have to following props:
98+
99+
- formApi
100+
101+
`onChange` and `onBlur` listeners have access to formApi, as well as:
102+
103+
- fieldName
104+
- fieldApi
105+
106+
```tsx
107+
const form = useForm({
108+
listeners: {
109+
onMount: ({ formApi }) => {
110+
// custom logging service
111+
loggingService('mount', formApi.state.values)
112+
},
113+
114+
onChange: ({ formApi }) => {
115+
// autosave logic
116+
if (formApi.state.isValid) {
117+
formApi.handleSubmit()
118+
}
119+
},
120+
},
121+
})
122+
```

packages/form-core/src/FieldApi.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,11 @@ export class FieldApi<
16451645

16461646
private triggerOnBlurListener() {
16471647
const debounceMs = this.options.listeners?.onBlurDebounceMs
1648+
this.form.options.listeners?.onBlur?.({
1649+
fieldName: this.name,
1650+
formApi: this.form,
1651+
fieldApi: this,
1652+
})
16481653

16491654
if (debounceMs && debounceMs > 0) {
16501655
if (this.timeoutIds.listeners.blur) {
@@ -1656,30 +1661,24 @@ export class FieldApi<
16561661
value: this.state.value,
16571662
fieldApi: this,
16581663
})
1659-
1660-
this.form.options.listeners?.onBlur?.({
1661-
fieldName: this.name,
1662-
formApi: this.form,
1663-
fieldApi: this,
1664-
})
16651664
}, debounceMs)
16661665
} else {
16671666
this.options.listeners?.onBlur?.({
16681667
value: this.state.value,
16691668
fieldApi: this,
16701669
})
1671-
1672-
this.form.options.listeners?.onBlur?.({
1673-
fieldName: this.name,
1674-
formApi: this.form,
1675-
fieldApi: this,
1676-
})
16771670
}
16781671
}
16791672

16801673
private triggerOnChangeListener() {
16811674
const debounceMs = this.options.listeners?.onChangeDebounceMs
16821675

1676+
this.form.options.listeners?.onChange?.({
1677+
fieldName: this.name as never,
1678+
formApi: this.form,
1679+
fieldApi: this,
1680+
})
1681+
16831682
if (debounceMs && debounceMs > 0) {
16841683
if (this.timeoutIds.listeners.change) {
16851684
clearTimeout(this.timeoutIds.listeners.change)
@@ -1690,24 +1689,12 @@ export class FieldApi<
16901689
value: this.state.value,
16911690
fieldApi: this,
16921691
})
1693-
1694-
this.form.options.listeners?.onChange?.({
1695-
fieldName: this.name as never,
1696-
formApi: this.form,
1697-
fieldApi: this,
1698-
})
16991692
}, debounceMs)
17001693
} else {
17011694
this.options.listeners?.onChange?.({
17021695
value: this.state.value,
17031696
fieldApi: this,
17041697
})
1705-
1706-
this.form.options.listeners?.onChange?.({
1707-
fieldName: this.name as never,
1708-
formApi: this.form,
1709-
fieldApi: this,
1710-
})
17111698
}
17121699
}
17131700
}

0 commit comments

Comments
 (0)