1

I've run into an issue. I'm using Stimulus within HAML and calling a controller using the keydown action. The event listener gets called twice.

This is my HAML code and controller code:

.comments-container{ "data-controller": "comments"}
%h3.short.pull-left
    = current_tenant.label_for_field(:comment).try(:pluralize)
.button-group.action-bar
    %a.fa.fa-cloud-download.export-comments-btn{ title: "Export" }
    %a.fa.fa-archive.archive-toggle-btn{ title: "View Comments Archives" }
.comment-form
    %form
        .form-group
            %textarea.form-control{ name: "Content" , "data-target": "comments.content" }
            .tag-inputs
                %span.help-block
                    Enter a tag followed by the tab, enter, comma or semi-colon key
                %span
                    %label.tags-input-label
                        Tags:
                    %div{ "data-target": "comments.tagsContainer" }
                    %input.tags-input-input{ type: "text" , "data-action": "keydown->comments#createTag" }
            .tag-inputs
                %span
                    %label.tags-input-label
                        Users to notify:
                    %select{ type: "select" , "data-action": "comments#notifiedUsers"}
                        = options_for_select(current_tenant.users.order(email: :asc).pluck(:email, :id).unshift(["Please Select a User",nil]))
            %button.btn.btn-info{ type: "submit" , "data-action": "comments#submit"}
                = "Post #{current_tenant.label_for_field(:comment)}"
.comment-search
    %input.search{ placeholder: "Search Comments" }
.comments-items
    = render @comments

controller.js:

    import { Controller } from "stimulus"

const getCSRFToken = () => { 
    return Array.from(document.getElementsByTagName('meta')).find(meta => meta.name === 'csrf-token').content
  }

export default class extends Controller {
    static targets = ["content", "tagsContainer", "tag"]
    connect() {

    }
    submit(e) {
        e.preventDefault()
        console.log(this.contentTarget.value)
    }
    createTag(e) {
        const keys = ["Enter", "Semicolon", "Tab", "Comma"]
        if(keys.includes(e.key)){
            e.preventDefault()
            console.log("Anything")
            this.tagsContainerTarget.insertAdjacentHTML('beforeend', `<span class="tags-input-tag"> ${e.currentTarget.value} </span>`)
        }
    }
    notifiedUsers(e) {
        e.preventDefault()
        //console.log(e.contentTarget.value)
        console.log(e.type)
    }
}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Welcome to Stack Overflow. We don't care what your experience level is, we just care that you've asked a good question. "[Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)" is a great resource to help you do so. – the Tin Man Dec 07 '19 at 03:08
  • Thanks @theTinMan. Your blog post was extremely helpful. –  Dec 08 '19 at 14:05
  • @theTinMan have you considered your answer may new people off the site? You haven't specifically stated what he "did wrong". His problem (like mine is quite) common (https://stackoverflow.com/questions/66618415/why-is-my-stimulus-js-controller-firing-twice). Specifically I think OP posted too much unrelevant code, you should: 1) strive for conciseness 2) search if others had the same issue – Adit Saxena Apr 02 '22 at 12:48

0 Answers0