Skip to content

fix(components): [form] reset stale props on dynamic fields#23706

Merged
btea merged 5 commits into
element-plus:devfrom
l246804:fix/resetFields
Mar 30, 2026
Merged

fix(components): [form] reset stale props on dynamic fields#23706
btea merged 5 commits into
element-plus:devfrom
l246804:fix/resetFields

Conversation

@l246804
Copy link
Copy Markdown
Contributor

@l246804 l246804 commented Mar 2, 2026

closed #23705

Please make sure these boxes are checked before submitting your PR, thank you!

  • Make sure you follow contributing guide English | (中文 | Español | Français).
  • Make sure you are merging your commits to dev branch.
  • Add some descriptions and refer to relative issues for your PR.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Prevents duplicate field registration in forms.
    • Fixed reset behavior for dynamic fields—resetting individual or all fields now correctly handles fields with changing properties without affecting other fields.
  • Tests

    • Added test coverage for dynamic field reset scenarios.

@pull-request-triage
Copy link
Copy Markdown

👋 @l246804, seems like this is your first time contribution to element-plus.
Please make sure that you have read our guidelines and code of conduct before making a contribution.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 2, 2026

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 2, 2026

📝 Walkthrough

Walkthrough

This PR fixes inconsistent behavior between Form.resetFields() and FormItem.resetField() when a FormItem's prop is dynamic. Changes include preventing duplicate field entries, updating the removeField signature to accept an optional oldPropString parameter for stale entry cleanup, tracking initial values separately for remounting support, and adding a watcher to re-register fields when their prop changes.

Changes

Cohort / File(s) Summary
Core Form Field Lifecycle
packages/components/form/src/form.vue, packages/components/form/src/form-item.vue, packages/components/form/src/types.ts
Added deduplication logic in addField, updated removeField signature to handle stale entries during prop changes (via oldPropString), introduced getInitialValue() method on FormItemContext, and added a propString watcher in FormItem to re-register fields when props change dynamically.
Tests
packages/components/form/__tests__/form.test.tsx
Added test case verifying that resetField() and resetFields() correctly handle dynamic prop switches, ensuring only the active field is reset while preserving initial values for previously active fields.

Sequence Diagram

sequenceDiagram
    actor User
    participant FormItem
    participant Form
    participant InitialValues as Form<br/>InitialValues<br/>Store
    participant Fields as Form<br/>Fields<br/>Array

    User->>FormItem: Change prop<br/>(e.g., "name" → "name2")
    activate FormItem
    FormItem->>FormItem: Detect propString change<br/>via watcher
    
    FormItem->>Form: removeField(context,<br/>oldPropString:"name")
    activate Form
    Form->>InitialValues: Delete stale entry<br/>for "name"
    Form->>Form: Return (no fields removal)
    deactivate Form
    
    FormItem->>FormItem: Re-initialize<br/>initialValue from<br/>current fieldValue
    
    FormItem->>Form: addField(context)
    activate Form
    Form->>Fields: Check for duplicates
    Form->>Fields: Add field if not<br/>already present
    deactivate Form
    deactivate FormItem
    
    User->>Form: Call resetFields()
    activate Form
    Form->>Fields: Iterate fields
    Form->>InitialValues: Lookup "name2"<br/>initial value
    Form->>Form: Reset only active field<br/>("name2")
    deactivate Form
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • btea
  • kooriookami

Poem

🐰 A rabbit's tale of props that dance,
No duplicates in the field's expanse!
When props do switch, old values stay,
Reset cleanly, the proper way! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description contains only a reference to the linked issue and an unchecked contributor checklist template, with no meaningful implementation details or explanation. Add a substantive description explaining the problem being fixed, the approach taken, and how dynamic prop changes are now handled correctly.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The code changes successfully address both core objectives: preventing duplicate field entries [23705], handling dynamic prop switches via a watcher [23705], and maintaining consistent reset behavior [23705].
Out of Scope Changes check ✅ Passed All changes are directly related to the linked issue #23705: form field lifecycle management, dynamic prop handling, initial value tracking, and reset consistency.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title directly and specifically describes the main change: fixing how form reset handles stale properties on dynamic fields, which is the core problem addressed across all modified files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 2, 2026

