fix(components): [form] reset stale props on dynamic fields#23706
Conversation
|
👋 @l246804, seems like this is your first time contribution to element-plus. |
📝 WalkthroughWalkthroughThis PR fixes inconsistent behavior between Changes
Sequence DiagramsequenceDiagram
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
commit: |
There was a problem hiding this comment.
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).
|
🧪 Playground Preview: https://element-plus.run/?pr=23706 |
|
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. |
现在的改动对原有组件功能影响较小,当然如果有更好的实现方式也欢迎修改 😄 |
|
你能添加一个测试用例吗?晚点我试一下前面提到的方式是否更简单。 |
|
好 |
|
这个PR还有什么问题吗? |
f31d4ff to
595409b
Compare
btea
left a comment
There was a problem hiding this comment.
I personally believe that the current logic is more reasonable.
|
👍 |
|
@l246804 Thanks for your contribution! ❤️ |

closed #23705
Please make sure these boxes are checked before submitting your PR, thank you!
devbranch.Summary by CodeRabbit
Release Notes
Bug Fixes
Tests