Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: allow extending defaultEventNames mapping #660

Open
grncdr opened this issue Feb 16, 2023 · 1 comment · May be fixed by #661 or #662
Open

Feature request: allow extending defaultEventNames mapping #660

grncdr opened this issue Feb 16, 2023 · 1 comment · May be fixed by #661 or #662

Comments

@grncdr
Copy link

grncdr commented Feb 16, 2023

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.

@marcoroth
Copy link
Member

marcoroth commented Feb 16, 2023

Thanks for opening this issue @grncdr!

I wouldn't be opposed to add support for something like this. I think it would make sense to provide some sort of public interface instead of just exporting the object.

grncdr added a commit to grncdr/stimulus that referenced this issue Feb 17, 2023
This allows configure default events for arbitrary elements.

Closes hotwired#660
@grncdr grncdr linked a pull request Feb 17, 2023 that will close this issue
grncdr added a commit to grncdr/stimulus that referenced this issue Feb 17, 2023
This allows applications to extend the mapping with support for custom
elements.

Closes hotwired#660
@grncdr grncdr linked a pull request Feb 17, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants