-
-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add field listeners (#1032)
* feat: add field listeners * feat: add onBlur listener * feat: add onSubmit listener * ci: apply automated fixes and generate docs * fix: stop onChange triggering onMount and add tests to assert this * feat: add listeners react documentation * fix: pull request comments * ci: apply automated fixes and generate docs * fix: pull request comments * feat: Vue docs * feat: Angular docs * feat: expose listeners on anguar wrapper and update docs * docs: fix react * docs: fix vue * ci: apply automated fixes and generate docs --------- Co-authored-by: ha1fstack <[email protected]> Co-authored-by: Harry Whorlow <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Leonardo Montini <[email protected]>
- Loading branch information
1 parent
1db18b2
commit 6968cfd
Showing
22 changed files
with
749 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
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 province from another drop-down. | ||
- User changes the selected country to a different one. | ||
|
||
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 field "province" when the listener is fired. | ||
|
||
Events that can be "listened" to are: | ||
|
||
- onChange | ||
- onBlur | ||
- onMount | ||
- onSubmit | ||
|
||
```angular-ts | ||
@Component({ | ||
selector: 'app-root', | ||
standalone: true, | ||
imports: [TanStackField], | ||
template: ` | ||
<ng-container | ||
[tanstackField]="form" | ||
name="country" | ||
[listeners]="{ | ||
onChange: onCountryChange | ||
}" | ||
#country="field" | ||
></ng-container> | ||
<ng-container | ||
[tanstackField]="form" | ||
name="province" | ||
#province="field" | ||
></ng-container> | ||
`, | ||
}) | ||
export class AppComponent { | ||
form = injectForm({ | ||
defaultValues: { | ||
country: '', | ||
province: '', | ||
}, | ||
}) | ||
onCountryChange: FieldListenerFn<any, any, any, any, string> = ({ | ||
value, | ||
}) => { | ||
console.log(`Country changed to: ${value}, resetting province`) | ||
this.form.setFieldValue('province', '') | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
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 province from another drop-down. | ||
- User changes the selected country to a different one. | ||
|
||
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 field "province" when the listener is fired. | ||
|
||
Events that can be "listened" to are: | ||
|
||
- onChange | ||
- onBlur | ||
- onMount | ||
- onSubmit | ||
|
||
```tsx | ||
function App() { | ||
const form = useForm({ | ||
defaultValues: { | ||
country: '', | ||
province: '', | ||
}, | ||
// ... | ||
}) | ||
|
||
return ( | ||
<div> | ||
<form.Field name="country"> | ||
{(field) => ( | ||
<label> | ||
<div>Country</div> | ||
<input | ||
value={field.state.value} | ||
onChange={(e) => field.handleChange(e.target.value)} | ||
listeners={{ | ||
onChange: ({ value }) => { | ||
console.log(`Country changed to: ${value}, resetting province`) | ||
form.setFieldValue('province', '') | ||
} | ||
}} | ||
/> | ||
</label> | ||
)} | ||
</form.Field> | ||
|
||
<form.Field name="province"> | ||
{(field) => ( | ||
<label> | ||
<div>Province</div> | ||
<input | ||
value={field.state.value} | ||
onChange={(e) => field.handleChange(e.target.value)} | ||
/> | ||
</label> | ||
)} | ||
</form.Field> | ||
</div> | ||
) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.