From 1479be787bc7691365698df024e475f0f348d859 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 15 Apr 2025 15:31:31 +1000 Subject: [PATCH] Prevent adding empty tag --- .../tag_list_input_controller.js | 6 +++++- .../stimulus/tag_list_input_controller_test.js | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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 3d0ca779aa..f0e9e3555b 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 @@ -13,7 +13,11 @@ export default class extends Controller { addTag() { // Check if tag already exist - const newTagName = this.newTagTarget.value; + const newTagName = this.newTagTarget.value.trim(); + if (newTagName.length == 0) { + return; + } + const tags = this.tagListTarget.value.split(","); const index = tags.indexOf(newTagName); if (index != -1) { diff --git a/spec/javascripts/stimulus/tag_list_input_controller_test.js b/spec/javascripts/stimulus/tag_list_input_controller_test.js index 7ebfdadcb4..a0c9f89eb8 100644 --- a/spec/javascripts/stimulus/tag_list_input_controller_test.js +++ b/spec/javascripts/stimulus/tag_list_input_controller_test.js @@ -103,6 +103,18 @@ describe("TagListInputController", () => { expect(variant_add_tag.value).toBe(""); }); + describe("with an empty new tag", () => { + it("doesn't add the tag", () => { + variant_add_tag.value = " "; + variant_add_tag.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter" })); + + const tagList = document.getElementsByClassName("tag-list")[0]; + + // 1 template + 3 tags + new tag (added in the beforeEach) + expect(tagList.childElementCount).toBe(5); + }); + }); + describe("when tag already exist", () => { beforeEach(() => { // Trying to add an existing tag