-
-
Notifications
You must be signed in to change notification settings - Fork 565
fix(form-core): fix deleteField method #1809
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
Conversation
|
|
View your CI Pipeline Execution ↗ for commit 3b5d966
☁️ Nx Cloud last updated this comment at |
|
Blocked by #1729 . This seems critical to test with the linked PR. |
|
@kusiewicz can you fix merge conflicts and then @LeCarbonator if you could, re review for @kusiewicz since the blocker is merged? Thanks! |
Will do today! |
d0d2271 to
28cd997
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1809 +/- ##
==========================================
- Coverage 90.35% 89.74% -0.62%
==========================================
Files 38 48 +10
Lines 1752 1940 +188
Branches 444 483 +39
==========================================
+ Hits 1583 1741 +158
- Misses 149 178 +29
- Partials 20 21 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
LeCarbonator
left a comment
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.
Sort of looks promising, but looking at the cause of the initial problem, it was because of the way defaultValues were handled in fieldApi.update. They have been changed in the recent updates due to arrays being stale.
I can't reproduce the linked issue on the latest TSF version. Can you confirm?
If the issue still exists, then I think this implementation would make sense. Adding tests to avoid regression would be needed before a merge, but let's confirm that it's still a needed fix first.
| if (this._deletedFields.has(field as string)) { | ||
| return | ||
| } | ||
|
|
||
| if (!opts?.dontUpdateMeta) { | ||
| this._deletedFields.delete(field as string) | ||
| } |
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 feels like a no-op.
- If the field is in the set, you return early.
- If the field is not in the set, delete it from the set.
| const dontRunListeners = opts?.dontRunListeners ?? false | ||
| const dontValidate = opts?.dontValidate ?? false | ||
|
|
||
| if (this._deletedFields.has(field as string)) { |
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.
Use your newly added isFieldDeleted instead.
Guys, problem does not exist in the newest |
|
Well, glad to hear! Closing the PR, then. |
🎯 Changes
Fixes: #1808
If this idea make sense, I will add tests
After calling
form.deleteField('fieldName'), the field is removed fromform.state.values, but after a moment it returns with defaultValue and appears at the end of the values object.The problem occurred due to React lifecycle and the logic in
FieldApi.mount()Sequence of events:
delete->form.deleteField('field')executes -> value is remo ved fromform.state.values-> field metadata is removed - cool, butFieldApi.update()or lifecycle hooks -> this callssetFieldValueFieldApi.mount()there was logic:I think this uncoditionally restored
defaultValueon every mount.updatePS: Even if we add a check in
mount()-setFieldValuecan be called from other places (e.g., fromFieldApi.update()during React lifecycle).I added a mechanism for tracking intentionally deleted fields to prevent their their accidental restoration.
_deletedFields: Set<string>inFormApi- it stores names of fields that were intentionally deleted by the userdeleteField()adds the field to_deletedFieldssetFieldValue()blocks operations on deleted fields✅ Checklist
pnpm test:pr.🚀 Release Impact