Don't add a leading coma when the tag list is empty
This commit is contained in:
Gaetan Craig-Riou
2025-07-16 13:05:20 +10:00
parent 693789d526
commit b2a3715a8b
2 changed files with 17 additions and 2 deletions

View File

@@ -22,7 +22,11 @@ export default class extends Controller {
}
// add to tagList
this.tagListTarget.value = this.tagListTarget.value.concat(`,${newTagName}`);
if (this.tagListTarget.value == "") {
this.tagListTarget.value = newTagName;
} else {
this.tagListTarget.value = this.tagListTarget.value.concat(`,${newTagName}`);
}
// Create new li component with value
const newTagElement = this.templateTarget.content.cloneNode(true);
@@ -40,7 +44,7 @@ export default class extends Controller {
// Remove tag from list
const tags = this.tagListTarget.value.split(",");
this.tagListTarget.value = tags.filter(tag => tag != tagName).join(",");
this.tagListTarget.value = tags.filter((tag) => tag != tagName).join(",");
// manualy dispatch an Input event so the change gets picked up by the bulk form controller
this.tagListTarget.dispatchEvent(new InputEvent("input"));

View File

@@ -126,6 +126,17 @@ describe("TagListInputController", () => {
expect(variant_add_tag.classList).toContain("tag-error");
});
});
describe("when no tag yet", () => {
it("doesn't include leading comma in hidden tag list input", () => {
variant_tag_list.value = "";
variant_add_tag.value = "latest";
variant_add_tag.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter" }));
expect(variant_tag_list.value).toBe("latest");
});
});
});
describe("removeTag", () => {