-
-
Notifications
You must be signed in to change notification settings - Fork 542
feat(core): add field listeners #1032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2aa4316
feat: add field listeners
ha1fstack ebcdf83
feat: add onBlur listener
e1716e0
feat: add onSubmit listener
da975e5
ci: apply automated fixes and generate docs
autofix-ci[bot] 79d1dad
fix: stop onChange triggering onMount and add tests to assert this
a88f993
feat: add listeners react documentation
084e8da
fix: pull request comments
a0dd15c
ci: apply automated fixes and generate docs
autofix-ci[bot] 78811fb
fix: pull request comments
42d1a73
feat: Vue docs
e07a05b
feat: Angular docs
1ce3541
feat: expose listeners on anguar wrapper and update docs
Balastrong 95d6815
docs: fix react
Balastrong bc86f5e
docs: fix vue
Balastrong dba1652
ci: apply automated fixes and generate docs
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.