hotwired / stimulus

A modest JavaScript framework for the HTML you already have
https://stimulus.hotwired.dev/
MIT License
12.74k stars 427 forks source link

Add support for `regex` key #720

Closed seb-jean closed 1 year ago

seb-jean commented 1 year ago

Hi,

It would be interesting to add the possibility of putting a regex in the keyMappings. For instance:

Then we could use it like this:

<div data-controller="menu" data-action="keydown.[:alpha:]->menu#search">
    [...]
</div>

Tell me what you think about it.

Cheers 😁

marcoroth commented 1 year ago

Personally I don't think it's really worth to add this additional complexity and syntax to the data-action attribute. If you want a "match all characters" type of event listener you should just do data-action="keydown->menu#search" and filter the thing you are looking for in the search() method.

Using the keydown.[:alpha:] syntax you'd anyway need to handle and fetch the value from something like event.detail.alpha and do something with, which means you are writing custom logic anyway.

seb-jean commented 1 year ago

@marcoroth Yeah, I see what you mean.