From bb882ddfa35212e5bc240266e95e9daf71844207 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Thu, 24 Apr 2025 13:53:23 +1000 Subject: [PATCH] Per review, simplify disabling of default action Turns out you can just call `event.preventDefault()` on the action and there is no need actually use hotkeys to do that. --- .../tag_list_input_controller.js | 11 +++-------- .../stimulus/tag_list_input_controller_test.js | 8 -------- 2 files changed, 3 insertions(+), 16 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 f0e9e3555b..47ddf44e23 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 @@ -3,15 +3,10 @@ import { Controller } from "stimulus"; export default class extends Controller { static targets = ["tagList", "newTag", "template", "list"]; - connect() { - // form hotkeys are enabled for "input" scope, we disable the form submit via enter - // on the tag input, so we can use the enter key to create new product tag - hotkeys("enter", { scope: "input", element: this.newTagTarget }, function () { - event.preventDefault(); - }); - } + addTag(event) { + // prevent hotkey form submitting the form (default action for "enter" key) + event.preventDefault(); - addTag() { // Check if tag already exist const newTagName = this.newTagTarget.value.trim(); if (newTagName.length == 0) { diff --git a/spec/javascripts/stimulus/tag_list_input_controller_test.js b/spec/javascripts/stimulus/tag_list_input_controller_test.js index a0c9f89eb8..dd8461a360 100644 --- a/spec/javascripts/stimulus/tag_list_input_controller_test.js +++ b/spec/javascripts/stimulus/tag_list_input_controller_test.js @@ -9,14 +9,6 @@ describe("TagListInputController", () => { beforeAll(() => { const application = Application.start(); application.register("tag-list-input-component--tag-list-input", tag_list_input_controller); - - // Mock hotkeys.js - const mockedHotkeys = jest.fn(); - global.hotkeys = mockedHotkeys; - }); - - afterAll(() => { - delete global.hotkeys; }); beforeEach(() => {