Open in StackBlitz

pnpm add https://pkg.pr.new/element-plus@23706
npm i https://pkg.pr.new/element-plus@23706
yarn add https://pkg.pr.new/[email protected]

commit: 115baa3

@l246804
Copy link
Copy Markdown
Contributor Author

l246804 commented Mar 2, 2026

@btea @Dsaquel PTAL

最主要的问题可能是 Form 里额外存储的 initialValues 在极端条件下可能与 FormItem 不同步。目前看来仍然需要在 removeField 时来缓存完整的 field 对象来确保重置逻辑一致。

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/components/form/src/utils.ts`:
- Around line 55-57: The current ternary treats a scalar string as a plain
string when skipNormalize is true, causing includes to do substring matches;
change the logic so normalized always uses ensureArray(props) to wrap scalars,
and only apply the array-to-dotstring transformation when skipNormalize is
false. Concretely, update the computation of normalized (referencing normalized,
skipNormalize, ensureArray, isArray, and props) so that when skipNormalize is
true you return ensureArray(props) unchanged, and when false you return
ensureArray(props).map(prop => isArray(prop) ? prop.join('.') : prop).

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 245722b and d9ac40b.

📒 Files selected for processing (2)
  • packages/components/form/src/form.vue
  • packages/components/form/src/utils.ts

Comment thread packages/components/form/src/utils.ts Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 2, 2026

🧪 Playground Preview: https://element-plus.run/?pr=23706
Please comment the example via this playground if needed.

@btea
Copy link
Copy Markdown
Member

btea commented Mar 10, 2026

This modification seems a bit cumbersome.

Perhaps we can watch for changes in props inside the form-item, pass out the new and old values, delete the cached old value inside the form component, and then re-run the original logic of caching the new field.

@l246804
Copy link
Copy Markdown
Contributor Author

l246804 commented Mar 10, 2026

This modification seems a bit cumbersome.

Perhaps we can watch for changes in props inside the form-item, pass out the new and old values, delete the cached old value inside the form component, and then re-run the original logic of caching the new field.

现在的改动对原有组件功能影响较小,当然如果有更好的实现方式也欢迎修改 😄

@btea
Copy link
Copy Markdown
Member

btea commented Mar 10, 2026

你能添加一个测试用例吗?晚点我试一下前面提到的方式是否更简单。

@l246804
Copy link
Copy Markdown
Contributor Author

l246804 commented Mar 10, 2026

Comment thread packages/components/form/__tests__/form.test.tsx Outdated
@l246804
Copy link
Copy Markdown
Contributor Author

l246804 commented Mar 26, 2026

这个PR还有什么问题吗?

@Dsaquel Dsaquel changed the title fix(components): resetFields fix(components): [form] reset stale props on dynamic fields Mar 29, 2026
Copy link
Copy Markdown
Member

@btea btea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally believe that the current logic is more reasonable.

@btea btea requested review from keeplearning66 and rzzf March 30, 2026 07:22
@l246804
Copy link
Copy Markdown
Contributor Author

l246804 commented Mar 30, 2026

👍

Comment thread packages/components/form/__tests__/form.test.tsx
Copy link
Copy Markdown
Member

@keeplearning66 keeplearning66 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@btea btea merged commit 6b77955 into element-plus:dev Mar 30, 2026
16 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

@l246804 Thanks for your contribution! ❤️

@element-bot element-bot mentioned this pull request Apr 10, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Component] [form, form-item] Form.resetFieldsFormItem.resetField 在动态 prop 时行为不一致

5 participants