Skip to content

Feature request: allow extending defaultEventNames mapping #660

Closed
@grncdr

Description

@grncdr

The logic for shorthand/default event names in stimulus is currently defined here:

const defaultEventNames: { [tagName: string]: (element: Element) => string } = {
a: () => "click",
button: () => "click",
form: () => "submit",
details: () => "toggle",
input: (e) => (e.getAttribute("type") == "submit" ? "click" : "input"),
select: () => "change",
textarea: () => "input",
}

This is pretty handy, but if you are using a library of custom elements (e.g. shoelace) you have to turn <button data-action="controller#method"></button> into <sl-button data-action="click->controller#method"></sl-button>.

It's a tiny thing, but if stimulus were to simply export that mapping it would be possible for users to do something like:

import { defaultEventNames } from '@hotwired/stimulus'

defaultEventNames['sl-button'] = () => 'click'
<sl-button data-action="controller#method"></sl-button>

If exporting the mapping directly is exposing too many implementation details, a function like setDefaultEventName(elementName, stringOrCallback) would also be pretty nice.

If that makes sense to the maintainers I can take a crack at a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions