-
-
Notifications
You must be signed in to change notification settings - Fork 582
fix(form-core): establish Field-over-Form prioritization for isDefaultValue and resets #2006
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
Kyujenius
wants to merge
4
commits into
TanStack:main
Choose a base branch
from
Kyujenius:fix/isDefaultValue-logic
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.
+136
−21
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4567071
fix(form-core): unify prioritized default logic for isDefaultValue an…
Kyujenius 75dbab5
ci: apply automated fixes and generate docs
autofix-ci[bot] 9dead10
chore(form-core): suppress eslint false positives for optional chaining
Kyujenius 53da788
test(form-core): add coverage for field-level defaultValue priority i…
Kyujenius 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': minor | ||
| --- | ||
|
|
||
| Introduced a **Prioritized Default System** that ensures consistency between field metadata and form reset behavior. This change prioritizes field-level default values over form-level defaults across `isDefaultValue` derivation, `form.reset()`, and `form.resetField()`. This ensures that field metadata accurately reflects the state the form would return to upon reset and prevents `undefined` from being incorrectly treated as a default when a value is explicitly specified. |
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 |
|---|---|---|
|
|
@@ -120,7 +120,7 @@ describe('field api', () => { | |
| expect(field.getMeta().isDefaultValue).toBe(false) | ||
|
|
||
| field.setValue('test') | ||
| expect(field.getMeta().isDefaultValue).toBe(true) | ||
| expect(field.getMeta().isDefaultValue).toBe(false) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reminder to self: Check git blame. I don't this change is good, but I want to know the context of why it was explicitly listed as unit test. |
||
|
|
||
| form.resetField('name') | ||
| expect(field.getMeta().isDefaultValue).toBe(true) | ||
|
|
@@ -130,6 +130,54 @@ describe('field api', () => { | |
| expect(field.getMeta().isDefaultValue).toBe(true) | ||
| }) | ||
|
|
||
| it('should be false when value is undefined and a default value is specified in form-level only', () => { | ||
| const form = new FormApi({ | ||
| defaultValues: { | ||
| name: 'foo', | ||
| }, | ||
| }) | ||
| form.mount() | ||
|
|
||
| const field = new FieldApi({ | ||
| form, | ||
| name: 'name', | ||
| }) | ||
| field.mount() | ||
|
|
||
| expect(field.getMeta().isDefaultValue).toBe(true) | ||
|
|
||
| // Set to undefined - should be false because 'foo' is the default | ||
| field.setValue(undefined as any) | ||
| expect(field.getMeta().isDefaultValue).toBe(false) | ||
| }) | ||
|
|
||
| it('should handle falsy values correctly in isDefaultValue', () => { | ||
| const form = new FormApi({ | ||
| defaultValues: { | ||
| count: 0, | ||
| active: false, | ||
| text: '', | ||
| }, | ||
| }) | ||
| form.mount() | ||
|
|
||
| const countField = new FieldApi({ form, name: 'count' }) | ||
| const activeField = new FieldApi({ form, name: 'active' }) | ||
| const textField = new FieldApi({ form, name: 'text' }) | ||
| countField.mount() | ||
| activeField.mount() | ||
| textField.mount() | ||
|
|
||
| expect(countField.getMeta().isDefaultValue).toBe(true) | ||
| expect(activeField.getMeta().isDefaultValue).toBe(true) | ||
| expect(textField.getMeta().isDefaultValue).toBe(true) | ||
|
|
||
| countField.setValue(1) | ||
| expect(countField.getMeta().isDefaultValue).toBe(false) | ||
| countField.setValue(0) | ||
| expect(countField.getMeta().isDefaultValue).toBe(true) | ||
| }) | ||
|
|
||
| it('should update the fields meta isDefaultValue with arrays - simple', () => { | ||
| const form = new FormApi({ | ||
| defaultValues: { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This lint error seems odd. Which part does it think does not need an optional chain?
From a surface level, this looks like a wrong type that should be adjusted to reflect (potential) runtime values.