Skip to content

Commit b9fd448

Browse files
authored
Fix: clear value for a list attribute using UI edit form (#6008)
1 parent 688680d commit b9fd448

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

changelog/5934.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an issue where list attribute could not be cleared using UI edit form.

frontend/app/src/shared/components/form/fields/list.field.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,28 @@ describe("List Field Component", () => {
161161
// THEN
162162
expect(formValue).toEqual({ field1: { source: { type: "user" }, value: ["test item"] } });
163163
});
164+
165+
test("returns null when list is cleared", async () => {
166+
// GIVEN
167+
let formValue;
168+
const defaultValue: FormAttributeValue = {
169+
source: { type: "user" },
170+
value: ["item 1", "item 2"],
171+
};
172+
const component = render(
173+
<TestForm onSubmit={(formData) => (formValue = formData)}>
174+
<ListField name="field1" label="Test List" defaultValue={defaultValue} />
175+
</TestForm>
176+
);
177+
178+
// WHEN
179+
await component.getByRole("button", { name: "Remove item 1" }).click();
180+
await component.getByRole("button", { name: "Remove item 2" }).click();
181+
await component.getByRole("button", { name: "Submit" }).click();
182+
183+
// THEN
184+
expect(formValue).toEqual({
185+
field1: { source: { type: "user" }, value: null },
186+
});
187+
});
164188
});

frontend/app/src/shared/components/form/fields/list.field.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ const ListField = ({
5050
{...props}
5151
value={fieldData?.value ?? ""}
5252
onChange={(newValue) => {
53-
field.onChange(updateFormFieldValue(newValue, defaultValue));
53+
field.onChange(
54+
updateFormFieldValue(newValue.length > 0 ? newValue : null, defaultValue)
55+
);
5456
}}
5557
/>
5658
</FormInput>

frontend/app/src/shared/components/list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const ListItems = ({
9595
variant="ghost"
9696
onClick={() => onDelete(item)}
9797
className="text-gray-500 hover:text-gray-800 h-4 w-4"
98-
aria-label="Remove"
98+
aria-label={`Remove ${item}`}
9999
>
100100
&times;
101101
</Button>

0 commit comments

Comments
 (0)