-
-
Notifications
You must be signed in to change notification settings - Fork 657
fix(FieldApi): fix race condition when using onChangeListenTo #2143
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
Open
Pascalmh
wants to merge
11
commits into
TanStack:main
Choose a base branch
from
Pascalmh:bugfix/onChangeListenTo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c29bc74
fix(FieldApi): fix race condition when using onChangeListenTo
Pascalmh d13697d
chore: add validationCount
Pascalmh a1de83a
ci: apply automated fixes and generate docs
autofix-ci[bot] d0cc2c2
Apply suggestion from @coderabbitai[bot]
Pascalmh 1404e24
fixup! chore: add validationCount
Pascalmh 3137ac3
fixup! chore: add validationCount
Pascalmh 72dde5f
ci: apply automated fixes and generate docs
autofix-ci[bot] c437abe
ci: apply automated fixes and generate docs
autofix-ci[bot] 2a6895f
chore: cleanup
Pascalmh b5f8023
chore: cleanup
Pascalmh 9dc6dec
chore: cleanup
Pascalmh 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/form-core': patch | ||
| --- | ||
|
|
||
| fix race condition when listening to multiple Fields in onChangeListenTo |
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
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
149 changes: 149 additions & 0 deletions
149
packages/react-form/tests/onChangeListenTo.adapter.test.tsx
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,149 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { fireEvent, render } from '@testing-library/react' | ||
| import { useSelector } from '@tanstack/react-store' | ||
| import { StrictMode } from 'react' | ||
| import { useForm } from '../src/useForm' | ||
| import { sleep } from './utils' | ||
| import type { ReadonlyStore } from '@tanstack/react-store' | ||
|
|
||
| function DebugSubscribe({ store }: { store: ReadonlyStore<any> }) { | ||
| const isFieldsValidating = useSelector(store, (s) => s.isFieldsValidating) | ||
| return ( | ||
| <span data-testid="isFieldsValidating">{String(isFieldsValidating)}</span> | ||
| ) | ||
| } | ||
|
|
||
| describe('React adapter - onChangeListenTo race', () => { | ||
| it('does not leave linked fields stuck in isValidating when multiple rapid updates occur', async () => { | ||
| vi.useFakeTimers() | ||
|
|
||
| const validationFn = vi.fn() | ||
|
|
||
| function Comp() { | ||
| const form = useForm({ | ||
| defaultValues: { | ||
| street: '', | ||
| houseNo: '', | ||
| zipCode: '', | ||
| city: '', | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <> | ||
| <form.Field | ||
| name="street" | ||
| validators={{ | ||
| onChangeListenTo: ['houseNo', 'zipCode', 'city'], | ||
| onChangeAsyncDebounceMs: 300, | ||
| onChangeAsync: async () => { | ||
| await sleep(500) | ||
| validationFn() | ||
| return undefined | ||
| }, | ||
| }} | ||
| children={(field) => ( | ||
| <div> | ||
| <input | ||
| data-testid="street" | ||
| value={field.state.value} | ||
| onChange={(e) => field.handleChange(e.target.value)} | ||
| /> | ||
| <span data-testid="street-validating"> | ||
| {String(field.state.meta.isValidating)} | ||
| </span> | ||
| </div> | ||
| )} | ||
| /> | ||
|
|
||
| <form.Field | ||
| name="houseNo" | ||
| children={(field) => ( | ||
| <div> | ||
| <input | ||
| data-testid="houseNo" | ||
| value={field.state.value} | ||
| onChange={(e) => field.handleChange(e.target.value)} | ||
| /> | ||
| <span data-testid="houseNo-validating"> | ||
| {String(field.state.meta.isValidating)} | ||
| </span> | ||
| </div> | ||
| )} | ||
| /> | ||
|
|
||
| <form.Field | ||
| name="zipCode" | ||
| children={(field) => ( | ||
| <div> | ||
| <input | ||
| data-testid="zipCode" | ||
| value={field.state.value} | ||
| onChange={(e) => field.handleChange(e.target.value)} | ||
| /> | ||
| <span data-testid="zipCode-validating"> | ||
| {String(field.state.meta.isValidating)} | ||
| </span> | ||
| </div> | ||
| )} | ||
| /> | ||
|
|
||
| <form.Field | ||
| name="city" | ||
| children={(field) => ( | ||
| <div> | ||
| <input | ||
| data-testid="city" | ||
| value={field.state.value} | ||
| onChange={(e) => field.handleChange(e.target.value)} | ||
| /> | ||
| <span data-testid="city-validating"> | ||
| {String(field.state.meta.isValidating)} | ||
| </span> | ||
| </div> | ||
| )} | ||
| /> | ||
|
|
||
| <DebugSubscribe store={form.store} /> | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| const { getByTestId } = render( | ||
| <StrictMode> | ||
| <Comp /> | ||
| </StrictMode>, | ||
| ) | ||
|
|
||
| const street = getByTestId('street') as HTMLInputElement | ||
| const houseNo = getByTestId('houseNo') as HTMLInputElement | ||
| const zipCode = getByTestId('zipCode') as HTMLInputElement | ||
| const city = getByTestId('city') as HTMLInputElement | ||
|
|
||
| // Simulate rapid updates (autofill) | ||
| fireEvent.change(street, { target: { value: 'Foo Street' } }) | ||
| fireEvent.change(houseNo, { target: { value: '2' } }) | ||
| fireEvent.change(zipCode, { target: { value: '12345' } }) | ||
| fireEvent.change(city, { target: { value: 'Barrington' } }) | ||
|
|
||
| // Run debounce + async validation | ||
| await vi.runAllTimersAsync() | ||
|
|
||
| expect(validationFn).toHaveBeenCalledTimes(1) | ||
|
|
||
| // Verify validation flags are not stuck | ||
| const isFieldsValidating = getByTestId('isFieldsValidating').textContent | ||
| const streetValidating = getByTestId('street-validating').textContent | ||
| const houseValidating = getByTestId('houseNo-validating').textContent | ||
| const zipValidating = getByTestId('zipCode-validating').textContent | ||
| const cityValidating = getByTestId('city-validating').textContent | ||
|
|
||
| expect(isFieldsValidating).toBe('false') | ||
| expect(streetValidating).toBe('false') | ||
| expect(houseValidating).toBe('false') | ||
| expect(zipValidating).toBe('false') | ||
| expect(cityValidating).toBe('false') | ||
|
|
||
| vi.useRealTimers() | ||
| }) | ||
| }) |
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.