From b2a3715a8b045d7bdc2d97db08716177ba61ee21 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 16 Jul 2025 13:05:20 +1000 Subject: [PATCH] Fix bug Don't add a leading coma when the tag list is empty --- .../tag_list_input_controller.js | 8 ++++++-- .../stimulus/tag_list_input_controller_test.js | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/components/tag_list_input_component/tag_list_input_controller.js b/app/components/tag_list_input_component/tag_list_input_controller.js index 5fb0795e59..2ded4bb5a7 100644 --- a/app/components/tag_list_input_component/tag_list_input_controller.js +++ b/app/components/tag_list_input_component/tag_list_input_controller.js @@ -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")); diff --git a/spec/javascripts/stimulus/tag_list_input_controller_test.js b/spec/javascripts/stimulus/tag_list_input_controller_test.js index 7df91dd750..00385a6623 100644 --- a/spec/javascripts/stimulus/tag_list_input_controller_test.js +++ b/spec/javascripts/stimulus/tag_list_input_controller_test.js @@ -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", () => {