Fix array editor element mode switch affect all the elements#1666
Conversation
📝 WalkthroughWalkthroughA utility function in the form array editor was modified to clone type objects instead of maintaining direct references. The change applies a shallow copy operation to prevent shared object references between array elements, addressing an issue where modifying one array element's state inadvertently affected all elements. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
workspaces/ballerina/ballerina-side-panel/src/components/editors/utils.ts (1)
180-183:⚠️ Potential issue | 🟡 MinorApply the same cloning fix to
getMapSubFormFieldFromTypesfor consistency.Line 182 passes
typesdirectly without cloning, which is the same pattern that caused the array editor bug. If map editors support similar mode-switching behavior, this could lead to the same issue where switching one map entry's input mode affects all entries.Proposed fix
{ key: `mp-val-${formId}`, label: "Value", type: getPrimaryInputType(types)?.fieldType || "", optional: false, editable: true, documentation: "", value: "", - types: types, + types: types.map(type => ({ ...type })), enabled: true }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@workspaces/ballerina/ballerina-side-panel/src/components/editors/utils.ts` around lines 180 - 183, getMapSubFormFieldFromTypes currently assigns the incoming types array directly to the returned object's types property, which can cause shared-reference bugs like the array editor issue; update getMapSubFormFieldFromTypes to clone the types array before assigning (e.g., use a shallow copy like [...types] or types.slice(), or a deeper clone if elements are mutated) so each map sub-form field gets its own types instance instead of a shared reference.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@workspaces/ballerina/ballerina-side-panel/src/components/editors/utils.ts`:
- Around line 180-183: getMapSubFormFieldFromTypes currently assigns the
incoming types array directly to the returned object's types property, which can
cause shared-reference bugs like the array editor issue; update
getMapSubFormFieldFromTypes to clone the types array before assigning (e.g., use
a shallow copy like [...types] or types.slice(), or a deeper clone if elements
are mutated) so each map sub-form field gets its own types instance instead of a
shared reference.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ab21da60-ac19-4c68-ad2d-ae19a458f0f0
📒 Files selected for processing (1)
workspaces/ballerina/ballerina-side-panel/src/components/editors/utils.ts
Purpose
Switching the input mode (e.g. Expression ↔ Text) on one element in an array editor field incorrectly changes the mode for all other elements in the array.
Resolves wso2/product-integrator#129
Goals
Ensure that mode switching in the array editor is scoped to the individual element being edited, with no effect on other elements in the same array.
Approach
The root cause was in
getArraySubFormFieldFromTypesinutils.ts. It was assigning the sametypesarray reference (from the field template) to every array element'sFormField. SinceFieldFactory.updateFieldTypesSelectionmutates theselectedproperty directly on those sharedInputTypeobjects, switching mode on one element propagated to all others.Fixed by deep-copying each
InputTypeobject when constructing the array element's field, so each element holds its own independenttypesarray:Summary by CodeRabbit