Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/components/mui/__tests__/additional-input-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,47 @@ describe("AdditionalInputList", () => {
expect(screen.getByTestId("field-count")).toHaveTextContent("2");
});
});

test("new meta field starts with empty values when an existing field already has values", async () => {
let capturedFields = null;

const TestWrapper = () => {
const { values } = useFormikContext();
capturedFields = values.meta_fields;
return <AdditionalInputList {...defaultProps} />;
};

render(
<Formik
initialValues={{
meta_fields: [
{
id: 1,
name: "Color",
type: "CheckBoxList",
is_required: false,
minimum_quantity: 0,
maximum_quantity: 0,
values: [{ name: "Red", value: "red", is_default: false }]
}
]
}}
onSubmit={jest.fn()}
>
<Form>
<TestWrapper />
</Form>
</Formik>
);

const addButton = screen.getByTestId("add-btn-0");
await userEvent.click(addButton);

await waitFor(() => {
expect(capturedFields).toHaveLength(2);
expect(capturedFields[1].values).toEqual([]);
});
});
});

describe("handleRemove", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ const MetaFieldValues = ({
};

const handleAddValue = () => {
const newFields = [...metaFields];
newFields[fieldIndex].values.push({
value: "",
name: "",
is_default: false
});
const newFields = metaFields.map((f, i) =>
i === fieldIndex
? { ...f, values: [...f.values, { value: "", name: "", is_default: false }] }
: f
Comment on lines +47 to +50

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Initialize order when appending a new value.

At Line 49, the new object omits order, but this component sorts by order (Line 35) and uses it for DnD updates. This can cause unstable ordering for newly added entries.

Suggested fix
-    const newFields = metaFields.map((f, i) =>
-      i === fieldIndex
-        ? { ...f, values: [...f.values, { value: "", name: "", is_default: false }] }
-        : f
-    );
+    const newFields = metaFields.map((f, i) => {
+      if (i !== fieldIndex) return f;
+      const nextOrder =
+        f.values.reduce((max, v) => Math.max(max, Number(v.order) || 0), 0) + 1;
+      return {
+        ...f,
+        values: [
+          ...f.values,
+          { value: "", name: "", is_default: false, order: nextOrder }
+        ]
+      };
+    });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/mui/formik-inputs/additional-input/meta-field-values.js`
around lines 47 - 50, When appending a new value in the metaFields map (inside
the expression that produces newFields and compares i === fieldIndex), include
an initialized order property so sorting and DnD remain stable; set order to the
next index (e.g., use f.values.length or compute
Math.max(...f.values.map(v=>v.order))+1) when creating the new value object for
values: [...f.values, { value: "", name: "", is_default: false, order:
<nextOrder> }].

);
setFieldValue(baseName, newFields);
};

Expand Down
Loading