fix(form-core): fix deleteField method #1809
Open
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.
🎯 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 callssetFieldValue
FieldApi.mount()
there was logic:I think this uncoditionally restored
defaultValue
on every mount.updatePS: Even if we add a check in
mount()
-setFieldValue
can 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_deletedFields
setFieldValue()
blocks operations on deleted fields✅ Checklist
pnpm test:pr
.🚀 Release Impact